-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_user_group.go
104 lines (84 loc) · 2.77 KB
/
resource_user_group.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package jumpcloud
import (
"context"
jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceUserGroup() *schema.Resource {
return &schema.Resource{
CreateContext: resourceUserGroupCreate,
ReadContext: resourceUserGroupRead,
UpdateContext: resourceUserGroupUpdate,
DeleteContext: resourceUserGroupDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceUserGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
body := jcapiv2.UserGroupPost{Name: d.Get("name").(string)}
req := map[string]interface{}{
"body": body,
}
group, res, err := client.UserGroupsApi.GroupsUserPost(ctx, "", headerAccept, req)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error creating user group %s: %s - response = %+v",
(req["body"].(jcapiv2.UserGroupPost)).Name, err, res)
}
d.SetId(group.Id)
return resourceUserGroupRead(ctx, d, m)
}
func resourceUserGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
res, _, err := client.UserGroupsApi.GroupsUserGet(context.TODO(), d.Id(), "", headerAccept, nil)
if err != nil {
if err.Error() == "EOF" {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
d.SetId(res.Id)
if err := d.Set("name", res.Name); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceUserGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
body := jcapiv2.UserGroupPost{Name: d.Get("name").(string)}
req := map[string]interface{}{
"body": body,
}
_, res, err := client.UserGroupsApi.GroupsUserPatch(context.TODO(),
d.Id(), "", headerAccept, req)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error updating user group:%s; response = %+v", err, res)
}
return resourceUserGroupRead(ctx, d, m)
}
func resourceUserGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
res, err := client.UserGroupsApi.GroupsUserDelete(context.TODO(),
d.Id(), "", headerAccept, nil)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error deleting user group:%s; response = %+v", err, res)
}
d.SetId("")
return nil
}