-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_system_group.go
147 lines (121 loc) · 4.11 KB
/
resource_system_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package jumpcloud
import (
"context"
"fmt"
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 resourceGroupsSystem() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGroupsSystemCreate,
ReadContext: resourceGroupsSystemRead,
UpdateContext: resourceGroupsSystemUpdate,
DeleteContext: resourceGroupsSystemDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"jc_id": {
Type: schema.TypeString,
Computed: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceGroupsSystemCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)}
req := map[string]interface{}{
"body": body,
}
group, res, err := client.SystemGroupsApi.GroupsSystemPost(ctx, "", headerAccept, req)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error creating system group %s: %s - response = %+v",
(req["body"].(jcapiv2.SystemGroupData)).Name, err, res)
}
d.SetId(group.Name)
d.Set("name", group.Name)
d.Set("jc_id", group.Id)
return resourceGroupsSystemRead(ctx, d, m)
}
// Helper to look up a system group by name
func resourceGroupsSystemList_match(ctx context.Context, d *schema.ResourceData, m interface{}) (jcapiv2.SystemGroup, error) {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
var filter []string
filter = append(filter, "name:eq:"+d.Id())
optional := map[string]interface{}{
"filter": filter,
}
result, _, err := client.SystemGroupsApi.GroupsSystemList(ctx, "", headerAccept, optional)
if err == nil {
if len(result) < 1 {
return jcapiv2.SystemGroup{}, fmt.Errorf("system group \"%s\" not found", d.Id())
} else {
return result[0], nil
}
} else {
return jcapiv2.SystemGroup{}, err
}
}
func resourceGroupsSystemRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
var id string = d.Get("jc_id").(string)
if id == "" {
id_lookup, err := resourceGroupsSystemList_match(ctx, d, m)
if err != nil {
return diag.Errorf("unable to locate ID for group %s, %+v", d.Get("name"), err)
}
id = id_lookup.Id
d.SetId(id_lookup.Name)
d.Set("name", id_lookup.Name)
d.Set("jc_id", id_lookup.Id)
}
group, res, err := client.SystemGroupsApi.GroupsSystemGet(ctx, id, "", headerAccept, nil)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error reading system group ID %s: %s - response = %+v", d.Id(), err, res)
}
d.SetId(group.Name)
d.Set("name", group.Name)
d.Set("jc_id", group.Id)
return nil
}
func resourceGroupsSystemUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
var id string = d.Get("jc_id").(string)
body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)}
req := map[string]interface{}{
"body": body,
}
group, res, err := client.SystemGroupsApi.GroupsSystemPut(ctx, id, "", headerAccept, req)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error updating system group %s: %s - response = %+v", d.Get("name"), err, res)
}
d.SetId(group.Name)
d.Set("name", group.Name)
d.Set("jc_id", group.Id)
return resourceGroupsSystemRead(ctx, d, m)
}
func resourceGroupsSystemDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)
var id string = d.Get("jc_id").(string)
res, err := client.SystemGroupsApi.GroupsSystemDelete(ctx, id, "", headerAccept, nil)
if err != nil {
// TODO: sort out error essentials
return diag.Errorf("error deleting system group:%s; response = %+v", err, res)
}
d.SetId("")
return nil
}