Treat alias values like other required fields in API and CLI (#4404)
* Treat Alias values like other required fields in API and CLI * Fix protoc versions since we recently rebased on main which uses updated version
This commit is contained in:
1 parent
a6c96d69ae
commit
305bb09c35
11 files changed
+51
-68
No files matched your search
@@ -115,7 +115,7 @@ func (c *Client) ApiClient() *api.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *Client) Create(ctx context.Context, resourceType string, alias string, scopeId string, opt ...Option) (*AliasCreateResult, error) {
|
||||
func (c *Client) Create(ctx context.Context, resourceType string, scopeId string, opt ...Option) (*AliasCreateResult, error) {
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("empty scopeId value passed into Create request")
|
||||
}
|
||||
@@ -130,11 +130,6 @@ func (c *Client) Create(ctx context.Context, resourceType string, alias string,
|
||||
} else {
|
||||
opts.postMap["type"] = resourceType
|
||||
}
|
||||
if alias == "" {
|
||||
return nil, fmt.Errorf("empty alias value passed into Create request")
|
||||
} else {
|
||||
opts.postMap["value"] = alias
|
||||
}
|
||||
|
||||
opts.postMap["scope_id"] = scopeId
|
||||
|
||||
|
||||
@@ -156,3 +156,9 @@ func WithValue(inValue string) Option {
|
||||
o.postMap["value"] = inValue
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultValue() Option {
|
||||
return func(o *options) {
|
||||
o.postMap["value"] = nil
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc-gen-go v1.32.0
|
||||
// protoc (unknown)
|
||||
// source: controller/storage/alias/target/store/v1/alias.proto
|
||||
|
||||
|
||||
@@ -799,11 +799,6 @@ var inputStructs = []*structInfo{
|
||||
Typ: "string",
|
||||
PostType: "type",
|
||||
},
|
||||
{
|
||||
Name: "alias",
|
||||
Typ: "string",
|
||||
PostType: "value",
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -813,12 +808,6 @@ var inputStructs = []*structInfo{
|
||||
deleteTemplate,
|
||||
listTemplate,
|
||||
},
|
||||
fieldOverrides: []fieldInfo{
|
||||
{
|
||||
Name: "Value",
|
||||
SkipDefault: true,
|
||||
},
|
||||
},
|
||||
pluralResourceName: "aliases",
|
||||
versionEnabled: true,
|
||||
createResponseTypes: []string{CreateResponseType, ReadResponseType, UpdateResponseType, DeleteResponseType, ListResponseType},
|
||||
|
||||
@@ -204,10 +204,20 @@ func (c *TargetCommand) Run(args []string) int {
|
||||
var resp *api.Response
|
||||
var item *aliases.Alias
|
||||
|
||||
var createResult *aliases.AliasCreateResult
|
||||
|
||||
var updateResult *aliases.AliasUpdateResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
createResult, err = aliasesClient.Create(c.Context, "target", c.FlagScopeId, opts...)
|
||||
if exitCode := c.checkFuncError(err); exitCode > 0 {
|
||||
return exitCode
|
||||
}
|
||||
resp = createResult.GetResponse()
|
||||
item = createResult.GetItem()
|
||||
|
||||
case "update":
|
||||
updateResult, err = aliasesClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
if exitCode := c.checkFuncError(err); exitCode > 0 {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package aliasescmd
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/aliases"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
)
|
||||
@@ -13,7 +12,6 @@ func init() {
|
||||
extraTargetActionsFlagsMapFunc = extraTargetActionsFlagsMapFuncImpl
|
||||
extraTargetFlagsFunc = extraTargetFlagsFuncImpl
|
||||
extraTargetFlagsHandlingFunc = extraTargetFlagHandlingFuncImpl
|
||||
executeExtraTargetActions = executeExtraTargetActionsImpl
|
||||
}
|
||||
|
||||
type extraTargetCmdVars struct {
|
||||
@@ -103,6 +101,7 @@ func extraTargetFlagHandlingFuncImpl(c *TargetCommand, _ *base.FlagSets, opts *[
|
||||
switch c.flagValue {
|
||||
case "":
|
||||
case "null":
|
||||
*opts = append(*opts, aliases.DefaultValue())
|
||||
default:
|
||||
*opts = append(*opts, aliases.WithValue(c.flagValue))
|
||||
}
|
||||
@@ -125,18 +124,3 @@ func extraTargetFlagHandlingFuncImpl(c *TargetCommand, _ *base.FlagSets, opts *[
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func executeExtraTargetActionsImpl(c *TargetCommand, inResp *api.Response, inItem *aliases.Alias, inErr error, aliasesClient *aliases.Client, version uint32, opts []aliases.Option) (*api.Response, *aliases.Alias, error) {
|
||||
switch c.Func {
|
||||
case "create":
|
||||
// we specify our own create action here because we have more required
|
||||
// fields in the sdk than is specified in the templates for all the other
|
||||
// resources.
|
||||
createResult, err := aliasesClient.Create(c.Context, "target", c.flagValue, c.FlagScopeId, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return createResult.GetResponse(), createResult.GetItem(), nil
|
||||
}
|
||||
return inResp, inItem, inErr
|
||||
}
|
||||
@@ -171,20 +171,19 @@ var inputStructs = map[string][]*cmdInfo{
|
||||
HasId: true,
|
||||
},
|
||||
{
|
||||
ResourceType: resource.Alias.String(),
|
||||
Pkg: "aliases",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "target",
|
||||
HasExtraCommandVars: true,
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
Container: "Scope",
|
||||
VersionedActions: []string{"update"},
|
||||
NeedsSubtypeInCreate: true,
|
||||
SkipClientCallActions: []string{"create"},
|
||||
ResourceType: resource.Alias.String(),
|
||||
Pkg: "aliases",
|
||||
StdActions: []string{"create", "update"},
|
||||
SubActionPrefix: "target",
|
||||
HasExtraCommandVars: true,
|
||||
SkipNormalHelp: true,
|
||||
HasExtraHelpFunc: true,
|
||||
HasId: true,
|
||||
HasName: true,
|
||||
HasDescription: true,
|
||||
Container: "Scope",
|
||||
VersionedActions: []string{"update"},
|
||||
NeedsSubtypeInCreate: true,
|
||||
},
|
||||
},
|
||||
"authmethods": {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc-gen-go v1.32.0
|
||||
// protoc (unknown)
|
||||
// source: controller/api/services/v1/alias_service.proto
|
||||
|
||||
|
||||
@@ -123,11 +123,7 @@ func request_AliasService_CreateAlias_0(ctx context.Context, marshaler runtime.M
|
||||
var protoReq CreateAliasRequest
|
||||
var metadata runtime.ServerMetadata
|
||||
|
||||
newReader, berr := utilities.IOReaderFactory(req.Body)
|
||||
if berr != nil {
|
||||
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
|
||||
}
|
||||
if err := marshaler.NewDecoder(newReader()).Decode(&protoReq.Item); err != nil && err != io.EOF {
|
||||
if err := marshaler.NewDecoder(req.Body).Decode(&protoReq.Item); err != nil && err != io.EOF {
|
||||
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
|
||||
}
|
||||
|
||||
@@ -140,11 +136,7 @@ func local_request_AliasService_CreateAlias_0(ctx context.Context, marshaler run
|
||||
var protoReq CreateAliasRequest
|
||||
var metadata runtime.ServerMetadata
|
||||
|
||||
newReader, berr := utilities.IOReaderFactory(req.Body)
|
||||
if berr != nil {
|
||||
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
|
||||
}
|
||||
if err := marshaler.NewDecoder(newReader()).Decode(&protoReq.Item); err != nil && err != io.EOF {
|
||||
if err := marshaler.NewDecoder(req.Body).Decode(&protoReq.Item); err != nil && err != io.EOF {
|
||||
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
|
||||
}
|
||||
|
||||
@@ -154,7 +146,7 @@ func local_request_AliasService_CreateAlias_0(ctx context.Context, marshaler run
|
||||
}
|
||||
|
||||
var (
|
||||
filter_AliasService_UpdateAlias_0 = &utilities.DoubleArray{Encoding: map[string]int{"item": 0, "id": 1}, Base: []int{1, 2, 4, 0, 0, 0, 0}, Check: []int{0, 1, 1, 2, 2, 3, 3}}
|
||||
filter_AliasService_UpdateAlias_0 = &utilities.DoubleArray{Encoding: map[string]int{"item": 0, "id": 1}, Base: []int{1, 1, 2, 0, 0}, Check: []int{0, 1, 1, 2, 3}}
|
||||
)
|
||||
|
||||
func request_AliasService_UpdateAlias_0(ctx context.Context, marshaler runtime.Marshaler, client AliasServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestList(t *testing.T) {
|
||||
expected := lr.Items
|
||||
assert.Len(expected, 0)
|
||||
|
||||
cr, err := aliasClient.Create(tc.Context(), "target", "alias0", scopeId)
|
||||
cr, err := aliasClient.Create(tc.Context(), "target", scopeId, aliases.WithValue("alias0"))
|
||||
require.NoError(err)
|
||||
require.NotNil(cr)
|
||||
expected = append(expected, cr.Item)
|
||||
@@ -49,7 +49,7 @@ func TestList(t *testing.T) {
|
||||
assert.ElementsMatch(comparableSlice(expected[:1]), comparableSlice(ulResult.Items))
|
||||
|
||||
for i := 1; i < 10; i++ {
|
||||
newAcctResult, err := aliasClient.Create(tc.Context(), "target", fmt.Sprintf("alias%d", i), scopeId)
|
||||
newAcctResult, err := aliasClient.Create(tc.Context(), "target", scopeId, aliases.WithValue(fmt.Sprintf("alias%d", i)))
|
||||
require.NoError(err)
|
||||
expected = append(expected, newAcctResult.Item)
|
||||
}
|
||||
@@ -110,7 +110,8 @@ func TestCrud(t *testing.T) {
|
||||
assert.EqualValues(wantedVersion, u.Version)
|
||||
}
|
||||
|
||||
u, err := aliasClient.Create(tc.Context(), "target", "alias.value", scopeId,
|
||||
u, err := aliasClient.Create(tc.Context(), "target", scopeId,
|
||||
aliases.WithValue("alias.value"),
|
||||
aliases.WithDestinationId(tar.Id),
|
||||
aliases.WithTargetAliasAuthorizeSessionArgumentsHostId("hst_1234567890"))
|
||||
assert.NoError(err)
|
||||
@@ -158,12 +159,12 @@ func TestErrors(t *testing.T) {
|
||||
client.SetToken(token.Token)
|
||||
aliasClient := aliases.NewClient(client)
|
||||
|
||||
u, err := aliasClient.Create(tc.Context(), "target", "first", scopeId)
|
||||
u, err := aliasClient.Create(tc.Context(), "target", scopeId, aliases.WithValue("first"))
|
||||
require.NoError(err)
|
||||
assert.NotNil(u)
|
||||
|
||||
// Creating an alias of an unknown type
|
||||
_, err = aliasClient.Create(tc.Context(), "unknown", "first", scopeId)
|
||||
_, err = aliasClient.Create(tc.Context(), "unknown", scopeId, aliases.WithValue("first"))
|
||||
require.Error(err)
|
||||
apiErr := api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
@@ -187,8 +188,15 @@ func TestErrors(t *testing.T) {
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
|
||||
|
||||
// Removing the value completely should fail.
|
||||
_, err = aliasClient.Update(tc.Context(), u.Item.Id, u.Item.Version, aliases.DefaultValue())
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Response().StatusCode())
|
||||
|
||||
// Create another resource with the same name.
|
||||
_, err = aliasClient.Create(tc.Context(), "target", "first", scopeId)
|
||||
_, err = aliasClient.Create(tc.Context(), "target", scopeId)
|
||||
require.Error(err)
|
||||
apiErr = api.AsServerError(err)
|
||||
require.NotNil(apiErr)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc-gen-go v1.32.0
|
||||
// protoc (unknown)
|
||||
// source: controller/api/resources/aliases/v1/alias.proto
|
||||
|
||||
|
||||
Reference in new issue
Block a user