Skip to content

Commit

Permalink
refactor: new schema and struct
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Apr 25, 2023
1 parent 4d12e7e commit 581c2d5
Show file tree
Hide file tree
Showing 29 changed files with 2,930 additions and 2,546 deletions.
2 changes: 1 addition & 1 deletion internal/client/vapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (v VAPP) IsVAPPOrgNetwork(networkName string) (bool, error) {
}
}

return false, fmt.Errorf("configured vApp Org network isn't found: %s", networkName)
return false, nil
}

// IsVAPPNetwork check if it is a vApp network (not vApp Org Network).
Expand Down
5 changes: 5 additions & 0 deletions internal/client/vdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (v VDC) GetID() string {
return v.Vdc.Vdc.ID
}

// GetDefaultPlacementPolicyID give you the ID of the default placement policy.
func (v VDC) GetDefaultPlacementPolicyID() string {
return v.Vdc.Vdc.DefaultComputePolicy.ID
}

// GetVAPP give you the vApp using the name provided in the argument.
func (v VDC) GetVAPP(nameOrID string, refresh bool) (*VAPP, error) {
vapp, err := v.GetVAppByNameOrId(nameOrID, refresh)
Expand Down
134 changes: 132 additions & 2 deletions internal/client/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
)

type VM struct {
name string
id string
*govcd.VM
}

Expand Down Expand Up @@ -74,7 +72,139 @@ func (v VM) SetOSType(osType string) (err error) {
return
}

// SetExposeHardwareVirtualization sets the expose hardware virtualization of a VM.
func (v VM) SetExposeHardwareVirtualization(isEnabled bool) (err error) {
task, err := v.ToggleHardwareVirtualization(isEnabled)
if err != nil {
return err
}

return task.WaitTaskCompletion()
}

// GetExposeHardwareVirtualization returns the expose hardware virtualization of a VM.
func (v VM) GetExposeHardwareVirtualization() bool {
return v.VM.VM.NestedHypervisorEnabled
}

// NetworkConnectionIsDefined checks if a network connection is defined.
func (v VM) NetworkConnectionIsDefined() bool {
return v.VM.VM.NetworkConnectionSection.NetworkConnection != nil
}

// GetNetworkConnection returns the network connection of a VM.
func (v VM) GetNetworkConnection() (networkConnections []*govcdtypes.NetworkConnection) {
if v.VM.VM.NetworkConnectionSection == nil {
return nil
}

return v.VM.VM.NetworkConnectionSection.NetworkConnection
}

// GetOSType returns the OS type of a VM.
func (v VM) GetOSType() string {
return v.VM.VM.VmSpecSection.OsType
}

// GetAffinityRuleID returns the affinity rule ID of a VM.
func (v VM) GetAffinityRuleID() string {
if v.VM.VM.ComputePolicy == nil || v.VM.VM.ComputePolicy.VmPlacementPolicy == nil {
return ""
}

return v.VM.VM.ComputePolicy.VmPlacementPolicy.ID
}

// GetDefaultAffinityRuleID returns the default affinity rule ID of a VM.
func (v VM) GetDefaultAffinityRuleID() (string, error) {
vdc, err := v.GetParentVdc()
if err != nil {
return "", err
}

return vdc.Vdc.DefaultComputePolicy.ID, nil
}

// GetAffinityRuleIDOrDefault returns the affinity rule ID of a VM or the default affinity rule ID if the VM has no affinity rule ID.
func (v VM) GetAffinityRuleIDOrDefault() (string, error) {
affinityRuleID := v.GetAffinityRuleID()
if affinityRuleID != "" {
return affinityRuleID, nil
}

return v.GetDefaultAffinityRuleID()
}

// GetStorageProfileName returns the storage profile name of a VM.
func (v VM) GetStorageProfileName() string {
if v.VM.VM.StorageProfile == nil {
return ""
}

return v.VM.VM.StorageProfile.Name
}

// IsCpusIsDefined returns true if the number of CPUs of a VM is defined.
func (v VM) CpusIsDefined() bool {
return v.VM.VM.VmSpecSection.NumCpus != nil
}

// IsCpusCoresIsDefined returns true if the number of cores per CPU of a VM is defined.
func (v VM) CpusCoresIsDefined() bool {
return v.VM.VM.VmSpecSection.NumCoresPerSocket != nil
}

// GetCpus returns the number of CPUs of a VM.
func (v VM) GetCpus() int {
if !v.CpusIsDefined() {
return 0
}

return *v.VM.VM.VmSpecSection.NumCpus
}

// GetCpusCores returns the number of cores per CPU of a VM.
func (v VM) GetCpusCores() int {
if !v.CpusCoresIsDefined() {
return 0
}

return *v.VM.VM.VmSpecSection.NumCoresPerSocket
}

// MemoryIsDefined returns true if the memory of a VM is defined.
func (v VM) MemoryIsDefined() bool {
return v.VM.VM.VmSpecSection.MemoryResourceMb != nil
}

// GetMemory returns the memory of a VM.
func (v VM) GetMemory() int64 {
if !v.MemoryIsDefined() {
return 0
}

return v.VM.VM.VmSpecSection.MemoryResourceMb.Configured
}

// HotAddIsDefined returns true if the hot add of a VM is defined.
func (v VM) HotAddIsDefined() bool {
return v.VM.VM.VMCapabilities != nil
}

// GetCpuHotAdd returns the hot add of a VM.
func (v VM) GetCpuHotAddEnabled() bool {
if !v.HotAddIsDefined() {
return false
}

return v.VM.VM.VMCapabilities.CPUHotAddEnabled
}

// GetMemoryHotAdd returns the hot add of a VM.
func (v VM) GetMemoryHotAddEnabled() bool {
if !v.HotAddIsDefined() {
return false
}

return v.VM.VM.VMCapabilities.MemoryHotAddEnabled
}
15 changes: 13 additions & 2 deletions internal/client/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/vmware/go-vcloud-director/v2/govcd"
govcdtypes "github.com/vmware/go-vcloud-director/v2/types/v56"
)

var (
Expand Down Expand Up @@ -55,8 +56,18 @@ func (c *CloudAvenue) GetTemplateWithVMName(iD, vmName string) (vAppTemplate *go
}

// getAffinityRule retrieves an affinity rule by name.
func (c *CloudAvenue) GetAffinityRule(iD string) (affinityRule *govcd.VdcComputePolicyV2, err error) {
return c.Vmware.GetVdcComputePolicyV2ById(iD)
func (c *CloudAvenue) GetAffinityRule(affinityRuleID string) (affinityRule *govcd.VdcComputePolicyV2, err error) {
return c.Vmware.GetVdcComputePolicyV2ById(affinityRuleID)
}

// GetBootImage retrieves a boot image by ID.
func (c *CloudAvenue) GetBootImage(bootImageID string) (bootImage *govcdtypes.Media, err error) {
bi, err := c.Vmware.QueryMediaById(bootImageID)
if err != nil {
return nil, fmt.Errorf("error retrieving boot image %s: %w", bootImageID, err)
}

return &govcdtypes.Media{HREF: bi.MediaRecord.HREF}, nil
}

// ! Deprecated
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/common/storageprofile/storage_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework/resource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"

superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common"
)

Expand Down Expand Up @@ -137,3 +140,21 @@ func Schema(opts ...common.AttributeOpts) schema.Attribute {
},
}
}

func SuperSchema() superschema.StringAttribute {
return superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The storage profile to use.",
Computed: true,
},
Resource: &schemaR.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
stringvalidator.OneOf(storageProfileValues...),
},
},
}
}
72 changes: 0 additions & 72 deletions internal/provider/common/vapp/vapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import (
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/mutex"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/vdc"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/vm"

govcdtypes "github.com/vmware/go-vcloud-director/v2/types/v56"
)

const (
Expand Down Expand Up @@ -151,7 +148,6 @@ func Init(_ *client.CloudAvenue, vdc vdc.VDC, vappID, vappName types.String) (va
return VAPP{VAPP: vappOut, vdc: vdc}, nil
}

<<<<<<< HEAD
/*
Create
Expand All @@ -164,35 +160,6 @@ func Create(vdc vdc.VDC, vappName, description string) (vapp VAPP, d diag.Diagno
return
}
return VAPP{VAPP: vappOut, vdc: vdc}, nil
=======
type GetVMOpts struct {
ID types.String
Name types.String
}

// vmIDOrName returns the ID or name of the VM.
func (v GetVMOpts) vmIDOrName() string {
if v.ID.IsNull() || v.ID.IsUnknown() {
return v.Name.ValueString()
}
return v.ID.ValueString()
}

// GetVM returns a VM from a vApp.
func (v VAPP) GetVM(vmInfo GetVMOpts, refresh bool) (VM, diag.Diagnostics) {
var d diag.Diagnostics

vmOut, err := v.GetVMByNameOrId(vmInfo.vmIDOrName(), refresh)
if err != nil {
if errors.Is(err, govcd.ErrorEntityNotFound) {
d.AddError("VM not found", err.Error())
return VM{}, nil
}
d.AddError("Error retrieving VM", err.Error())
return VM{}, nil
}
return VM{VM: &client.VM{VM: vmOut}, vApp: v}, nil
>>>>>>> 2821807 (refactor: meta object vm_disk resource)
}

// LockVAPP locks the parent vApp.
Expand All @@ -216,42 +183,3 @@ func (v VAPP) UnlockVAPP(ctx context.Context) (d diag.Diagnostics) {
vcdMutexKV.KvUnlock(ctx, key)
return
}

// CreateVMWithTemplate.
func (v VAPP) CreateVMWithTemplate(config vm., vappTemplate govcd.VAppTemplate) (vm vm.VM, d diag.Diagnostics) {

networkConfig, err := v.constructNetworkConnection()
if err != nil {
d.AddError("Error retrieving network config", err.Error())
return
}

vmFromTemplateParams := &govcdtypes.ReComposeVAppParams{
Ovf: govcdtypes.XMLNamespaceOVF,
Xsi: govcdtypes.XMLNamespaceXSI,
Xmlns: govcdtypes.XMLNamespaceVCloud,
AllEULAsAccepted: config.AllEULAsAccepted.ValueBool(),
Name: v.GetName(),
PowerOn: false, // VM will be powered on after all configuration is done
SourcedItem: &govcdtypes.SourcedCompositionItemParam{
Source: &govcdtypes.Reference{
HREF: vappTemplate.VAppTemplate.HREF,
Name: config.Name.ValueString(), // This VM name defines the VM name after creation
},
VMGeneralParams: &govcdtypes.VMGeneralParams{
Description: config.Description.ValueString(),
},
InstantiationParams: &govcdtypes.InstantiationParams{
// If a MAC address is specified for NIC - it does not get set with this call,
// therefore an additional `vm.UpdateNetworkConnectionSection` is required.
NetworkConnectionSection: networkConfig,
},
ComputePolicy: vmComputePolicy,
StorageProfile: storageProfilePtr,
},
}

return
}

// CreateVMWithBootImage
44 changes: 0 additions & 44 deletions internal/provider/common/vapp/vm.go

This file was deleted.

Loading

0 comments on commit 581c2d5

Please sign in to comment.