diff --git a/builtin/providers/consul/resource_consul_service.go b/builtin/providers/consul/resource_consul_service.go index 57f95a856..c66041413 100644 --- a/builtin/providers/consul/resource_consul_service.go +++ b/builtin/providers/consul/resource_consul_service.go @@ -22,9 +22,11 @@ func resourceConsulService() *schema.Resource { ForceNew: true, }, - "id": &schema.Schema{ + "service_id": &schema.Schema{ Type: schema.TypeString, + Optional: true, Computed: true, + ForceNew: true, }, "name": &schema.Schema{ @@ -53,7 +55,13 @@ func resourceConsulServiceCreate(d *schema.ResourceData, meta interface{}) error agent := client.Agent() name := d.Get("name").(string) - registration := consulapi.AgentServiceRegistration{Name: name} + identifier := name + + if serviceId, ok := d.GetOk("service_id"); ok { + identifier = serviceId.(string) + } + + registration := consulapi.AgentServiceRegistration{Name: name, ID: identifier} if address, ok := d.GetOk("address"); ok { registration.Address = address.(string) @@ -79,12 +87,13 @@ func resourceConsulServiceCreate(d *schema.ResourceData, meta interface{}) error // Update the resource if serviceMap, err := agent.Services(); err != nil { return fmt.Errorf("Failed to read services from Consul agent: %v", err) - } else if service, ok := serviceMap[name]; !ok { - return fmt.Errorf("Failed to read service '%s' from Consul agent: %v", name, err) + } else if service, ok := serviceMap[identifier]; !ok { + return fmt.Errorf("Failed to read service '%s' from Consul agent: %v", identifier, err) } else { - d.Set("address", service.Address) - d.Set("id", service.ID) d.SetId(service.ID) + + d.Set("address", service.Address) + d.Set("service_id", service.ID) d.Set("name", service.Service) d.Set("port", service.Port) tags := make([]string, 0, len(service.Tags)) @@ -102,15 +111,21 @@ func resourceConsulServiceRead(d *schema.ResourceData, meta interface{}) error { agent := client.Agent() name := d.Get("name").(string) + identifier := name + + if serviceId, ok := d.GetOk("service_id"); ok { + identifier = serviceId.(string) + } if services, err := agent.Services(); err != nil { return fmt.Errorf("Failed to get services from Consul agent: %v", err) - } else if service, ok := services[name]; !ok { - return fmt.Errorf("Failed to get service '%s' from Consul agent", name) + } else if service, ok := services[identifier]; !ok { + return fmt.Errorf("Failed to get service '%s' from Consul agent", identifier) } else { - d.Set("address", service.Address) - d.Set("id", service.ID) d.SetId(service.ID) + + d.Set("address", service.Address) + d.Set("service_id", service.ID) d.Set("name", service.Service) d.Set("port", service.Port) tags := make([]string, 0, len(service.Tags)) @@ -127,7 +142,7 @@ func resourceConsulServiceDelete(d *schema.ResourceData, meta interface{}) error client := meta.(*consulapi.Client) catalog := client.Agent() - id := d.Get("id").(string) + id := d.Get("service_id").(string) if err := catalog.ServiceDeregister(id); err != nil { return fmt.Errorf("Failed to deregister service '%s' from Consul agent: %v", id, err) diff --git a/builtin/providers/consul/resource_consul_service_test.go b/builtin/providers/consul/resource_consul_service_test.go index f4df71542..2ed4405e8 100644 --- a/builtin/providers/consul/resource_consul_service_test.go +++ b/builtin/providers/consul/resource_consul_service_test.go @@ -21,6 +21,7 @@ func TestAccConsulService_basic(t *testing.T) { testAccCheckConsulServiceExists(), testAccCheckConsulServiceValue("consul_service.app", "address", "www.google.com"), testAccCheckConsulServiceValue("consul_service.app", "id", "google"), + testAccCheckConsulServiceValue("consul_service.app", "service_id", "google"), testAccCheckConsulServiceValue("consul_service.app", "name", "google"), testAccCheckConsulServiceValue("consul_service.app", "port", "80"), testAccCheckConsulServiceValue("consul_service.app", "tags.#", "2"), @@ -83,6 +84,7 @@ func testAccCheckConsulServiceValue(n, attr, val string) resource.TestCheckFunc const testAccConsulServiceConfig = ` resource "consul_service" "app" { address = "www.google.com" + service_id = "google" name = "google" port = 80 tags = ["tag0", "tag1"] diff --git a/website/source/docs/providers/consul/r/service.html.markdown b/website/source/docs/providers/consul/r/service.html.markdown index a91370c2c..e1bc23563 100644 --- a/website/source/docs/providers/consul/r/service.html.markdown +++ b/website/source/docs/providers/consul/r/service.html.markdown @@ -25,14 +25,16 @@ resource "consul_service" "google" { The following arguments are supported: -* `address` - (Optional) The address of the service. Defaults to the +* `service_id` - (Optional, string) The id of the service, defaults to the value of `name` if not supplied. + +* `address` - (Optional, string) The address of the service. Defaults to the address of the agent. -* `name` - (Required) The name of the service. +* `name` - (Required, string) The name of the service. -* `port` - (Optional) The port of the service. +* `port` - (Optional, int) The port of the service. -* `tags` - (Optional) A list of values that are opaque to Consul, +* `tags` - (Optional, set of strings) A list of values that are opaque to Consul, but can be used to distinguish between services or nodes. @@ -40,8 +42,8 @@ The following arguments are supported: The following attributes are exported: +* `service_id` - The id of the service, defaults to the value of `name`. * `address` - The address of the service. -* `id` - The id of the service, defaults to the value of `name`. * `name` - The name of the service. * `port` - The port of the service. * `tags` - The tags of the service.