Skip to content

Commit

Permalink
api reduction (#1478)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmorris3827 authored and venkytv committed May 21, 2021
1 parent a12aecb commit d18fca8
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 69 deletions.
6 changes: 3 additions & 3 deletions crm-platforms/vcd/vcd-network.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (v *VcdPlatform) AttachPortToServer(ctx context.Context, serverName, subnet
log.SpanLog(ctx, log.DebugLevelInfra, "unable to retrieve current vdc", "err", err)
return err
}
vapp, err := v.FindVApp(ctx, vappName, vcdClient)
vapp, err := v.FindVApp(ctx, vappName, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "AttachPortToServer server not found", "vapp", vappName, "for server", serverName)
return err
Expand Down Expand Up @@ -375,7 +375,7 @@ func (v *VcdPlatform) DetachPortFromServer(ctx context.Context, serverName, subn
return err
}
vappName := serverName + v.GetVappServerSuffix()
vapp, err := v.FindVApp(ctx, vappName, vcdClient)
vapp, err := v.FindVApp(ctx, vappName, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "DetachPortFromServer server not found", "vapp", vappName, "for server", serverName)
return err
Expand Down Expand Up @@ -1104,7 +1104,7 @@ func (v *VcdPlatform) RebuildIsoNamesAndFreeMaps(ctx context.Context) error {
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "Shared LB find fail", "err", err)
} else {
lbVm, err := v.FindVMByName(ctx, v.vmProperties.SharedRootLBName, vcdClient)
lbVm, err := v.FindVMByName(ctx, v.vmProperties.SharedRootLBName, vcdClient, vdc)
if err != nil {
return fmt.Errorf("Cannot find rootlb vm -- %v", err)
}
Expand Down
51 changes: 35 additions & 16 deletions crm-platforms/vcd/vcd-vapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
var VmHardwareVersion = 14

var ResolvedStateMaxWait = 4 * 60 // 4 mins
var ResolvedStateTickTime time.Duration = time.Second * 3

// Compose a new vapp from the given template, using vmgrp orch params
// Creates one or more vms.
Expand All @@ -41,7 +42,7 @@ func (v *VcdPlatform) CreateVApp(ctx context.Context, vappTmpl *govcd.VAppTempla
log.SpanLog(ctx, log.DebugLevelInfra, "CreateVapp", "name", vmgp.GroupName, "tmpl", vappTmpl.VAppTemplate.Name)

vappName := vmgp.GroupName + "-vapp"
vapp, err = v.FindVApp(ctx, vappName, vcdClient)
vapp, err = v.FindVApp(ctx, vappName, vcdClient, vdc)
if err == nil {
log.SpanLog(ctx, log.DebugLevelInfra, "CreateVApp vapp alredy exists", "name", vmgp.GroupName, "vapp", vapp)
return vapp, nil
Expand Down Expand Up @@ -84,8 +85,7 @@ func (v *VcdPlatform) CreateVApp(ctx context.Context, vappTmpl *govcd.VAppTempla
}

// wait before adding vms
err = vapp.BlockWhileStatus("UNRESOLVED", ResolvedStateMaxWait) // upto seconds

err = v.BlockWhileStatusWithTickTime(ctx, vapp, "UNRESOLVED", ResolvedStateMaxWait, ResolvedStateTickTime)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "wait for RESOLVED error", "VAppName", vmgp.GroupName, "error", err)
return nil, err
Expand Down Expand Up @@ -196,10 +196,13 @@ func (v *VcdPlatform) DeleteVapp(ctx context.Context, vapp *govcd.VApp, vcdClien
vappName := vapp.VApp.Name

log.SpanLog(ctx, log.DebugLevelInfra, "DeleteVapp", "name", vappName)

vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return fmt.Errorf("GetVdc Failed - %v", err)
}
// First, does this guy even exist?
// If not, ok, its deleted
vapp, err := v.FindVApp(ctx, vappName, vcdClient)
vapp, err = v.FindVApp(ctx, vappName, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "DeleteVapp vapp not found return success", "vapp", vappName)
return nil
Expand All @@ -209,12 +212,6 @@ func (v *VcdPlatform) DeleteVapp(ctx context.Context, vapp *govcd.VApp, vcdClien
// DetachPortFromServer has already been called, and can't delete the network
// because it's still in use, possibly by this vapp (shared clusterInst)

vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "DeleteVapp err getting vdc", "vapp", vappName, "err", err)
return err
}

// Notes on deletion order related to isolated Org VDC networks:
// - GetVappIsoNetwork must happen before VMs are deleted or the network will not be found
// - VMs are deleted next
Expand Down Expand Up @@ -337,13 +334,9 @@ func (v *VcdPlatform) DeleteVapp(ctx context.Context, vapp *govcd.VApp, vcdClien
return nil

}
func (v *VcdPlatform) FindVApp(ctx context.Context, vappName string, vcdClient *govcd.VCDClient) (*govcd.VApp, error) {
func (v *VcdPlatform) FindVApp(ctx context.Context, vappName string, vcdClient *govcd.VCDClient, vdc *govcd.Vdc) (*govcd.VApp, error) {
log.SpanLog(ctx, log.DebugLevelInfra, "FindVApp", "vappName", vappName)

vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return nil, err
}
vapp, err := vdc.GetVAppByName(vappName, true)
return vapp, err
}
Expand Down Expand Up @@ -539,3 +532,29 @@ func (v *VcdPlatform) validateVMSpecSection(ctx context.Context, vapp govcd.VApp
}
return nil
}

// BlockWhileStatusWithTickTime is the same as the govcd version vapp.BlockWhileStatus. The only difference is that it
// allows a variable tickTime instead of every 200msec so that the number of API calls can be reduced
func (v *VcdPlatform) BlockWhileStatusWithTickTime(ctx context.Context, vapp *govcd.VApp, unwantedStatus string, timeOutAfterSeconds int, tickTime time.Duration) error {
log.SpanLog(ctx, log.DebugLevelInfra, "BlockWhileStatusWithTimer", "timeOutAfterSeconds", timeOutAfterSeconds, "tickTime", tickTime)

timeoutAfter := time.After(time.Duration(timeOutAfterSeconds) * time.Second)
tick := time.NewTicker(tickTime)

for {
select {
case <-timeoutAfter:
return fmt.Errorf("timed out waiting for vApp to exit state %s after %d seconds",
unwantedStatus, timeOutAfterSeconds)
case <-tick.C:
currentStatus, err := vapp.GetStatus()

if err != nil {
return fmt.Errorf("could not get vApp status %s", err)
}
if currentStatus != unwantedStatus {
return nil
}
}
}
}
38 changes: 23 additions & 15 deletions crm-platforms/vcd/vcd-vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ func (v *VcdPlatform) FindVM(ctx context.Context, serverName, vappName string, v
}

// If all you have is the serverName (vmName)
func (v *VcdPlatform) FindVMByName(ctx context.Context, serverName string, vcdClient *govcd.VCDClient) (*govcd.VM, error) {
func (v *VcdPlatform) FindVMByName(ctx context.Context, serverName string, vcdClient *govcd.VCDClient, vdc *govcd.Vdc) (*govcd.VM, error) {
log.SpanLog(ctx, log.DebugLevelInfra, "FindVMByName", "serverName", serverName)

vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return nil, fmt.Errorf("GetVdc Failed - %v", err)
}
vm := &govcd.VM{}

vappRefList := vdc.GetVappList()
Expand Down Expand Up @@ -650,8 +646,12 @@ func (v *VcdPlatform) UpdateVMs(ctx context.Context, vmgp *vmlayer.VMGroupOrches
log.SpanLog(ctx, log.DebugLevelInfra, NoVCDClientInContext)
return fmt.Errorf(NoVCDClientInContext)
}
vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return fmt.Errorf("GetVdc Failed - %v", err)
}

vapp, err := v.FindVApp(ctx, vappName, vcdClient)
vapp, err := v.FindVApp(ctx, vappName, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "UpdateVMs GroupName not found", "Vapp", vappName, "err", err)
return err
Expand Down Expand Up @@ -802,9 +802,13 @@ func (v *VcdPlatform) DeleteVMs(ctx context.Context, vmGroupName string) error {
log.SpanLog(ctx, log.DebugLevelInfra, NoVCDClientInContext)
return fmt.Errorf(NoVCDClientInContext)
}
vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return fmt.Errorf("GetVdc Failed - %v", err)
}
vappName := vmGroupName + "-vapp"
log.SpanLog(ctx, log.DebugLevelInfra, "DeleteVMs check", "vappName", vappName)
vapp, err := v.FindVApp(ctx, vappName, vcdClient)
vapp, err := v.FindVApp(ctx, vappName, vcdClient, vdc)
if err == nil {
log.SpanLog(ctx, log.DebugLevelInfra, "DeleteVMs deleting", "VApp", vappName)
err := v.DeleteVapp(ctx, vapp, vcdClient)
Expand All @@ -830,14 +834,18 @@ func (v *VcdPlatform) GetVMStats(ctx context.Context, key *edgeproto.AppInstKey)
log.SpanLog(ctx, log.DebugLevelInfra, NoVCDClientInContext)
return nil, fmt.Errorf(NoVCDClientInContext, err)
}
vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return nil, fmt.Errorf("GetVdc Failed - %v", err)
}

vmName := cloudcommon.GetAppFQN(&key.AppKey)
if vmName == "" {
return nil, fmt.Errorf("GetAppFQN failed to return vmName for AppInst %s\n", key.AppKey.Name)
}
log.SpanLog(ctx, log.DebugLevelInfra, "GetVMStats for", "vm", vmName)

vm, err = v.FindVMByName(ctx, vmName, vcdClient)
vm, err = v.FindVMByName(ctx, vmName, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "GetVMStats vm not found", "vnname", vmName)
return nil, err
Expand Down Expand Up @@ -880,7 +888,11 @@ func (v *VcdPlatform) SetPowerState(ctx context.Context, serverName, serverActio
log.SpanLog(ctx, log.DebugLevelInfra, NoVCDClientInContext)
return fmt.Errorf(NoVCDClientInContext)
}
vm, err := v.FindVMByName(ctx, serverName, vcdClient)
vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return fmt.Errorf("GetVdc Failed - %v", err)
}
vm, err := v.FindVMByName(ctx, serverName, vcdClient, vdc)
if err != nil {
return err
}
Expand Down Expand Up @@ -948,13 +960,9 @@ func (v *VcdPlatform) VerifyVMs(ctx context.Context, vms []edgeproto.VM) error {
return nil
}

func (v *VcdPlatform) GetVMAddresses(ctx context.Context, vm *govcd.VM, vcdClient *govcd.VCDClient) ([]vmlayer.ServerIP, error) {

vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return nil, fmt.Errorf("GetVdc Failed - %v", err)
}
func (v *VcdPlatform) GetVMAddresses(ctx context.Context, vm *govcd.VM, vcdClient *govcd.VCDClient, vdc *govcd.Vdc) ([]vmlayer.ServerIP, error) {
log.SpanLog(ctx, log.DebugLevelInfra, "GetVMAddresses", "vmname", vm.VM.Name)

var serverIPs []vmlayer.ServerIP
if vm == nil || vm.VM == nil || vm.VM.NetworkConnectionSection == nil {
return serverIPs, fmt.Errorf("Nil vm received")
Expand Down
22 changes: 15 additions & 7 deletions crm-platforms/vcd/vcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mobiledgex/edge-cloud/log"
"github.com/mobiledgex/edge-cloud/vault"
ssh "github.com/mobiledgex/golang-ssh"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)

// Note regarding govcd SDK:
Expand Down Expand Up @@ -127,10 +128,15 @@ func (v *VcdPlatform) GetResourceID(ctx context.Context, resourceType vmlayer.Re
log.SpanLog(ctx, log.DebugLevelInfra, NoVCDClientInContext)
return "", fmt.Errorf(NoVCDClientInContext)
}
vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
fmt.Printf("GetVdc failed: %s\n", err.Error())
return "", err
}
// VM, Subnet and SecGrp are the current potential values of Type
// The only one we have so far is VMs, (subnets soon, and secGrps eventually)
if resourceType == vmlayer.ResourceTypeVM {
vm, err := v.FindVMByName(ctx, resourceName, vcdClient)
vm, err := v.FindVMByName(ctx, resourceName, vcdClient, vdc)
if err != nil {
return "", fmt.Errorf("resource %s not found", resourceName)
}
Expand Down Expand Up @@ -247,24 +253,26 @@ func (v *VcdPlatform) IdSanitize(name string) string {
}

func (v *VcdPlatform) GetServerDetail(ctx context.Context, serverName string) (*vmlayer.ServerDetail, error) {
log.SpanLog(ctx, log.DebugLevelInfra, "GetServerDetail", "serverName", serverName)

vcdClient := v.GetVcdClientFromContext(ctx)
if vcdClient == nil {
log.SpanLog(ctx, log.DebugLevelInfra, NoVCDClientInContext)
return nil, fmt.Errorf(NoVCDClientInContext)
}
vm, err := v.FindVMByName(ctx, serverName, vcdClient)
vdc, err := v.GetVdc(ctx, vcdClient)
if err != nil {
return nil, fmt.Errorf("GetVdcFailed - %v", err)
}
vm, err := v.FindVMByName(ctx, serverName, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "GetServerDetail not found", "vmname", serverName)
return nil, fmt.Errorf(vmlayer.ServerDoesNotExistError)
}
detail := vmlayer.ServerDetail{}
detail.Name = vm.VM.Name
detail.ID = vm.VM.ID
vmStatus, err := vm.GetStatus()
if err != nil {
return nil, err
}
vmStatus := types.VAppStatuses[vm.VM.Status]

if vmStatus == "POWERED_ON" {
detail.Status = vmlayer.ServerActive
Expand All @@ -274,7 +282,7 @@ func (v *VcdPlatform) GetServerDetail(ctx context.Context, serverName string) (*
detail.Status = vmStatus
}

addresses, err := v.GetVMAddresses(ctx, vm, vcdClient)
addresses, err := v.GetVMAddresses(ctx, vm, vcdClient, vdc)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "GetServerDetail err getting VMAddresses for", "vmname", serverName, "err", err)
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion crm-platforms/vcd/vcd_custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestProdSec(t *testing.T) {
live, ctx, err := InitVcdTestEnv()
require.Nil(t, err, "InitVcdTestEnv")
defer testVcdClient.Disconnect()
vdc, err := tv.GetVdc(ctx, testVcdClient)
if err != nil {
fmt.Printf("GetVdc failed: %s\n", err.Error())
return
}
if live {
fmt.Printf("TestProdSec:")

Expand All @@ -57,7 +62,7 @@ func TestProdSec(t *testing.T) {
// and then go to the vm's productSection to update the runtime info which you want to pass int"
// Uh huh... sure buddy, we'll see
//
vapp, err := tv.FindVApp(ctx, *vappName, testVcdClient)
vapp, err := tv.FindVApp(ctx, *vappName, testVcdClient, vdc)
if err != nil {
fmt.Printf("%s not found\n", *vappName)
return
Expand Down
7 changes: 6 additions & 1 deletion crm-platforms/vcd/vcd_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ func TestMeta(t *testing.T) {
live, ctx, err := InitVcdTestEnv()
require.Nil(t, err, "InitVcdTestEnv")
defer testVcdClient.Disconnect()
vdc, err := tv.GetVdc(ctx, testVcdClient)
if err != nil {
fmt.Printf("GetVdc failed: %s\n", err.Error())
return
}
if live {

fmt.Printf("TestMeta:")

vapp, err := tv.FindVApp(ctx, *vappName, testVcdClient)
vapp, err := tv.FindVApp(ctx, *vappName, testVcdClient, vdc)
require.Nil(t, err, "FindVApp")

vm, err := vapp.GetVMByName(*vmName, false)
Expand Down
Loading

0 comments on commit d18fca8

Please sign in to comment.