From 5ac4b93c476b36afcc800d7c7eadddac922fe4c3 Mon Sep 17 00:00:00 2001 From: Predrag Janosevic Date: Tue, 6 Feb 2024 16:05:02 +0100 Subject: [PATCH] Refactor iam commands --- cmd/iam.go | 53 ++++++++++++++++++++++++++++++++++++ cmd/iam_org_policy_update.go | 47 ++------------------------------ cmd/iam_role_create.go | 48 +++----------------------------- cmd/iam_role_update.go | 53 ++++-------------------------------- 4 files changed, 64 insertions(+), 137 deletions(-) diff --git a/cmd/iam.go b/cmd/iam.go index 2f193962c..8cd9d4255 100644 --- a/cmd/iam.go +++ b/cmd/iam.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "fmt" "os" @@ -8,6 +9,8 @@ import ( "github.com/exoscale/cli/pkg/output" "github.com/exoscale/cli/table" + + exoscale "github.com/exoscale/egoscale/v2" ) var iamCmd = &cobra.Command{ @@ -67,3 +70,53 @@ func (o *iamPolicyOutput) ToTable() { } } } + +func iamPolicyFromJSON(data []byte) (*exoscale.IAMPolicy, error) { + var obj iamPolicyOutput + err := json.Unmarshal(data, &obj) + if err != nil { + return nil, fmt.Errorf("failed to parse policy: %w", err) + } + + policy := exoscale.IAMPolicy{ + DefaultServiceStrategy: obj.DefaultServiceStrategy, + Services: map[string]exoscale.IAMPolicyService{}, + } + + if len(obj.Services) > 0 { + for name, sv := range obj.Services { + service := exoscale.IAMPolicyService{ + Type: func() *string { + t := sv.Type + return &t + }(), + } + + if len(sv.Rules) > 0 { + service.Rules = []exoscale.IAMPolicyServiceRule{} + for _, rl := range sv.Rules { + + rule := exoscale.IAMPolicyServiceRule{ + Action: func() *string { + t := rl.Action + return &t + }(), + } + + if rl.Expression != "" { + rule.Expression = func() *string { + t := rl.Expression + return &t + }() + } + + service.Rules = append(service.Rules, rule) + } + } + + policy.Services[name] = service + } + } + + return &policy, nil +} diff --git a/cmd/iam_org_policy_update.go b/cmd/iam_org_policy_update.go index 0bc38bae9..bddd9c096 100644 --- a/cmd/iam_org_policy_update.go +++ b/cmd/iam_org_policy_update.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "fmt" "io" "strings" @@ -11,7 +10,6 @@ import ( "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" - exoscale "github.com/exoscale/egoscale/v2" exoapi "github.com/exoscale/egoscale/v2/api" ) @@ -64,50 +62,9 @@ func (c *iamOrgPolicyUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error { c.Policy = string(b) } - var obj iamPolicyOutput - err := json.Unmarshal([]byte(c.Policy), &obj) + policy, err := iamPolicyFromJSON([]byte(c.Policy)) if err != nil { - return fmt.Errorf("failed to parse policy: %w", err) - } - - policy := &exoscale.IAMPolicy{ - DefaultServiceStrategy: obj.DefaultServiceStrategy, - Services: map[string]exoscale.IAMPolicyService{}, - } - - if len(obj.Services) > 0 { - for name, sv := range obj.Services { - service := exoscale.IAMPolicyService{ - Type: func() *string { - t := sv.Type - return &t - }(), - } - - if len(sv.Rules) > 0 { - service.Rules = []exoscale.IAMPolicyServiceRule{} - for _, rl := range sv.Rules { - - rule := exoscale.IAMPolicyServiceRule{ - Action: func() *string { - t := rl.Action - return &t - }(), - } - - if rl.Expression != "" { - rule.Expression = func() *string { - t := rl.Expression - return &t - }() - } - - service.Rules = append(service.Rules, rule) - } - } - - policy.Services[name] = service - } + return fmt.Errorf("failed to parse IAM policy: %w", err) } err = globalstate.EgoscaleClient.UpdateIAMOrgPolicy(ctx, zone, policy) diff --git a/cmd/iam_role_create.go b/cmd/iam_role_create.go index 516aed345..3a5802f83 100644 --- a/cmd/iam_role_create.go +++ b/cmd/iam_role_create.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "errors" "fmt" "io" @@ -77,50 +76,11 @@ func (c *iamRoleCreateCmd) cmdRun(cmd *cobra.Command, _ []string) error { c.Policy = string(b) } - var obj iamPolicyOutput - err := json.Unmarshal([]byte(c.Policy), &obj) - if err != nil { - return fmt.Errorf("failed to parse policy: %w", err) - } - - policy = &exoscale.IAMPolicy{ - DefaultServiceStrategy: obj.DefaultServiceStrategy, - Services: map[string]exoscale.IAMPolicyService{}, - } + var err error - if len(obj.Services) > 0 { - for name, sv := range obj.Services { - service := exoscale.IAMPolicyService{ - Type: func() *string { - t := sv.Type - return &t - }(), - } - - if len(sv.Rules) > 0 { - service.Rules = []exoscale.IAMPolicyServiceRule{} - for _, rl := range sv.Rules { - - rule := exoscale.IAMPolicyServiceRule{ - Action: func() *string { - t := rl.Action - return &t - }(), - } - - if rl.Expression != "" { - rule.Expression = func() *string { - t := rl.Expression - return &t - }() - } - - service.Rules = append(service.Rules, rule) - } - } - - policy.Services[name] = service - } + policy, err = iamPolicyFromJSON([]byte(c.Policy)) + if err != nil { + return fmt.Errorf("failed to parse IAM policy: %w", err) } } diff --git a/cmd/iam_role_update.go b/cmd/iam_role_update.go index f6403dbd7..6ecdfaa75 100644 --- a/cmd/iam_role_update.go +++ b/cmd/iam_role_update.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "errors" "fmt" "io" @@ -73,9 +72,8 @@ func (c *iamRoleUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error { } } - role, err := globalstate.EgoscaleClient.GetIAMRole(ctx, zone, c.Role) - if err != nil { - return err + role := &exoscale.IAMRole{ + ID: &c.Role, } if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) { @@ -88,7 +86,7 @@ func (c *iamRoleUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error { role.Permissions = c.Permissions } - err = globalstate.EgoscaleClient.UpdateIAMRole(ctx, zone, role) + err := globalstate.EgoscaleClient.UpdateIAMRole(ctx, zone, role) if err != nil { return err } @@ -115,50 +113,9 @@ func (c *iamRoleUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error { c.Policy = string(b) } - var obj iamPolicyOutput - err = json.Unmarshal([]byte(c.Policy), &obj) + policy, err := iamPolicyFromJSON([]byte(c.Policy)) if err != nil { - return fmt.Errorf("failed to parse policy: %w", err) - } - - policy := &exoscale.IAMPolicy{ - DefaultServiceStrategy: obj.DefaultServiceStrategy, - Services: map[string]exoscale.IAMPolicyService{}, - } - - if len(obj.Services) > 0 { - for name, sv := range obj.Services { - service := exoscale.IAMPolicyService{ - Type: func() *string { - t := sv.Type - return &t - }(), - } - - if len(sv.Rules) > 0 { - service.Rules = []exoscale.IAMPolicyServiceRule{} - for _, rl := range sv.Rules { - - rule := exoscale.IAMPolicyServiceRule{ - Action: func() *string { - t := rl.Action - return &t - }(), - } - - if rl.Expression != "" { - rule.Expression = func() *string { - t := rl.Expression - return &t - }() - } - - service.Rules = append(service.Rules, rule) - } - } - - policy.Services[name] = service - } + return fmt.Errorf("failed to parse IAM policy: %w", err) } role.Policy = policy