refactor(api module): enforce typed definitions (#2238)
This commit is contained in:
1 parent
d6c1402223
commit
c2bfcc0664
79 files changed
+906
-551
No files matched your search
@@ -184,24 +184,36 @@ func (c *Command) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *authmethods.AuthMethod
|
||||
|
||||
var listResult api.GenericListResult
|
||||
var items []*authmethods.AuthMethod
|
||||
|
||||
var readResult *authmethods.AuthMethodReadResult
|
||||
|
||||
var deleteResult *authmethods.AuthMethodDeleteResult
|
||||
|
||||
var listResult *authmethods.AuthMethodListResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "read":
|
||||
result, err = authmethodsClient.Read(c.Context, c.FlagId, opts...)
|
||||
readResult, err = authmethodsClient.Read(c.Context, c.FlagId, opts...)
|
||||
resp = readResult.GetResponse()
|
||||
item = readResult.GetItem()
|
||||
|
||||
case "delete":
|
||||
result, err = authmethodsClient.Delete(c.Context, c.FlagId, opts...)
|
||||
deleteResult, err = authmethodsClient.Delete(c.Context, c.FlagId, opts...)
|
||||
resp = deleteResult.GetResponse()
|
||||
|
||||
case "list":
|
||||
listResult, err = authmethodsClient.List(c.Context, c.FlagScopeId, opts...)
|
||||
resp = listResult.GetResponse()
|
||||
items = listResult.GetItems()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraActions(c, result, err, authmethodsClient, version, opts)
|
||||
resp, item, items, err = executeExtraActions(c, resp, item, items, err, authmethodsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -228,7 +240,7 @@ func (c *Command) Run(args []string) int {
|
||||
case "delete":
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
@@ -241,13 +253,12 @@ func (c *Command) Run(args []string) int {
|
||||
case "list":
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
if ok := c.PrintJsonItems(listResult); !ok {
|
||||
if ok := c.PrintJsonItems(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
case "table":
|
||||
listedItems := listResult.GetItems().([]*authmethods.AuthMethod)
|
||||
c.UI.Output(c.printListTable(listedItems))
|
||||
c.UI.Output(c.printListTable(items))
|
||||
}
|
||||
|
||||
return base.CommandSuccess
|
||||
@@ -256,10 +267,10 @@ func (c *Command) Run(args []string) int {
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
c.UI.Output(printItemTable(item, resp))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
@@ -274,8 +285,8 @@ var (
|
||||
extraSynopsisFunc = func(*Command) string { return "" }
|
||||
extraFlagsFunc = func(*Command, *base.FlagSets, *base.FlagSet) {}
|
||||
extraFlagsHandlingFunc = func(*Command, *base.FlagSets, *[]authmethods.Option) bool { return true }
|
||||
executeExtraActions = func(_ *Command, inResult api.GenericResult, inErr error, _ *authmethods.Client, _ uint32, _ []authmethods.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraActions = func(_ *Command, inResp *api.Response, inItem *authmethods.AuthMethod, inItems []*authmethods.AuthMethod, inErr error, _ *authmethods.Client, _ uint32, _ []authmethods.Option) (*api.Response, *authmethods.AuthMethod, []*authmethods.AuthMethod, error) {
|
||||
return inResp, inItem, inItems, inErr
|
||||
}
|
||||
printCustomActionOutput = func(*Command) (bool, error) { return false, nil }
|
||||
)
|
||||
@@ -133,8 +133,7 @@ func (c *Command) printListTable(items []*authmethods.AuthMethod) string {
|
||||
return base.WrapForHelpText(output)
|
||||
}
|
||||
|
||||
func printItemTable(result api.GenericResult) string {
|
||||
item := result.GetItem().(*authmethods.AuthMethod)
|
||||
func printItemTable(item *authmethods.AuthMethod, resp *api.Response) string {
|
||||
nonAttributeMap := map[string]interface{}{}
|
||||
if item.Id != "" {
|
||||
nonAttributeMap["ID"] = item.Id
|
||||
@@ -157,8 +156,8 @@ func printItemTable(result api.GenericResult) string {
|
||||
if item.Description != "" {
|
||||
nonAttributeMap["Description"] = item.Description
|
||||
}
|
||||
if result.GetResponse() != nil && result.GetResponse().Map != nil {
|
||||
if result.GetResponse().Map[globals.IsPrimaryField] != nil {
|
||||
if resp != nil && resp.Map != nil {
|
||||
if resp.Map[globals.IsPrimaryField] != nil {
|
||||
nonAttributeMap["Is Primary For Scope"] = item.IsPrimary
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,19 +206,28 @@ func (c *OidcCommand) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *authmethods.AuthMethod
|
||||
|
||||
var createResult *authmethods.AuthMethodCreateResult
|
||||
|
||||
var updateResult *authmethods.AuthMethodUpdateResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
result, err = authmethodsClient.Create(c.Context, "oidc", c.FlagScopeId, opts...)
|
||||
createResult, err = authmethodsClient.Create(c.Context, "oidc", c.FlagScopeId, opts...)
|
||||
resp = createResult.GetResponse()
|
||||
item = createResult.GetItem()
|
||||
|
||||
case "update":
|
||||
result, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
updateResult, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
resp = updateResult.GetResponse()
|
||||
item = updateResult.GetItem()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraOidcActions(c, result, err, authmethodsClient, version, opts)
|
||||
resp, item, err = executeExtraOidcActions(c, resp, item, err, authmethodsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -246,10 +255,10 @@ func (c *OidcCommand) Run(args []string) int {
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
c.UI.Output(printItemTable(item, resp))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
@@ -262,8 +271,8 @@ var (
|
||||
extraOidcSynopsisFunc = func(*OidcCommand) string { return "" }
|
||||
extraOidcFlagsFunc = func(*OidcCommand, *base.FlagSets, *base.FlagSet) {}
|
||||
extraOidcFlagsHandlingFunc = func(*OidcCommand, *base.FlagSets, *[]authmethods.Option) bool { return true }
|
||||
executeExtraOidcActions = func(_ *OidcCommand, inResult api.GenericResult, inErr error, _ *authmethods.Client, _ uint32, _ []authmethods.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraOidcActions = func(_ *OidcCommand, inResp *api.Response, inItem *authmethods.AuthMethod, inErr error, _ *authmethods.Client, _ uint32, _ []authmethods.Option) (*api.Response, *authmethods.AuthMethod, error) {
|
||||
return inResp, inItem, inErr
|
||||
}
|
||||
printCustomOidcActionOutput = func(*OidcCommand) (bool, error) { return false, nil }
|
||||
)
|
||||
@@ -284,13 +284,14 @@ func extraOidcFlagHandlingFuncImpl(c *OidcCommand, f *base.FlagSets, opts *[]aut
|
||||
return true
|
||||
}
|
||||
|
||||
func executeExtraOidcActionsImpl(c *OidcCommand, origResult api.GenericResult, origError error, amClient *authmethods.Client, version uint32, opts []authmethods.Option) (api.GenericResult, error) {
|
||||
func executeExtraOidcActionsImpl(c *OidcCommand, origResp *api.Response, origItem *authmethods.AuthMethod, origError error, amClient *authmethods.Client, version uint32, opts []authmethods.Option) (*api.Response, *authmethods.AuthMethod, error) {
|
||||
switch c.Func {
|
||||
case "change-state":
|
||||
if c.flagDisableDiscoveredConfigValidation {
|
||||
opts = append(opts, authmethods.WithOidcAuthMethodDisableDiscoveredConfigValidation(true))
|
||||
}
|
||||
return amClient.ChangeState(c.Context, c.FlagId, version, c.flagState, opts...)
|
||||
result, err := amClient.ChangeState(c.Context, c.FlagId, version, c.flagState, opts...)
|
||||
return result.GetResponse(), result.GetItem(), err
|
||||
}
|
||||
return origResult, origError
|
||||
return origResp, origItem, origError
|
||||
}
|
||||
@@ -198,19 +198,28 @@ func (c *PasswordCommand) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var resp *api.Response
|
||||
var item *authmethods.AuthMethod
|
||||
|
||||
var createResult *authmethods.AuthMethodCreateResult
|
||||
|
||||
var updateResult *authmethods.AuthMethodUpdateResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
result, err = authmethodsClient.Create(c.Context, "password", c.FlagScopeId, opts...)
|
||||
createResult, err = authmethodsClient.Create(c.Context, "password", c.FlagScopeId, opts...)
|
||||
resp = createResult.GetResponse()
|
||||
item = createResult.GetItem()
|
||||
|
||||
case "update":
|
||||
result, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
updateResult, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
resp = updateResult.GetResponse()
|
||||
item = updateResult.GetItem()
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraPasswordActions(c, result, err, authmethodsClient, version, opts)
|
||||
resp, item, err = executeExtraPasswordActions(c, resp, item, err, authmethodsClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
@@ -238,10 +247,10 @@ func (c *PasswordCommand) Run(args []string) int {
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(result))
|
||||
c.UI.Output(printItemTable(item, resp))
|
||||
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(result); !ok {
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
}
|
||||
@@ -254,8 +263,8 @@ var (
|
||||
extraPasswordSynopsisFunc = func(*PasswordCommand) string { return "" }
|
||||
extraPasswordFlagsFunc = func(*PasswordCommand, *base.FlagSets, *base.FlagSet) {}
|
||||
extraPasswordFlagsHandlingFunc = func(*PasswordCommand, *base.FlagSets, *[]authmethods.Option) bool { return true }
|
||||
executeExtraPasswordActions = func(_ *PasswordCommand, inResult api.GenericResult, inErr error, _ *authmethods.Client, _ uint32, _ []authmethods.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
executeExtraPasswordActions = func(_ *PasswordCommand, inResp *api.Response, inItem *authmethods.AuthMethod, inErr error, _ *authmethods.Client, _ uint32, _ []authmethods.Option) (*api.Response, *authmethods.AuthMethod, error) {
|
||||
return inResp, inItem, inErr
|
||||
}
|
||||
printCustomPasswordActionOutput = func(*PasswordCommand) (bool, error) { return false, nil }
|
||||
)
|
||||
Reference in new issue
Block a user