Skip to content

Commit

Permalink
Added resource 'cloudtemple_compute_iaas_opensource_network_adapter'
Browse files Browse the repository at this point in the history
  • Loading branch information
pbesret committed Nov 19, 2024
1 parent 79dd184 commit 56c09ba
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/resources/compute_iaas_opensource_virtual_disk.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ To manage this resource you will need the following roles:
### Read-Only

- `id` (String) The ID of the virtual disk.
- `usage` (String) The usage of the virtual disk.
- `usage` (Number) The usage of the virtual disk.


39 changes: 39 additions & 0 deletions internal/client/compute_openiaas_network_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ type OpenIaaSNetworkAdapter struct {
} `terraform:"network"`
}

type CreateOpenIaasNetworkAdapterRequest struct {
VirtualMachineID string `json:"virtualMachineId"`
NetworkID string `json:"networkId"`
MAC string `json:"mac,omitempty"`
}

func (v *OpenIaaSNetworkAdapterClient) Create(ctx context.Context, req *CreateOpenIaasNetworkAdapterRequest) (string, error) {
r := v.c.newRequest("POST", "/compute/v1/open_iaas/network_adapters")
r.obj = req
return v.c.doRequestAndReturnActivity(ctx, r)
}

func (v *OpenIaaSNetworkAdapterClient) Read(ctx context.Context, id string) (*OpenIaaSNetworkAdapter, error) {
r := v.c.newRequest("GET", "/compute/v1/open_iaas/network_adapters/%s", id)
resp, err := v.c.doRequest(ctx, r)
Expand Down Expand Up @@ -65,3 +77,30 @@ func (v *OpenIaaSNetworkAdapterClient) List(ctx context.Context, virtualMachineI

return out, nil
}

type UpdateOpenIaasNetworkAdapterRequest struct {
NetworkID string `json:"networkId"`
MAC string `json:"mac,omitempty"`
Attached bool `json:"attached,omitempty"`
}

func (v *OpenIaaSNetworkAdapterClient) Update(ctx context.Context, id string, req *UpdateOpenIaasNetworkAdapterRequest) (string, error) {
r := v.c.newRequest("PATCH", "/compute/v1/open_iaas/network_adapters/%s", id)
r.obj = req
return v.c.doRequestAndReturnActivity(ctx, r)
}

func (v *OpenIaaSNetworkAdapterClient) Connect(ctx context.Context, id string) (string, error) {
r := v.c.newRequest("PATCH", "/compute/v1/open_iaas/network_adapters/%s/connect", id)
return v.c.doRequestAndReturnActivity(ctx, r)
}

func (v *OpenIaaSNetworkAdapterClient) Disconnect(ctx context.Context, id string) (string, error) {
r := v.c.newRequest("PATCH", "/compute/v1/open_iaas/network_adapters/%s/disconnect", id)
return v.c.doRequestAndReturnActivity(ctx, r)
}

func (v *OpenIaaSNetworkAdapterClient) Delete(ctx context.Context, id string) (string, error) {
r := v.c.newRequest("DELETE", "/compute/v1/open_iaas/network_adapters/%s", id)
return v.c.doRequestAndReturnActivity(ctx, r)
}
8 changes: 6 additions & 2 deletions internal/client/compute_openiaas_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ func (c *ComputeOpenIaaSClient) Pool() *OpenIaasPoolClient {
}

type OpenIaasPool struct {
ID string `terraform:"id"`
MachineManager string `terraform:"machine_manager_id"`
ID string `terraform:"id"`
MachineManager struct {
ID string `terraform:"id"`
Name string `terraform:"name"`
Type string `terraform:"type"`
} `terraform:"machine_manager"`
InternalID string `terraform:"internal_id"`
Name string `terraform:"name"`
HighAvailabilityEnabled bool `terraform:"high_availability_enabled"`
Expand Down
48 changes: 34 additions & 14 deletions internal/provider/data_source_compute_openiaas_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package provider

import (
"context"
"fmt"

"github.com/cloud-temple/terraform-provider-cloudtemple/internal/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand All @@ -13,30 +13,50 @@ func dataSourceOpenIaasPool() *schema.Resource {
return &schema.Resource{
Description: "Used to retrieve a specific pool from an Open IaaS infrastructure.",

ReadContext: readFullResource(func(ctx context.Context, c *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
ReadContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := getClient(meta)
var pool *client.OpenIaasPool
name := d.Get("name").(string)
if name != "" {
pools, err := c.Compute().OpenIaaS().Pool().List(ctx, &client.OpenIaasPoolFilter{
MachineManagerId: d.Get("machine_manager_id").(string),
})
if err != nil {
return nil, fmt.Errorf("failed to find pool named %q: %s", name, err)
diag.Errorf("failed to find pool named %q: %s", name, err)
}
for _, pool := range pools {
if pool.Name == name {
return pool, nil
for _, currPool := range pools {
if currPool.Name == name {
pool = currPool
}
}
return nil, fmt.Errorf("failed to find pool named %q", name)
diag.Errorf("failed to find pool named %q", name)
} else {
id := d.Get("id").(string)
pool, err := c.Compute().OpenIaaS().Pool().Read(ctx, id)
if err == nil && pool == nil {
diag.Errorf("failed to find pool with id %q", id)
}
}

id := d.Get("id").(string)
pool, err := c.Compute().OpenIaaS().Pool().Read(ctx, id)
if err == nil && pool == nil {
return nil, fmt.Errorf("failed to find pool with id %q", id)
}
return pool, err
}),
sw := newStateWriter(d)

d.SetId(pool.ID)

d.Set("name", pool.Name)
d.Set("machine_manager_id", pool.MachineManager.ID)
d.Set("internal_id", pool.InternalID)
d.Set("high_availability_enabled", pool.HighAvailabilityEnabled)
d.Set("cpu", []interface{}{
map[string]interface{}{
"cores": pool.Cpu.Cores,
"sockets": pool.Cpu.Sockets,
},
})
d.Set("master", pool.Master)
d.Set("hosts", pool.Hosts)

return sw.diags
},

Schema: map[string]*schema.Schema{
// In
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func New(version string) func() *schema.Provider {
// Compute - Open IaaS
"cloudtemple_compute_iaas_opensource_virtual_machine": documentResource(resourceOpenIaasVirtualMachine(), "compute_iaas_opensource_management", "compute_iaas_opensource_read", "compute_iaas_opensource_virtual_machine_power", "backup_iaas_opensource_read", "backup_iaas_opensource_write", "activity_read", "tag_read", "tag_write"),
"cloudtemple_compute_iaas_opensource_virtual_disk": documentResource(resourceOpenIaasVirtualDisk(), "compute_iaas_opensource_management", "compute_iaas_opensource_read", "activity_read"),
"cloudtemple_compute_iaas_opensource_network_adapter": documentResource(resourceOpenIaasNetworkAdapter(), "compute_iaas_opensource_management", "compute_iaas_opensource_read", "activity_read"),
},
}

Expand Down
17 changes: 9 additions & 8 deletions internal/provider/resource_compute_openiaas_virtual_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func resourceOpenIaasVirtualDisk() *schema.Resource {
return &schema.Resource{
CreateContext: openIaasVirtualDiskCreate,
ReadContext: openIaasVirtualDiskRead,
UpdateContext: openIaasVirtualDiskUpdate,
//UpdateContext: openIaasVirtualDiskUpdate,
DeleteContext: openIaasVirtualDiskDelete,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -66,7 +66,7 @@ func resourceOpenIaasVirtualDisk() *schema.Resource {
Description: "The ID of the virtual disk.",
},
"usage": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
Description: "The usage of the virtual disk.",
},
Expand Down Expand Up @@ -94,7 +94,7 @@ func openIaasVirtualDiskCreate(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf("the virtual disk could not be created: %s", err)
}

return openIaasVirtualDiskUpdate(ctx, d, meta)
return openIaasVirtualDiskRead(ctx, d, meta) // DevNote : Call update instead when it will be implemented
}

func openIaasVirtualDiskRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand All @@ -108,12 +108,13 @@ func openIaasVirtualDiskRead(ctx context.Context, d *schema.ResourceData, meta i
}

// Set the retrieved data to the schema
d.Set("name", virtualDisk.Name)
d.Set("size", virtualDisk.Size)
d.Set("usage", virtualDisk.Usage)
d.Set("storage_repository_id", virtualDisk.StorageRepository.ID)
sw := newStateWriter(d)
sw.set("name", virtualDisk.Name)
sw.set("size", virtualDisk.Size)
sw.set("usage", virtualDisk.Usage)
sw.set("storage_repository_id", virtualDisk.StorageRepository.ID)

return nil
return sw.diags
}

func openIaasVirtualDiskUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down

0 comments on commit 56c09ba

Please sign in to comment.