Skip to content

Commit

Permalink
rm util module, add implementation comments, make attributes worthy o…
Browse files Browse the repository at this point in the history
…f resource renewal, switch to sdk fake patch op
  • Loading branch information
gypsydiver committed Dec 18, 2018
1 parent a3f5b64 commit db24469
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
44 changes: 28 additions & 16 deletions jumpcloud/resource_user_group.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package jumpcloud

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

jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
"github.com/cognotektgmbh/terraform-provider-jumpcloud/util"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -32,6 +30,7 @@ func resourceUserGroup() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"posix_groups": {
Expand Down Expand Up @@ -88,6 +87,10 @@ func resourceUserGroupCreate(d *schema.ResourceData, m interface{}) error {
return resourceUserGroupRead(d, m)
}

// resourceUserGroupRead uses a helper function that consumes the
// JC's HTTP API directly; the groups' attributes need to be kept in state
// as they are required for resourceUserGroupUpdate and the current
// implementation of the JC SDK doesn't support their retrieval
func resourceUserGroupRead(d *schema.ResourceData, m interface{}) error {
config := m.(*jcapiv2.Configuration)

Expand Down Expand Up @@ -116,10 +119,17 @@ func resourceUserGroupRead(d *schema.ResourceData, m interface{}) error {
func userGroupReadHelper(config *jcapiv2.Configuration, id string) (ug *UserGroup,
ok bool, err error) {

res, err := util.RequestHTTP(http.MethodGet,
config.DefaultHeader,
config.BasePath+"/usergroups/"+id,
nil)
req, err := http.NewRequest(http.MethodGet,
config.BasePath+"/usergroups/"+id, nil)
if err != nil {
return
}

req.Header.Add("x-api-key", config.DefaultHeader["x-api-key"])
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
return
}
Expand All @@ -136,24 +146,26 @@ func userGroupReadHelper(config *jcapiv2.Configuration, id string) (ug *UserGrou

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

body := jcapiv2.UserGroupPost{Name: d.Get("name").(string)}
if attr, ok := expandAttributes(d.Get("attributes")); ok {
body.Attributes = attr
} else {
return errors.New("unable to update, attributes not expandable")
}
b, err := json.Marshal(body)
if err != nil {
return err
}

_, err = util.RequestHTTP(http.MethodPatch,
config.DefaultHeader,
config.BasePath+"/usergroups/"+d.Id(),
bytes.NewBuffer(b))
req := map[string]interface{}{
"body": body,
"xOrgId": d.Get("xorgid").(string),
}
// behaves like PUT, will fail if
// attributes.posixGroups isn't sent, see GODOC
_, res, err := client.UserGroupsApi.GroupsUserPatch(context.TODO(),
d.Id(), "", headerAccept, req)
if err != nil {
return err
// TODO: sort out error essentials
return fmt.Errorf("error deleting user group:%s; response = %+v", err, res)
}

return resourceUserGroupRead(d, m)
Expand All @@ -167,7 +179,7 @@ func resourceUserGroupDelete(d *schema.ResourceData, m interface{}) error {
d.Id(), "", headerAccept, nil)
if err != nil {
// TODO: sort out error essentials
return fmt.Errorf("error deleting user group: %s-response = %+v", err, res)
return fmt.Errorf("error deleting user group:%s; response = %+v", err, res)
}
d.SetId("")
return nil
Expand Down
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 := userGroupReadHelper(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: 0 additions & 25 deletions util/http.go

This file was deleted.

0 comments on commit db24469

Please sign in to comment.