Skip to content

Commit

Permalink
Refactor iam commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kobajagi committed Feb 6, 2024
1 parent 20b5e32 commit 5ac4b93
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 137 deletions.
53 changes: 53 additions & 0 deletions cmd/iam.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"

exoscale "github.com/exoscale/egoscale/v2"
)

var iamCmd = &cobra.Command{
Expand Down Expand Up @@ -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
}
47 changes: 2 additions & 45 deletions cmd/iam_org_policy_update.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"strings"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
48 changes: 4 additions & 44 deletions cmd/iam_role_create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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)
}
}

Expand Down
53 changes: 5 additions & 48 deletions cmd/iam_role_update.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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)) {
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 5ac4b93

Please sign in to comment.