yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/order.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  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/log"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SOrder struct {
    28  	ErrorCode *string     `json:"error_code"` // 只有失败时才返回此参数
    29  	ErrorMsg  *string     `json:"error_msg"`  //只有失败时才返回此参数
    30  	TotalSize int         `json:"totalSize"`  // 只有成功时才返回此参数
    31  	Resources []SResource `json:"resources"`
    32  }
    33  
    34  type SResource struct {
    35  	ResourceID       string `json:"resourceId"`
    36  	CloudServiceType string `json:"cloudServiceType"`
    37  	RegionCode       string `json:"regionCode"`
    38  	ResourceType     string `json:"resourceType"`
    39  	ResourceSpecCode string `json:"resourceSpecCode"`
    40  	Status           int64  `json:"status"`
    41  }
    42  
    43  type SResourceDetail struct {
    44  	ID                   string    `json:"id"`
    45  	Status               int64     `json:"status"`
    46  	ResourceID           string    `json:"resource_id"`
    47  	ResourceName         string    `json:"resource_name"`
    48  	RegionCode           string    `json:"region_code"`
    49  	CloudServiceTypeCode string    `json:"cloud_service_type_code"`
    50  	ResourceTypeCode     string    `json:"resource_type_code"`
    51  	ResourceSpecCode     string    `json:"resource_spec_code"`
    52  	ProjectCode          string    `json:"project_code"`
    53  	ProductID            string    `json:"product_id"`
    54  	MainResourceID       string    `json:"main_resource_id"`
    55  	IsMainResource       int64     `json:"is_main_resource"`
    56  	ValidTime            time.Time `json:"valid_time"`
    57  	ExpireTime           time.Time `json:"expire_time"`
    58  	NextOperationPolicy  string    `json:"next_operation_policy"`
    59  }
    60  
    61  func (self *SRegion) getDomianId() (string, error) {
    62  	domains, err := self.client.getEnabledDomains()
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  
    67  	if domains == nil || len(domains) == 0 {
    68  		return "", fmt.Errorf("GetAllResByOrderId domain is empty")
    69  	} else if len(domains) > 1 {
    70  		// not supported??
    71  		return "", fmt.Errorf("GetAllResByOrderId mutliple domain(%d) found", len(domains))
    72  	}
    73  
    74  	return domains[0].ID, nil
    75  }
    76  
    77  /*
    78  获取订单信息  https://support.huaweicloud.com/api-oce/api_order_00001.html
    79  */
    80  func (self *SRegion) GetOrder(orderId string) (SOrder, error) {
    81  	var order SOrder
    82  	domain, err := self.getDomianId()
    83  	if err != nil {
    84  		return order, err
    85  	}
    86  
    87  	err = self.ecsClient.Orders.SetDomainId(domain)
    88  	if err != nil {
    89  		return order, err
    90  	}
    91  
    92  	err = DoGet(self.ecsClient.Orders.Get, orderId, nil, &order)
    93  	return order, err
    94  }
    95  
    96  /*
    97  获取订单资源详情列表 https://support.huaweicloud.com/api-oce/zh-cn_topic_0084961226.html
    98  */
    99  func (self *SRegion) GetOrderResources(orderId string, resource_ids []string, only_main_resource bool) ([]SResourceDetail, error) {
   100  	domain, err := self.getDomianId()
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	err = self.ecsClient.Orders.SetDomainId(domain)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	resources := make([]SResourceDetail, 0)
   111  	queries := map[string]string{"customer_id": domain}
   112  	if len(orderId) > 0 {
   113  		queries["order_id"] = orderId
   114  	}
   115  
   116  	if len(resource_ids) > 0 {
   117  		queries["resource_ids"] = strings.Join(resource_ids, ",")
   118  	}
   119  
   120  	if only_main_resource {
   121  		queries["only_main_resource"] = "1"
   122  	}
   123  
   124  	err = doListAll(self.ecsClient.Orders.GetPeriodResourceList, queries, &resources)
   125  	return resources, err
   126  }
   127  
   128  /*
   129  获取资源详情 https://support.huaweicloud.com/api-oce/zh-cn_topic_0084961226.html
   130  */
   131  func (self *SRegion) GetOrderResourceDetail(resourceId string) (SResourceDetail, error) {
   132  	var res SResourceDetail
   133  	if len(resourceId) == 0 {
   134  		return res, fmt.Errorf("GetOrderResourceDetail resource id should not be empty")
   135  	}
   136  
   137  	resources, err := self.GetOrderResources("", []string{resourceId}, false)
   138  	if err != nil {
   139  		return res, err
   140  	}
   141  
   142  	switch len(resources) {
   143  	case 0:
   144  		return res, cloudprovider.ErrNotFound
   145  	case 1:
   146  		return resources[0], nil
   147  	default:
   148  		return res, fmt.Errorf("%d resources with id %s found, Expect 1", len(resources), resourceId)
   149  	}
   150  }
   151  
   152  func (self *SRegion) GetAllResByOrderId(orderId string) ([]SResource, error) {
   153  	order, err := self.GetOrder(orderId)
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	log.Debugf("GetAllResByOrderId %#v", order.Resources)
   159  	return order.Resources, nil
   160  }
   161  
   162  func (self *SRegion) getAllResByType(orderId string, resourceType string) ([]SResource, error) {
   163  	res, err := self.GetAllResByOrderId(orderId)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	ret := make([]SResource, 0)
   169  	for i := range res {
   170  		r := res[i]
   171  		if r.ResourceType == resourceType {
   172  			ret = append(ret, r)
   173  		}
   174  	}
   175  
   176  	return ret, nil
   177  }
   178  
   179  func (self *SRegion) getAllResIdsByType(orderId string, resourceType string) ([]string, error) {
   180  	res, err := self.getAllResByType(orderId, resourceType)
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	ids := make([]string, 0)
   186  	for _, r := range res {
   187  		if len(r.ResourceID) > 0 {
   188  			ids = append(ids, r.ResourceID)
   189  		}
   190  	}
   191  
   192  	return ids, nil
   193  }