github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/consul/resource_consul_prepared_query.go (about)

     1  package consul
     2  
     3  import (
     4  	"strings"
     5  
     6  	consulapi "github.com/hashicorp/consul/api"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceConsulPreparedQuery() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceConsulPreparedQueryCreate,
    13  		Update: resourceConsulPreparedQueryUpdate,
    14  		Read:   resourceConsulPreparedQueryRead,
    15  		Delete: resourceConsulPreparedQueryDelete,
    16  
    17  		SchemaVersion: 0,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"id": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Computed: true,
    23  			},
    24  
    25  			"datacenter": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  			},
    29  
    30  			"name": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  			},
    34  
    35  			"session": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  			},
    39  
    40  			"token": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  			},
    44  
    45  			"stored_token": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  			},
    49  
    50  			"service": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Required: true,
    53  			},
    54  
    55  			"tags": &schema.Schema{
    56  				Type:     schema.TypeSet,
    57  				Optional: true,
    58  				Elem:     &schema.Schema{Type: schema.TypeString},
    59  			},
    60  
    61  			"near": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Optional: true,
    64  			},
    65  
    66  			"only_passing": &schema.Schema{
    67  				Type:     schema.TypeBool,
    68  				Optional: true,
    69  			},
    70  
    71  			"failover": &schema.Schema{
    72  				Type:     schema.TypeList,
    73  				Optional: true,
    74  				MaxItems: 1,
    75  				Elem: &schema.Resource{
    76  					Schema: map[string]*schema.Schema{
    77  						"nearest_n": &schema.Schema{
    78  							Type:     schema.TypeInt,
    79  							Optional: true,
    80  						},
    81  						"datacenters": &schema.Schema{
    82  							Type:     schema.TypeList,
    83  							Optional: true,
    84  							Elem:     &schema.Schema{Type: schema.TypeString},
    85  						},
    86  					},
    87  				},
    88  			},
    89  
    90  			"dns": &schema.Schema{
    91  				Type:     schema.TypeList,
    92  				Optional: true,
    93  				MaxItems: 1,
    94  				Elem: &schema.Resource{
    95  					Schema: map[string]*schema.Schema{
    96  						"ttl": &schema.Schema{
    97  							Type:     schema.TypeString,
    98  							Optional: true,
    99  						},
   100  					},
   101  				},
   102  			},
   103  
   104  			"template": &schema.Schema{
   105  				Type:     schema.TypeList,
   106  				MaxItems: 1,
   107  				Optional: true,
   108  				Elem: &schema.Resource{
   109  					Schema: map[string]*schema.Schema{
   110  						"type": &schema.Schema{
   111  							Type:     schema.TypeString,
   112  							Required: true,
   113  						},
   114  						"regexp": &schema.Schema{
   115  							Type:     schema.TypeString,
   116  							Required: true,
   117  						},
   118  					},
   119  				},
   120  			},
   121  		},
   122  	}
   123  }
   124  
   125  func resourceConsulPreparedQueryCreate(d *schema.ResourceData, meta interface{}) error {
   126  	client := meta.(*consulapi.Client)
   127  	wo := &consulapi.WriteOptions{
   128  		Datacenter: d.Get("datacenter").(string),
   129  		Token:      d.Get("token").(string),
   130  	}
   131  
   132  	pq := preparedQueryDefinitionFromResourceData(d)
   133  
   134  	id, _, err := client.PreparedQuery().Create(pq, wo)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	d.SetId(id)
   140  	return resourceConsulPreparedQueryRead(d, meta)
   141  }
   142  
   143  func resourceConsulPreparedQueryUpdate(d *schema.ResourceData, meta interface{}) error {
   144  	client := meta.(*consulapi.Client)
   145  	wo := &consulapi.WriteOptions{
   146  		Datacenter: d.Get("datacenter").(string),
   147  		Token:      d.Get("token").(string),
   148  	}
   149  
   150  	pq := preparedQueryDefinitionFromResourceData(d)
   151  
   152  	if _, err := client.PreparedQuery().Update(pq, wo); err != nil {
   153  		return err
   154  	}
   155  
   156  	return resourceConsulPreparedQueryRead(d, meta)
   157  }
   158  
   159  func resourceConsulPreparedQueryRead(d *schema.ResourceData, meta interface{}) error {
   160  	client := meta.(*consulapi.Client)
   161  	qo := &consulapi.QueryOptions{
   162  		Datacenter: d.Get("datacenter").(string),
   163  		Token:      d.Get("token").(string),
   164  	}
   165  
   166  	queries, _, err := client.PreparedQuery().Get(d.Id(), qo)
   167  	if err != nil {
   168  		// Check for a 404/not found, these are returned as errors.
   169  		if strings.Contains(err.Error(), "not found") {
   170  			d.SetId("")
   171  			return nil
   172  		}
   173  		return err
   174  	}
   175  
   176  	if len(queries) != 1 {
   177  		d.SetId("")
   178  		return nil
   179  	}
   180  	pq := queries[0]
   181  
   182  	d.Set("name", pq.Name)
   183  	d.Set("session", pq.Session)
   184  	d.Set("stored_token", pq.Token)
   185  	d.Set("service", pq.Service.Service)
   186  	d.Set("near", pq.Service.Near)
   187  	d.Set("only_passing", pq.Service.OnlyPassing)
   188  	d.Set("tags", pq.Service.Tags)
   189  
   190  	if pq.Service.Failover.NearestN > 0 {
   191  		d.Set("failover.0.nearest_n", pq.Service.Failover.NearestN)
   192  	}
   193  	if len(pq.Service.Failover.Datacenters) > 0 {
   194  		d.Set("failover.0.datacenters", pq.Service.Failover.Datacenters)
   195  	}
   196  
   197  	if pq.DNS.TTL != "" {
   198  		d.Set("dns.0.ttl", pq.DNS.TTL)
   199  	}
   200  
   201  	if pq.Template.Type != "" {
   202  		d.Set("template.0.type", pq.Template.Type)
   203  		d.Set("template.0.regexp", pq.Template.Regexp)
   204  	}
   205  
   206  	return nil
   207  }
   208  
   209  func resourceConsulPreparedQueryDelete(d *schema.ResourceData, meta interface{}) error {
   210  	client := meta.(*consulapi.Client)
   211  	qo := &consulapi.QueryOptions{
   212  		Datacenter: d.Get("datacenter").(string),
   213  		Token:      d.Get("token").(string),
   214  	}
   215  
   216  	if _, err := client.PreparedQuery().Delete(d.Id(), qo); err != nil {
   217  		return err
   218  	}
   219  
   220  	d.SetId("")
   221  	return nil
   222  }
   223  
   224  func preparedQueryDefinitionFromResourceData(d *schema.ResourceData) *consulapi.PreparedQueryDefinition {
   225  	pq := &consulapi.PreparedQueryDefinition{
   226  		ID:      d.Id(),
   227  		Name:    d.Get("name").(string),
   228  		Session: d.Get("session").(string),
   229  		Token:   d.Get("stored_token").(string),
   230  		Service: consulapi.ServiceQuery{
   231  			Service:     d.Get("service").(string),
   232  			Near:        d.Get("near").(string),
   233  			OnlyPassing: d.Get("only_passing").(bool),
   234  		},
   235  	}
   236  
   237  	tags := d.Get("tags").(*schema.Set).List()
   238  	pq.Service.Tags = make([]string, len(tags))
   239  	for i, v := range tags {
   240  		pq.Service.Tags[i] = v.(string)
   241  	}
   242  
   243  	if _, ok := d.GetOk("failover.0"); ok {
   244  		failover := consulapi.QueryDatacenterOptions{
   245  			NearestN: d.Get("failover.0.nearest_n").(int),
   246  		}
   247  
   248  		dcs := d.Get("failover.0.datacenters").([]interface{})
   249  		failover.Datacenters = make([]string, len(dcs))
   250  		for i, v := range dcs {
   251  			failover.Datacenters[i] = v.(string)
   252  		}
   253  
   254  		pq.Service.Failover = failover
   255  	}
   256  
   257  	if _, ok := d.GetOk("template.0"); ok {
   258  		pq.Template = consulapi.QueryTemplate{
   259  			Type:   d.Get("template.0.type").(string),
   260  			Regexp: d.Get("template.0.regexp").(string),
   261  		}
   262  	}
   263  
   264  	if _, ok := d.GetOk("dns.0"); ok {
   265  		pq.DNS = consulapi.QueryDNSOptions{
   266  			TTL: d.Get("dns.0.ttl").(string),
   267  		}
   268  	}
   269  
   270  	return pq
   271  }