yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/client/modules/mod_projects.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 SProjectManager struct {
    28  	SResourceManager
    29  }
    30  
    31  func NewProjectManager(cfg manager.IManagerConfig) *SProjectManager {
    32  	m := &SProjectManager{SResourceManager: SResourceManager{
    33  		SBaseManager:  NewBaseManager(cfg),
    34  		ServiceName:   ServiceNameIAM,
    35  		Region:        cfg.GetRegionId(),
    36  		ProjectId:     "",
    37  		version:       "v3",
    38  		Keyword:       "project",
    39  		KeywordPlural: "projects",
    40  
    41  		ResourceKeyword: "projects",
    42  	}}
    43  	m.SetDomainId(cfg.GetDomainId())
    44  	return m
    45  }
    46  
    47  func (manager *SProjectManager) ListRoles(projectId, groupId string) (*responses.ListResult, error) {
    48  	if len(projectId) == 0 {
    49  		return nil, fmt.Errorf("missing projectId")
    50  	}
    51  	if len(groupId) == 0 {
    52  		return nil, fmt.Errorf("missing groupId")
    53  	}
    54  	res := fmt.Sprintf("%s/groups/%s/roles", projectId, groupId)
    55  	return manager.ListInContextWithSpec(nil, res, nil, "roles")
    56  }
    57  
    58  func (manager *SProjectManager) DeleteProjectRole(projectId, groupId, roleId string) error {
    59  	if len(projectId) == 0 {
    60  		return fmt.Errorf("missing projectId")
    61  	}
    62  	if len(groupId) == 0 {
    63  		return fmt.Errorf("missing groupId")
    64  	}
    65  	if len(roleId) == 0 {
    66  		return fmt.Errorf("missing roleId")
    67  	}
    68  	res := fmt.Sprintf("groups/%s/roles/%s", groupId, roleId)
    69  	_, err := manager.DeleteInContextWithSpec(nil, projectId, res, nil, nil, "")
    70  	if err != nil && errors.Cause(err) == cloudprovider.ErrNotFound {
    71  		return nil
    72  	}
    73  	return err
    74  }
    75  
    76  func (manager *SProjectManager) AddProjectRole(projectId string, groupId, roleId string) error {
    77  	if len(projectId) == 0 {
    78  		return fmt.Errorf("missing projectId")
    79  	}
    80  	if len(groupId) == 0 {
    81  		return fmt.Errorf("missing groupId")
    82  	}
    83  	if len(roleId) == 0 {
    84  		return fmt.Errorf("missing roleId")
    85  	}
    86  	res := fmt.Sprintf("groups/%s/roles/%s", groupId, roleId)
    87  	_, err := manager.UpdateInContextWithSpec(nil, projectId, res, nil, "")
    88  	return err
    89  }