yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/client/modules/mod_images.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  	"yunion.io/x/jsonutils"
    19  
    20  	"yunion.io/x/onecloud/pkg/httperrors"
    21  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/manager"
    22  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/requests"
    23  )
    24  
    25  type SImageManager struct {
    26  	SResourceManager
    27  }
    28  
    29  type imageProject struct {
    30  	projectId string
    31  }
    32  
    33  // image创建接口若非默认project,需要在header中指定X-Project-ID。url中未携带project信息(与其他接口相比有一点特殊)
    34  // 绕过了ResourceManager中的projectid。直接在发送json请求前注入X-Project-ID
    35  func (self *imageProject) Process(request requests.IRequest) {
    36  	request.AddHeaderParam("X-Project-Id", self.projectId)
    37  }
    38  
    39  func NewImageManager(cfg manager.IManagerConfig) *SImageManager {
    40  	var requestHook imageProject
    41  	if len(cfg.GetProjectId()) > 0 {
    42  		requestHook = imageProject{projectId: cfg.GetProjectId()}
    43  	}
    44  
    45  	return &SImageManager{SResourceManager: SResourceManager{
    46  		SBaseManager:  NewBaseManager2(cfg, &requestHook),
    47  		ServiceName:   ServiceNameIMS,
    48  		Region:        cfg.GetRegionId(),
    49  		ProjectId:     "",
    50  		version:       "v2",
    51  		Keyword:       "image",
    52  		KeywordPlural: "images",
    53  
    54  		ResourceKeyword: "cloudimages",
    55  	}}
    56  }
    57  
    58  //https://support.huaweicloud.com/api-ims/zh-cn_topic_0020091566.html
    59  func (self *SImageManager) Get(id string, querys map[string]string) (jsonutils.JSONObject, error) {
    60  	if querys == nil {
    61  		querys = make(map[string]string, 0)
    62  	}
    63  
    64  	querys["id"] = id
    65  	// 这里默认使用private
    66  	// if t, exists := querys["__imagetype"]; !exists || len(t) == 0 {
    67  	// 	querys["__imagetype"] = "private"
    68  	// }
    69  
    70  	ret, err := self.ListInContext(nil, querys)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	if ret.Data == nil || len(ret.Data) == 0 {
    76  		return nil, httperrors.NewNotFoundError("image %s not found", id)
    77  	}
    78  
    79  	return ret.Data[0], nil
    80  }
    81  
    82  // https://support.huaweicloud.com/api-ims/zh-cn_topic_0020092108.html
    83  // 删除image只能用这个manager
    84  func NewOpenstackImageManager(cfg manager.IManagerConfig) *SImageManager {
    85  	var requestHook imageProject
    86  	if len(cfg.GetProjectId()) > 0 {
    87  		requestHook = imageProject{projectId: cfg.GetProjectId()}
    88  	}
    89  
    90  	return &SImageManager{SResourceManager: SResourceManager{
    91  		SBaseManager:  NewBaseManager2(cfg, &requestHook),
    92  		ServiceName:   ServiceNameIMS,
    93  		Region:        cfg.GetRegionId(),
    94  		ProjectId:     "",
    95  		version:       "v2",
    96  		Keyword:       "image",
    97  		KeywordPlural: "images",
    98  
    99  		ResourceKeyword: "images",
   100  	}}
   101  }