github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/data_source_circonus_collector.go (about)

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/circonus-labs/circonus-gometrics/api"
     7  	"github.com/circonus-labs/circonus-gometrics/api/config"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  const (
    13  	collectorCNAttr           = "cn"
    14  	collectorIDAttr           = "id"
    15  	collectorDetailsAttr      = "details"
    16  	collectorExternalHostAttr = "external_host"
    17  	collectorExternalPortAttr = "external_port"
    18  	collectorIPAttr           = "ip"
    19  	collectorLatitudeAttr     = "latitude"
    20  	collectorLongitudeAttr    = "longitude"
    21  	collectorMinVersionAttr   = "min_version"
    22  	collectorModulesAttr      = "modules"
    23  	collectorNameAttr         = "name"
    24  	collectorPortAttr         = "port"
    25  	collectorSkewAttr         = "skew"
    26  	collectorStatusAttr       = "status"
    27  	collectorTagsAttr         = "tags"
    28  	collectorTypeAttr         = "type"
    29  	collectorVersionAttr      = "version"
    30  )
    31  
    32  var collectorDescription = map[schemaAttr]string{
    33  	collectorDetailsAttr: "Details associated with individual collectors (a.k.a. broker)",
    34  	collectorTagsAttr:    "Tags assigned to a collector",
    35  }
    36  
    37  func dataSourceCirconusCollector() *schema.Resource {
    38  	return &schema.Resource{
    39  		Read: dataSourceCirconusCollectorRead,
    40  
    41  		Schema: map[string]*schema.Schema{
    42  			collectorDetailsAttr: &schema.Schema{
    43  				Type:        schema.TypeList,
    44  				Computed:    true,
    45  				Description: collectorDescription[collectorDetailsAttr],
    46  				Elem: &schema.Resource{
    47  					Schema: map[string]*schema.Schema{
    48  						collectorCNAttr: &schema.Schema{
    49  							Type:        schema.TypeString,
    50  							Computed:    true,
    51  							Description: collectorDescription[collectorCNAttr],
    52  						},
    53  						collectorExternalHostAttr: &schema.Schema{
    54  							Type:        schema.TypeString,
    55  							Computed:    true,
    56  							Description: collectorDescription[collectorExternalHostAttr],
    57  						},
    58  						collectorExternalPortAttr: &schema.Schema{
    59  							Type:        schema.TypeInt,
    60  							Computed:    true,
    61  							Description: collectorDescription[collectorExternalPortAttr],
    62  						},
    63  						collectorIPAttr: &schema.Schema{
    64  							Type:        schema.TypeString,
    65  							Computed:    true,
    66  							Description: collectorDescription[collectorIPAttr],
    67  						},
    68  						collectorMinVersionAttr: &schema.Schema{
    69  							Type:        schema.TypeInt,
    70  							Computed:    true,
    71  							Description: collectorDescription[collectorMinVersionAttr],
    72  						},
    73  						collectorModulesAttr: &schema.Schema{
    74  							Type:     schema.TypeList,
    75  							Computed: true,
    76  							Elem: &schema.Schema{
    77  								Type: schema.TypeString,
    78  							},
    79  							Description: collectorDescription[collectorModulesAttr],
    80  						},
    81  						collectorPortAttr: &schema.Schema{
    82  							Type:        schema.TypeInt,
    83  							Computed:    true,
    84  							Description: collectorDescription[collectorPortAttr],
    85  						},
    86  						collectorSkewAttr: &schema.Schema{
    87  							Type:        schema.TypeString,
    88  							Computed:    true,
    89  							Description: collectorDescription[collectorSkewAttr],
    90  						},
    91  						collectorStatusAttr: &schema.Schema{
    92  							Type:        schema.TypeString,
    93  							Computed:    true,
    94  							Description: collectorDescription[collectorStatusAttr],
    95  						},
    96  						collectorVersionAttr: &schema.Schema{
    97  							Type:        schema.TypeInt,
    98  							Computed:    true,
    99  							Description: collectorDescription[collectorVersionAttr],
   100  						},
   101  					},
   102  				},
   103  			},
   104  			collectorIDAttr: &schema.Schema{
   105  				Type:         schema.TypeString,
   106  				Optional:     true,
   107  				Computed:     true,
   108  				ValidateFunc: validateRegexp(collectorIDAttr, config.BrokerCIDRegex),
   109  				Description:  collectorDescription[collectorIDAttr],
   110  			},
   111  			collectorLatitudeAttr: &schema.Schema{
   112  				Type:        schema.TypeString,
   113  				Computed:    true,
   114  				Description: collectorDescription[collectorLatitudeAttr],
   115  			},
   116  			collectorLongitudeAttr: &schema.Schema{
   117  				Type:        schema.TypeString,
   118  				Computed:    true,
   119  				Description: collectorDescription[collectorLongitudeAttr],
   120  			},
   121  			collectorNameAttr: &schema.Schema{
   122  				Type:        schema.TypeString,
   123  				Computed:    true,
   124  				Description: collectorDescription[collectorNameAttr],
   125  			},
   126  			collectorTagsAttr: tagMakeConfigSchema(collectorTagsAttr),
   127  			collectorTypeAttr: &schema.Schema{
   128  				Type:        schema.TypeString,
   129  				Computed:    true,
   130  				Description: collectorDescription[collectorTypeAttr],
   131  			},
   132  		},
   133  	}
   134  }
   135  
   136  func dataSourceCirconusCollectorRead(d *schema.ResourceData, meta interface{}) error {
   137  	ctxt := meta.(*providerContext)
   138  
   139  	var collector *api.Broker
   140  	var err error
   141  	cid := d.Id()
   142  	if cidRaw, ok := d.GetOk(collectorIDAttr); ok {
   143  		cid = cidRaw.(string)
   144  	}
   145  	collector, err = ctxt.client.FetchBroker(api.CIDType(&cid))
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	d.SetId(collector.CID)
   151  
   152  	if err := d.Set(collectorDetailsAttr, collectorDetailsToState(collector)); err != nil {
   153  		return errwrap.Wrapf(fmt.Sprintf("Unable to store collector %q attribute: {{err}}", collectorDetailsAttr), err)
   154  	}
   155  
   156  	d.Set(collectorIDAttr, collector.CID)
   157  	d.Set(collectorLatitudeAttr, collector.Latitude)
   158  	d.Set(collectorLongitudeAttr, collector.Longitude)
   159  	d.Set(collectorNameAttr, collector.Name)
   160  	d.Set(collectorTagsAttr, collector.Tags)
   161  	d.Set(collectorTypeAttr, collector.Type)
   162  
   163  	return nil
   164  }
   165  
   166  func collectorDetailsToState(c *api.Broker) []interface{} {
   167  	details := make([]interface{}, 0, len(c.Details))
   168  
   169  	for _, collector := range c.Details {
   170  		collectorDetails := make(map[string]interface{}, defaultCollectorDetailAttrs)
   171  
   172  		collectorDetails[collectorCNAttr] = collector.CN
   173  
   174  		if collector.ExternalHost != nil {
   175  			collectorDetails[collectorExternalHostAttr] = *collector.ExternalHost
   176  		}
   177  
   178  		if collector.ExternalPort != 0 {
   179  			collectorDetails[collectorExternalPortAttr] = collector.ExternalPort
   180  		}
   181  
   182  		if collector.IP != nil {
   183  			collectorDetails[collectorIPAttr] = *collector.IP
   184  		}
   185  
   186  		if collector.MinVer != 0 {
   187  			collectorDetails[collectorMinVersionAttr] = collector.MinVer
   188  		}
   189  
   190  		if len(collector.Modules) > 0 {
   191  			collectorDetails[collectorModulesAttr] = collector.Modules
   192  		}
   193  
   194  		if collector.Port != nil {
   195  			collectorDetails[collectorPortAttr] = *collector.Port
   196  		}
   197  
   198  		if collector.Skew != nil {
   199  			collectorDetails[collectorSkewAttr] = *collector.Skew
   200  		}
   201  
   202  		if collector.Status != "" {
   203  			collectorDetails[collectorStatusAttr] = collector.Status
   204  		}
   205  
   206  		if collector.Version != nil {
   207  			collectorDetails[collectorVersionAttr] = *collector.Version
   208  		}
   209  
   210  		details = append(details, collectorDetails)
   211  	}
   212  
   213  	return details
   214  }