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