Skip to content

Commit d261591

Browse files
authored
Merge pull request #8 from dbolack/add_system_groups
Add system groups
2 parents eba9d08 + 2aca520 commit d261591

File tree

5 files changed

+257
-3
lines changed

5 files changed

+257
-3
lines changed

examples/system-groups/base.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "jumpcloud" {
2+
api_key = "MY_API_KEY"
3+
}

examples/system-groups/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "jumpcloud_system_group" "test_group" {
2+
name = "Jumpcloud Provider Group"
3+
}

jumpcloud/provider.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ func Provider() *schema.Provider {
2323
},
2424
},
2525
ResourcesMap: map[string]*schema.Resource{
26-
"jumpcloud_user": resourceUser(),
27-
"jumpcloud_user_group": resourceUserGroup(),
28-
"jumpcloud_user_group_membership": resourceUserGroupMembership(),
26+
"jumpcloud_user": resourceUser(),
27+
"jumpcloud_user_group": resourceUserGroup(),
28+
"jumpcloud_user_group_membership": resourceUserGroupMembership(),
29+
"jumpcloud_system_group": resourceGroupsSystem(),
30+
"jumpcloud_system_group_user_group_membership": resourceSystemGroupUserGroupMembership(),
2931
},
3032
ConfigureFunc: providerConfigure,
3133
}

jumpcloud/resource_system_group.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package jumpcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
8+
"github.com/hashicorp/terraform/helper/schema"
9+
)
10+
11+
func resourceGroupsSystem() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceGroupsSystemCreate,
14+
Read: resourceGroupsSystemRead,
15+
Update: resourceGroupsSystemUpdate,
16+
Delete: resourceGroupsSystemDelete,
17+
Schema: map[string]*schema.Schema{
18+
"name": {
19+
Type: schema.TypeString,
20+
Optional: true,
21+
},
22+
"jc_id": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
},
26+
},
27+
Importer: &schema.ResourceImporter{
28+
State: schema.ImportStatePassthrough,
29+
},
30+
}
31+
}
32+
33+
func resourceGroupsSystemCreate(d *schema.ResourceData, m interface{}) error {
34+
config := m.(*jcapiv2.Configuration)
35+
client := jcapiv2.NewAPIClient(config)
36+
37+
body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)}
38+
39+
req := map[string]interface{}{
40+
"body": body,
41+
}
42+
group, res, err := client.SystemGroupsApi.GroupsSystemPost(context.TODO(),
43+
"", headerAccept, req)
44+
if err != nil {
45+
// TODO: sort out error essentials
46+
return fmt.Errorf("error creating system group %s: %s - response = %+v",
47+
(req["body"].(jcapiv2.SystemGroupData)).Name, err, res)
48+
}
49+
50+
d.SetId(group.Name)
51+
d.Set("name", group.Name)
52+
d.Set("jc_id", group.Id)
53+
return resourceGroupsSystemRead(d, m)
54+
}
55+
56+
// Helper to look up a system group by name
57+
func resourceGroupsSystemList_match(d *schema.ResourceData, m interface{}) (jcapiv2.SystemGroup, error) {
58+
config := m.(*jcapiv2.Configuration)
59+
client := jcapiv2.NewAPIClient(config)
60+
61+
var filter []string
62+
63+
filter = append(filter, "name:eq:"+d.Id())
64+
65+
optional := map[string]interface{}{
66+
"filter": filter,
67+
}
68+
69+
result, _, err := client.SystemGroupsApi.GroupsSystemList(context.TODO(),
70+
"", headerAccept, optional)
71+
if err == nil {
72+
if len(result) < 1 {
73+
return jcapiv2.SystemGroup{}, fmt.Errorf("System Group \"%s\" not found.", d.Id())
74+
} else {
75+
return result[0], nil
76+
}
77+
} else {
78+
return jcapiv2.SystemGroup{}, err
79+
}
80+
}
81+
82+
func resourceGroupsSystemRead(d *schema.ResourceData, m interface{}) error {
83+
config := m.(*jcapiv2.Configuration)
84+
client := jcapiv2.NewAPIClient(config)
85+
86+
var id string
87+
88+
id = d.Get("jc_id").(string)
89+
90+
if id == "" {
91+
id_lookup, err := resourceGroupsSystemList_match(d, m)
92+
if err != nil {
93+
return fmt.Errorf("Unable to locate ID for group %s, %+v",
94+
d.Get("name"), err)
95+
}
96+
id = id_lookup.Id
97+
d.SetId(id_lookup.Name)
98+
d.Set("name", id_lookup.Name)
99+
d.Set("jc_id", id_lookup.Id)
100+
}
101+
102+
group, res, err := client.SystemGroupsApi.GroupsSystemGet(context.TODO(),
103+
id, "", headerAccept, nil)
104+
if err != nil {
105+
// TODO: sort out error essentials
106+
return fmt.Errorf("error reading system group ID %s: %s - response = %+v",
107+
d.Id(), err, res)
108+
}
109+
110+
d.SetId(group.Name)
111+
d.Set("name", group.Name)
112+
d.Set("jc_id", group.Id)
113+
return nil
114+
}
115+
116+
func resourceGroupsSystemUpdate(d *schema.ResourceData, m interface{}) error {
117+
config := m.(*jcapiv2.Configuration)
118+
client := jcapiv2.NewAPIClient(config)
119+
120+
var id string
121+
id = d.Get("jc_id").(string)
122+
123+
body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)}
124+
125+
req := map[string]interface{}{
126+
"body": body,
127+
}
128+
129+
group, res, err := client.SystemGroupsApi.GroupsSystemPut(context.TODO(),
130+
id, "", headerAccept, req)
131+
if err != nil {
132+
// TODO: sort out error essentials
133+
return fmt.Errorf("error updating system group %s: %s - response = %+v",
134+
d.Get("name"), err, res)
135+
}
136+
137+
d.SetId(group.Name)
138+
d.Set("name", group.Name)
139+
d.Set("jc_id", group.Id)
140+
return resourceGroupsSystemRead(d, m)
141+
}
142+
143+
func resourceGroupsSystemDelete(d *schema.ResourceData, m interface{}) error {
144+
config := m.(*jcapiv2.Configuration)
145+
client := jcapiv2.NewAPIClient(config)
146+
147+
var id string
148+
id = d.Get("jc_id").(string)
149+
150+
res, err := client.SystemGroupsApi.GroupsSystemDelete(context.TODO(),
151+
id, "", headerAccept, nil)
152+
if err != nil {
153+
// TODO: sort out error essentials
154+
return fmt.Errorf("error deleting system group:%s; response = %+v", err, res)
155+
}
156+
d.SetId("")
157+
return nil
158+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package jumpcloud
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
10+
"github.com/hashicorp/terraform/helper/acctest"
11+
"github.com/hashicorp/terraform/helper/resource"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/suite"
14+
)
15+
16+
func TestAccSystemGroup(t *testing.T) {
17+
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
18+
posixName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
19+
gid := acctest.RandIntRange(1, 1000)
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { testAccPreCheck(t) },
23+
Providers: testAccProviders,
24+
CheckDestroy: nil,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccSystemGroup(rName, gid, posixName),
28+
Check: resource.ComposeAggregateTestCheckFunc(
29+
resource.TestCheckResourceAttr("jumpcloud_system_group.test_group", "name", rName),
30+
resource.TestCheckResourceAttr("jumpcloud_system_group.test_group",
31+
"attributes.posix_groups", fmt.Sprintf("%d:%s", gid, posixName)),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testAccSystemGroup(name string, gid int, posixName string) string {
39+
return fmt.Sprintf(`
40+
resource "jumpcloud_system_group" "test_group" {
41+
name = "%s"
42+
}`, name, gid, posixName,
43+
)
44+
}
45+
46+
func TestResourceSystemGroup(t *testing.T) {
47+
suite.Run(t, new(ResourceSystemGroupSuite))
48+
}
49+
50+
type ResourceSystemGroupSuite struct {
51+
suite.Suite
52+
A *assert.Assertions
53+
TestHTTPServer *httptest.Server
54+
}
55+
56+
func (s *ResourceSystemGroupSuite) SetupSuite() {
57+
s.A = assert.New(s.Suite.T())
58+
}
59+
60+
func (s *ResourceSystemGroupSuite) TestTrueSystemGroupRead() {
61+
cases := []struct {
62+
ResponseStatus int
63+
SystemGroupNil bool
64+
OK bool
65+
ErrorNil bool
66+
Payload []byte
67+
}{
68+
{http.StatusNotFound, true, false, true, []byte("irrelevant")},
69+
{http.StatusOK, false, true, true, []byte("{}")},
70+
}
71+
72+
for _, c := range cases {
73+
testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
74+
rw.WriteHeader(c.ResponseStatus)
75+
rw.Write(c.Payload)
76+
}))
77+
78+
config := &jcapiv2.Configuration{
79+
BasePath: testServer.URL,
80+
}
81+
82+
ug, ok, err := systemGroupReadHelper(config, "id")
83+
s.A.Equal(c.OK, ok)
84+
s.A.Equal(c.SystemGroupNil, ug == nil)
85+
s.A.Equal(c.ErrorNil, err == nil)
86+
testServer.Close()
87+
}
88+
}

0 commit comments

Comments
 (0)