yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/project.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 ctyun
    16  
    17  import (
    18  	"strings"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  )
    24  
    25  // GET http://ctyun-api-url/apiproxy/v3/ondemand/queryProjectIds
    26  type SProject struct {
    27  	Enabled     bool   `json:"enabled"`
    28  	Name        string `json:"name"`
    29  	Description string `json:"description"`
    30  	ID          string `json:"id"`
    31  }
    32  
    33  func (self *SProject) GetRegionID() string {
    34  	return strings.Split(self.Name, "_")[1]
    35  }
    36  
    37  func (self *SProject) GetHealthStatus() string {
    38  	if self.Enabled {
    39  		return api.CLOUD_PROVIDER_HEALTH_NORMAL
    40  	}
    41  
    42  	return api.CLOUD_PROVIDER_HEALTH_SUSPENDED
    43  }
    44  
    45  func (self *SCtyunClient) FetchProjects() ([]SProject, error) {
    46  	client, err := NewSCtyunClient(
    47  		NewSCtyunClientConfig(
    48  			self.accessKey, self.accessSecret, self.options,
    49  		).Debug(self.debug),
    50  	)
    51  	if err != nil {
    52  		return nil, errors.Wrap(err, "CtyunClient.FetchProjects")
    53  	}
    54  	projects := make([]SProject, 0)
    55  	resp, err := client.DoGet("/apiproxy/v3/ondemand/queryProjectIds", map[string]string{})
    56  	if err != nil {
    57  		return nil, errors.Wrap(err, "CtyunClient.FetchProjects.DoGet")
    58  	}
    59  
    60  	err = resp.Unmarshal(&projects, "returnObj")
    61  	if err != nil {
    62  		return nil, errors.Wrap(err, "CtyunClient.FetchProjects.Unmarshal")
    63  	}
    64  
    65  	return projects, err
    66  }
    67  
    68  func (self *SRegion) FetchProjects() ([]SProject, error) {
    69  	return self.client.FetchProjects()
    70  }