Skip to content

Commit

Permalink
Fixed implementation of virtual controller management + added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pbesret committed Nov 7, 2023
1 parent 1c4b17e commit 3053c2d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
cloudtemple = {
source = "Cloud-Temple/cloudtemple"
version = "0.1.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
data "cloudtemple_compute_machine_manager" "vstack01" {
name = "vc-vstack-001-t0001"
}

data "cloudtemple_compute_virtual_datacenter" "th3s" {
name = "DC-TH3S"
machine_manager_id = data.cloudtemple_compute_machine_manager.vstack01.id
}

data "cloudtemple_compute_host_cluster" "clu001" {
name = "clu001-ucs12"
datacenter_id = data.cloudtemple_compute_virtual_datacenter.th3s.id
}

data "cloudtemple_compute_datastore_cluster" "sdrs001" {
name = "sdrs001-LIVE_"
datacenter_id = data.cloudtemple_compute_virtual_datacenter.th3s.id
}

data "cloudtemple_compute_content_library" "cl001" {
name = "local-vc-vstack-001-t0001"
}

data "cloudtemple_compute_content_library_item" "ubuntu-cloudinit" {
content_library_id = data.cloudtemple_compute_content_library.cl001.id
name = "ubuntu-22.04.1-desktop-amd64"
}

resource "cloudtemple_compute_virtual_machine" "foo" {
name = "test-terraform-example-controller"
power_state = "on"

memory = 8 * 1024 * 1024 * 1024
cpu = 4
num_cores_per_socket = 1

datacenter_id = data.cloudtemple_compute_virtual_datacenter.th3s.id
host_cluster_id = data.cloudtemple_compute_host_cluster.clu001.id
datastore_cluster_id = data.cloudtemple_compute_datastore_cluster.sdrs001.id
guest_operating_system_moref = "ubuntu64Guest"
}

resource "cloudtemple_compute_virtual_controller" "bar" {
virtual_machine_id = cloudtemple_compute_virtual_machine.foo.id
type = "CD/DVD"
content_library_item_id = data.cloudtemple_compute_content_library_item.ubuntu-cloudinit.id
connected = true
mounted = true
}

resource "cloudtemple_compute_virtual_controller" "baz" {
virtual_machine_id = cloudtemple_compute_virtual_machine.pbt-crashtest.id
type = "SCSI"
sub_type = "ParaVirtual"
}
4 changes: 2 additions & 2 deletions internal/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func closeResponseBody(resp *http.Response) error {

// requireOK is used to wrap doRequest and check for a 200
func requireOK(resp *http.Response) error {
return requireHttpCodes(resp, 200, 201)
return requireHttpCodes(resp, 200, 201, 206)
}

// requireHttpCodes checks for the "allowable" http codes for a response
Expand All @@ -339,7 +339,7 @@ func requireHttpCodes(resp *http.Response, httpCodes ...int) error {

func requireNotFoundOrOK(resp *http.Response, notFoundCode int) (bool, error) {
switch resp.StatusCode {
case 200:
case 200, 206:
return true, nil
case 404, notFoundCode:
return false, nil
Expand Down
10 changes: 4 additions & 6 deletions internal/client/compute_virtual_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ func (v *VirtualControllerClient) List(
}

type CreateVirtualControllerRequest struct {
VirtualMachineId string `json:"virtualMachineId"`
Type string `json:"type"`
SubType string `json:"subType,omitempty"`
IsoPath string `json:"isoPath,omitempty"`
ContentLibraryItemId string `json:"contentLibraryitemId,omitempty"`
VirtualMachineId string `json:"virtualMachineId"`
Type string `json:"type"`
SubType string `json:"subType,omitempty"`
}

func (n *VirtualControllerClient) Create(ctx context.Context, req *CreateVirtualControllerRequest) (string, error) {
Expand Down Expand Up @@ -83,7 +81,7 @@ func (v *VirtualControllerClient) Read(ctx context.Context, id string) (*Virtual
type MountVirtualControllerRequest struct {
ID string `json:"id"`
IsoPath string `json:"isoPath,omitempty"`
ContentLibraryItemId string `json:"contentLibraryitemId,omitempty"`
ContentLibraryItemId string `json:"contentLibraryItemId,omitempty"`
}

func (n *VirtualControllerClient) Mount(ctx context.Context, req *MountVirtualControllerRequest) (string, error) {
Expand Down
92 changes: 36 additions & 56 deletions internal/provider/resource_compute_virtual_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (

func resourceVirtualController() *schema.Resource {
return &schema.Resource{
Description: "",
Description: "Create and manage virtual controllers of a virtual machine.",

CreateContext: computeVirtualControllerCreate,
ReadContext: computeVirtualControllerRead,
UpdateContext: computeVirtualControllerUpdate,
DeleteContext: computeVirtualControllerDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -35,35 +34,42 @@ func resourceVirtualController() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"USB2", "USB3", "SCSI", "CD/DVD"}, false),
Description: "Can be one of : USB2, USB3, SCSI, CD/DVD",
},
"sub_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"BusLogic", "LSILogic", "LSILogicSAS", "ParaVirtual"}, false),
Description: "Can be one of : BusLogic, LSILogic, LSILogicSAS, ParaVirtual",
},
"iso_path": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"content_library_item_id"},
Description: "If exists, the datastore ISO path. (Conflicts with `content_library_item_id`)",
},
"content_library_item_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
ConflictsWith: []string{"iso_path"},
Description: "Content library item identifier. (Conflicts with `iso_path`)",
},
"connected": {
Type: schema.TypeBool,
Optional: true,
Type: schema.TypeBool,
Optional: true,
Description: "Only compatible with CDROM controllers",
},
"mounted": {
Type: schema.TypeBool,
Optional: true,
Type: schema.TypeBool,
Optional: true,
Description: "Only compatible with CDROM controllers",
},

//Out
"hot_add_remove": {
Type: schema.TypeString,
Type: schema.TypeBool,
Computed: true,
},
"shared_bus": {
Expand All @@ -78,6 +84,14 @@ func resourceVirtualController() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"virtual_disks": {
Type: schema.TypeList,
Computed: true,

Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand All @@ -86,11 +100,9 @@ func computeVirtualControllerCreate(ctx context.Context, d *schema.ResourceData,
c := getClient(meta)

activityId, err := c.Compute().VirtualController().Create(ctx, &client.CreateVirtualControllerRequest{
VirtualMachineId: d.Get("virtual_machine_id").(string),
Type: d.Get("type").(string),
SubType: d.Get("sub_type").(string),
IsoPath: d.Get("iso_path").(string),
ContentLibraryItemId: d.Get("content_library_item_id").(string),
VirtualMachineId: d.Get("virtual_machine_id").(string),
Type: d.Get("type").(string),
SubType: d.Get("sub_type").(string),
})
if err != nil {
return diag.Errorf("the virtual controller could not be created: %s", err)
Expand All @@ -101,38 +113,6 @@ func computeVirtualControllerCreate(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("failed to create virtual controller, %s", err)
}

// if d.Get("connected").(bool) {
// activityId, err = c.Compute().VirtualController().Connect(ctx, d.Id())
// if err != nil {
// return diag.Errorf("failed to connect virtual controller: %s", err)
// }
// _, err := c.Activity().WaitForCompletion(ctx, activityId, getWaiterOptions(ctx))
// if err != nil {
// return diag.Errorf("failed to connect virtual controller, %s", err)
// }
// }
// if err != nil {
// return diag.Errorf("the virtual controller could not be connected: %s", err)
// }

// if d.Get("mounted").(bool) {
// activityId, err = c.Compute().VirtualController().Mount(ctx, &client.MountVirtualControllerRequest{
// ID: d.Id(),
// IsoPath: d.Get("iso_path").(string),
// ContentLibraryItemId: d.Get("content_library_item_id").(string),
// })
// if err != nil {
// return diag.Errorf("failed to mount virtual controller: %s", err)
// }
// _, err := c.Activity().WaitForCompletion(ctx, activityId, getWaiterOptions(ctx))
// if err != nil {
// return diag.Errorf("failed to mount virtual controller, %s", err)
// }
// }
// if err != nil {
// return diag.Errorf("the virtual controller could not be mounted: %s", err)
// }

return computeVirtualControllerUpdate(ctx, d, meta)
}

Expand All @@ -154,13 +134,17 @@ func computeVirtualControllerRead(ctx context.Context, d *schema.ResourceData, m
func computeVirtualControllerUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
c := getClient(meta)

if d.HasChange("connected") {
if d.HasChange("mounted") {
var activityId string
var err error
if d.Get("connected").(bool) {
activityId, err = c.Compute().VirtualController().Connect(ctx, d.Id())
if d.Get("mounted").(bool) {
activityId, err = c.Compute().VirtualController().Mount(ctx, &client.MountVirtualControllerRequest{
ID: d.Id(),
IsoPath: d.Get("iso_path").(string),
ContentLibraryItemId: d.Get("content_library_item_id").(string),
})
} else {
activityId, err = c.Compute().VirtualController().Disconnect(ctx, d.Id())
activityId, err = c.Compute().VirtualController().Unmount(ctx, d.Id())
}
if err != nil {
return diag.Errorf("the virtual controller could not be connected: %s", err)
Expand All @@ -172,17 +156,13 @@ func computeVirtualControllerUpdate(ctx context.Context, d *schema.ResourceData,
}
}

if d.HasChange("mounted") {
if d.HasChange("connected") {
var activityId string
var err error
if d.Get("mounted").(bool) {
activityId, err = c.Compute().VirtualController().Mount(ctx, &client.MountVirtualControllerRequest{
ID: d.Id(),
IsoPath: d.Get("iso_path").(string),
ContentLibraryItemId: d.Get("content_library_item_id").(string),
})
if d.Get("connected").(bool) {
activityId, err = c.Compute().VirtualController().Connect(ctx, d.Id())
} else {
activityId, err = c.Compute().VirtualController().Unmount(ctx, d.Id())
activityId, err = c.Compute().VirtualController().Disconnect(ctx, d.Id())
}
if err != nil {
return diag.Errorf("the virtual controller could not be connected: %s", err)
Expand Down

0 comments on commit 3053c2d

Please sign in to comment.