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

+22 -6
View File
@@ -1,6 +1,8 @@
package scopes
import (
"fmt"
"github.com/hashicorp/boundary/api"
)
@@ -14,14 +16,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 +59,36 @@ 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
}
}
func WithSkipRoleCreation(inSkipRoleCreation bool) Option {
return func(o *options) {
o.queryMap["skip_role_creation"] = fmt.Sprintf("%v", inSkipRoleCreation)
}
}
func DefaultSkipRoleCreation() Option {
return func(o *options) {
o.postMap["skip_role_creation"] = nil
}
}
+48 -12
View File
@@ -40,14 +40,20 @@ func (c *ScopesClient) Create(ctx context.Context, scopeId string, opt ...Option
return nil, nil, fmt.Errorf("nil client")
}
req, err := c.client.NewRequest(ctx, "POST", "scopes", opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "POST", "scopes", opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
}
q := url.Values{}
q.Add("scope_id", scopeId)
req.URL.RawQuery = q.Encode()
opts.queryMap["scope_id"] = scopeId
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 {
@@ -74,13 +80,21 @@ func (c *ScopesClient) Read(ctx context.Context, scopeId 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("scopes/%s", scopeId), 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)
@@ -124,13 +138,21 @@ func (c *ScopesClient) Update(ctx context.Context, scopeId string, version uint3
version = existingTarget.Version
}
opts.valueMap["version"] = version
opts.postMap["version"] = version
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("scopes/%s", scopeId), opts.valueMap, apiOpts...)
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("scopes/%s", scopeId), 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)
@@ -156,13 +178,21 @@ func (c *ScopesClient) Delete(ctx context.Context, scopeId 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("scopes/%s", scopeId), 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)
@@ -191,16 +221,22 @@ func (c *ScopesClient) List(ctx context.Context, scopeId string, opt ...Option)
return nil, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", "scopes", nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
q := url.Values{}
q.Add("scope_id", scopeId)
req.URL.RawQuery = q.Encode()
opts.queryMap["scope_id"] = scopeId
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 {