yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/cam_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  	"strconv"
    19  
    20  	"yunion.io/x/cloudmux/pkg/multicloud/qcloud"
    21  	"yunion.io/x/onecloud/pkg/util/shellutils"
    22  )
    23  
    24  func init() {
    25  	type RoleListOptions struct {
    26  		Offset int
    27  		Limit  int
    28  	}
    29  	shellutils.R(&RoleListOptions{}, "cloud-role-list", "List roles", func(cli *qcloud.SRegion, args *RoleListOptions) error {
    30  		roles, _, err := cli.GetClient().DescribeRoleList(args.Offset, args.Limit)
    31  		if err != nil {
    32  			return err
    33  		}
    34  		printList(roles, 0, 0, 0, nil)
    35  		return nil
    36  	})
    37  
    38  	type RoleNameOptions struct {
    39  		ROLE string
    40  	}
    41  
    42  	shellutils.R(&RoleNameOptions{}, "cloud-role-show", "Show role details", func(cli *qcloud.SRegion, args *RoleNameOptions) error {
    43  		role, err := cli.GetClient().GetRole(args.ROLE)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		printObject(role)
    48  		return nil
    49  	})
    50  
    51  	shellutils.R(&RoleNameOptions{}, "cloud-role-delete", "Delete role", func(cli *qcloud.SRegion, args *RoleNameOptions) error {
    52  		return cli.GetClient().DeleteRole(args.ROLE)
    53  	})
    54  
    55  	type RolePolicyOptions struct {
    56  		ROLE       string
    57  		PolicyType string `choices:"User|QCS"`
    58  		Offset     int
    59  		Limit      int
    60  	}
    61  
    62  	shellutils.R(&RolePolicyOptions{}, "cloud-role-policy-list", "List role policies", func(cli *qcloud.SRegion, args *RolePolicyOptions) error {
    63  		policies, _, err := cli.GetClient().ListAttachedRolePolicies(args.ROLE, args.PolicyType, args.Offset, args.Limit)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		printList(policies, 0, 0, 0, nil)
    68  		return nil
    69  	})
    70  
    71  	type RolePolicyActionOptions struct {
    72  		ROLE      string
    73  		POLICY_ID int
    74  	}
    75  
    76  	shellutils.R(&RolePolicyActionOptions{}, "cloud-role-attach-policy", "Attach role policy", func(cli *qcloud.SRegion, args *RolePolicyActionOptions) error {
    77  		return cli.GetClient().AttachRolePolicy(args.ROLE, strconv.Itoa(args.POLICY_ID))
    78  	})
    79  
    80  	shellutils.R(&RolePolicyActionOptions{}, "cloud-role-detach-policy", "Detach role policy", func(cli *qcloud.SRegion, args *RolePolicyActionOptions) error {
    81  		return cli.GetClient().AttachRolePolicy(args.ROLE, strconv.Itoa(args.POLICY_ID))
    82  	})
    83  
    84  	type RoleCreateOption struct {
    85  		NAME     string
    86  		DOCUMENT string
    87  		Desc     string
    88  	}
    89  
    90  	shellutils.R(&RoleCreateOption{}, "cloud-role-create", "Create role", func(cli *qcloud.SRegion, args *RoleCreateOption) error {
    91  		role, err := cli.GetClient().CreateRole(args.NAME, args.DOCUMENT, args.Desc)
    92  		if err != nil {
    93  			return err
    94  		}
    95  		printObject(role)
    96  		return nil
    97  	})
    98  }