yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/ram_group.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 CloudgroupCreateOptions struct {
    24  		NAME     string
    25  		Comments string
    26  	}
    27  
    28  	shellutils.R(&CloudgroupCreateOptions{}, "cloud-group-create", "Create Cloud group", func(cli *aliyun.SRegion, args *CloudgroupCreateOptions) error {
    29  		group, err := cli.GetClient().CreateGroup(args.NAME, args.Comments)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		printObject(group)
    34  		return nil
    35  	})
    36  
    37  	type CloudgroupListOptions struct {
    38  		Offset string
    39  		Limit  int
    40  	}
    41  
    42  	shellutils.R(&CloudgroupListOptions{}, "cloud-group-list", "List Cloud groups", func(cli *aliyun.SRegion, args *CloudgroupListOptions) error {
    43  		groups, err := cli.GetClient().ListGroups(args.Offset, args.Limit)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		printList(groups.Groups.Group, 0, 0, 0, nil)
    48  		return nil
    49  	})
    50  
    51  	type CloudgroupDeleteOptions struct {
    52  		NAME string
    53  	}
    54  
    55  	shellutils.R(&CloudgroupDeleteOptions{}, "cloud-group-delete", "Delete Cloud group", func(cli *aliyun.SRegion, args *CloudgroupDeleteOptions) error {
    56  		return cli.GetClient().DeleteGroup(args.NAME)
    57  	})
    58  
    59  	type GroupExtListOptions struct {
    60  		GROUP  string
    61  		Offset string
    62  		Limit  int
    63  	}
    64  	shellutils.R(&GroupExtListOptions{}, "cloud-group-user-list", "List Cloud group users", func(cli *aliyun.SRegion, args *GroupExtListOptions) error {
    65  		users, err := cli.GetClient().ListUsersForGroup(args.GROUP, args.Offset, args.Limit)
    66  		if err != nil {
    67  			return err
    68  		}
    69  		printList(users.Users.User, 0, 0, 0, nil)
    70  		return nil
    71  	})
    72  
    73  	type GroupUserOptions struct {
    74  		GROUP string
    75  		USER  string
    76  	}
    77  
    78  	shellutils.R(&GroupUserOptions{}, "cloud-group-remove-user", "Remove user from group", func(cli *aliyun.SRegion, args *GroupUserOptions) error {
    79  		return cli.GetClient().RemoveUserFromGroup(args.GROUP, args.USER)
    80  	})
    81  
    82  	shellutils.R(&GroupUserOptions{}, "cloud-group-add-user", "Add user to group", func(cli *aliyun.SRegion, args *GroupUserOptions) error {
    83  		return cli.GetClient().AddUserToGroup(args.GROUP, args.USER)
    84  	})
    85  
    86  	shellutils.R(&GroupExtListOptions{}, "cloud-group-policy-list", "List Cloud group policies", func(cli *aliyun.SRegion, args *GroupExtListOptions) error {
    87  		policies, err := cli.GetClient().ListPoliciesForGroup(args.GROUP)
    88  		if err != nil {
    89  			return err
    90  		}
    91  		printList(policies, 0, 0, 0, nil)
    92  		return nil
    93  	})
    94  
    95  	type GroupPolicyOptions struct {
    96  		GROUP      string
    97  		PolicyType string `default:"System" choices:"System|Custom"`
    98  		POLICY     string
    99  	}
   100  
   101  	shellutils.R(&GroupPolicyOptions{}, "cloud-group-attach-policy", "Attach policy for group", func(cli *aliyun.SRegion, args *GroupPolicyOptions) error {
   102  		return cli.GetClient().AttachPolicyToGroup(args.PolicyType, args.POLICY, args.GROUP)
   103  	})
   104  
   105  	shellutils.R(&GroupPolicyOptions{}, "cloud-group-detach-policy", "Detach policy from group", func(cli *aliyun.SRegion, args *GroupPolicyOptions) error {
   106  		return cli.GetClient().DetachPolicyFromGroup(args.PolicyType, args.POLICY, args.GROUP)
   107  	})
   108  
   109  }