Add ability to skip role creation on scope create (#308)

* Initial work on scope creation skipping

* Fix things
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-26 23:27:25 -04:00
1 parent e4da5e9ab5
commit 490be8a7e4
27 files changed
+962 -304

No files matched your search

+81 -16
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"time"
"github.com/kr/pretty"
@@ -39,11 +40,19 @@ func (c *GroupsClient) Create(ctx context.Context, opt ...Option) (*Group, *api.
return nil, nil, fmt.Errorf("nil client")
}
req, err := c.client.NewRequest(ctx, "POST", "groups", opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "POST", "groups", opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
@@ -69,13 +78,21 @@ func (c *GroupsClient) Read(ctx context.Context, groupId string, opt ...Option)
return nil, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("groups/%s", groupId), nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
@@ -119,13 +136,21 @@ func (c *GroupsClient) Update(ctx context.Context, groupId string, version uint3
version = existingTarget.Version
}
opts.valueMap["version"] = version
opts.postMap["version"] = version
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("groups/%s", groupId), opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("groups/%s", groupId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
@@ -151,13 +176,21 @@ func (c *GroupsClient) Delete(ctx context.Context, groupId string, opt ...Option
return false, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("groups/%s", groupId), nil, apiOpts...)
if err != nil {
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
@@ -182,13 +215,21 @@ func (c *GroupsClient) List(ctx context.Context, opt ...Option) ([]*Group, *api.
return nil, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", "groups", nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
@@ -235,17 +276,25 @@ func (c *GroupsClient) AddMembers(ctx context.Context, groupId string, version u
version = existingTarget.Version
}
opts.valueMap["version"] = version
opts.postMap["version"] = version
if len(memberIds) > 0 {
opts.valueMap["member_ids"] = memberIds
opts.postMap["member_ids"] = memberIds
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:add-members", groupId), opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:add-members", groupId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating AddMembers request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during AddMembers call: %w", err)
@@ -289,20 +338,28 @@ func (c *GroupsClient) SetMembers(ctx context.Context, groupId string, version u
version = existingTarget.Version
}
opts.valueMap["version"] = version
opts.postMap["version"] = version
if len(memberIds) > 0 {
opts.valueMap["member_ids"] = memberIds
opts.postMap["member_ids"] = memberIds
} else if memberIds != nil {
// In this function, a non-nil but empty list means clear out
opts.valueMap["member_ids"] = nil
opts.postMap["member_ids"] = nil
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:set-members", groupId), opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:set-members", groupId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating SetMembers request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during SetMembers call: %w", err)
@@ -346,17 +403,25 @@ func (c *GroupsClient) RemoveMembers(ctx context.Context, groupId string, versio
version = existingTarget.Version
}
opts.valueMap["version"] = version
opts.postMap["version"] = version
if len(memberIds) > 0 {
opts.valueMap["member_ids"] = memberIds
opts.postMap["member_ids"] = memberIds
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:remove-members", groupId), opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("groups/%s:remove-members", groupId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating RemoveMembers request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during RemoveMembers call: %w", err)
+8 -6
View File
@@ -14,14 +14,16 @@ import (
type Option func(*options)
type options struct {
valueMap map[string]interface{}
postMap map[string]interface{}
queryMap map[string]string
withScopeId string
withAutomaticVersioning bool
}
func getDefaultOptions() options {
return options{
valueMap: make(map[string]interface{}),
postMap: make(map[string]interface{}),
queryMap: make(map[string]string),
}
}
@@ -55,24 +57,24 @@ func WithAutomaticVersioning() Option {
func WithDescription(inDescription string) Option {
return func(o *options) {
o.valueMap["description"] = inDescription
o.postMap["description"] = inDescription
}
}
func DefaultDescription() Option {
return func(o *options) {
o.valueMap["description"] = nil
o.postMap["description"] = nil
}
}
func WithName(inName string) Option {
return func(o *options) {
o.valueMap["name"] = inName
o.postMap["name"] = inName
}
}
func DefaultName() Option {
return func(o *options) {
o.valueMap["name"] = nil
o.postMap["name"] = nil
}
}