yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/shell/role.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  	"fmt"
    19  	"io/ioutil"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  
    24  	cloudid_api "yunion.io/x/cloudmux/pkg/apis/cloudid"
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	huawei "yunion.io/x/cloudmux/pkg/multicloud/hcso"
    27  	"yunion.io/x/onecloud/pkg/util/shellutils"
    28  )
    29  
    30  func init() {
    31  	type RoleListOptions struct {
    32  		DomainId string
    33  		Name     string
    34  	}
    35  	shellutils.R(&RoleListOptions{}, "cloud-policy-list", "List cloudpolicy", func(cli *huawei.SRegion, args *RoleListOptions) error {
    36  		roles, err := cli.GetClient().GetRoles(args.DomainId, args.Name)
    37  		if err != nil {
    38  			return err
    39  		}
    40  		printList(roles, 0, 0, 0, nil)
    41  		return nil
    42  	})
    43  
    44  	shellutils.R(&RoleListOptions{}, "cloud-custom-policy-list", "List cloudpolicy", func(cli *huawei.SRegion, args *RoleListOptions) error {
    45  		roles, err := cli.GetClient().GetCustomRoles()
    46  		if err != nil {
    47  			return err
    48  		}
    49  		printList(roles, 0, 0, 0, nil)
    50  		return nil
    51  	})
    52  
    53  	shellutils.R(&RoleListOptions{}, "cloud-policy-export", "Export cloudpolicy", func(cli *huawei.SRegion, args *RoleListOptions) error {
    54  		roles, err := cli.GetClient().GetRoles(args.DomainId, args.Name)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		type sRule struct {
    59  			Name        string
    60  			Id          string
    61  			ExternalId  string
    62  			CloudEnv    string
    63  			Provider    string
    64  			Description string
    65  			Document    jsonutils.JSONDict
    66  			PolicyType  string
    67  			Status      string
    68  		}
    69  		idMap := map[string]bool{}
    70  		ret := []sRule{}
    71  		for i := range roles {
    72  			if _, ok := idMap[roles[i].DisplayName]; ok {
    73  				log.Errorf("duplicate id: %s", roles[i].DisplayName)
    74  				continue
    75  			}
    76  			idMap[roles[i].DisplayName] = true
    77  			ret = append(ret, sRule{
    78  				Name:        roles[i].DisplayName,
    79  				Id:          roles[i].Id,
    80  				ExternalId:  roles[i].DisplayName,
    81  				CloudEnv:    api.CLOUD_PROVIDER_HCSO,
    82  				Provider:    api.CLOUD_PROVIDER_HCSO,
    83  				Description: roles[i].DescriptionCn,
    84  				Document:    roles[i].Policy,
    85  				PolicyType:  cloudid_api.CLOUD_POLICY_TYPE_SYSTEM,
    86  				Status:      cloudid_api.CLOUD_POLICY_STATUS_AVAILABLE,
    87  			})
    88  		}
    89  		return ioutil.WriteFile(fmt.Sprintf("%s.json", api.CLOUD_PROVIDER_HCSO), []byte(jsonutils.Marshal(ret).PrettyString()), 0644)
    90  	})
    91  }