yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/shell/ram_user.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/apsara"
    19  	"yunion.io/x/onecloud/pkg/util/shellutils"
    20  )
    21  
    22  func init() {
    23  	type ClouduserCreateOptions struct {
    24  		NAME        string
    25  		MobilePhone string
    26  		Comments    string
    27  		Email       string
    28  	}
    29  
    30  	shellutils.R(&ClouduserCreateOptions{}, "cloud-user-create", "Create Cloud user", func(cli *apsara.SRegion, args *ClouduserCreateOptions) error {
    31  		user, err := cli.GetClient().CreateUser(args.NAME, args.MobilePhone, args.Email, args.Comments)
    32  		if err != nil {
    33  			return err
    34  		}
    35  		printObject(user)
    36  		return nil
    37  	})
    38  
    39  	type ClouduserListOptions struct {
    40  		Offset string
    41  		Limit  int
    42  	}
    43  
    44  	shellutils.R(&ClouduserListOptions{}, "cloud-user-list", "List Cloud users", func(cli *apsara.SRegion, args *ClouduserListOptions) error {
    45  		users, err := cli.GetClient().ListUsers(args.Offset, args.Limit)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		printList(users.Users.User, 0, 0, 0, nil)
    50  		return nil
    51  	})
    52  
    53  	type UserPolicyListOptions struct {
    54  		USER string
    55  	}
    56  
    57  	shellutils.R(&UserPolicyListOptions{}, "cloud-user-policy-list", "List Cloud user policies", func(cli *apsara.SRegion, args *UserPolicyListOptions) error {
    58  		policies, err := cli.GetClient().ListPoliciesForUser(args.USER)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		printList(policies, 0, 0, 0, nil)
    63  		return nil
    64  	})
    65  
    66  	type ClouduserOptions struct {
    67  		NAME string
    68  	}
    69  
    70  	shellutils.R(&ClouduserOptions{}, "cloud-user-group-list", "List Cloud user groups", func(cli *apsara.SRegion, args *ClouduserOptions) error {
    71  		groups, err := cli.GetClient().ListGroupsForUser(args.NAME)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		printList(groups, 0, 0, 0, nil)
    76  		return nil
    77  	})
    78  
    79  	shellutils.R(&ClouduserOptions{}, "cloud-user-delete", "Delete Cloud user", func(cli *apsara.SRegion, args *ClouduserOptions) error {
    80  		return cli.GetClient().DeleteClouduser(args.NAME)
    81  	})
    82  
    83  	shellutils.R(&ClouduserOptions{}, "cloud-user-loginprofile", "Get Cloud user loginprofile", func(cli *apsara.SRegion, args *ClouduserOptions) error {
    84  		profile, err := cli.GetClient().GetLoginProfile(args.NAME)
    85  		if err != nil {
    86  			return err
    87  		}
    88  		printObject(profile)
    89  		return nil
    90  	})
    91  
    92  	shellutils.R(&ClouduserOptions{}, "cloud-user-loginprofile-delete", "Delete Cloud user loginprofile", func(cli *apsara.SRegion, args *ClouduserOptions) error {
    93  		return cli.GetClient().DeleteLoginProfile(args.NAME)
    94  	})
    95  
    96  	type LoginProfileCreateOptions struct {
    97  		NAME     string
    98  		PASSWORD string
    99  	}
   100  
   101  	shellutils.R(&LoginProfileCreateOptions{}, "cloud-user-loginprofile-create", "Create Cloud user loginprofile", func(cli *apsara.SRegion, args *LoginProfileCreateOptions) error {
   102  		profile, err := cli.GetClient().CreateLoginProfile(args.NAME, args.PASSWORD)
   103  		if err != nil {
   104  			return err
   105  		}
   106  		printObject(profile)
   107  		return nil
   108  	})
   109  
   110  	shellutils.R(&LoginProfileCreateOptions{}, "cloud-user-rest-password", "Reset Cloud user password", func(cli *apsara.SRegion, args *LoginProfileCreateOptions) error {
   111  		return cli.GetClient().ResetClouduserPassword(args.NAME, args.PASSWORD)
   112  	})
   113  
   114  	type ClouduserPolicyOptions struct {
   115  		PolicyType string `default:"System" choices:"System|Custom"`
   116  		POLICY     string
   117  		USER       string
   118  	}
   119  
   120  	shellutils.R(&ClouduserPolicyOptions{}, "cloud-user-attach-policy", "Attach policy for user", func(cli *apsara.SRegion, args *ClouduserPolicyOptions) error {
   121  		return cli.GetClient().AttachPolicyToUser(args.POLICY, args.PolicyType, args.USER)
   122  	})
   123  
   124  	shellutils.R(&ClouduserPolicyOptions{}, "cloud-user-detach-policy", "Detach policy from user", func(cli *apsara.SRegion, args *ClouduserPolicyOptions) error {
   125  		return cli.GetClient().DetachPolicyFromUser(args.POLICY, args.PolicyType, args.USER)
   126  	})
   127  
   128  }