yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/shell/iam_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/aws"
    19  	"yunion.io/x/onecloud/pkg/util/shellutils"
    20  )
    21  
    22  func init() {
    23  	type CloudgroupListOptions struct {
    24  		Offset     string
    25  		Limit      int
    26  		PathPrefix string
    27  	}
    28  	shellutils.R(&CloudgroupListOptions{}, "cloud-group-list", "List cloudgroups", func(cli *aws.SRegion, args *CloudgroupListOptions) error {
    29  		groups, err := cli.GetClient().ListGroups(args.Offset, args.Limit, args.PathPrefix)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		printList(groups.Groups, 0, 0, 0, nil)
    34  		return nil
    35  	})
    36  
    37  	type CloudgroupCreateOptions struct {
    38  		NAME string
    39  		Path string
    40  	}
    41  
    42  	shellutils.R(&CloudgroupCreateOptions{}, "cloud-group-create", "Create cloudgroup", func(cli *aws.SRegion, args *CloudgroupCreateOptions) error {
    43  		group, err := cli.GetClient().CreateGroup(args.NAME, args.Path)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		printObject(group)
    48  		return nil
    49  	})
    50  
    51  	type CloudgroupShowOptions struct {
    52  		NAME   string
    53  		Offset string
    54  		Limit  int
    55  	}
    56  
    57  	shellutils.R(&CloudgroupShowOptions{}, "cloud-group-show", "Show cloudgroup details", func(cli *aws.SRegion, args *CloudgroupShowOptions) error {
    58  		group, err := cli.GetClient().GetGroup(args.NAME, args.Offset, args.Limit)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		printObject(group)
    63  		return nil
    64  	})
    65  
    66  	type CloudgroupOptions struct {
    67  		NAME string
    68  	}
    69  
    70  	shellutils.R(&CloudgroupOptions{}, "cloud-group-user-list", "List cloudgroup users", func(cli *aws.SRegion, args *CloudgroupOptions) error {
    71  		users, err := cli.GetClient().ListGroupUsers(args.NAME)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		printList(users, 0, 0, 0, nil)
    76  		return nil
    77  	})
    78  
    79  	shellutils.R(&CloudgroupOptions{}, "cloud-group-delete", "Delete cloudgroup", func(cli *aws.SRegion, args *CloudgroupOptions) error {
    80  		return cli.GetClient().DeleteGroup(args.NAME)
    81  	})
    82  
    83  	type CloudgroupPolicyListOptions struct {
    84  		NAME   string
    85  		Offset string
    86  		Limit  int
    87  	}
    88  
    89  	shellutils.R(&CloudgroupPolicyListOptions{}, "cloud-group-policy-list", "List cloudgroup policies", func(cli *aws.SRegion, args *CloudgroupPolicyListOptions) error {
    90  		policies, err := cli.GetClient().ListGroupPolicies(args.NAME, args.Offset, args.Limit)
    91  		if err != nil {
    92  			return err
    93  		}
    94  		printList(policies.Policies, 0, 0, 0, nil)
    95  		return nil
    96  	})
    97  
    98  	shellutils.R(&CloudgroupPolicyListOptions{}, "cloud-group-attached-policy-list", "List cloudgroup policies", func(cli *aws.SRegion, args *CloudgroupPolicyListOptions) error {
    99  		policies, err := cli.GetClient().ListAttachedGroupPolicies(args.NAME, args.Offset, args.Limit)
   100  		if err != nil {
   101  			return err
   102  		}
   103  		printList(policies.AttachedPolicies, 0, 0, 0, nil)
   104  		return nil
   105  	})
   106  
   107  	type GroupUserOptions struct {
   108  		GROUP string
   109  		USER  string
   110  	}
   111  
   112  	shellutils.R(&GroupUserOptions{}, "cloud-group-add-user", "Add user to cloudgroup", func(cli *aws.SRegion, args *GroupUserOptions) error {
   113  		return cli.GetClient().AddUserToGroup(args.GROUP, args.USER)
   114  	})
   115  
   116  	shellutils.R(&GroupUserOptions{}, "cloud-group-remove-user", "Remove user from cloudgroup", func(cli *aws.SRegion, args *GroupUserOptions) error {
   117  		return cli.GetClient().RemoveUserFromGroup(args.GROUP, args.USER)
   118  	})
   119  
   120  	type GroupPolicyOptions struct {
   121  		GROUP  string
   122  		POLICY string
   123  	}
   124  
   125  	shellutils.R(&GroupPolicyOptions{}, "cloud-group-attach-policy", "Attach policy to cloudgroup", func(cli *aws.SRegion, args *GroupPolicyOptions) error {
   126  		return cli.GetClient().AttachGroupPolicy(args.GROUP, args.POLICY)
   127  	})
   128  
   129  	shellutils.R(&GroupPolicyOptions{}, "cloud-group-detach-policy", "Detach policy from cloudgroup", func(cli *aws.SRegion, args *GroupPolicyOptions) error {
   130  		return cli.GetClient().DetachGroupPolicy(args.GROUP, args.POLICY)
   131  	})
   132  
   133  }