#2087: add consul agent_service and catalog_entry resources; create resource works; TODO support more fields and add tests
This commit is contained in:
parent
e7f33bf523
commit
63a045f4ff
|
@ -9,6 +9,7 @@ import (
|
|||
type Config struct {
|
||||
Datacenter string `mapstructure:"datacenter"`
|
||||
Address string `mapstructure:"address"`
|
||||
Token string `mapstructure:"token"`
|
||||
Scheme string `mapstructure:"scheme"`
|
||||
}
|
||||
|
||||
|
@ -25,6 +26,9 @@ func (c *Config) Client() (*consulapi.Client, error) {
|
|||
if c.Scheme != "" {
|
||||
config.Scheme = c.Scheme
|
||||
}
|
||||
if c.Token != "" {
|
||||
config.Token = c.Token
|
||||
}
|
||||
client, err := consulapi.NewClient(config)
|
||||
|
||||
log.Printf("[INFO] Consul Client configured with address: '%s', scheme: '%s', datacenter: '%s'",
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
consulapi "github.com/hashicorp/consul/api"
|
||||
)
|
||||
|
||||
// getDC is used to get the datacenter of the local agent
|
||||
func getDC(client *consulapi.Client) (string, error) {
|
||||
info, err := client.Agent().Self()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to get datacenter from Consul agent: %v", err)
|
||||
}
|
||||
dc := info["Config"]["Datacenter"].(string)
|
||||
return dc, nil
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
consulapi "github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
func resourceConsulAgentService() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceConsulAgentServiceCreate,
|
||||
Update: resourceConsulAgentServiceCreate,
|
||||
Read: resourceConsulAgentServiceRead,
|
||||
Delete: resourceConsulAgentServiceDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"address": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceConsulAgentServiceCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*consulapi.Client)
|
||||
agent := client.Agent()
|
||||
|
||||
name := d.Get("name").(string)
|
||||
|
||||
registration := consulapi.AgentServiceRegistration{Name: name}
|
||||
|
||||
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))
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceConsulAgentServiceRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*consulapi.Client)
|
||||
agent := client.Agent()
|
||||
|
||||
name := d.Get("name").(string)
|
||||
|
||||
if services, err := agent.Services(); err != nil {
|
||||
return fmt.Errorf("Failed to get services from Consul agent: %v", err)
|
||||
} else {
|
||||
if _, ok := services[name]; !ok {
|
||||
return fmt.Errorf("Failed to get service '%s' from Consul agent: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceConsulAgentServiceDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*consulapi.Client)
|
||||
catalog := client.Agent()
|
||||
|
||||
name := d.Get("name").(string)
|
||||
|
||||
if err := catalog.ServiceDeregister(name); err != nil {
|
||||
return fmt.Errorf("Failed to deregister service '%s' from Consul agent: %v", name, err)
|
||||
}
|
||||
|
||||
// Clear the ID
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,199 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
consulapi "github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/terraform/helper/hashcode"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
func resourceConsulCatalogEntry() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceConsulCatalogEntryCreate,
|
||||
Update: resourceConsulCatalogEntryCreate,
|
||||
Read: resourceConsulCatalogEntryRead,
|
||||
Delete: resourceConsulCatalogEntryDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"address": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"datacenter": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"node": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"service": &schema.Schema{
|
||||
Type: schema.TypeSet,
|
||||
Optional: true,
|
||||
Elem: &schema.Resource {
|
||||
Schema: map[string]*schema.Schema{
|
||||
"service": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Set: resourceConsulCatalogEntryServicesHash,
|
||||
},
|
||||
|
||||
"token": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceConsulCatalogEntryServicesHash(v interface{}) int {
|
||||
var buf bytes.Buffer
|
||||
m := v.(map[string]interface{})
|
||||
buf.WriteString(fmt.Sprintf("%s-", m["service"].(string)))
|
||||
return hashcode.String(buf.String())
|
||||
}
|
||||
|
||||
func resourceConsulCatalogEntryCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*consulapi.Client)
|
||||
catalog := client.Catalog()
|
||||
|
||||
// Resolve the datacenter first, all the other keys are dependent on this
|
||||
var dc string
|
||||
if v, ok := d.GetOk("datacenter"); ok {
|
||||
dc = v.(string)
|
||||
log.Printf("[DEBUG] Consul datacenter: %s", dc)
|
||||
} else {
|
||||
log.Printf("[DEBUG] Resolving Consul datacenter...")
|
||||
var err error
|
||||
dc, err = getDC(client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var token string
|
||||
if v, ok := d.GetOk("token"); ok {
|
||||
token = v.(string)
|
||||
}
|
||||
|
||||
// Setup the operations using the datacenter
|
||||
wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
|
||||
|
||||
address := d.Get("address").(string)
|
||||
node := d.Get("node").(string)
|
||||
|
||||
if rawServiceDefinition, ok := d.GetOk("service"); ok {
|
||||
rawServiceList := rawServiceDefinition.(*schema.Set).List()
|
||||
for _, rawService := range rawServiceList {
|
||||
service, ok := rawService.(map[string]interface{})
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("Failed to unroll: %#v", rawService)
|
||||
}
|
||||
|
||||
serviceName := service["service"].(string)
|
||||
|
||||
registration := consulapi.CatalogRegistration{
|
||||
Node: node, Address: address, Datacenter: dc,
|
||||
Service: &consulapi.AgentService{Service: serviceName},
|
||||
}
|
||||
|
||||
if _, err := catalog.Register(®istration, &wOpts); err != nil {
|
||||
return fmt.Errorf("Failed to register Consul catalog entry with node '%s' at address '%s' with service %s in %s: %v",
|
||||
node, address, serviceName, dc, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
registration := consulapi.CatalogRegistration{
|
||||
Node: node, Address: address, Datacenter: dc,
|
||||
}
|
||||
|
||||
if _, err := catalog.Register(®istration, &wOpts); err != nil {
|
||||
return fmt.Errorf("Failed to register Consul catalog entry with node '%s' at address '%s' in %s: %v",
|
||||
node, address, dc, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Update the resource
|
||||
d.SetId(fmt.Sprintf("consul-catalog-node-%s-%s", node, address))
|
||||
d.Set("datacenter", dc)
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceConsulCatalogEntryRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*consulapi.Client)
|
||||
catalog := client.Catalog()
|
||||
|
||||
// Get the DC, error if not available.
|
||||
var dc string
|
||||
if v, ok := d.GetOk("datacenter"); ok {
|
||||
dc = v.(string)
|
||||
log.Printf("[DEBUG] Consul datacenter: %s", dc)
|
||||
} else {
|
||||
return fmt.Errorf("Missing datacenter configuration")
|
||||
}
|
||||
var token string
|
||||
if v, ok := d.GetOk("token"); ok {
|
||||
token = v.(string)
|
||||
}
|
||||
|
||||
node := d.Get("node").(string)
|
||||
|
||||
// Setup the operations using the datacenter
|
||||
qOpts := consulapi.QueryOptions{Datacenter: dc, Token: token}
|
||||
|
||||
if _, _, err := catalog.Node(node, &qOpts); err != nil {
|
||||
return fmt.Errorf("Failed to get node '%s' from Consul catalog: %v", node, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceConsulCatalogEntryDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*consulapi.Client)
|
||||
catalog := client.Catalog()
|
||||
|
||||
// Get the DC, error if not available.
|
||||
var dc string
|
||||
if v, ok := d.GetOk("datacenter"); ok {
|
||||
dc = v.(string)
|
||||
log.Printf("[DEBUG] Consul datacenter: %s", dc)
|
||||
} else {
|
||||
return fmt.Errorf("Missing datacenter configuration")
|
||||
}
|
||||
var token string
|
||||
if v, ok := d.GetOk("token"); ok {
|
||||
token = v.(string)
|
||||
}
|
||||
|
||||
// Setup the operations using the datacenter
|
||||
wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
|
||||
|
||||
address := d.Get("address").(string)
|
||||
node := d.Get("node").(string)
|
||||
|
||||
deregistration := consulapi.CatalogDeregistration{
|
||||
Node: node, Address: address, Datacenter: dc,
|
||||
}
|
||||
|
||||
if _, err := catalog.Deregister(&deregistration, &wOpts); err != nil {
|
||||
return fmt.Errorf("Failed to deregister Consul catalog entry with node '%s' at address '%s' in %s: %v",
|
||||
node, address, dc, err)
|
||||
}
|
||||
|
||||
// Clear the ID
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
|
@ -283,13 +283,3 @@ func attributeValue(sub map[string]interface{}, key string, pair *consulapi.KVPa
|
|||
// No value
|
||||
return ""
|
||||
}
|
||||
|
||||
// getDC is used to get the datacenter of the local agent
|
||||
func getDC(client *consulapi.Client) (string, error) {
|
||||
info, err := client.Agent().Self()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to get datacenter from Consul agent: %v", err)
|
||||
}
|
||||
dc := info["Config"]["Datacenter"].(string)
|
||||
return dc, nil
|
||||
}
|
||||
|
|
|
@ -26,9 +26,16 @@ func Provider() terraform.ResourceProvider {
|
|||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
|
||||
"token": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"consul_agent_service": resourceConsulAgentService(),
|
||||
"consul_catalog_entry": resourceConsulCatalogEntry(),
|
||||
"consul_keys": resourceConsulKeys(),
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue