yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/client/modules/mod_servers.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 modules
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  
    24  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/manager"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/responses"
    26  )
    27  
    28  type SServerManager struct {
    29  	SResourceManager
    30  }
    31  
    32  // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212668.html
    33  // v.1.1 新增支持创建包年/包月的弹性云服务器。!!但是不支持查询等调用 https://support.huaweicloud.com/api-ecs/zh-cn_topic_0093055772.html
    34  func NewServerManager(cfg manager.IManagerConfig) *SServerManager {
    35  	return &SServerManager{SResourceManager: SResourceManager{
    36  		SBaseManager:  NewBaseManager(cfg),
    37  		ServiceName:   ServiceNameECS,
    38  		Region:        cfg.GetRegionId(),
    39  		ProjectId:     cfg.GetProjectId(),
    40  		version:       "v1",
    41  		Keyword:       "server",
    42  		KeywordPlural: "servers",
    43  
    44  		ResourceKeyword: "cloudservers",
    45  	}}
    46  }
    47  
    48  func (self *SServerManager) List(querys map[string]string) (*responses.ListResult, error) {
    49  	if offset, exists := querys["offset"]; !exists {
    50  		// 华为云分页参数各式各样。cloudserver offset从1开始。部分其他接口从0开始。
    51  		// 另外部分接口使用pager分页 或者 maker分页
    52  		querys["offset"] = "1"
    53  	} else {
    54  		n, err := strconv.Atoi(offset)
    55  		if err != nil {
    56  			return nil, fmt.Errorf("offset is invalid: %s", offset)
    57  		}
    58  		querys["offset"] = strconv.Itoa(n + 1)
    59  	}
    60  	return self.ListInContextWithSpec(nil, "detail", querys, self.KeywordPlural)
    61  }
    62  
    63  /*
    64  返回job id 或者 order id
    65  
    66  https://support.huaweicloud.com/api-ecs/zh-cn_topic_0093055772.html
    67  创建按需的弹性云服务 ——> job_id 任务ID (返回数据uuid举例:"70a599e0-31e7-49b7-b260-868f441e862b")
    68  包年包月机器  --> order_id (返回数据举例: "CS1711152257C60TL")
    69  */
    70  func (self *SServerManager) AsyncCreate(params jsonutils.JSONObject) (string, error) {
    71  	origin_version := self.version
    72  	self.version = "v1.1"
    73  	defer func() { self.version = origin_version }()
    74  
    75  	ret, err := self.CreateInContextWithSpec(nil, "", params, "")
    76  	if err != nil {
    77  		log.Debugf("AsyncCreate %s", err)
    78  		return "", err
    79  	}
    80  
    81  	log.Debugf("AsyncCreate result %s", ret.String())
    82  	// 按需机器
    83  	jobId, err := ret.GetString("job_id")
    84  	if err == nil {
    85  		return jobId, nil
    86  	}
    87  
    88  	// 包年包月机器
    89  	return ret.GetString("order_id")
    90  }
    91  
    92  func (self *SServerManager) Create(params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
    93  	return nil, fmt.Errorf("not supported.please use AsyncCreate")
    94  }
    95  
    96  // 不推荐使用这个manager
    97  func NewNovaServerManager(cfg manager.IManagerConfig) *SServerManager {
    98  	return &SServerManager{SResourceManager: SResourceManager{
    99  		SBaseManager:  NewBaseManager(cfg),
   100  		ServiceName:   ServiceNameECS,
   101  		Region:        cfg.GetRegionId(),
   102  		ProjectId:     cfg.GetProjectId(),
   103  		version:       "v2.1",
   104  		Keyword:       "server",
   105  		KeywordPlural: "servers",
   106  
   107  		ResourceKeyword: "servers",
   108  	}}
   109  }
   110  
   111  // 重装弹性云服务器操作系统(安装Cloud-init),请用这个manager
   112  func NewServerV2Manager(cfg manager.IManagerConfig) *SServerManager {
   113  	return &SServerManager{SResourceManager: SResourceManager{
   114  		SBaseManager:  NewBaseManager(cfg),
   115  		ServiceName:   ServiceNameECS,
   116  		Region:        cfg.GetRegionId(),
   117  		ProjectId:     cfg.GetProjectId(),
   118  		version:       "v2",
   119  		Keyword:       "server",
   120  		KeywordPlural: "servers",
   121  
   122  		ResourceKeyword: "cloudservers",
   123  	}}
   124  }