github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/data_source_circonus_account.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  	accountAddress1Attr      = "address1"
    14  	accountAddress2Attr      = "address2"
    15  	accountCCEmailAttr       = "cc_email"
    16  	accountCityAttr          = "city"
    17  	accountContactGroupsAttr = "contact_groups"
    18  	accountCountryAttr       = "country"
    19  	accountCurrentAttr       = "current"
    20  	accountDescriptionAttr   = "description"
    21  	accountEmailAttr         = "email"
    22  	accountIDAttr            = "id"
    23  	accountInvitesAttr       = "invites"
    24  	accountLimitAttr         = "limit"
    25  	accountNameAttr          = "name"
    26  	accountOwnerAttr         = "owner"
    27  	accountRoleAttr          = "role"
    28  	accountStateProvAttr     = "state"
    29  	accountTimezoneAttr      = "timezone"
    30  	accountTypeAttr          = "type"
    31  	accountUIBaseURLAttr     = "ui_base_url"
    32  	accountUsageAttr         = "usage"
    33  	accountUsedAttr          = "used"
    34  	accountUserIDAttr        = "id"
    35  	accountUsersAttr         = "users"
    36  )
    37  
    38  var accountDescription = map[schemaAttr]string{
    39  	accountContactGroupsAttr: "Contact Groups in this account",
    40  	accountInvitesAttr:       "Outstanding invites attached to the account",
    41  	accountUsageAttr:         "Account's usage limits",
    42  	accountUsersAttr:         "Users attached to this account",
    43  }
    44  
    45  func dataSourceCirconusAccount() *schema.Resource {
    46  	return &schema.Resource{
    47  		Read: dataSourceCirconusAccountRead,
    48  
    49  		Schema: map[string]*schema.Schema{
    50  			accountAddress1Attr: &schema.Schema{
    51  				Type:        schema.TypeString,
    52  				Computed:    true,
    53  				Description: accountDescription[accountAddress1Attr],
    54  			},
    55  			accountAddress2Attr: &schema.Schema{
    56  				Type:        schema.TypeString,
    57  				Computed:    true,
    58  				Description: accountDescription[accountAddress2Attr],
    59  			},
    60  			accountCCEmailAttr: &schema.Schema{
    61  				Type:        schema.TypeString,
    62  				Computed:    true,
    63  				Description: accountDescription[accountCCEmailAttr],
    64  			},
    65  			accountIDAttr: &schema.Schema{
    66  				Type:          schema.TypeString,
    67  				Optional:      true,
    68  				Computed:      true,
    69  				ConflictsWith: []string{accountCurrentAttr},
    70  				ValidateFunc: validateFuncs(
    71  					validateRegexp(accountIDAttr, config.AccountCIDRegex),
    72  				),
    73  				Description: accountDescription[accountIDAttr],
    74  			},
    75  			accountCityAttr: &schema.Schema{
    76  				Type:        schema.TypeString,
    77  				Computed:    true,
    78  				Description: accountDescription[accountCityAttr],
    79  			},
    80  			accountContactGroupsAttr: &schema.Schema{
    81  				Type:        schema.TypeList,
    82  				Computed:    true,
    83  				Elem:        &schema.Schema{Type: schema.TypeString},
    84  				Description: accountDescription[accountContactGroupsAttr],
    85  			},
    86  			accountCountryAttr: &schema.Schema{
    87  				Type:        schema.TypeString,
    88  				Computed:    true,
    89  				Description: accountDescription[accountCountryAttr],
    90  			},
    91  			accountCurrentAttr: &schema.Schema{
    92  				Type:          schema.TypeBool,
    93  				Optional:      true,
    94  				Computed:      true,
    95  				ConflictsWith: []string{accountIDAttr},
    96  				Description:   accountDescription[accountCurrentAttr],
    97  			},
    98  			accountDescriptionAttr: &schema.Schema{
    99  				Type:        schema.TypeString,
   100  				Computed:    true,
   101  				Description: accountDescription[accountDescriptionAttr],
   102  			},
   103  			accountInvitesAttr: &schema.Schema{
   104  				Type:        schema.TypeList,
   105  				Computed:    true,
   106  				Description: accountDescription[accountInvitesAttr],
   107  				Elem: &schema.Resource{
   108  					Schema: map[string]*schema.Schema{
   109  						accountEmailAttr: &schema.Schema{
   110  							Type:        schema.TypeString,
   111  							Computed:    true,
   112  							Description: accountDescription[accountEmailAttr],
   113  						},
   114  						accountRoleAttr: &schema.Schema{
   115  							Type:        schema.TypeString,
   116  							Computed:    true,
   117  							Description: accountDescription[accountRoleAttr],
   118  						},
   119  					},
   120  				},
   121  			},
   122  			accountNameAttr: &schema.Schema{
   123  				Type:        schema.TypeString,
   124  				Computed:    true,
   125  				Description: accountDescription[accountNameAttr],
   126  			},
   127  			accountOwnerAttr: &schema.Schema{
   128  				Type:        schema.TypeString,
   129  				Computed:    true,
   130  				Description: accountDescription[accountOwnerAttr],
   131  			},
   132  			accountStateProvAttr: &schema.Schema{
   133  				Type:        schema.TypeString,
   134  				Computed:    true,
   135  				Description: accountDescription[accountStateProvAttr],
   136  			},
   137  			accountTimezoneAttr: &schema.Schema{
   138  				Type:        schema.TypeString,
   139  				Computed:    true,
   140  				Description: accountDescription[accountTimezoneAttr],
   141  			},
   142  			accountUIBaseURLAttr: &schema.Schema{
   143  				Type:        schema.TypeString,
   144  				Computed:    true,
   145  				Description: accountDescription[accountUIBaseURLAttr],
   146  			},
   147  			accountUsageAttr: &schema.Schema{
   148  				Type:        schema.TypeList,
   149  				Computed:    true,
   150  				Description: accountDescription[accountUsageAttr],
   151  				Elem: &schema.Resource{
   152  					Schema: map[string]*schema.Schema{
   153  						accountLimitAttr: &schema.Schema{
   154  							Type:        schema.TypeInt,
   155  							Computed:    true,
   156  							Description: accountDescription[accountLimitAttr],
   157  						},
   158  						accountTypeAttr: &schema.Schema{
   159  							Type:        schema.TypeString,
   160  							Computed:    true,
   161  							Description: accountDescription[accountTypeAttr],
   162  						},
   163  						accountUsedAttr: &schema.Schema{
   164  							Type:        schema.TypeInt,
   165  							Computed:    true,
   166  							Description: accountDescription[accountUsedAttr],
   167  						},
   168  					},
   169  				},
   170  			},
   171  			accountUsersAttr: &schema.Schema{
   172  				Type:        schema.TypeList,
   173  				Computed:    true,
   174  				Description: accountDescription[accountUsersAttr],
   175  				Elem: &schema.Resource{
   176  					Schema: map[string]*schema.Schema{
   177  						accountUserIDAttr: &schema.Schema{
   178  							Type:        schema.TypeString,
   179  							Computed:    true,
   180  							Description: accountDescription[accountUserIDAttr],
   181  						},
   182  						accountRoleAttr: &schema.Schema{
   183  							Type:        schema.TypeString,
   184  							Computed:    true,
   185  							Description: accountDescription[accountRoleAttr],
   186  						},
   187  					},
   188  				},
   189  			},
   190  		},
   191  	}
   192  }
   193  
   194  func dataSourceCirconusAccountRead(d *schema.ResourceData, meta interface{}) error {
   195  	c := meta.(*providerContext)
   196  
   197  	var cid string
   198  
   199  	var a *api.Account
   200  	var err error
   201  	if v, ok := d.GetOk(accountIDAttr); ok {
   202  		cid = v.(string)
   203  	}
   204  
   205  	if v, ok := d.GetOk(accountCurrentAttr); ok {
   206  		if v.(bool) {
   207  			cid = ""
   208  		}
   209  	}
   210  
   211  	a, err = c.client.FetchAccount(api.CIDType(&cid))
   212  	if err != nil {
   213  		return err
   214  	}
   215  
   216  	invitesList := make([]interface{}, 0, len(a.Invites))
   217  	for i := range a.Invites {
   218  		invitesList = append(invitesList, map[string]interface{}{
   219  			accountEmailAttr: a.Invites[i].Email,
   220  			accountRoleAttr:  a.Invites[i].Role,
   221  		})
   222  	}
   223  
   224  	usageList := make([]interface{}, 0, len(a.Usage))
   225  	for i := range a.Usage {
   226  		usageList = append(usageList, map[string]interface{}{
   227  			accountLimitAttr: a.Usage[i].Limit,
   228  			accountTypeAttr:  a.Usage[i].Type,
   229  			accountUsedAttr:  a.Usage[i].Used,
   230  		})
   231  	}
   232  
   233  	usersList := make([]interface{}, 0, len(a.Users))
   234  	for i := range a.Users {
   235  		usersList = append(usersList, map[string]interface{}{
   236  			accountUserIDAttr: a.Users[i].UserCID,
   237  			accountRoleAttr:   a.Users[i].Role,
   238  		})
   239  	}
   240  
   241  	d.SetId(a.CID)
   242  
   243  	d.Set(accountAddress1Attr, a.Address1)
   244  	d.Set(accountAddress2Attr, a.Address2)
   245  	d.Set(accountCCEmailAttr, a.CCEmail)
   246  	d.Set(accountIDAttr, a.CID)
   247  	d.Set(accountCityAttr, a.City)
   248  	d.Set(accountContactGroupsAttr, a.ContactGroups)
   249  	d.Set(accountCountryAttr, a.Country)
   250  	d.Set(accountDescriptionAttr, a.Description)
   251  
   252  	if err := d.Set(accountInvitesAttr, invitesList); err != nil {
   253  		return errwrap.Wrapf(fmt.Sprintf("Unable to store account %q attribute: {{err}}", accountInvitesAttr), err)
   254  	}
   255  
   256  	d.Set(accountNameAttr, a.Name)
   257  	d.Set(accountOwnerAttr, a.OwnerCID)
   258  	d.Set(accountStateProvAttr, a.StateProv)
   259  	d.Set(accountTimezoneAttr, a.Timezone)
   260  	d.Set(accountUIBaseURLAttr, a.UIBaseURL)
   261  
   262  	if err := d.Set(accountUsageAttr, usageList); err != nil {
   263  		return errwrap.Wrapf(fmt.Sprintf("Unable to store account %q attribute: {{err}}", accountUsageAttr), err)
   264  	}
   265  
   266  	if err := d.Set(accountUsersAttr, usersList); err != nil {
   267  		return errwrap.Wrapf(fmt.Sprintf("Unable to store account %q attribute: {{err}}", accountUsersAttr), err)
   268  	}
   269  
   270  	return nil
   271  }