yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/zone.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  	"net/url"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SZone struct {
    28  	multicloud.SResourceBase
    29  	ProxmoxTags
    30  
    31  	region *SRegion
    32  
    33  	Name    string
    34  	Nodes   int
    35  	Quorate int
    36  	Version int
    37  }
    38  
    39  type ClusterStatus struct {
    40  	Nodes   int    `json:"nodes,omitempty"`
    41  	Quorate int    `json:"quorate,omitempty"`
    42  	Version int    `json:"version,omitempty"`
    43  	Type    string `json:"type"`
    44  	ID      string `json:"id"`
    45  	Name    string `json:"name"`
    46  	Online  int    `json:"online,omitempty"`
    47  	Level   string `json:"level,omitempty"`
    48  	Nodeid  int    `json:"nodeid,omitempty"`
    49  	Local   int    `json:"local,omitempty"`
    50  	IP      string `json:"ip,omitempty"`
    51  }
    52  
    53  func (self *SZone) GetId() string {
    54  	return self.Name
    55  }
    56  
    57  func (self *SZone) GetGlobalId() string {
    58  	return self.Name
    59  }
    60  
    61  func (self *SZone) GetName() string {
    62  	return self.Name
    63  }
    64  
    65  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
    66  	return self.region
    67  }
    68  
    69  func (self *SZone) GetStatus() string {
    70  	return api.ZONE_ENABLE
    71  }
    72  
    73  func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
    74  	table := cloudprovider.SModelI18nTable{}
    75  	table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
    76  	return table
    77  }
    78  
    79  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
    80  	host, err := self.region.GetHost(id)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	host.zone = self
    85  
    86  	return host, nil
    87  }
    88  
    89  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
    90  	hosts, err := self.region.GetHosts()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	ret := []cloudprovider.ICloudHost{}
    95  	for i := range hosts {
    96  		hosts[i].zone = self
    97  		ret = append(ret, &hosts[i])
    98  	}
    99  	return ret, nil
   100  }
   101  
   102  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   103  	storages, err := self.region.GetStorages()
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	ret := []cloudprovider.ICloudStorage{}
   108  	for i := range storages {
   109  		storages[i].zone = self
   110  		ret = append(ret, &storages[i])
   111  	}
   112  	return ret, nil
   113  }
   114  
   115  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   116  	storage, err := self.region.GetStorage(id)
   117  	if err != nil {
   118  		return nil, errors.Wrapf(err, "GetStorage %s", id)
   119  	}
   120  
   121  	storage.zone = self
   122  	return storage, nil
   123  }
   124  
   125  func (self *SRegion) GetZone() (*SZone, error) {
   126  	ret := &SZone{region: self}
   127  	css := []ClusterStatus{}
   128  
   129  	noneClusterMsg := true
   130  	nodeNum := 0
   131  
   132  	err := self.get("/cluster/status", url.Values{}, &css)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	for _, cs := range css {
   138  		if cs.Type == "cluster" {
   139  			ret.Name = cs.Name
   140  			ret.Nodes = cs.Nodes
   141  			ret.Quorate = cs.Quorate
   142  			ret.Version = cs.Version
   143  			noneClusterMsg = false
   144  			break
   145  		} else {
   146  			nodeNum++
   147  		}
   148  
   149  	}
   150  
   151  	if noneClusterMsg == true {
   152  		ret.Name = "cluster"
   153  		ret.Nodes = nodeNum
   154  	}
   155  
   156  	return ret, nil
   157  }