Skip to content

Commit

Permalink
List gateway pools gateways (#133)
Browse files Browse the repository at this point in the history
* WIP

* refactor displayer

* CL

* go linter

* add description for a list-gateways flag

* Update internal/commands/vaultsecrets/gatewaypools/list_gateway_pools_gateways.go

Co-authored-by: Nick Cabatoff <[email protected]>

* fix a typo

---------

Co-authored-by: Nick Cabatoff <[email protected]>
  • Loading branch information
hghaf099 and ncabatoff authored Jul 25, 2024
1 parent 40546ea commit 5f50ad8
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .changelog/133.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
vault-secrets: adding list gateway pools gateways command to vault-secrets gateway-pools
```
21 changes: 15 additions & 6 deletions internal/commands/vaultsecrets/gatewaypools/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,24 @@ func NewCmdCreate(ctx *cmd.Context, runF func(*CreateOpts) error) *cmd.Command {
return cmd
}

func extraFields(showOauth bool) []format.Field {
extraFields := []format.Field{
func createFields(showOauth bool) []format.Field {
fields := []format.Field{
{
Name: "GatewayPool Name",
ValueFormat: "{{ .GatewayPool.Name }}",
},
{
Name: "Description",
ValueFormat: "{{ .GatewayPool.Description }}",
},
{
Name: "Resource Name",
ValueFormat: "{{ .GatewayPool.ResourceName }}",
},
}

if showOauth {
extraFields = append(extraFields, []format.Field{
fields = append(fields, []format.Field{
{
Name: "Client ID",
ValueFormat: "{{ .Oauth.ClientID }}",
Expand All @@ -135,7 +143,7 @@ func extraFields(showOauth bool) []format.Field {
},
}...)
}
return extraFields
return fields
}

func createRun(opts *CreateOpts) error {
Expand Down Expand Up @@ -185,14 +193,15 @@ func createRun(opts *CreateOpts) error {
}

// Display Oauth in the output if explicitly asked for it
displayerOpts := &gatewayPoolWithIntegrations{
displayerOpts := &gatewayPoolWithOauth{
GatewayPool: resp.Payload.GatewayPool,
}
if opts.ShowClientSecret {
displayerOpts.Oauth = oauth
}

return opts.Output.Display(newDisplayer(true, displayerOpts).SetDefaultFormat(format.Pretty).AddExtraFields(extraFields(opts.ShowClientSecret)...))
d := newDisplayer(true).GatewayPoolsWithOauth(displayerOpts)
return opts.Output.Display(d.SetDefaultFormat(format.Pretty).AddFields(createFields(opts.ShowClientSecret)...))
}

type gatewayCreds struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/commands/vaultsecrets/gatewaypools/create_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package gatewaypools

import (
Expand Down
108 changes: 69 additions & 39 deletions internal/commands/vaultsecrets/gatewaypools/displayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,58 @@ import (

type gatewayPoolWithIntegrations struct {
GatewayPool *preview_models.Secrets20231128GatewayPool
Oauth *auth.OauthConfig
Integrations []string
}

type displayer struct {
gatewayPools []*gatewayPoolWithIntegrations
type gatewayPoolWithOauth struct {
GatewayPool *preview_models.Secrets20231128GatewayPool
Oauth *auth.OauthConfig
}

extraFields []format.Field
format format.Format
single bool
type displayer struct {
gatewayPools []*preview_models.Secrets20231128GatewayPool
gatewayPoolsWithIntegrations *gatewayPoolWithIntegrations
gatewayPoolWithOauth *gatewayPoolWithOauth
gateways []*preview_models.Secrets20231128Gateway

fields []format.Field
format format.Format
single bool
}

func newDisplayer(single bool, gatewayPools ...*gatewayPoolWithIntegrations) *displayer {
func newDisplayer(single bool) *displayer {
return &displayer{
gatewayPools: gatewayPools,
single: single,
format: format.Table,
single: single,
format: format.Table,
}
}

func (d *displayer) GatewayPools(gatewayPools ...*preview_models.Secrets20231128GatewayPool) *displayer {
d.gatewayPools = gatewayPools
return d
}

func (d *displayer) GatewayPoolWithIntegrations(gatewayPool *preview_models.Secrets20231128GatewayPool, integrations ...string) *displayer {
d.gatewayPoolsWithIntegrations = &gatewayPoolWithIntegrations{
GatewayPool: gatewayPool,
Integrations: integrations,
}

return d
}

func (d *displayer) Gateways(gateways ...*preview_models.Secrets20231128Gateway) *displayer {
d.gateways = gateways
return d
}

func (d *displayer) GatewayPoolsWithOauth(gpo *gatewayPoolWithOauth) *displayer {
d.gatewayPoolWithOauth = gpo
return d
}

func (d *displayer) AddExtraFields(fields ...format.Field) *displayer {
d.extraFields = append(d.extraFields, fields...)
func (d *displayer) AddFields(fields ...format.Field) *displayer {
d.fields = append(d.fields, fields...)
return d
}

Expand All @@ -45,45 +75,45 @@ func (d *displayer) DefaultFormat() format.Format {
return d.format
}

func (d *displayer) Payload() any {
if d.gatewayPools != nil {
if d.single {
if len(d.gatewayPools) != 1 {
return nil
}

return d.gatewayPools[0]
}

return d.gatewayPools
}

func (d *displayer) previewGatewayPools() any {
if d.single {
if len(d.gatewayPools) != 1 {
return nil
}

return d.gatewayPools[0]
}

return d.gatewayPools
}

func (d *displayer) FieldTemplates() []format.Field {
fields := []format.Field{
{
Name: "GatewayPool Name",
ValueFormat: "{{ .GatewayPool.Name }}",
},
{
Name: "Description",
ValueFormat: "{{ .GatewayPool.Description }}",
},
func (d *displayer) previewGateways() any {
if d.single {
if len(d.gateways) != 1 {
return nil
}
return d.gateways[0]
}

if d.extraFields != nil {
fields = append(fields, d.extraFields...)
return d.gateways
}

func (d *displayer) Payload() any {
if d.gatewayPools != nil {
return d.previewGatewayPools()
}
if d.gatewayPoolsWithIntegrations != nil {
return d.gatewayPoolsWithIntegrations
}
if d.gateways != nil {
return d.previewGateways()
}
if d.gatewayPoolWithOauth != nil {
return d.gatewayPoolWithOauth
}

return nil
}

return fields
func (d *displayer) FieldTemplates() []format.Field {
return d.fields
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func NewCmdGatewayPools(ctx *cmd.Context) *cmd.Command {
command.AddChild(NewCmdUpdate(ctx, nil))
command.AddChild(NewCmdDelete(ctx, nil))
command.AddChild(NewCmdRead(ctx, nil))
command.AddChild(NewCmdListGatewayPoolsGateway(ctx, nil))
return command
}
27 changes: 17 additions & 10 deletions internal/commands/vaultsecrets/gatewaypools/list.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package gatewaypools

import (
Expand Down Expand Up @@ -54,6 +57,19 @@ func NewCmdList(ctx *cmd.Context, runF func(*ListOpts) error) *cmd.Command {
return cmd
}

func listFields() []format.Field {
return []format.Field{
{
Name: "GatewayPool Name",
ValueFormat: "{{ .Name }}",
},
{
Name: "Description",
ValueFormat: "{{ .Description }}",
},
}
}

func listRun(opts *ListOpts) error {
params := &preview_secret_service.ListGatewayPoolsParams{
Context: opts.Ctx,
Expand All @@ -65,15 +81,6 @@ func listRun(opts *ListOpts) error {
if err != nil {
return fmt.Errorf("failed to list gateway pools: %w", err)
}
if resp.Payload == nil || resp.Payload.GatewayPools == nil {
return fmt.Errorf("failed to list gateway pools: empty response")
}

gws := make([]*gatewayPoolWithIntegrations, 0, len(resp.Payload.GatewayPools))
for _, gp := range resp.Payload.GatewayPools {
gws = append(gws, &gatewayPoolWithIntegrations{
GatewayPool: gp,
})
}
return opts.Output.Display(newDisplayer(false, gws...))
return opts.Output.Display(newDisplayer(false).GatewayPools(resp.Payload.GatewayPools...).AddFields(listFields()...))
}
Loading

0 comments on commit 5f50ad8

Please sign in to comment.