Skip to content

Commit

Permalink
Convert jc_id to computed value
Browse files Browse the repository at this point in the history
By converting jc_id to a computed value, we are able to use the name as
the Resource ID and store the neccesary values for the resource without
the user needing to know the jumpcloud system group id.
  • Loading branch information
dbolack committed Sep 14, 2020
1 parent b62ce28 commit 99626e1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
6 changes: 3 additions & 3 deletions examples/system-groups/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
resource "jumpcloud_system_group" "test_group" {
name = "Jumpcloud Provider Group"
}
# resource "jumpcloud_system_group" "test_group" {
# name = "Jumpcloud Provider Group"
# }
2 changes: 1 addition & 1 deletion jumpcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Provider() *schema.Provider {
"jumpcloud_user_group_membership": resourceUserGroupMembership(),
"jumpcloud_system_group": resourceGroupsSystem(),
// "jumpcloud_system_group_membership": resourceSystemGroupMembership(),
// "jumpcloud_system_group_user_group_membership": resourceSystemGroupUserGroupMembership(),
//"jumpcloud_system_group_user_group_membership": resourceSystemGroupUserGroupMembership(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
65 changes: 57 additions & 8 deletions jumpcloud/resource_system_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func resourceGroupsSystem() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"jc_id": {
Type: schema.TypeString,
Computed: true,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -43,28 +47,69 @@ func resourceGroupsSystemCreate(d *schema.ResourceData, m interface{}) error {
(req["body"].(jcapiv2.SystemGroupData)).Name, err, res)
}

d.SetId(group.Id)
d.SetId(group.Name)
d.Set("name", group.Name)
d.Set("jc_id", group.Id)
return resourceGroupsSystemRead(d, m)
}

// Helper to look up a system group by name
func resourceGroupsSystemList_match(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(context.TODO(),
"", 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(d *schema.ResourceData, m interface{}) error {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)

var id string

id = d.Id()
id = d.Get("jc_id").(string)

if id == "" {
id_lookup, err := resourceGroupsSystemList_match(d, m)
if err != nil {
return fmt.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(context.TODO(),
id, "", headerAccept, nil)
if err != nil {
// TODO: sort out error essentials
return fmt.Errorf("error reading system group ID %s: %s - response = %+v",
d.Get("id").(string), err, res)
d.Id(), err, res)
}

d.SetId(group.Id)
d.SetId(group.Name)
d.Set("name", group.Name)
d.Set("jc_id", group.Id)
return nil
}

Expand All @@ -73,8 +118,7 @@ func resourceGroupsSystemUpdate(d *schema.ResourceData, m interface{}) error {
client := jcapiv2.NewAPIClient(config)

var id string

id = d.Id()
id = d.Get("jc_id").(string)

body := jcapiv2.SystemGroupData{Name: d.Get("name").(string)}

Expand All @@ -90,16 +134,21 @@ func resourceGroupsSystemUpdate(d *schema.ResourceData, m interface{}) error {
d.Get("name"), err, res)
}

d.SetId(group.Id)
d.SetId(group.Name)
d.Set("name", group.Name)
d.Set("jc_id", group.Id)
return resourceGroupsSystemRead(d, m)
}

func resourceGroupsSystemDelete(d *schema.ResourceData, m interface{}) error {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)

var id string
id = d.Get("jc_id").(string)

res, err := client.SystemGroupsApi.GroupsSystemDelete(context.TODO(),
d.Id(), "", headerAccept, nil)
id, "", headerAccept, nil)
if err != nil {
// TODO: sort out error essentials
return fmt.Errorf("error deleting system group:%s; response = %+v", err, res)
Expand Down
2 changes: 1 addition & 1 deletion jumpcloud/resource_user_group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func resourceUserGroupMembership() *schema.Resource {
},
},
Importer: &schema.ResourceImporter{
schema.ImportStatePassthrough,
State: userGroupMembershipImporter,
},
}
}
Expand Down

0 comments on commit 99626e1

Please sign in to comment.