Skip to content

Commit

Permalink
added new methods to operate better
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangda committed Mar 20, 2021
1 parent 1ac0096 commit 8e17026
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 63 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go v1.33.7
github.com/golang/mock v1.5.0
github.com/hashicorp/go-retryablehttp v0.6.7
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v1.0.0
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand Down Expand Up @@ -571,8 +574,9 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
69 changes: 68 additions & 1 deletion internal/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
var (
ErrUserNotFound = errors.New("user not found")
ErrGroupNotFound = errors.New("group not found")
ErrNoGroupsFound = errors.New("no groups found")
ErrUserNotSpecified = errors.New("user not specified")
ErrGroupNotSpecified = errors.New("group not specified")
)
Expand All @@ -45,7 +46,7 @@ const (
OperationRemove = "remove"
)

// IClient represents an interface of methods used
// Client represents an interface of methods used
// to communicate with AWS SSO
type Client interface {
AddUserToGroup(*User, *Group) error
Expand All @@ -55,7 +56,9 @@ type Client interface {
DeleteUser(*User) error
FindGroupByDisplayName(string) (*Group, error)
FindUserByEmail(string) (*User, error)
GetUsers() ([]*User, error)
IsUserInGroup(*User, *Group) (bool, error)
GetGroups() ([]*Group, error)
UpdateUser(*User) (*User, error)
RemoveUserFromGroup(*User, *Group) error
}
Expand Down Expand Up @@ -427,3 +430,67 @@ func (c *client) DeleteGroup(g *Group) error {

return nil
}

// GetGroups will return existing groups
func (c *client) GetGroups() ([]*Group, error) {
startURL, err := url.Parse(c.endpointURL.String())
if err != nil {
return nil, err
}

startURL.Path = path.Join(startURL.Path, "/Groups")

resp, err := c.sendRequest(http.MethodGet, startURL.String())
if err != nil {
return nil, err
}

var r GroupFilterResults
err = json.Unmarshal(resp, &r)
if err != nil {
return nil, err
}

if r.TotalResults != 1 {
return nil, ErrNoGroupsFound
}

gps := make([]*Group, len(r.Resources))
for i := range r.Resources {
gps[i] = &r.Resources[i]
}

return gps, nil
}

// GetUsers will return existing users
func (c *client) GetUsers() ([]*User, error) {
startURL, err := url.Parse(c.endpointURL.String())
if err != nil {
return nil, err
}

startURL.Path = path.Join(startURL.Path, "/Users")

resp, err := c.sendRequest(http.MethodGet, startURL.String())
if err != nil {
return nil, err
}

var r UserFilterResults
err = json.Unmarshal(resp, &r)
if err != nil {
return nil, err
}

if r.TotalResults != 1 {
return nil, ErrUserNotFound
}

usrs := make([]*User, len(r.Resources))
for i := range r.Resources {
usrs[i] = &r.Resources[i]
}

return usrs, nil
}
48 changes: 25 additions & 23 deletions internal/google/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package google ...
package google

import (
Expand All @@ -25,6 +26,7 @@ import (
// Client is the Interface for the Client
type Client interface {
GetUsers() ([]*admin.User, error)
GetUsersMatch(string) ([]*admin.User, error)
GetDeletedUsers() ([]*admin.User, error)
GetGroups() ([]*admin.Group, error)
GetGroupsMatch(string) ([]*admin.Group, error)
Expand Down Expand Up @@ -83,7 +85,29 @@ func (c *client) GetUsers() ([]*admin.User, error) {
return u, err
}

// GetUsers will get the users from Google's Admin API
// GetGroups will get the groups from Google's Admin API
func (c *client) GetGroups() ([]*admin.Group, error) {
g := make([]*admin.Group, 0)
err := c.service.Groups.List().Customer("my_customer").Pages(context.TODO(), func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})

return g, err
}

// GetGroupMembers will get the members of the group specified
func (c *client) GetGroupMembers(g *admin.Group) ([]*admin.Member, error) {
m := make([]*admin.Member, 0)
err := c.service.Members.List(g.Id).Pages(context.TODO(), func(members *admin.Members) error {
m = append(m, members.Members...)
return nil
})

return m, err
}

// GetUsersMatch will get the users from Google's Admin API
// using the Method: users.list with parameter "query"
// References:
// * https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
Expand All @@ -105,17 +129,6 @@ func (c *client) GetUsersMatch(query string) ([]*admin.User, error) {
return u, err
}

// GetGroups will get the groups from Google's Admin API
func (c *client) GetGroups() ([]*admin.Group, error) {
g := make([]*admin.Group, 0)
err := c.service.Groups.List().Customer("my_customer").Pages(context.TODO(), func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})

return g, err
}

// GetGroupsMatch will get the groups from Google's Admin API
// using the Method: groups.list with parameter "query"
// References:
Expand All @@ -137,14 +150,3 @@ func (c *client) GetGroupsMatch(query string) ([]*admin.Group, error) {

return g, err
}

// GetGroupMembers will get the members of the group specified
func (c *client) GetGroupMembers(g *admin.Group) ([]*admin.Member, error) {
m := make([]*admin.Member, 0)
err := c.service.Members.List(g.Id).Pages(context.TODO(), func(members *admin.Members) error {
m = append(m, members.Members...)
return nil
})

return m, err
}
Loading

0 comments on commit 8e17026

Please sign in to comment.