yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/region.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 proxmox
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SRegion struct {
    30  	multicloud.SRegion
    31  	multicloud.SRegionEipBase
    32  	multicloud.SNoObjectStorageRegion
    33  	multicloud.SRegionLbBase
    34  
    35  	client *SProxmoxClient
    36  }
    37  
    38  func (self *SRegion) GetClient() *SProxmoxClient {
    39  	return self.client
    40  }
    41  
    42  func (self *SRegion) GetName() string {
    43  	return self.client.cpcfg.Name
    44  }
    45  
    46  func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
    47  	table := cloudprovider.SModelI18nTable{}
    48  	table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
    49  	return table
    50  }
    51  
    52  func (self *SRegion) GetId() string {
    53  	return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_PROXMOX, self.client.cpcfg.Id)
    54  }
    55  
    56  func (self *SRegion) GetGlobalId() string {
    57  	return self.GetId()
    58  }
    59  
    60  func (self *SRegion) GetProvider() string {
    61  	return CLOUD_PROVIDER_PROXMOX
    62  }
    63  
    64  func (self *SRegion) GetCloudEnv() string {
    65  	return ""
    66  }
    67  
    68  func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
    69  	return cloudprovider.SGeographicInfo{}
    70  }
    71  
    72  func (self *SRegion) GetStatus() string {
    73  	return api.CLOUD_REGION_STATUS_INSERVER
    74  }
    75  
    76  func (self *SRegion) Refresh() error {
    77  	return nil
    78  }
    79  
    80  func (self *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
    81  	return nil, cloudprovider.ErrNotSupported
    82  }
    83  
    84  func (self *SRegion) GetISecurityGroupByName(opts *cloudprovider.SecurityGroupFilterOptions) (cloudprovider.ICloudSecurityGroup, error) {
    85  	return nil, cloudprovider.ErrNotSupported
    86  }
    87  
    88  func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
    89  	return nil, cloudprovider.ErrNotSupported
    90  }
    91  
    92  func (self *SRegion) CreateIVpc(conf *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
    93  	return nil, cloudprovider.ErrNotSupported
    94  }
    95  
    96  func (self *SRegion) GetCapabilities() []string {
    97  	return self.client.GetCapabilities()
    98  }
    99  
   100  func (self *SRegion) getVpc() *SVpc {
   101  	return &SVpc{region: self}
   102  }
   103  
   104  func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
   105  	vpc := self.getVpc()
   106  	return []cloudprovider.ICloudVpc{vpc}, nil
   107  }
   108  
   109  func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
   110  	vpcs, err := self.GetIVpcs()
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	for i := range vpcs {
   115  		if vpcs[i].GetGlobalId() == id {
   116  			return vpcs[i], nil
   117  		}
   118  	}
   119  	return nil, cloudprovider.ErrNotFound
   120  }
   121  
   122  func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
   123  	zone, err := self.GetZone()
   124  	if err != nil {
   125  		return nil, errors.Wrapf(err, "GetZone()")
   126  	}
   127  	return zone, nil
   128  }
   129  
   130  func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
   131  	zone, err := self.GetZone()
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	ret := []cloudprovider.ICloudZone{}
   136  	zone.region = self
   137  	ret = append(ret, zone)
   138  	return ret, nil
   139  }
   140  
   141  func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
   142  	vm, err := self.GetInstance(id)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return vm, nil
   147  }
   148  
   149  func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   150  	host, err := self.GetHost(id)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	zone, err := self.GetZone()
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	host.zone = zone
   159  	return host, nil
   160  }
   161  
   162  func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
   163  	zone, err := self.GetZone()
   164  	if err != nil {
   165  		return nil, cloudprovider.ErrNotSupported
   166  	}
   167  	ret := []cloudprovider.ICloudStoragecache{}
   168  	cache := &SStoragecache{zone: zone}
   169  	ret = append(ret, cache)
   170  	return ret, nil
   171  }
   172  
   173  func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) {
   174  	caches, err := self.GetIStoragecaches()
   175  	if err != nil {
   176  		return nil, cloudprovider.ErrNotSupported
   177  	}
   178  	for i := range caches {
   179  		if caches[i].GetGlobalId() == id {
   180  			return caches[i], nil
   181  		}
   182  	}
   183  	return nil, cloudprovider.ErrNotFound
   184  }
   185  
   186  func (self *SRegion) get(res string, params url.Values, retVal interface{}) error {
   187  	return self.client.get(res, params, retVal)
   188  }
   189  
   190  func (self *SRegion) getAgent(res string, params url.Values, retVal interface{}) error {
   191  	return self.client.getAgent(res, params, retVal)
   192  }
   193  
   194  func (self *SRegion) post(res string, params interface{}) (jsonutils.JSONObject, error) {
   195  	return self.client.post(res, params)
   196  }
   197  
   198  func (self *SRegion) put(res string, params url.Values, body jsonutils.JSONObject, retVal interface{}) error {
   199  	return self.client.put(res, params, body, retVal)
   200  }
   201  
   202  func (self *SRegion) del(res string, params url.Values, retVal interface{}) error {
   203  	return self.client.del(res, params, retVal)
   204  }