yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/shell/iam_role.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package shell 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/pkg/errors" 21 22 "yunion.io/x/cloudmux/pkg/cloudprovider" 23 "yunion.io/x/cloudmux/pkg/multicloud/aws" 24 "yunion.io/x/onecloud/pkg/util/shellutils" 25 ) 26 27 func init() { 28 type RoleListOptions struct { 29 Offset string 30 Limit int 31 PathPrefix string 32 } 33 shellutils.R(&RoleListOptions{}, "cloud-role-list", "List roles", func(cli *aws.SRegion, args *RoleListOptions) error { 34 roles, err := cli.GetClient().ListRoles(args.Offset, args.Limit, args.PathPrefix) 35 if err != nil { 36 return err 37 } 38 printList(roles.Roles, 0, 0, 0, []string{}) 39 return nil 40 }) 41 42 type RoleNameOptions struct { 43 ROLE string 44 } 45 46 shellutils.R(&RoleNameOptions{}, "cloud-role-show", "Show role", func(cli *aws.SRegion, args *RoleNameOptions) error { 47 role, err := cli.GetClient().GetRole(args.ROLE) 48 if err != nil { 49 return err 50 } 51 printObject(role) 52 document := role.GetDocument() 53 if document != nil { 54 printObject(document) 55 } 56 return nil 57 }) 58 59 type RoleAttachPolicyListOptions struct { 60 ROLE string 61 Marker string 62 MaxItems int 63 PathPrefix string 64 } 65 66 shellutils.R(&RoleAttachPolicyListOptions{}, "cloud-role-attach-policy-list", "List Role attach policy", func(cli *aws.SRegion, args *RoleAttachPolicyListOptions) error { 67 policy, err := cli.GetClient().ListAttachedRolePolicies(args.ROLE, args.Marker, args.MaxItems, args.PathPrefix) 68 if err != nil { 69 return errors.Wrapf(err, "ListAttachedRolePolicies") 70 } 71 printList(policy.AttachedPolicies, 0, 0, 0, nil) 72 if len(policy.Marker) > 0 { 73 fmt.Println("marker: ", policy.Marker) 74 } 75 return nil 76 }) 77 78 shellutils.R(&RoleNameOptions{}, "cloud-role-delete", "Delete role", func(cli *aws.SRegion, args *RoleNameOptions) error { 79 return cli.GetClient().DeleteRole(args.ROLE) 80 }) 81 82 shellutils.R(&cloudprovider.SRoleCreateOptions{}, "cloud-role-create", "Create role", func(cli *aws.SRegion, args *cloudprovider.SRoleCreateOptions) error { 83 role, err := cli.GetClient().CreateRole(args) 84 if err != nil { 85 return err 86 } 87 printObject(role) 88 return nil 89 }) 90 91 }