yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/cluster.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  	"sort"
    21  )
    22  
    23  type SClusterResource struct {
    24  	Maxcpu     int    `json:"maxcpu,omitempty"`
    25  	Uptime     int    `json:"uptime,omitempty"`
    26  	Template   int    `json:"template,omitempty"`
    27  	Netin      int    `json:"netin,omitempty"`
    28  	Mem        int    `json:"mem,omitempty"`
    29  	Node       string `json:"node"`
    30  	VmId       int    `json:"vmid,omitempty"`
    31  	Maxdisk    int64  `json:"maxdisk"`
    32  	Netout     int    `json:"netout,omitempty"`
    33  	Diskwrite  int    `json:"diskwrite,omitempty"`
    34  	Diskread   int    `json:"diskread,omitempty"`
    35  	Maxmem     int64  `json:"maxmem,omitempty"`
    36  	Disk       int    `json:"disk"`
    37  	CPU        int    `json:"cpu,omitempty"`
    38  	Id         string `json:"id"`
    39  	Type       string `json:"type"`
    40  	Status     string `json:"status"`
    41  	Name       string `json:"name,omitempty"`
    42  	Level      string `json:"level,omitempty"`
    43  	Storage    string `json:"storage,omitempty"`
    44  	Plugintype string `json:"plugintype,omitempty"`
    45  	Content    string `json:"content,omitempty"`
    46  	Shared     int    `json:"shared,omitempty"`
    47  }
    48  
    49  type SStorageResource struct {
    50  	Id     string
    51  	Path   string
    52  	Node   string
    53  	Name   string
    54  	Shared int
    55  }
    56  
    57  type SNodeResource struct {
    58  	Id   string
    59  	Node string
    60  }
    61  
    62  type SVmResource struct {
    63  	VmId     int
    64  	Id       string
    65  	Name     string
    66  	Node     string
    67  	NodeId   string
    68  	Status   string
    69  	Template bool
    70  }
    71  
    72  func (self *SRegion) GetClusterAllResources() ([]SClusterResource, error) {
    73  	resources := []SClusterResource{}
    74  	err := self.get("/cluster/resources", url.Values{}, &resources)
    75  	return resources, err
    76  }
    77  
    78  func (self *SRegion) GetClusterStoragesResources() (map[string]SStorageResource, error) {
    79  	resources := []SClusterResource{}
    80  	storageResources := map[string]SStorageResource{}
    81  	err := self.get("/cluster/resources", url.Values{}, &resources)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	for _, res := range resources {
    87  		if res.Type == "storage" {
    88  			sres := SStorageResource{
    89  				Id:     res.Id,
    90  				Path:   fmt.Sprintf("/nodes/%s/storage/%s", res.Node, res.Storage),
    91  				Node:   res.Node,
    92  				Name:   res.Storage,
    93  				Shared: res.Shared,
    94  			}
    95  
    96  			storageResources[sres.Name] = sres
    97  		}
    98  	}
    99  
   100  	return storageResources, nil
   101  }
   102  
   103  func (self *SRegion) GetClusterNodeResources() (map[string]SNodeResource, error) {
   104  	resources := []SClusterResource{}
   105  	nodeResources := map[string]SNodeResource{}
   106  	err := self.get("/cluster/resources", url.Values{}, &resources)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	for _, res := range resources {
   112  		if res.Type == "node" {
   113  			nres := SNodeResource{
   114  				Id:   res.Id,
   115  				Node: res.Node,
   116  			}
   117  
   118  			nodeResources[nres.Id] = nres
   119  		}
   120  	}
   121  
   122  	return nodeResources, nil
   123  }
   124  
   125  func (self *SRegion) GetClusterVmResources() (map[int]SVmResource, error) {
   126  	resources := []SClusterResource{}
   127  	VmResources := map[int]SVmResource{}
   128  	err := self.get("/cluster/resources", url.Values{}, &resources)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	for _, res := range resources {
   134  		if res.Type == "qemu" {
   135  			vres := SVmResource{
   136  				VmId:     res.VmId,
   137  				Id:       res.Id,
   138  				Name:     res.Name,
   139  				Node:     res.Node,
   140  				NodeId:   fmt.Sprintf("node/%s", res.Node),
   141  				Status:   res.Status,
   142  				Template: Itob(res.Template),
   143  			}
   144  
   145  			VmResources[vres.VmId] = vres
   146  		}
   147  	}
   148  
   149  	return VmResources, nil
   150  }
   151  
   152  func (self *SRegion) GetClusterVmMaxId() int {
   153  	resources := []SClusterResource{}
   154  	idxs := []int{}
   155  	err := self.get("/cluster/resources", url.Values{}, &resources)
   156  	if err != nil {
   157  		return -1
   158  	}
   159  
   160  	for i := range resources {
   161  		if resources[i].Type == "qemu" {
   162  			idxs = append(idxs, resources[i].VmId)
   163  		}
   164  	}
   165  
   166  	if len(idxs) < 1 {
   167  		return 0
   168  	}
   169  
   170  	sort.Ints(idxs)
   171  	return idxs[len(idxs)-1]
   172  }