Skip to content

Commit

Permalink
feat(service-account): add support for service account SDK (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 authored Sep 5, 2022
1 parent 64ebafb commit dcf2275
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docs/commands/rhoas_service-account_describe.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/commands/rhoas_service-account_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ require (
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/redhat-developer/app-services-sdk-go v0.10.0 // indirect
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ github.com/redhat-developer/app-services-sdk-go/registryinstance v0.5.1 h1:pgOtC
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.5.1/go.mod h1:HkNzOWHTW/SomobQ4343+yR4oTmiyvm85BIWlsh0qbA=
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0 h1:9kqTCGoz5jR6VE+cgz8fl5Zt9Ctctg9GN1XlkzheVQw=
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0/go.mod h1:UoxuqkUN+g5Ni8zgsCA7zidR5s774m9fqhZ5o4eOSIM=
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0 h1:kMvH66RXnxrF7FKraWu7n1BnaWrCUchw2unYa9rl/IM=
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0/go.mod h1:kpEKXWqyD6GUiQjBHCGzp/LIbCBfkTWpPo4VqkQ9zq4=
github.com/redhat-developer/service-binding-operator v0.9.0 h1:CS+eEtzu/PtWuyvYQFQpZXd6ukSuFtN+U0EKKtTsvlA=
github.com/redhat-developer/service-binding-operator v0.9.0/go.mod h1:D415gZQiz5Q8zyRbmrNrlieb6Xp73oFtCb+nCuTL6GA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
Expand Down
25 changes: 25 additions & 0 deletions pkg/cmd/serviceaccount/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"

svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"

"github.com/spf13/cobra"
)

type options struct {
id string
outputFormat string
enableAuthV2 bool

IO *iostreams.IOStreams
Config config.IConfig
Connection factory.ConnectionFunc
localizer localize.Localizer
Context context.Context
Logger logging.Logger
}

func NewDescribeCommand(f *factory.Factory) *cobra.Command {
Expand All @@ -32,6 +37,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
Logger: f.Logger,
}

cmd := &cobra.Command{
Expand All @@ -52,6 +58,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {

cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("serviceAccount.describe.flag.id.description"))
cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("serviceAccount.common.flag.output.description"))
cmd.Flags().BoolVar(&opts.enableAuthV2, "enable-auth-v2", false, opts.localizer.MustLocalize("serviceAccount.common.flag.enableAuthV2"))

_ = cmd.MarkFlagRequired("id")

Expand Down Expand Up @@ -85,5 +92,23 @@ func runDescribe(opts *options) error {
}
}

// Temporary workaround to be removed
if opts.enableAuthV2 {

timeInt := res.CreatedAt.Unix()

formattedRes := svcacctmgmtclient.ServiceAccountData{
Id: &res.Id,
ClientId: res.ClientId,
Name: res.Name,
Description: res.Description,
CreatedBy: res.CreatedBy,
CreatedAt: &timeInt,
}

return dump.Formatted(opts.IO.Out, opts.outputFormat, formattedRes)
}

opts.Logger.Info(opts.localizer.MustLocalize("serviceAccount.common.breakingChangeNotice.SDK"))
return dump.Formatted(opts.IO.Out, opts.outputFormat, res)
}
37 changes: 36 additions & 1 deletion pkg/cmd/serviceaccount/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/spf13/cobra"

svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"
)

type options struct {
Expand All @@ -22,7 +24,8 @@ type options struct {
localizer localize.Localizer
Context context.Context

output string
output string
enableAuthV2 bool
}

// svcAcctRow contains the properties used to
Expand Down Expand Up @@ -62,6 +65,7 @@ func NewListCommand(f *factory.Factory) *cobra.Command {
}

cmd.Flags().StringVarP(&opts.output, "output", "o", "", opts.localizer.MustLocalize("serviceAccount.list.flag.output.description"))
cmd.Flags().BoolVar(&opts.enableAuthV2, "enable-auth-v2", false, opts.localizer.MustLocalize("serviceAccount.common.flag.enableAuthV2"))

flagutil.EnableOutputFlagCompletion(cmd)

Expand Down Expand Up @@ -91,6 +95,12 @@ func runList(opts *options) (err error) {
rows := mapResponseItemsToRows(serviceaccounts)
dump.Table(outStream, rows)
default:
// Temporary workaround to be removed
if opts.enableAuthV2 {
formattedRes := mapResponseToNewFormat(res)
return dump.Formatted(opts.IO.Out, opts.output, formattedRes)
}
opts.Logger.Info(opts.localizer.MustLocalize("serviceAccount.common.breakingChangeNotice.SDK"))
return dump.Formatted(opts.IO.Out, opts.output, res)
}

Expand All @@ -114,3 +124,28 @@ func mapResponseItemsToRows(svcAccts []kafkamgmtclient.ServiceAccountListItem) [

return rows
}

// mapResponseToNewFormat accepts response of old sdk and transforms it to response of new sdk
func mapResponseToNewFormat(res kafkamgmtclient.ServiceAccountList) []svcacctmgmtclient.ServiceAccountData {

var serviceaccounts []svcacctmgmtclient.ServiceAccountData

for _, svcAcct := range res.GetItems() {

timeInt := svcAcct.CreatedAt.Unix()

formattedServiceAccount := svcacctmgmtclient.ServiceAccountData{
Id: &svcAcct.Id,
ClientId: svcAcct.ClientId,
Name: svcAcct.Name,
Description: svcAcct.Description,
CreatedBy: svcAcct.CreatedBy,
CreatedAt: &timeInt,
}

serviceaccounts = append(serviceaccounts, formattedServiceAccount)
}

return serviceaccounts

}
10 changes: 10 additions & 0 deletions pkg/core/localize/locales/en/cmd/serviceaccount.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ rhoas service-account list
description = "Description for --output flag"
one = 'Format in which to display the service account (choose from: "json", "yml", "yaml")'

[serviceAccount.common.flag.enableAuthV2]
one = 'Format output according to new Service Account SDK'

[serviceAccount.list.flag.output.description]
one = 'Format in which to display the service accounts (choose from: "json", "yml", "yaml")'

Expand Down Expand Up @@ -105,3 +108,10 @@ one = 'invalid service account description: only lowercase letters (a-z), number

[serviceAccount.common.validation.id.error.invalidID]
one = '"{{.ID}}" is not a valid UUID'

[serviceAccount.common.breakingChangeNotice.SDK]
one = '''
Breaking Change Notice: The CLI is soon going to have the output format for this command altered.
To try with the latest format, pass the "--enable-auth-v2" flag
'''

0 comments on commit dcf2275

Please sign in to comment.