yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/cam_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/qcloud"
    19  	"yunion.io/x/onecloud/pkg/util/shellutils"
    20  )
    21  
    22  func init() {
    23  	type CloudgroupListOptions struct {
    24  		Keyword string
    25  		Offset  int
    26  		Limit   int
    27  	}
    28  	shellutils.R(&CloudgroupListOptions{}, "cloud-group-list", "List cloudgroups", func(cli *qcloud.SRegion, args *CloudgroupListOptions) error {
    29  		groups, _, err := cli.GetClient().ListGroups(args.Keyword, args.Offset, args.Limit)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		printList(groups, 0, 0, 0, nil)
    34  		return nil
    35  	})
    36  
    37  	type CloudgroupIdOptions struct {
    38  		ID int
    39  	}
    40  
    41  	shellutils.R(&CloudgroupIdOptions{}, "cloud-group-delete", "Delete cloudgroup", func(cli *qcloud.SRegion, args *CloudgroupIdOptions) error {
    42  		return cli.GetClient().DeleteGroup(args.ID)
    43  	})
    44  
    45  	shellutils.R(&CloudgroupIdOptions{}, "cloud-group-show", "Show cloudgroup", func(cli *qcloud.SRegion, args *CloudgroupIdOptions) error {
    46  		group, err := cli.GetClient().GetGroup(args.ID)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		printObject(group)
    51  		return nil
    52  	})
    53  
    54  	type CloudgroupCreateOptions struct {
    55  		NAME   string
    56  		Remark string
    57  	}
    58  
    59  	shellutils.R(&CloudgroupCreateOptions{}, "cloud-group-create", "Create cloudgroup", func(cli *qcloud.SRegion, args *CloudgroupCreateOptions) error {
    60  		group, err := cli.GetClient().CreateGroup(args.NAME, args.Remark)
    61  		if err != nil {
    62  			return err
    63  		}
    64  		printObject(group)
    65  		return nil
    66  	})
    67  
    68  	type CloudgroupUserListOptions struct {
    69  		GROUPID int
    70  		Offset  int
    71  		Limit   int
    72  	}
    73  	shellutils.R(&CloudgroupUserListOptions{}, "cloud-group-user-list", "List cloudgroup users", func(cli *qcloud.SRegion, args *CloudgroupUserListOptions) error {
    74  		users, _, err := cli.GetClient().ListGroupUsers(args.GROUPID, args.Offset, args.Limit)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		printList(users, 0, 0, 0, nil)
    79  		return nil
    80  	})
    81  
    82  	type GroupUserOptions struct {
    83  		GROUP int
    84  		USER  int
    85  	}
    86  
    87  	shellutils.R(&GroupUserOptions{}, "cloud-group-add-user", "Add user to cloudgroup", func(cli *qcloud.SRegion, args *GroupUserOptions) error {
    88  		return cli.GetClient().AddUserToGroup(args.GROUP, args.USER)
    89  	})
    90  
    91  	shellutils.R(&GroupUserOptions{}, "cloud-group-remove-user", "Remove user from cloudgroup", func(cli *qcloud.SRegion, args *GroupUserOptions) error {
    92  		return cli.GetClient().RemoveUserFromGroup(args.GROUP, args.USER)
    93  	})
    94  
    95  	type GroupPolicyOptions struct {
    96  		GROUP  int
    97  		POLICY string
    98  	}
    99  
   100  	shellutils.R(&GroupPolicyOptions{}, "cloud-group-attach-policy", "Attach policy to cloudgroup", func(cli *qcloud.SRegion, args *GroupPolicyOptions) error {
   101  		return cli.GetClient().AttachGroupPolicy(args.GROUP, args.POLICY)
   102  	})
   103  
   104  	shellutils.R(&GroupPolicyOptions{}, "cloud-group-detach-policy", "Detach policy from cloudgroup", func(cli *qcloud.SRegion, args *GroupPolicyOptions) error {
   105  		return cli.GetClient().DetachGroupPolicy(args.GROUP, args.POLICY)
   106  	})
   107  
   108  }