yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/client/modules/mod_domains.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 modules
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/manager"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/responses"
    25  )
    26  
    27  type SDomainManager struct {
    28  	SResourceManager
    29  }
    30  
    31  func NewDomainManager(cfg manager.IManagerConfig) *SDomainManager {
    32  	m := &SDomainManager{SResourceManager: SResourceManager{
    33  		SBaseManager:  NewBaseManager(cfg),
    34  		ServiceName:   ServiceNameIAM,
    35  		Region:        cfg.GetRegionId(),
    36  		ProjectId:     "",
    37  		version:       "v3/auth",
    38  		Keyword:       "domain",
    39  		KeywordPlural: "domains",
    40  
    41  		ResourceKeyword: "domains",
    42  	}}
    43  	m.SetDomainId(cfg.GetDomainId())
    44  	return m
    45  }
    46  
    47  func (manager *SDomainManager) DeleteRole(domainId string, groupId, roleId string) error {
    48  	if len(domainId) == 0 {
    49  		return fmt.Errorf("missing domainId")
    50  	}
    51  	if len(groupId) == 0 {
    52  		return fmt.Errorf("missing groupId")
    53  	}
    54  	if len(roleId) == 0 {
    55  		return fmt.Errorf("missing roleId")
    56  	}
    57  	manager.SetVersion("v3")
    58  	defer manager.SetVersion("v3/auth")
    59  	res := fmt.Sprintf("groups/%s/roles/%s", groupId, roleId)
    60  	_, err := manager.DeleteInContextWithSpec(nil, domainId, res, nil, nil, "")
    61  	if err != nil && errors.Cause(err) == cloudprovider.ErrNotFound {
    62  		return nil
    63  	}
    64  	return err
    65  }
    66  
    67  func (manager *SDomainManager) ListRoles(domainId string, groupId string) (*responses.ListResult, error) {
    68  	if len(domainId) == 0 {
    69  		return nil, fmt.Errorf("missing domainId")
    70  	}
    71  	if len(groupId) == 0 {
    72  		return nil, fmt.Errorf("missing groupId")
    73  	}
    74  	manager.SetVersion("v3")
    75  	defer manager.SetVersion("v3/auth")
    76  	res := fmt.Sprintf("%s/groups/%s/roles", domainId, groupId)
    77  	return manager.ListInContextWithSpec(nil, res, nil, "roles")
    78  }
    79  
    80  func (manager *SDomainManager) AddRole(domainId string, groupId, roleId string) error {
    81  	if len(domainId) == 0 {
    82  		return fmt.Errorf("missing domainId")
    83  	}
    84  	if len(groupId) == 0 {
    85  		return fmt.Errorf("missing groupId")
    86  	}
    87  	if len(roleId) == 0 {
    88  		return fmt.Errorf("missing roleId")
    89  	}
    90  	manager.SetVersion("v3")
    91  	defer manager.SetVersion("v3/auth")
    92  	res := fmt.Sprintf("groups/%s/roles/%s", groupId, roleId)
    93  	_, err := manager.UpdateInContextWithSpec(nil, domainId, res, nil, "")
    94  	return err
    95  }