github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/consul/resource_consul_agent_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 resourceConsulAgentService() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceConsulAgentServiceCreate, 13 Update: resourceConsulAgentServiceCreate, 14 Read: resourceConsulAgentServiceRead, 15 Delete: resourceConsulAgentServiceDelete, 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 "id": &schema.Schema{ 26 Type: schema.TypeString, 27 Computed: true, 28 }, 29 30 "name": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 }, 34 35 "port": &schema.Schema{ 36 Type: schema.TypeInt, 37 Optional: true, 38 ForceNew: true, 39 }, 40 41 "tags": &schema.Schema{ 42 Type: schema.TypeList, 43 Optional: true, 44 Elem: &schema.Schema{Type: schema.TypeString}, 45 ForceNew: true, 46 }, 47 }, 48 } 49 } 50 51 func resourceConsulAgentServiceCreate(d *schema.ResourceData, meta interface{}) error { 52 client := meta.(*consulapi.Client) 53 agent := client.Agent() 54 55 name := d.Get("name").(string) 56 registration := consulapi.AgentServiceRegistration{Name: name} 57 58 if address, ok := d.GetOk("address"); ok { 59 registration.Address = address.(string) 60 } 61 62 if port, ok := d.GetOk("port"); ok { 63 registration.Port = port.(int) 64 } 65 66 if v, ok := d.GetOk("tags"); ok { 67 vs := v.([]interface{}) 68 s := make([]string, len(vs)) 69 for i, raw := range vs { 70 s[i] = raw.(string) 71 } 72 registration.Tags = s 73 } 74 75 if err := agent.ServiceRegister(®istration); err != nil { 76 return fmt.Errorf("Failed to register service '%s' with Consul agent: %v", name, err) 77 } 78 79 // Update the resource 80 if serviceMap, err := agent.Services(); err != nil { 81 return fmt.Errorf("Failed to read services from Consul agent: %v", err) 82 } else if service, ok := serviceMap[name]; !ok { 83 return fmt.Errorf("Failed to read service '%s' from Consul agent: %v", name, err) 84 } else { 85 d.Set("address", service.Address) 86 d.Set("id", service.ID) 87 d.SetId(service.ID) 88 d.Set("name", service.Service) 89 d.Set("port", service.Port) 90 tags := make([]string, 0, len(service.Tags)) 91 for _, tag := range service.Tags { 92 tags = append(tags, tag) 93 } 94 d.Set("tags", tags) 95 } 96 97 return nil 98 } 99 100 func resourceConsulAgentServiceRead(d *schema.ResourceData, meta interface{}) error { 101 client := meta.(*consulapi.Client) 102 agent := client.Agent() 103 104 name := d.Get("name").(string) 105 106 if services, err := agent.Services(); err != nil { 107 return fmt.Errorf("Failed to get services from Consul agent: %v", err) 108 } else if service, ok := services[name]; !ok { 109 d.Set("id", "") 110 } else { 111 d.Set("address", service.Address) 112 d.Set("id", service.ID) 113 d.SetId(service.ID) 114 d.Set("name", service.Service) 115 d.Set("port", service.Port) 116 tags := make([]string, 0, len(service.Tags)) 117 for _, tag := range service.Tags { 118 tags = append(tags, tag) 119 } 120 d.Set("tags", tags) 121 } 122 123 return nil 124 } 125 126 func resourceConsulAgentServiceDelete(d *schema.ResourceData, meta interface{}) error { 127 client := meta.(*consulapi.Client) 128 catalog := client.Agent() 129 130 id := d.Get("id").(string) 131 132 if err := catalog.ServiceDeregister(id); err != nil { 133 return fmt.Errorf("Failed to deregister service '%s' from Consul agent: %v", id, err) 134 } 135 136 // Clear the ID 137 d.SetId("") 138 return nil 139 }