github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ns1/resource_team.go (about)

     1  package ns1
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  
     6  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
     7  	"gopkg.in/ns1/ns1-go.v2/rest/model/account"
     8  )
     9  
    10  func teamResource() *schema.Resource {
    11  	s := map[string]*schema.Schema{
    12  		"id": &schema.Schema{
    13  			Type:     schema.TypeString,
    14  			Computed: true,
    15  		},
    16  		"name": &schema.Schema{
    17  			Type:     schema.TypeString,
    18  			Required: true,
    19  		},
    20  	}
    21  	s = addPermsSchema(s)
    22  	return &schema.Resource{
    23  		Schema: s,
    24  		Create: TeamCreate,
    25  		Read:   TeamRead,
    26  		Update: TeamUpdate,
    27  		Delete: TeamDelete,
    28  	}
    29  }
    30  
    31  func teamToResourceData(d *schema.ResourceData, t *account.Team) error {
    32  	d.SetId(t.ID)
    33  	d.Set("name", t.Name)
    34  	permissionsToResourceData(d, t.Permissions)
    35  	return nil
    36  }
    37  
    38  func resourceDataToTeam(t *account.Team, d *schema.ResourceData) error {
    39  	t.ID = d.Id()
    40  	t.Name = d.Get("name").(string)
    41  	t.Permissions = resourceDataToPermissions(d)
    42  	return nil
    43  }
    44  
    45  // TeamCreate creates the given team in ns1
    46  func TeamCreate(d *schema.ResourceData, meta interface{}) error {
    47  	client := meta.(*ns1.Client)
    48  	t := account.Team{}
    49  	if err := resourceDataToTeam(&t, d); err != nil {
    50  		return err
    51  	}
    52  	if _, err := client.Teams.Create(&t); err != nil {
    53  		return err
    54  	}
    55  	return teamToResourceData(d, &t)
    56  }
    57  
    58  // TeamRead reads the team data from ns1
    59  func TeamRead(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*ns1.Client)
    61  	t, _, err := client.Teams.Get(d.Id())
    62  	if err != nil {
    63  		return err
    64  	}
    65  	return teamToResourceData(d, t)
    66  }
    67  
    68  // TeamDelete deletes the given team from ns1
    69  func TeamDelete(d *schema.ResourceData, meta interface{}) error {
    70  	client := meta.(*ns1.Client)
    71  	_, err := client.Teams.Delete(d.Id())
    72  	d.SetId("")
    73  	return err
    74  }
    75  
    76  // TeamUpdate updates the given team in ns1
    77  func TeamUpdate(d *schema.ResourceData, meta interface{}) error {
    78  	client := meta.(*ns1.Client)
    79  	t := account.Team{
    80  		ID: d.Id(),
    81  	}
    82  	if err := resourceDataToTeam(&t, d); err != nil {
    83  		return err
    84  	}
    85  	if _, err := client.Teams.Update(&t); err != nil {
    86  		return err
    87  	}
    88  	return teamToResourceData(d, &t)
    89  }