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