yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/shell/iam_policy.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 PolicyListOptions struct {
    24  		Limit             int
    25  		Offset            string
    26  		OnlyAttached      bool
    27  		PathPrefix        string
    28  		PolicyUsageFilter string `choices:"PermissionsPolicy|PermissionsBoundary"`
    29  		Scope             string `choices:"All|AWS|Local"`
    30  	}
    31  
    32  	shellutils.R(&PolicyListOptions{}, "cloud-policy-list", "List policies", func(cli *aws.SRegion, args *PolicyListOptions) error {
    33  		policies, err := cli.GetClient().ListPolicies(args.Offset, args.Limit, args.OnlyAttached, args.PathPrefix, args.PolicyUsageFilter, args.Scope)
    34  		if err != nil {
    35  			return err
    36  		}
    37  		printList(policies.Policies, 0, 0, 0, nil)
    38  		return nil
    39  	})
    40  
    41  	type PolicyVersionListOptions struct {
    42  		Offset string
    43  		Limit  int
    44  		ARN    string
    45  	}
    46  	shellutils.R(&PolicyVersionListOptions{}, "cloud-policy-version-list", "List policy versions", func(cli *aws.SRegion, args *PolicyVersionListOptions) error {
    47  		versions, err := cli.GetClient().ListPolicyVersions(args.Offset, args.Limit, args.ARN)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		printList(versions.Versions, 0, 0, 0, nil)
    52  		return nil
    53  	})
    54  
    55  	type PolicyVersionOptions struct {
    56  		ARN     string
    57  		VERSION string
    58  	}
    59  
    60  	shellutils.R(&PolicyVersionOptions{}, "cloud-policy-version-show", "Show policy version details", func(cli *aws.SRegion, args *PolicyVersionOptions) error {
    61  		version, err := cli.GetClient().GetPolicyVersion(args.ARN, args.VERSION)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		printObject(version)
    66  		return nil
    67  	})
    68  
    69  	type PolicyArnOptions struct {
    70  		ARN string
    71  	}
    72  
    73  	shellutils.R(&PolicyArnOptions{}, "cloud-policy-show", "Show policy details by policy arn", func(cli *aws.SRegion, args *PolicyArnOptions) error {
    74  		policy, err := cli.GetClient().GetPolicy(args.ARN)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		printObject(policy)
    79  		return nil
    80  	})
    81  
    82  	shellutils.R(&PolicyArnOptions{}, "cloud-policy-delete", "Delete policy", func(cli *aws.SRegion, args *PolicyArnOptions) error {
    83  		return cli.GetClient().DeletePolicy(args.ARN)
    84  	})
    85  
    86  	type PolicyVersionSHowOptions struct {
    87  		ARN     string
    88  		VERSION string
    89  	}
    90  
    91  	shellutils.R(&PolicyVersionSHowOptions{}, "cloud-policy-version-show", "Show policy version details", func(cli *aws.SRegion, args *PolicyVersionSHowOptions) error {
    92  		version, err := cli.GetClient().GetPolicyVersion(args.ARN, args.VERSION)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		printObject(version)
    97  		return nil
    98  	})
    99  
   100  	type PolicyCreateOptions struct {
   101  		NAME     string
   102  		DOCUMENT string
   103  		Path     string
   104  		Desc     string
   105  	}
   106  
   107  	shellutils.R(&PolicyCreateOptions{}, "cloud-policy-create", "Create policy", func(cli *aws.SRegion, args *PolicyCreateOptions) error {
   108  		policy, err := cli.GetClient().CreatePolicy(args.NAME, args.DOCUMENT, args.Path, args.Desc)
   109  		if err != nil {
   110  			return err
   111  		}
   112  		printObject(policy)
   113  		return nil
   114  	})
   115  
   116  	type PolicyVersionCreateOptions struct {
   117  		ARN       string
   118  		DOCUMENT  string
   119  		IsDefault bool
   120  	}
   121  
   122  	shellutils.R(&PolicyVersionCreateOptions{}, "cloud-policy-version-create", "Create policy version", func(cli *aws.SRegion, args *PolicyVersionCreateOptions) error {
   123  		return cli.GetClient().CreatePolicyVersion(args.ARN, args.DOCUMENT, args.IsDefault)
   124  	})
   125  
   126  }