github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rabbitmq/resource_binding.go (about) 1 package rabbitmq 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/michaelklishin/rabbit-hole" 9 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceBinding() *schema.Resource { 14 return &schema.Resource{ 15 Create: CreateBinding, 16 Read: ReadBinding, 17 Delete: DeleteBinding, 18 Importer: &schema.ResourceImporter{ 19 State: schema.ImportStatePassthrough, 20 }, 21 22 Schema: map[string]*schema.Schema{ 23 "source": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "vhost": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 35 "destination": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "destination_type": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 }, 46 47 "properties_key": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 ForceNew: true, 51 }, 52 53 "routing_key": &schema.Schema{ 54 Type: schema.TypeString, 55 Optional: true, 56 ForceNew: true, 57 }, 58 59 "arguments": &schema.Schema{ 60 Type: schema.TypeMap, 61 Optional: true, 62 ForceNew: true, 63 }, 64 }, 65 } 66 } 67 68 func CreateBinding(d *schema.ResourceData, meta interface{}) error { 69 rmqc := meta.(*rabbithole.Client) 70 71 vhost := d.Get("vhost").(string) 72 bindingInfo := rabbithole.BindingInfo{ 73 Source: d.Get("source").(string), 74 Destination: d.Get("destination").(string), 75 DestinationType: d.Get("destination_type").(string), 76 RoutingKey: d.Get("routing_key").(string), 77 PropertiesKey: d.Get("properties_key").(string), 78 Arguments: d.Get("arguments").(map[string]interface{}), 79 } 80 81 if err := declareBinding(rmqc, vhost, bindingInfo); err != nil { 82 return err 83 } 84 85 name := fmt.Sprintf("%s/%s/%s/%s/%s", vhost, bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType, bindingInfo.PropertiesKey) 86 d.SetId(name) 87 88 return ReadBinding(d, meta) 89 } 90 91 func ReadBinding(d *schema.ResourceData, meta interface{}) error { 92 rmqc := meta.(*rabbithole.Client) 93 94 bindingId := strings.Split(d.Id(), "/") 95 if len(bindingId) < 5 { 96 return fmt.Errorf("Unable to determine binding ID") 97 } 98 99 vhost := bindingId[0] 100 source := bindingId[1] 101 destination := bindingId[2] 102 destinationType := bindingId[3] 103 propertiesKey := bindingId[4] 104 105 bindings, err := rmqc.ListBindingsIn(vhost) 106 if err != nil { 107 return err 108 } 109 110 log.Printf("[DEBUG] RabbitMQ: Bindings retrieved: %#v", bindings) 111 bindingFound := false 112 for _, binding := range bindings { 113 if binding.Source == source && binding.Destination == destination && binding.DestinationType == destinationType && binding.PropertiesKey == propertiesKey { 114 log.Printf("[DEBUG] RabbitMQ: Found Binding: %#v", binding) 115 bindingFound = true 116 117 d.Set("vhost", binding.Vhost) 118 d.Set("source", binding.Source) 119 d.Set("destination", binding.Destination) 120 d.Set("destination_type", binding.DestinationType) 121 d.Set("routing_key", binding.RoutingKey) 122 d.Set("properties_key", binding.PropertiesKey) 123 d.Set("arguments", binding.Arguments) 124 } 125 } 126 127 // The binding could not be found, 128 // so consider it deleted and remove from state 129 if !bindingFound { 130 d.SetId("") 131 } 132 133 return nil 134 } 135 136 func DeleteBinding(d *schema.ResourceData, meta interface{}) error { 137 rmqc := meta.(*rabbithole.Client) 138 139 bindingId := strings.Split(d.Id(), "/") 140 if len(bindingId) < 5 { 141 return fmt.Errorf("Unable to determine binding ID") 142 } 143 144 vhost := bindingId[0] 145 source := bindingId[1] 146 destination := bindingId[2] 147 destinationType := bindingId[3] 148 propertiesKey := bindingId[4] 149 150 bindingInfo := rabbithole.BindingInfo{ 151 Vhost: vhost, 152 Source: source, 153 Destination: destination, 154 DestinationType: destinationType, 155 PropertiesKey: propertiesKey, 156 } 157 158 log.Printf("[DEBUG] RabbitMQ: Attempting to delete binding for %s/%s/%s/%s/%s", 159 vhost, source, destination, destinationType, propertiesKey) 160 161 resp, err := rmqc.DeleteBinding(vhost, bindingInfo) 162 if err != nil { 163 return err 164 } 165 166 log.Printf("[DEBUG] RabbitMQ: Binding delete response: %#v", resp) 167 168 if resp.StatusCode == 404 { 169 // The binding was already deleted 170 return nil 171 } 172 173 if resp.StatusCode >= 400 { 174 return fmt.Errorf("Error deleting RabbitMQ binding: %s", resp.Status) 175 } 176 177 return nil 178 } 179 180 func declareBinding(rmqc *rabbithole.Client, vhost string, bindingInfo rabbithole.BindingInfo) error { 181 log.Printf("[DEBUG] RabbitMQ: Attempting to declare binding for %s/%s/%s/%s/%s", 182 vhost, bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType, bindingInfo.PropertiesKey) 183 184 resp, err := rmqc.DeclareBinding(vhost, bindingInfo) 185 log.Printf("[DEBUG] RabbitMQ: Binding declare response: %#v", resp) 186 if err != nil { 187 return err 188 } 189 190 if resp.StatusCode >= 400 { 191 return fmt.Errorf("Error declaring RabbitMQ binding: %s", resp.Status) 192 } 193 194 return nil 195 }