github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/consul/query_options.go (about)

     1  package consul
     2  
     3  import (
     4  	"time"
     5  
     6  	consulapi "github.com/hashicorp/consul/api"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  const (
    11  	queryOptAllowStale        = "allow_stale"
    12  	queryOptDatacenter        = "datacenter"
    13  	queryOptNear              = "near"
    14  	queryOptNodeMeta          = "node_meta"
    15  	queryOptRequireConsistent = "require_consistent"
    16  	queryOptToken             = "token"
    17  	queryOptWaitIndex         = "wait_index"
    18  	queryOptWaitTime          = "wait_time"
    19  )
    20  
    21  var schemaQueryOpts = &schema.Schema{
    22  	Optional: true,
    23  	Type:     schema.TypeSet,
    24  	Elem: &schema.Resource{
    25  		Schema: map[string]*schema.Schema{
    26  			queryOptAllowStale: &schema.Schema{
    27  				Optional: true,
    28  				Default:  true,
    29  				Type:     schema.TypeBool,
    30  			},
    31  			queryOptDatacenter: &schema.Schema{
    32  				// Optional because we'll pull the default from the local agent if it's
    33  				// not specified, but we can query remote data centers as a result.
    34  				Optional: true,
    35  				Type:     schema.TypeString,
    36  			},
    37  			queryOptNear: &schema.Schema{
    38  				Optional: true,
    39  				Type:     schema.TypeString,
    40  			},
    41  			queryOptNodeMeta: &schema.Schema{
    42  				Optional: true,
    43  				Type:     schema.TypeMap,
    44  			},
    45  			queryOptRequireConsistent: &schema.Schema{
    46  				Optional: true,
    47  				Default:  false,
    48  				Type:     schema.TypeBool,
    49  			},
    50  			queryOptToken: &schema.Schema{
    51  				Optional: true,
    52  				Type:     schema.TypeString,
    53  			},
    54  			queryOptWaitIndex: &schema.Schema{
    55  				Optional: true,
    56  				Type:     schema.TypeInt,
    57  				ValidateFunc: makeValidationFunc(queryOptWaitIndex, []interface{}{
    58  					validateIntMin(0),
    59  				}),
    60  			},
    61  			queryOptWaitTime: &schema.Schema{
    62  				Optional: true,
    63  				Type:     schema.TypeString,
    64  				ValidateFunc: makeValidationFunc(queryOptWaitTime, []interface{}{
    65  					validateDurationMin("0ns"),
    66  				}),
    67  			},
    68  		},
    69  	},
    70  }
    71  
    72  func getQueryOpts(d *schema.ResourceData, client *consulapi.Client) (*consulapi.QueryOptions, error) {
    73  	queryOpts := &consulapi.QueryOptions{}
    74  
    75  	if v, ok := d.GetOk(queryOptAllowStale); ok {
    76  		queryOpts.AllowStale = v.(bool)
    77  	}
    78  
    79  	if v, ok := d.GetOk(queryOptDatacenter); ok {
    80  		queryOpts.Datacenter = v.(string)
    81  	}
    82  
    83  	if queryOpts.Datacenter == "" {
    84  		dc, err := getDC(d, client)
    85  		if err != nil {
    86  			return nil, err
    87  		}
    88  		queryOpts.Datacenter = dc
    89  	}
    90  
    91  	if v, ok := d.GetOk(queryOptNear); ok {
    92  		queryOpts.Near = v.(string)
    93  	}
    94  
    95  	if v, ok := d.GetOk(queryOptRequireConsistent); ok {
    96  		queryOpts.RequireConsistent = v.(bool)
    97  	}
    98  
    99  	if v, ok := d.GetOk(queryOptNodeMeta); ok {
   100  		m := v.(map[string]interface{})
   101  		nodeMetaMap := make(map[string]string, len(queryOptNodeMeta))
   102  		for s, t := range m {
   103  			nodeMetaMap[s] = t.(string)
   104  		}
   105  		queryOpts.NodeMeta = nodeMetaMap
   106  	}
   107  
   108  	if v, ok := d.GetOk(queryOptToken); ok {
   109  		queryOpts.Token = v.(string)
   110  	}
   111  
   112  	if v, ok := d.GetOk(queryOptWaitIndex); ok {
   113  		queryOpts.WaitIndex = uint64(v.(int))
   114  	}
   115  
   116  	if v, ok := d.GetOk(queryOptWaitTime); ok {
   117  		d, _ := time.ParseDuration(v.(string))
   118  		queryOpts.WaitTime = d
   119  	}
   120  
   121  	return queryOpts, nil
   122  }