Skip to content

Commit

Permalink
Remove iam org-policy update & refactor iam policy output
Browse files Browse the repository at this point in the history
  • Loading branch information
kobajagi committed Nov 15, 2023
1 parent cc3212f commit 67d3ce8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 248 deletions.
58 changes: 57 additions & 1 deletion cmd/iam.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package cmd

import "github.com/spf13/cobra"
import (
"fmt"
"os"

"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/spf13/cobra"
)

var iamCmd = &cobra.Command{
Use: "iam",
Expand All @@ -10,3 +17,52 @@ var iamCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(iamCmd)
}

type iamPolicyOutput struct {
DefaultServiceStrategy string `json:"default-service-strategy"`
Services map[string]iamPolicyServiceOutput `json:"services"`
}

type iamPolicyServiceOutput struct {
Type string `json:"type"`
Rules []iamPolicyServiceRuleOutput `json:"rules"`
}

type iamPolicyServiceRuleOutput struct {
Action string `json:"action"`
Expression string `json:"expression"`
}

func (o *iamPolicyOutput) ToJSON() { output.JSON(o) }
func (o *iamPolicyOutput) ToText() { output.Text(o) }
func (o *iamPolicyOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetAutoMergeCellsByColumnIndex([]int{0, 1})

t.SetHeader([]string{
"Service",
fmt.Sprintf("Type (default strategy \"%s\")", o.DefaultServiceStrategy),
"Rule Action",
"Rule Expression",
})

// use underlying tablewriter.Render to display table even with empty rows
// as default strategy is in header.
defer t.Table.Render()

for name, service := range o.Services {
if len(service.Rules) == 0 {
t.Append([]string{name, service.Type, "", "", ""})
continue
}

for _, rule := range service.Rules {
t.Append([]string{
name,
service.Type,
rule.Action,
rule.Expression,
})
}
}
}
4 changes: 2 additions & 2 deletions cmd/iam_org_policy_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Pro Tip: you can get policy in JSON format with command:
exo iam org-policy show --output-format json
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&iamOrgPolicyShowOutput{}), ", "))
strings.Join(output.TemplateAnnotations(&iamPolicyOutput{}), ", "))
}

func (c *iamOrgPolicyReplaceCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
Expand All @@ -62,7 +62,7 @@ func (c *iamOrgPolicyReplaceCmd) cmdRun(cmd *cobra.Command, _ []string) error {
c.Policy = string(b)
}

var obj iamOrgPolicyShowOutput
var obj iamPolicyOutput
err := json.Unmarshal([]byte(c.Policy), &obj)
if err != nil {
return fmt.Errorf("failed to parse policy: %w", err)
Expand Down
64 changes: 6 additions & 58 deletions cmd/iam_org_policy_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,17 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)

type iamOrgPolicyShowOutput struct {
DefaultServiceStrategy string `json:"default-service-strategy"`
Services map[string]iamOrgPolicyServiceShowOutput `json:"services"`
}

type iamOrgPolicyServiceShowOutput struct {
Type string `json:"type"`
Rules []iamOrgPolicyServiceRuleShowOutput `json:"rules"`
}

type iamOrgPolicyServiceRuleShowOutput struct {
Action string `json:"action"`
Expression string `json:"expression"`
}

func (o *iamOrgPolicyShowOutput) ToJSON() { output.JSON(o) }
func (o *iamOrgPolicyShowOutput) ToText() { output.Text(o) }
func (o *iamOrgPolicyShowOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetAutoMergeCells(true)
t.SetAutoMergeCellsByColumnIndex([]int{0, 1})

t.SetHeader([]string{
"Service",
fmt.Sprintf("Type (default strategy \"%s\")", o.DefaultServiceStrategy),
"Rule Action",
"Rule Expression",
})

// use underlying tablewriter.Render to display table even with empty rows
// as default strategy is in header.
defer t.Table.Render()

for name, service := range o.Services {
if len(service.Rules) == 0 {
t.Append([]string{name, service.Type, "", "", ""})
continue
}

for _, rule := range service.Rules {
t.Append([]string{
name,
service.Type,
rule.Action,
rule.Expression,
})
}
}
}

type iamOrgPolicyShowCmd struct {
cliCommandSettings `cli-cmd:"-"`

Expand All @@ -81,7 +29,7 @@ func (c *iamOrgPolicyShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows IAM Org Policy details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&iamOrgPolicyShowOutput{}), ", "))
strings.Join(output.TemplateAnnotations(&iamPolicyServiceOutput{}), ", "))
}

func (c *iamOrgPolicyShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
Expand All @@ -97,21 +45,21 @@ func (c *iamOrgPolicyShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
return err
}

out := iamOrgPolicyShowOutput{
out := iamPolicyOutput{
DefaultServiceStrategy: policy.DefaultServiceStrategy,
Services: map[string]iamOrgPolicyServiceShowOutput{},
Services: map[string]iamPolicyServiceOutput{},
}

for name, service := range policy.Services {
rules := []iamOrgPolicyServiceRuleShowOutput{}
rules := []iamPolicyServiceRuleOutput{}
for _, rule := range service.Rules {
rules = append(rules, iamOrgPolicyServiceRuleShowOutput{
rules = append(rules, iamPolicyServiceRuleOutput{
Action: utils.DefaultString(rule.Action, ""),
Expression: utils.DefaultString(rule.Expression, ""),
})
}

out.Services[name] = iamOrgPolicyServiceShowOutput{
out.Services[name] = iamPolicyServiceOutput{
Type: utils.DefaultString(service.Type, ""),
Rules: rules,
}
Expand Down
187 changes: 0 additions & 187 deletions cmd/iam_org_policy_update.go

This file was deleted.

0 comments on commit 67d3ce8

Please sign in to comment.