Skip to content

Commit

Permalink
better user group read, noop update for now
Browse files Browse the repository at this point in the history
  • Loading branch information
gypsydiver committed Dec 15, 2018
1 parent ae13951 commit b74fd8d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 46 deletions.
2 changes: 1 addition & 1 deletion jumpcloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func (c *Config) Client() (interface{}, error) {
config.AddDefaultHeader("x-api-key", c.APIKey)

// Instantiate the API client
return jcapiv2.NewAPIClient(config), nil
return config, nil
}
98 changes: 53 additions & 45 deletions jumpcloud/resource_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package jumpcloud

import (
"context"
"encoding/json"
"fmt"
"net/http"

jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -12,50 +14,37 @@ func resourceUserGroup() *schema.Resource {
return &schema.Resource{
Create: resourceUserGroupCreate,
Read: resourceUserGroupRead,
Update: resourceUserGroupUpdate, //optional
Update: resourceUserGroupUpdate,
Delete: resourceUserGroupDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"posix_group_id": {
Type: schema.TypeInt,
Optional: true,
},
"posix_group_name": {
Type: schema.TypeString,
Optional: true,
// ValidateFunc: // Group names may contain a maximum of sixteen alphanumeric characters and hyphens.
},
"enable_samba": {
Type: schema.TypeBool,
Optional: true,
},
"xorgid": {
Type: schema.TypeString,
Optional: true,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceUserGroupCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*jcapiv2.APIClient)

posixID := d.Get("posix_group_id").(int)
posixName := d.Get("posix_group_name").(string)
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)

req := map[string]interface{}{
"body": jcapiv2.UserGroupPost{
Name: d.Get("name").(string),
Attributes: &jcapiv2.UserGroupPostAttributes{
// Note: PosixGroups cannot be edited after group creation, only first member of slice is considered
PosixGroups: []jcapiv2.UserGroupPostAttributesPosixGroups{
jcapiv2.UserGroupPostAttributesPosixGroups{Id: int32(posixID), Name: posixName},
},
SambaEnabled: d.Get("enable_samba").(bool),
},
// Attributes: &jcapiv2.UserGroupPostAttributes{
// // Note: PosixGroups cannot be edited after group creation, only first member of slice is considered
// PosixGroups: []jcapiv2.UserGroupPostAttributesPosixGroups{
// jcapiv2.UserGroupPostAttributesPosixGroups{Id: int32(posixID), Name: posixName},
// },
// },
},
"xOrgId": d.Get("xorgid").(string),
}
Expand All @@ -66,53 +55,72 @@ func resourceUserGroupCreate(d *schema.ResourceData, m interface{}) error {
}

d.SetId(group.Id)

return resourceUserGroupRead(d, m)
}

func resourceUserGroupRead(d *schema.ResourceData, m interface{}) error {
client := m.(*jcapiv2.APIClient)
group, _, err := client.UserGroupsApi.GroupsUserGet(context.TODO(), d.Id(), "", Accept, nil)
config := m.(*jcapiv2.Configuration)

group, ok, err := trueUserGroupRead(config, d.Id())
if err != nil {
return err
}

d.SetId(group.Id)
if err = d.Set("name", group.Name); err != nil {
if !ok {
// not found
d.SetId("")
return nil
}
// TODO: attributes?

d.SetId(group.Id)
if err := d.Set("name", group.Name); err != nil {
return err
}
// if err := d.Set("attributes", flattenAttributes(&group.Attributes)); err != nil {
// return err
// }

return nil
}

func resourceUserGroupUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*jcapiv2.APIClient)

body := jcapiv2.UserGroupPut{
Name: d.Get("name").(string), // Always set since it is a required value, and PUT is not supported by JCAPI
func trueUserGroupRead(config *jcapiv2.Configuration, id string) (ug *UserGroup, ok bool, err error) {
req, err := http.NewRequest(http.MethodGet, config.BasePath+"/usergroups/"+id, nil)
if err != nil {
return
}

if d.HasChange("enable_samba") {
body.Attributes = &jcapiv2.UserGroupPutAttributes{
SambaEnabled: d.Get("enable_samba").(bool),
}
}
req.Header.Add("x-api-key", config.DefaultHeader["x-api-key"])
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

req := map[string]interface{}{"body": body, "xOrgId": d.Get("xorgid").(string)}
_, _, err := client.UserGroupsApi.GroupsUserPut(context.TODO(), d.Id(), "", Accept, req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
return
}
defer res.Body.Close()

if res.StatusCode == http.StatusNotFound {
return
}

ok = true
err = json.NewDecoder(res.Body).Decode(&ug)
return
}

func resourceUserGroupUpdate(d *schema.ResourceData, m interface{}) error {
return resourceUserGroupRead(d, m)
}

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

res, err := client.UserGroupsApi.GroupsUserDelete(context.TODO(), d.Id(), "", Accept, nil)
if err != nil {
// TODO: sort out error essentials
return fmt.Errorf("error deleting user group: %s - response = %+v", err, res)
}
d.SetId("")
return nil
}
55 changes: 55 additions & 0 deletions jumpcloud/resource_user_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package jumpcloud

import (
"net/http"
"net/http/httptest"
"testing"

jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type ResourceUserGroupSuite struct {
suite.Suite
A *assert.Assertions
TestHTTPServer *httptest.Server
}

func (s *ResourceUserGroupSuite) SetupSuite() {
s.A = assert.New(s.Suite.T())
}

func (s *ResourceUserGroupSuite) TestTrueUserGroupRead() {
cases := []struct {
ResponseStatus int
UserGroupNil bool
OK bool
ErrorNil bool
Payload []byte
}{
{http.StatusNotFound, true, false, true, []byte("irrelevant")},
{http.StatusOK, false, true, true, []byte("{}")},
}

for _, c := range cases {
testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(c.ResponseStatus)
rw.Write(c.Payload)
}))

config := &jcapiv2.Configuration{
BasePath: testServer.URL,
}

ug, ok, err := trueUserGroupRead(config, "id")
s.A.Equal(c.OK, ok)
s.A.Equal(c.UserGroupNil, ug == nil)
s.A.Equal(c.ErrorNil, err == nil)
testServer.Close()
}
}

func TestResourceUserGroup(t *testing.T) {
suite.Run(t, new(ResourceUserGroupSuite))
}
25 changes: 25 additions & 0 deletions jumpcloud/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package jumpcloud

import jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"

type UserGroup struct {
// ObjectId uniquely identifying a User Group.
Id string `json:"id,omitempty"`

// The type of the group.
Type_ string `json:"type,omitempty"`

// Display name of a User Group.
Name string `json:"name,omitempty"`
Attributes Attributes `json:"attributes,omitempty"`
}

type Attributes struct {
LDAPGroups []LDAPGroup `json:"ldapGroups,omitempty"`
POSIXGroups []jcapiv2.UserGroupPostAttributesPosixGroups `json:"posixGroups,omitempty"`
// SambaEnabled bool `json:"sambaEnable,omitempty"`
}

type LDAPGroup struct {
Name string `json:"name,omitempty"`
}

0 comments on commit b74fd8d

Please sign in to comment.