yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/enterpriceprojects.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 hcso
    16  
    17  import (
    18  	"strings"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    28  )
    29  
    30  // https://support.huaweicloud.com/api-em/zh-cn_topic_0121230880.html
    31  type SEnterpriseProject struct {
    32  	multicloud.SProjectBase
    33  	huawei.HuaweiTags
    34  
    35  	Id          string
    36  	Name        string
    37  	Description string
    38  	Status      int
    39  	CreatedAt   time.Time
    40  	UpdatedAt   time.Time
    41  }
    42  
    43  func (self *SHuaweiClient) GetEnterpriseProjects() ([]SEnterpriseProject, error) {
    44  	projects := []SEnterpriseProject{}
    45  	client, err := self.newGeneralAPIClient()
    46  	if err != nil {
    47  		return nil, errors.Wrap(err, "newGeneralAPIClient")
    48  	}
    49  	err = doListAllWithOffset(client.EnterpriseProjects.List, map[string]string{}, &projects)
    50  	if err != nil {
    51  		return nil, errors.Wrap(err, "doListAllWithOffset")
    52  	}
    53  	return projects, nil
    54  }
    55  
    56  func (ep *SEnterpriseProject) GetId() string {
    57  	return ep.Id
    58  }
    59  
    60  func (ep *SEnterpriseProject) GetGlobalId() string {
    61  	return ep.Id
    62  }
    63  
    64  func (ep *SEnterpriseProject) GetStatus() string {
    65  	if ep.Status == 1 {
    66  		return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
    67  	}
    68  	return api.EXTERNAL_PROJECT_STATUS_UNAVAILABLE
    69  }
    70  
    71  func (ep *SEnterpriseProject) GetName() string {
    72  	return ep.Name
    73  }
    74  
    75  func (self *SHuaweiClient) CreateExterpriseProject(name, desc string) (*SEnterpriseProject, error) {
    76  	client, err := self.newGeneralAPIClient()
    77  	if err != nil {
    78  		return nil, errors.Wrap(err, "newGeneralAPIClient")
    79  	}
    80  	params := map[string]string{
    81  		"name": name,
    82  	}
    83  	if len(desc) > 0 {
    84  		params["description"] = desc
    85  	}
    86  	resp, err := client.EnterpriseProjects.Create(jsonutils.Marshal(params))
    87  	if err != nil {
    88  		if strings.Contains(err.Error(), "EPS.0004") {
    89  			return nil, cloudprovider.ErrNotSupported
    90  		}
    91  		return nil, errors.Wrap(err, "EnterpriseProjects.Create")
    92  	}
    93  	project := &SEnterpriseProject{}
    94  	err = resp.Unmarshal(&project)
    95  	if err != nil {
    96  		return nil, errors.Wrap(err, "resp.Unmarshal")
    97  	}
    98  	return project, nil
    99  }
   100  
   101  func (self *SHuaweiClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
   102  	return self.CreateExterpriseProject(name, "")
   103  }
   104  
   105  func (self *SHuaweiClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   106  	projects, err := self.GetEnterpriseProjects()
   107  	if err != nil {
   108  		return nil, errors.Wrap(err, "GetProjects")
   109  	}
   110  	ret := []cloudprovider.ICloudProject{}
   111  	for i := range projects {
   112  		ret = append(ret, &projects[i])
   113  	}
   114  	return ret, nil
   115  }