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