Skip to content

Commit

Permalink
(WIP) Implementing resource cloudtemple_compute_virtual_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pbesret committed Oct 31, 2023
1 parent b488cef commit 10c5b62
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 deletions.
69 changes: 69 additions & 0 deletions internal/client/compute_virtual_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,72 @@ func (v *VirtualControllerClient) List(

return out, nil
}

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"`
}

func (n *VirtualControllerClient) Create(ctx context.Context, req *CreateVirtualControllerRequest) (string, error) {
r := n.c.newRequest("POST", "/api/compute/v1/vcenters/virtual_controllers")
r.obj = req
return n.c.doRequestAndReturnActivity(ctx, r)
}

func (v *VirtualControllerClient) Read(ctx context.Context, id string) (*VirtualController, error) {
r := v.c.newRequest("GET", "/api/compute/v1/vcenters/virtual_controllers/%s", id)
resp, err := v.c.doRequest(ctx, r)
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
found, err := requireNotFoundOrOK(resp, 403)
if err != nil || !found {
return nil, err
}

var out VirtualController
if err := decodeBody(resp, &out); err != nil {
return nil, err
}

return &out, nil
}

type MountVirtualControllerRequest struct {
ID string `json:"id"`
IsoPath string `json:"isoPath,omitempty"`
ContentLibraryItemId string `json:"contentLibraryitemId,omitempty"`
}

func (n *VirtualControllerClient) Mount(ctx context.Context, req *MountVirtualControllerRequest) (string, error) {
r := n.c.newRequest("PATCH", "/api/compute/v1/vcenters/virtual_controllers/cdrom/mount")
r.obj = req
return n.c.doRequestAndReturnActivity(ctx, r)
}

func (n *VirtualControllerClient) Unmount(ctx context.Context, id string) (string, error) {
r := n.c.newRequest("PATCH", "/api/compute/v1/vcenters/virtual_controllers/cdrom/unmount")
r.obj = map[string]string{"id": id}
return n.c.doRequestAndReturnActivity(ctx, r)
}

func (n *VirtualControllerClient) Connect(ctx context.Context, id string) (string, error) {
r := n.c.newRequest("PATCH", "/api/compute/v1/vcenters/virtual_controllers/cdrom/connect")
r.obj = map[string]string{"id": id}
return n.c.doRequestAndReturnActivity(ctx, r)
}

func (n *VirtualControllerClient) Disconnect(ctx context.Context, id string) (string, error) {
r := n.c.newRequest("PATCH", "/api/compute/v1/vcenters/virtual_controllers/cdrom/disconnect")
r.obj = map[string]string{"id": id}
return n.c.doRequestAndReturnActivity(ctx, r)
}

func (n *VirtualControllerClient) Delete(ctx context.Context, id string) (string, error) {
r := n.c.newRequest("DELETE", "/api/compute/v1/vcenters/virtual_controllers/%s", id)
return n.c.doRequestAndReturnActivity(ctx, r)
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func New(version string) func() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"cloudtemple_backup_sla_policy_assignment": documentResource(resourceBackupSLAPolicyAssignment(), "backup_read", "backup_write", "activity_read"),
"cloudtemple_compute_network_adapter": documentResource(resourceNetworkAdapter(), "compute_write", "compute_read", "activity_read"),
"cloudtemple_compute_virtual_controller": documentResource(resourceVirtualController(), "compute_write", "compute_read", "activity_read"),
"cloudtemple_compute_virtual_disk": documentResource(resourceVirtualDisk(), "compute_write", "compute_read", "compute_management_read", "compute_management_write", "activity_read"),
"cloudtemple_compute_virtual_machine": documentResource(resourceVirtualMachine(), "compute_write", "compute_read", "activity_read", "tag_read", "tag_write"),
"cloudtemple_iam_personal_access_token": documentResource(resourcePersonalAccessToken(), "iam_offline_access"),
Expand Down
36 changes: 18 additions & 18 deletions internal/provider/resource_compute_virtual_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ func computeVirtualControllerCreate(ctx context.Context, d *schema.ResourceData,
// 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)
}
// 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 Down Expand Up @@ -175,7 +175,7 @@ func computeVirtualControllerUpdate(ctx context.Context, d *schema.ResourceData,
if d.HasChange("mounted") {
var activityId string
var err error
if d.Get("mounted") == true {
if d.Get("mounted").(bool) {
activityId, err = c.Compute().VirtualController().Mount(ctx, &client.MountVirtualControllerRequest{
ID: d.Id(),
IsoPath: d.Get("iso_path").(string),
Expand Down

0 comments on commit 10c5b62

Please sign in to comment.