Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use v3 api to manage powerstate & display host #16

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cmd/displayers/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func (o Clusters) header() []string {
return []string{
"UUID",
"Name",
"nos_version",
"ncc_version",
"OperationMode",
"AOS Version",
"Operation Mode",
"Hypervisor",
"Hosts",
"Inefficient VMs",
"ExternalIP",
"Nodes",
"Categories",
}
}
Expand All @@ -68,10 +68,13 @@ func (o Clusters) TableData(w io.Writer) error {
continue
}
var hosts = 0
hypervisor := ""
if cluster.Status.Resources.Nodes != nil {
hypervisor = cluster.Status.Resources.Nodes.HypervisorServerList[0].Type
for _, host := range cluster.Status.Resources.Nodes.HypervisorServerList {
if host.IP != "127.0.0.1" {
hosts++

}
}

Expand All @@ -89,11 +92,11 @@ func (o Clusters) TableData(w io.Writer) error {
cluster.Metadata.UUID,
cluster.Spec.Name,
cluster.Spec.Resources.Config.SoftwareMap["NOS"].Version,
cluster.Spec.Resources.Config.SoftwareMap["NCC"].Version,
cluster.Spec.Resources.Config.OperationMode,
hypervisor,
strconv.Itoa(hosts),
*cluster.Status.Resources.Analysis.VMEfficiencyMap.InefficientVMNum,
cluster.Spec.Resources.Network.ExternalIP,
strconv.Itoa(hosts),
strings.Join(categories, ", "),
}
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/displayers/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (o VMs) header() []string {
"Subnet",
"IP",
"Cluster",
"Host",
"MiB",
"CPU",
"Disks",
Expand All @@ -78,6 +79,10 @@ func (o VMs) TableData(w io.Writer) error {
if vm.Metadata.ProjectReference != nil {
state = vm.Metadata.ProjectReference.Name
}
host := ""
if vm.Status.Resources.HostReference != nil {
host = vm.Status.Resources.HostReference.Name
}
data[i] = []string{
vm.Metadata.UUID,
vm.Spec.Name,
Expand All @@ -86,6 +91,7 @@ func (o VMs) TableData(w io.Writer) error {
subnet,
ip,
vm.Spec.ClusterReference.Name,
host,
strconv.FormatInt(vm.Spec.Resources.MemorySizeMib, 10),
fmt.Sprintf("%d/%d", vm.Spec.Resources.NumSockets, vm.Spec.Resources.NumVcpusPerSocket),
fmt.Sprintf("%d", len(vm.Spec.Resources.DiskList)),
Expand Down
4 changes: 1 addition & 3 deletions cmd/host_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cmd

import (
"context"

"github.com/simonfuhrer/nutactl/cmd/displayers"
"github.com/spf13/cobra"
"github.com/tecbiz-ch/nutanix-go-sdk/pkg/utils"
Expand Down Expand Up @@ -63,7 +61,7 @@ func runHostList(cli *CLI, cmd *cobra.Command, args []string) error {
list.Entities = append(list.Entities, item.Entities...)
}

clusters, err := cli.client.Cluster.All(context.Background())
clusters, err := cli.client.Cluster.All(cli.Context)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/vm_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,20 @@ func runVMList(cli *CLI, cmd *cobra.Command, args []string) error {
list.Entities = append(list.Entities, item.Entities...)
}

hosts, err := cli.Client().Host.All(cli.Context)
if err != nil {
return err
}

m := make(map[string]string)
for _, h := range hosts.Entities {
m[h.Metadata.UUID] = h.Spec.Name
}
for _, vm := range list.Entities {
if vm.Status.Resources.HostReference != nil {
vm.Status.Resources.HostReference.Name = m[vm.Status.Resources.HostReference.UUID]
}
}

return outputResponse(displayers.VMs{VMListIntent: list})
}
9 changes: 5 additions & 4 deletions cmd/vm_powerstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ func changeVMPowerState(cli *CLI, powerState v2.PowerState, args []string) error
if vm == nil {
return fmt.Errorf("vm not found: %s", idOrName)
}
task, err := cli.Client().VM.SetPowerState(cli.Context, powerState, vm)
if err != nil {
return err
if vm.Spec.Resources.PowerState == string(powerState) {
return fmt.Errorf("VM already powered %s", string(powerState))
}
err = cli.WaitTask(cli.Context, task.TaskUUID, 180)

vm.Spec.Resources.PowerState = string(powerState)
_, err = cli.Client().VM.Update(cli.Context, vm)
if err != nil {
return err
}
Expand Down