-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathvm-instance-status.go
74 lines (53 loc) · 2.92 KB
/
vm-instance-status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package gcp
import (
"fmt"
"strings"
"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/log"
"google.golang.org/api/compute/v1"
)
// GetVMInstanceStatus returns the status of a VM instance
func GetVMInstanceStatus(computeService *compute.Service, instanceName string, gcpProjectID string, instanceZone string) (string, error) {
var err error
// get information about the requisite VM instance
response, err := computeService.Instances.Get(gcpProjectID, instanceZone, instanceName).Do()
if err != nil {
return "", cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{vmName: %s, zone: %s}", instanceName, instanceZone), Reason: err.Error()}
}
// return the VM status
return response.Status, nil
}
// InstanceStatusCheckByName is used to check the status of all the VM instances under chaos
func InstanceStatusCheckByName(computeService *compute.Service, managedInstanceGroup string, delay, timeout int, check string, instanceNames string, gcpProjectId string, instanceZones string) error {
instanceNamesList := strings.Split(instanceNames, ",")
instanceZonesList := strings.Split(instanceZones, ",")
if managedInstanceGroup != "enable" && managedInstanceGroup != "disable" {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{vmNames: %s, zones: %s}", instanceNamesList, instanceZonesList), Reason: fmt.Sprintf("invalid value for MANAGED_INSTANCE_GROUP: %s", managedInstanceGroup)}
}
if len(instanceNamesList) == 0 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{vmNames: %v}", instanceNamesList), Reason: "no vm instance name found to stop"}
}
if len(instanceNamesList) != len(instanceZonesList) {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{vmNames: %v, zones: %v}", instanceNamesList, instanceZonesList), Reason: "unequal number of vm instance names and zones"}
}
log.Infof("[Info]: The vm instances under chaos (IUC) are: %v", instanceNamesList)
return InstanceStatusCheck(computeService, instanceNamesList, gcpProjectId, instanceZonesList)
}
// InstanceStatusCheck is used to check whether all VM instances under chaos are running or not
func InstanceStatusCheck(computeService *compute.Service, instanceNamesList []string, gcpProjectId string, instanceZonesList []string) error {
var zone string
for i := range instanceNamesList {
zone = instanceZonesList[0]
if len(instanceZonesList) > 1 {
zone = instanceZonesList[i]
}
instanceState, err := GetVMInstanceStatus(computeService, instanceNamesList[i], gcpProjectId, zone)
if err != nil {
return err
}
if instanceState != "RUNNING" {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeStatusChecks, Target: fmt.Sprintf("{vmName: %s, zone: %s}", instanceNamesList[i], zone), Reason: fmt.Sprintf("vm instance is not in RUNNING state, current state: %s", instanceState)}
}
}
return nil
}