yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/ram_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  	"yunion.io/x/cloudmux/pkg/multicloud/aliyun"
    19  	"yunion.io/x/onecloud/pkg/util/shellutils"
    20  )
    21  
    22  func init() {
    23  	type ListRolesOptions struct {
    24  		Offset string
    25  		Limit  int
    26  	}
    27  	shellutils.R(&ListRolesOptions{}, "cloud-role-list", "List ram roles", func(cli *aliyun.SRegion, args *ListRolesOptions) error {
    28  		roles, err := cli.GetClient().ListRoles(args.Offset, args.Limit)
    29  		if err != nil {
    30  			return err
    31  		}
    32  		printList(roles.Roles.Role, 0, 0, 0, []string{})
    33  		return nil
    34  	})
    35  
    36  	type GetRoleOptions struct {
    37  		ROLENAME string
    38  	}
    39  	shellutils.R(&GetRoleOptions{}, "cloud-role-show", "Show ram role", func(cli *aliyun.SRegion, args *GetRoleOptions) error {
    40  		role, err := cli.GetClient().GetRole(args.ROLENAME)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		printObject(role)
    45  		return nil
    46  	})
    47  
    48  	type RoleCreateOptions struct {
    49  		NAME     string
    50  		DOCUMENT string
    51  		Desc     string
    52  	}
    53  
    54  	shellutils.R(&RoleCreateOptions{}, "cloud-role-create", "Create ram role", func(cli *aliyun.SRegion, args *RoleCreateOptions) error {
    55  		role, err := cli.GetClient().CreateRole(args.NAME, args.DOCUMENT, args.Desc)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		printObject(role)
    60  		return nil
    61  	})
    62  
    63  	type RolePolicyOptions struct {
    64  		ROLENAME   string
    65  		POLICYNAME string
    66  		POLICYTYPE string `choices:"Custom|System"`
    67  	}
    68  
    69  	shellutils.R(&RolePolicyOptions{}, "cloud-role-attach-policy", "Attach policy for role", func(cli *aliyun.SRegion, args *RolePolicyOptions) error {
    70  		return cli.GetClient().AttachPolicy2Role(args.POLICYTYPE, args.POLICYNAME, args.ROLENAME)
    71  	})
    72  
    73  	shellutils.R(&RolePolicyOptions{}, "cloud-role-detach-policy", "Detach policy from role", func(cli *aliyun.SRegion, args *RolePolicyOptions) error {
    74  		return cli.GetClient().DetachPolicyFromRole(args.POLICYTYPE, args.POLICYNAME, args.ROLENAME)
    75  	})
    76  
    77  	type RolePolicyListOptions struct {
    78  		ROLE string
    79  	}
    80  
    81  	shellutils.R(&RolePolicyListOptions{}, "cloud-role-policy-list", "List cloud role policies", func(cli *aliyun.SRegion, args *RolePolicyListOptions) error {
    82  		policies, err := cli.GetClient().ListPoliciesForRole(args.ROLE)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		printList(policies, 0, 0, 0, nil)
    87  		return nil
    88  	})
    89  
    90  	type DeleteRoleOptions struct {
    91  		NAME string
    92  	}
    93  	shellutils.R(&DeleteRoleOptions{}, "cloud-role-delete", "Delete role", func(cli *aliyun.SRegion, args *DeleteRoleOptions) error {
    94  		return cli.GetClient().DeleteRole(args.NAME)
    95  	})
    96  
    97  	shellutils.R(&ListRolesOptions{}, "enable-image-import", "Enable image import privilege", func(cli *aliyun.SRegion, args *ListRolesOptions) error {
    98  		return cli.GetClient().EnableImageImport()
    99  	})
   100  
   101  	shellutils.R(&ListRolesOptions{}, "enable-image-export", "Enable image export privilege", func(cli *aliyun.SRegion, args *ListRolesOptions) error {
   102  		return cli.GetClient().EnableImageExport()
   103  	})
   104  
   105  	type CallerShowOptions struct {
   106  	}
   107  
   108  	shellutils.R(&CallerShowOptions{}, "caller-show", "Show caller info", func(cli *aliyun.SRegion, args *CallerShowOptions) error {
   109  		caller, err := cli.GetClient().GetCallerIdentity()
   110  		if err != nil {
   111  			return err
   112  		}
   113  		printObject(caller)
   114  		return nil
   115  	})
   116  
   117  }