yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/client/modules/manager_resource.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  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  
    24  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/manager"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/requests"
    26  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/responses"
    27  )
    28  
    29  type ServiceNameType string
    30  
    31  const (
    32  	ServiceNameECS  ServiceNameType = "ecs"  // 弹性云服务
    33  	ServiceNameCCE  ServiceNameType = "cce"  // 云容器服务
    34  	ServiceNameAS   ServiceNameType = "as"   // 弹性伸缩服务
    35  	ServiceNameIAM  ServiceNameType = "iam"  // 统一身份认证服务
    36  	ServiceNameIMS  ServiceNameType = "ims"  // 镜像服务
    37  	ServiceNameCSBS ServiceNameType = "csbs" // 云服务器备份服务
    38  	ServiceNameCCI  ServiceNameType = "cci"  // 云容器实例 CCI
    39  	ServiceNameBMS  ServiceNameType = "bms"  // 裸金属服务器
    40  	ServiceNameEVS  ServiceNameType = "evs"  // 云硬盘 EVS
    41  	ServiceNameVBS  ServiceNameType = "vbs"  // 云硬盘备份 VBS
    42  	ServiceNameOBS  ServiceNameType = "obs"  // 对象存储服务 OBS
    43  	ServiceNameVPC  ServiceNameType = "vpc"  // 虚拟私有云 VPC
    44  	ServiceNameELB  ServiceNameType = "elb"  // 弹性负载均衡 ELB
    45  	ServiceNameBSS  ServiceNameType = "bss"  // 合作伙伴运营能力
    46  	ServiceNameNAT  ServiceNameType = "nat"  // Nat网关 NAT
    47  	ServiceNameDCS  ServiceNameType = "dcs"  // 分布式缓存服务
    48  	ServiceNameRDS  ServiceNameType = "rds"  // 关系型数据库 RDS
    49  	ServiceNameCTS  ServiceNameType = "cts"  // 云审计服务
    50  	ServiceNameCES  ServiceNameType = "ces"  // 监控服务 CloudEye
    51  	ServiceNameEPS  ServiceNameType = "eps"  // 企业项目
    52  
    53  	ServiceNameSFSTurbo ServiceNameType = "sfs-turbo" // 文件系统
    54  )
    55  
    56  type SManagerContext struct {
    57  	InstanceManager manager.IManager
    58  	InstanceId      string
    59  }
    60  
    61  func (self *SManagerContext) GetPath() string {
    62  	path := self.InstanceManager.KeyString()
    63  	if len(self.InstanceId) > 0 {
    64  		path += fmt.Sprintf("/%s", url.PathEscape(self.InstanceId))
    65  	}
    66  
    67  	return path
    68  }
    69  
    70  type SResourceManager struct {
    71  	SBaseManager
    72  	ctx           manager.IManagerContext
    73  	ServiceName   ServiceNameType // 服务名称: ecs
    74  	Region        string          // 区域: cn-north-1
    75  	DomainId      string
    76  	ProjectId     string // 项目ID: uuid
    77  	version       string // api 版本号
    78  	Keyword       string // 资源名称单数。构建URL时使用
    79  	KeywordPlural string // 资源名称复数形式。构建URL时使用
    80  
    81  	ResourceKeyword string // 资源名称。url中使用
    82  }
    83  
    84  func getContent(params jsonutils.JSONObject) string {
    85  	if params == nil {
    86  		return ""
    87  	}
    88  
    89  	return params.String()
    90  }
    91  
    92  func (self *SResourceManager) Version() string {
    93  	return self.version
    94  }
    95  
    96  func (self *SResourceManager) KeyString() string {
    97  	return self.ResourceKeyword
    98  }
    99  
   100  func (self *SResourceManager) ServiceType() string {
   101  	return string(self.ServiceName)
   102  }
   103  
   104  func (self *SResourceManager) GetEndpoint() string {
   105  	return self.cfg.GetEndpoints().GetEndpoint(self.cfg.GetDefaultRegion(), self.ServiceType(), self.Region)
   106  }
   107  
   108  func (self *SResourceManager) GetColumns() []string {
   109  	return []string{}
   110  }
   111  
   112  func (self *SResourceManager) SetDomainId(domainId string) {
   113  	self.DomainId = domainId
   114  }
   115  
   116  func (self *SResourceManager) getReourcePath(ctx manager.IManagerContext, rid string, spec string) string {
   117  	segs := []string{}
   118  	if ctx != nil {
   119  		segs = append(segs, ctx.GetPath())
   120  	}
   121  
   122  	segs = append(segs, self.KeyString())
   123  
   124  	if len(rid) > 0 {
   125  		segs = append(segs, url.PathEscape(rid))
   126  	}
   127  
   128  	if len(spec) > 0 {
   129  		specSegs := strings.Split(spec, "/")
   130  		for _, specSeg := range specSegs {
   131  			segs = append(segs, url.PathEscape(specSeg))
   132  		}
   133  	}
   134  
   135  	return strings.Join(segs, "/")
   136  }
   137  
   138  func (self *SResourceManager) newRequest(method, rid, spec string, ctx manager.IManagerContext) *requests.SRequest {
   139  	resourcePath := self.getReourcePath(ctx, rid, spec)
   140  	return requests.NewResourceRequest(self.GetEndpoint(), method, string(self.ServiceName), self.version, self.Region, self.ProjectId, resourcePath)
   141  }
   142  
   143  func (self *SResourceManager) List(queries map[string]string) (*responses.ListResult, error) {
   144  	return self.ListInContext(self.ctx, queries)
   145  }
   146  
   147  func (self *SResourceManager) ListInContext(ctx manager.IManagerContext, queries map[string]string) (*responses.ListResult, error) {
   148  	return self.ListInContextWithSpec(ctx, "", queries, self.KeywordPlural)
   149  }
   150  
   151  func (self *SResourceManager) ListInContextWithSpec(ctx manager.IManagerContext, spec string, queries map[string]string, responseKey string) (*responses.ListResult, error) {
   152  	request := self.newRequest("GET", "", spec, ctx)
   153  	for k, v := range queries {
   154  		request.AddQueryParam(k, v)
   155  	}
   156  	if len(self.DomainId) > 0 {
   157  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   158  	}
   159  
   160  	return self._list(request, responseKey)
   161  }
   162  
   163  func (self *SResourceManager) Get(id string, queries map[string]string) (jsonutils.JSONObject, error) {
   164  	return self.GetInContext(self.ctx, id, queries)
   165  }
   166  
   167  func (self *SResourceManager) GetInContext(ctx manager.IManagerContext, id string, queries map[string]string) (jsonutils.JSONObject, error) {
   168  	return self.GetInContextWithSpec(ctx, id, "", queries, self.Keyword)
   169  }
   170  
   171  func (self *SResourceManager) GetInContextWithSpec(ctx manager.IManagerContext, id string, spec string, queries map[string]string, responseKey string) (jsonutils.JSONObject, error) {
   172  	request := self.newRequest("GET", id, spec, ctx)
   173  	for k, v := range queries {
   174  		request.AddQueryParam(k, v)
   175  	}
   176  
   177  	if len(self.DomainId) > 0 {
   178  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   179  	}
   180  
   181  	return self._get(request, responseKey)
   182  }
   183  
   184  func (self *SResourceManager) Create(params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   185  	return self.CreateInContext(self.ctx, params)
   186  }
   187  
   188  func (self *SResourceManager) CreateInContext(ctx manager.IManagerContext, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   189  	return self.CreateInContextWithSpec(ctx, "", params, self.Keyword)
   190  }
   191  
   192  func (self *SResourceManager) CreateInContextWithSpec(ctx manager.IManagerContext, spec string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error) {
   193  	request := self.newRequest("POST", "", spec, ctx)
   194  	request.SetContent([]byte(params.String()))
   195  	if len(self.DomainId) > 0 {
   196  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   197  	}
   198  
   199  	return self._do(request, responseKey)
   200  }
   201  
   202  func (self *SResourceManager) AsyncCreate(params jsonutils.JSONObject) (string, error) {
   203  	return "", fmt.Errorf("not supported")
   204  }
   205  
   206  func (self *SResourceManager) Update(id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   207  	return self.UpdateInContext(self.ctx, id, params)
   208  }
   209  
   210  func (self *SResourceManager) UpdateInContext(ctx manager.IManagerContext, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   211  	return self.UpdateInContextWithSpec(ctx, id, "", params, self.Keyword)
   212  }
   213  
   214  func (self *SResourceManager) UpdateInContextWithSpec(ctx manager.IManagerContext, id string, spec string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error) {
   215  	request := self.newRequest("PUT", id, spec, ctx)
   216  	content := getContent(params)
   217  	if len(content) > 0 {
   218  		request.SetContent([]byte(content))
   219  	}
   220  	if len(self.DomainId) > 0 {
   221  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   222  	}
   223  
   224  	return self._do(request, responseKey)
   225  }
   226  
   227  func (self *SResourceManager) Patch(id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   228  	return self.PatchInContext(self.ctx, id, params)
   229  }
   230  
   231  func (self *SResourceManager) PatchInContext(ctx manager.IManagerContext, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   232  	return self.PatchInContextWithSpec(ctx, id, "", params, self.Keyword)
   233  }
   234  
   235  func (self *SResourceManager) PatchInContextWithSpec(ctx manager.IManagerContext, id string, spec string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error) {
   236  	request := self.newRequest("PATCH", id, spec, ctx)
   237  	content := getContent(params)
   238  	if len(content) > 0 {
   239  		request.SetContent([]byte(content))
   240  	}
   241  	if len(self.DomainId) > 0 {
   242  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   243  	}
   244  
   245  	return self._do(request, responseKey)
   246  }
   247  
   248  func (self *SResourceManager) Delete(id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   249  	return self.DeleteInContext(self.ctx, id, params)
   250  }
   251  
   252  func (self *SResourceManager) DeleteInContext(ctx manager.IManagerContext, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   253  	return self.DeleteInContextWithSpec(ctx, id, "", nil, params, "")
   254  }
   255  
   256  func (self *SResourceManager) DeleteInContextWithSpec(ctx manager.IManagerContext, id string, spec string, queries map[string]string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error) {
   257  	request := self.newRequest("DELETE", id, spec, ctx)
   258  	for k, v := range queries {
   259  		request.AddQueryParam(k, v)
   260  	}
   261  
   262  	content := getContent(params)
   263  	if len(content) > 0 {
   264  		request.SetContent([]byte(content))
   265  	}
   266  	if len(self.DomainId) > 0 {
   267  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   268  	}
   269  
   270  	return self._do(request, responseKey)
   271  }
   272  
   273  func (self *SResourceManager) PerformAction(action string, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
   274  	return self.PerformAction2(action, id, params, self.Keyword)
   275  }
   276  
   277  func (self *SResourceManager) PerformAction2(action string, id string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error) {
   278  	request := self.newRequest("POST", id, action, nil)
   279  	request.SetContent([]byte(getContent(params)))
   280  	if len(self.DomainId) > 0 {
   281  		request.AddHeaderParam("X-Domain-Id", self.DomainId)
   282  	}
   283  
   284  	return self._do(request, responseKey)
   285  }
   286  
   287  func (self *SResourceManager) SetVersion(v string) {
   288  	self.version = v
   289  }
   290  
   291  func (self *SResourceManager) versionedURL(path string) string {
   292  	return ""
   293  }
   294  
   295  // todo: Init a manager with environment variables
   296  func (self *SResourceManager) Init() error {
   297  	return nil
   298  }