github.com/sarguru/terraform@v0.6.17-0.20160525232901-8fcdfd7e3dc9/builtin/providers/nsone/resource_apikey.go (about)

     1  package nsone
     2  
     3  import (
     4  	"github.com/bobtfish/go-nsone-api"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  )
     7  
     8  func apikeyResource() *schema.Resource {
     9  	s := map[string]*schema.Schema{
    10  		"id": &schema.Schema{
    11  			Type:     schema.TypeString,
    12  			Computed: true,
    13  		},
    14  		"name": &schema.Schema{
    15  			Type:     schema.TypeString,
    16  			Required: true,
    17  		},
    18  		"key": &schema.Schema{
    19  			Type:     schema.TypeString,
    20  			Computed: true,
    21  		},
    22  		"teams": &schema.Schema{
    23  			Type:     schema.TypeList,
    24  			Optional: true,
    25  			Elem:     &schema.Schema{Type: schema.TypeString},
    26  		},
    27  	}
    28  	s = addPermsSchema(s)
    29  	return &schema.Resource{
    30  		Schema: s,
    31  		Create: ApikeyCreate,
    32  		Read:   ApikeyRead,
    33  		Update: ApikeyUpdate,
    34  		Delete: ApikeyDelete,
    35  	}
    36  }
    37  
    38  func apikeyToResourceData(d *schema.ResourceData, u *nsone.Apikey) error {
    39  	d.SetId(u.Id)
    40  	d.Set("name", u.Name)
    41  	d.Set("key", u.Key)
    42  	d.Set("teams", u.Teams)
    43  	permissionsToResourceData(d, u.Permissions)
    44  	return nil
    45  }
    46  
    47  func resourceDataToPermissions(d *schema.ResourceData) nsone.PermissionsMap {
    48  	var p nsone.PermissionsMap
    49  	if v, ok := d.GetOk("dns_view_zones"); ok {
    50  		p.Dns.ViewZones = v.(bool)
    51  	}
    52  	if v, ok := d.GetOk("dns_manage_zones"); ok {
    53  		p.Dns.ManageZones = v.(bool)
    54  	}
    55  	if v, ok := d.GetOk("dns_zones_allow_by_default"); ok {
    56  		p.Dns.ZonesAllowByDefault = v.(bool)
    57  	}
    58  	if v, ok := d.GetOk("dns_zones_deny"); ok {
    59  		denyRaw := v.([]interface{})
    60  		p.Dns.ZonesDeny = make([]string, len(denyRaw))
    61  		for i, deny := range denyRaw {
    62  			p.Dns.ZonesDeny[i] = deny.(string)
    63  		}
    64  	} else {
    65  		p.Dns.ZonesDeny = make([]string, 0)
    66  	}
    67  	if v, ok := d.GetOk("dns_zones_allow"); ok {
    68  		allowRaw := v.([]interface{})
    69  		p.Dns.ZonesAllow = make([]string, len(allowRaw))
    70  		for i, allow := range allowRaw {
    71  			p.Dns.ZonesAllow[i] = allow.(string)
    72  		}
    73  	} else {
    74  		p.Dns.ZonesAllow = make([]string, 0)
    75  	}
    76  	if v, ok := d.GetOk("data_push_to_datafeeds"); ok {
    77  		p.Data.PushToDatafeeds = v.(bool)
    78  	}
    79  	if v, ok := d.GetOk("data_manage_datasources"); ok {
    80  		p.Data.ManageDatasources = v.(bool)
    81  	}
    82  	if v, ok := d.GetOk("data_manage_datafeeds"); ok {
    83  		p.Data.ManageDatafeeds = v.(bool)
    84  	}
    85  	if v, ok := d.GetOk("account_manage_users"); ok {
    86  		p.Account.ManageUsers = v.(bool)
    87  	}
    88  	if v, ok := d.GetOk("account_manage_payment_methods"); ok {
    89  		p.Account.ManagePaymentMethods = v.(bool)
    90  	}
    91  	if v, ok := d.GetOk("account_manage_plan"); ok {
    92  		p.Account.ManagePlan = v.(bool)
    93  	}
    94  	if v, ok := d.GetOk("account_manage_teams"); ok {
    95  		p.Account.ManageTeams = v.(bool)
    96  	}
    97  	if v, ok := d.GetOk("account_manage_apikeys"); ok {
    98  		p.Account.ManageApikeys = v.(bool)
    99  	}
   100  	if v, ok := d.GetOk("account_manage_account_settings"); ok {
   101  		p.Account.ManageAccountSettings = v.(bool)
   102  	}
   103  	if v, ok := d.GetOk("account_view_activity_log"); ok {
   104  		p.Account.ViewActivityLog = v.(bool)
   105  	}
   106  	if v, ok := d.GetOk("account_view_invoices"); ok {
   107  		p.Account.ViewInvoices = v.(bool)
   108  	}
   109  	if v, ok := d.GetOk("monitoring_manage_lists"); ok {
   110  		p.Monitoring.ManageLists = v.(bool)
   111  	}
   112  	if v, ok := d.GetOk("monitoring_manage_jobs"); ok {
   113  		p.Monitoring.ManageJobs = v.(bool)
   114  	}
   115  	if v, ok := d.GetOk("monitoring_view_jobs"); ok {
   116  		p.Monitoring.ViewJobs = v.(bool)
   117  	}
   118  	return p
   119  }
   120  
   121  func resourceDataToApikey(u *nsone.Apikey, d *schema.ResourceData) error {
   122  	u.Id = d.Id()
   123  	u.Name = d.Get("name").(string)
   124  	if v, ok := d.GetOk("teams"); ok {
   125  		teamsRaw := v.([]interface{})
   126  		u.Teams = make([]string, len(teamsRaw))
   127  		for i, team := range teamsRaw {
   128  			u.Teams[i] = team.(string)
   129  		}
   130  	} else {
   131  		u.Teams = make([]string, 0)
   132  	}
   133  	u.Permissions = resourceDataToPermissions(d)
   134  	return nil
   135  }
   136  
   137  // ApikeyCreate creates ns1 API key
   138  func ApikeyCreate(d *schema.ResourceData, meta interface{}) error {
   139  	client := meta.(*nsone.APIClient)
   140  	mj := nsone.Apikey{}
   141  	if err := resourceDataToApikey(&mj, d); err != nil {
   142  		return err
   143  	}
   144  	if err := client.CreateApikey(&mj); err != nil {
   145  		return err
   146  	}
   147  	return apikeyToResourceData(d, &mj)
   148  }
   149  
   150  // ApikeyRead reads API key from ns1
   151  func ApikeyRead(d *schema.ResourceData, meta interface{}) error {
   152  	client := meta.(*nsone.APIClient)
   153  	mj, err := client.GetApikey(d.Id())
   154  	if err != nil {
   155  		return err
   156  	}
   157  	apikeyToResourceData(d, &mj)
   158  	return nil
   159  }
   160  
   161  //ApikeyDelete deletes the given ns1 api key
   162  func ApikeyDelete(d *schema.ResourceData, meta interface{}) error {
   163  	client := meta.(*nsone.APIClient)
   164  	err := client.DeleteApikey(d.Id())
   165  	d.SetId("")
   166  	return err
   167  }
   168  
   169  //ApikeyUpdate updates the given api key in ns1
   170  func ApikeyUpdate(d *schema.ResourceData, meta interface{}) error {
   171  	client := meta.(*nsone.APIClient)
   172  	mj := nsone.Apikey{
   173  		Id: d.Id(),
   174  	}
   175  	if err := resourceDataToApikey(&mj, d); err != nil {
   176  		return err
   177  	}
   178  	if err := client.UpdateApikey(&mj); err != nil {
   179  		return err
   180  	}
   181  	apikeyToResourceData(d, &mj)
   182  	return nil
   183  }