#2087 agent_service resource supports address, port, and tag keys, can be read (refreshed) and deleted
This commit is contained in:
parent
63a045f4ff
commit
65ebbda776
|
@ -16,15 +16,32 @@ func resourceConsulAgentService() *schema.Resource {
|
|||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"address": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"port": &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
},
|
||||
|
||||
"tags": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{Type: schema.TypeString},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -34,15 +51,46 @@ func resourceConsulAgentServiceCreate(d *schema.ResourceData, meta interface{})
|
|||
agent := client.Agent()
|
||||
|
||||
name := d.Get("name").(string)
|
||||
|
||||
registration := consulapi.AgentServiceRegistration{Name: name}
|
||||
|
||||
if address, ok := d.GetOk("address"); ok {
|
||||
registration.Address = address.(string)
|
||||
}
|
||||
|
||||
if id, ok := d.GetOk("id"); ok {
|
||||
registration.ID = id.(string)
|
||||
}
|
||||
|
||||
if port, ok := d.GetOk("port"); ok {
|
||||
registration.Port = port.(int)
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("tags"); ok {
|
||||
vs := v.([]interface{})
|
||||
s := make([]string, len(vs))
|
||||
for i, raw := range vs {
|
||||
s[i] = raw.(string)
|
||||
}
|
||||
registration.Tags = s
|
||||
}
|
||||
|
||||
if err := agent.ServiceRegister(®istration); err != nil {
|
||||
return fmt.Errorf("Failed to register service '%s' with Consul agent: %v", name, err)
|
||||
}
|
||||
|
||||
// Update the resource
|
||||
d.SetId(fmt.Sprintf("consul-agent-service-%s", name))
|
||||
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 {
|
||||
d.Set("address", service.Address)
|
||||
d.Set("name", service.Service)
|
||||
d.Set("port", service.Port)
|
||||
d.Set("tags", service.Tags)
|
||||
d.SetId(service.ID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -54,10 +102,14 @@ func resourceConsulAgentServiceRead(d *schema.ResourceData, meta interface{}) er
|
|||
|
||||
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: %v", name, err)
|
||||
} else {
|
||||
if _, ok := services[name]; !ok {
|
||||
return fmt.Errorf("Failed to get service '%s' from Consul agent: %v", name, err)
|
||||
}
|
||||
d.Set("address", service.Address)
|
||||
d.Set("name", service.Service)
|
||||
d.Set("port", service.Port)
|
||||
d.Set("tags", service.Tags)
|
||||
d.SetId(service.ID)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue