yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/deal.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 qcloud
    16  
    17  import (
    18  	"yunion.io/x/log"
    19  	"yunion.io/x/pkg/errors"
    20  
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  )
    23  
    24  type SElasticcacheDeal struct {
    25  	region *SRegion
    26  
    27  	DealID      string   `json:"DealId"`
    28  	DealName    string   `json:"DealName"`
    29  	ZoneID      int64    `json:"ZoneId"`
    30  	GoodsNum    int64    `json:"GoodsNum"`
    31  	Creater     string   `json:"Creater"`
    32  	CreatTime   string   `json:"CreatTime"`
    33  	OverdueTime string   `json:"OverdueTime"`
    34  	EndTime     string   `json:"EndTime"`
    35  	Status      int64    `json:"Status"`
    36  	Description string   `json:"Description"`
    37  	Price       int64    `json:"Price"`
    38  	InstanceIDS []string `json:"InstanceIds"`
    39  }
    40  
    41  // https://cloud.tencent.com/document/product/239/30602
    42  func (self *SRegion) GetElasticcacheIdByDeal(dealId string) (string, error) {
    43  	params := map[string]string{}
    44  	params["DealIds.0"] = dealId
    45  	resp, err := self.redisRequest("DescribeInstanceDealDetail", params)
    46  	if err != nil {
    47  		return "", errors.Wrap(err, "DescribeInstanceDealDetail")
    48  	}
    49  
    50  	ret := []SElasticcacheDeal{}
    51  	err = resp.Unmarshal(&ret, "DealDetails")
    52  	if err != nil {
    53  		return "", errors.Wrap(err, "DealDetails")
    54  	}
    55  
    56  	if len(ret) == 0 {
    57  		return "", cloudprovider.ErrNotFound
    58  	} else if len(ret) > 1 {
    59  		log.Infof("%#v", ret)
    60  		return "", cloudprovider.ErrDuplicateId
    61  	} else {
    62  		if ret[0].InstanceIDS != nil && len(ret[0].InstanceIDS) == 1 {
    63  			return ret[0].InstanceIDS[0], nil
    64  		} else {
    65  			log.Infof("%#v", ret)
    66  			return "", cloudprovider.ErrNotFound
    67  		}
    68  	}
    69  }