github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/consul/resource_consul_service.go (about) 1 package consul 2 3 import ( 4 "fmt" 5 6 consulapi "github.com/hashicorp/consul/api" 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 func resourceConsulService() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceConsulServiceCreate, 13 Update: resourceConsulServiceCreate, 14 Read: resourceConsulServiceRead, 15 Delete: resourceConsulServiceDelete, 16 17 Schema: map[string]*schema.Schema{ 18 "address": &schema.Schema{ 19 Type: schema.TypeString, 20 Optional: true, 21 Computed: true, 22 ForceNew: true, 23 }, 24 25 "service_id": &schema.Schema{ 26 Type: schema.TypeString, 27 Optional: true, 28 Computed: true, 29 ForceNew: true, 30 }, 31 32 "name": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 }, 36 37 "port": &schema.Schema{ 38 Type: schema.TypeInt, 39 Optional: true, 40 ForceNew: true, 41 }, 42 43 "tags": &schema.Schema{ 44 Type: schema.TypeList, 45 Optional: true, 46 Elem: &schema.Schema{Type: schema.TypeString}, 47 ForceNew: true, 48 }, 49 }, 50 } 51 } 52 53 func resourceConsulServiceCreate(d *schema.ResourceData, meta interface{}) error { 54 client := meta.(*consulapi.Client) 55 agent := client.Agent() 56 57 name := d.Get("name").(string) 58 identifier := name 59 60 if serviceId, ok := d.GetOk("service_id"); ok { 61 identifier = serviceId.(string) 62 } 63 64 registration := consulapi.AgentServiceRegistration{Name: name, ID: identifier} 65 66 if address, ok := d.GetOk("address"); ok { 67 registration.Address = address.(string) 68 } 69 70 if port, ok := d.GetOk("port"); ok { 71 registration.Port = port.(int) 72 } 73 74 if v, ok := d.GetOk("tags"); ok { 75 vs := v.([]interface{}) 76 s := make([]string, len(vs)) 77 for i, raw := range vs { 78 s[i] = raw.(string) 79 } 80 registration.Tags = s 81 } 82 83 if err := agent.ServiceRegister(®istration); err != nil { 84 return fmt.Errorf("Failed to register service '%s' with Consul agent: %v", name, err) 85 } 86 87 // Update the resource 88 if serviceMap, err := agent.Services(); err != nil { 89 return fmt.Errorf("Failed to read services from Consul agent: %v", err) 90 } else if service, ok := serviceMap[identifier]; !ok { 91 return fmt.Errorf("Failed to read service '%s' from Consul agent: %v", identifier, err) 92 } else { 93 d.SetId(service.ID) 94 95 d.Set("address", service.Address) 96 d.Set("service_id", service.ID) 97 d.Set("name", service.Service) 98 d.Set("port", service.Port) 99 tags := make([]string, 0, len(service.Tags)) 100 for _, tag := range service.Tags { 101 tags = append(tags, tag) 102 } 103 d.Set("tags", tags) 104 } 105 106 return nil 107 } 108 109 func resourceConsulServiceRead(d *schema.ResourceData, meta interface{}) error { 110 client := meta.(*consulapi.Client) 111 agent := client.Agent() 112 113 name := d.Get("name").(string) 114 identifier := name 115 116 if serviceId, ok := d.GetOk("service_id"); ok { 117 identifier = serviceId.(string) 118 } 119 120 if services, err := agent.Services(); err != nil { 121 return fmt.Errorf("Failed to get services from Consul agent: %v", err) 122 } else if service, ok := services[identifier]; !ok { 123 return fmt.Errorf("Failed to get service '%s' from Consul agent", identifier) 124 } else { 125 d.SetId(service.ID) 126 127 d.Set("address", service.Address) 128 d.Set("service_id", service.ID) 129 d.Set("name", service.Service) 130 d.Set("port", service.Port) 131 tags := make([]string, 0, len(service.Tags)) 132 for _, tag := range service.Tags { 133 tags = append(tags, tag) 134 } 135 d.Set("tags", tags) 136 } 137 138 return nil 139 } 140 141 func resourceConsulServiceDelete(d *schema.ResourceData, meta interface{}) error { 142 client := meta.(*consulapi.Client) 143 catalog := client.Agent() 144 145 id := d.Get("service_id").(string) 146 147 if err := catalog.ServiceDeregister(id); err != nil { 148 return fmt.Errorf("Failed to deregister service '%s' from Consul agent: %v", id, err) 149 } 150 151 // Clear the ID 152 d.SetId("") 153 return nil 154 }