Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added many filters on virtual switches and virtual networks #85

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/data-sources/compute_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ data "cloudtemple_compute_network" "name" {

### Optional

- `datacenter_id` (String)
- `folder_id` (String)
- `host_cluster_id` (String)
- `host_id` (String)
- `machine_manager_id` (String)
- `name` (String)
- `type` (String)
- `virtual_machine_id` (String)
- `virtual_switch_id` (String)

### Read-Only

- `host_names` (List of String)
- `host_number` (Number)
- `id` (String) The ID of this resource.
- `machine_manager_id` (String)
- `moref` (String)
- `virtual_machines_number` (Number)

Expand Down
4 changes: 3 additions & 1 deletion docs/data-sources/compute_virtual_switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ data "cloudtemple_compute_virtual_switch" "name" {

### Optional

- `datacenter_id` (String)
- `host_cluster_id` (String)
- `machine_manager_id` (String)
- `name` (String)

### Read-Only

- `folder_id` (String)
- `id` (String) The ID of this resource.
- `machine_manager_id` (String)
- `moref` (String)


7 changes: 5 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ provider "cloudtemple" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `client_id` (String) The client ID to login to the API with. Can also be specified with the environment variable `CLOUDTEMPLE_CLIENT_ID`.
- `secret_id` (String, Sensitive) The secret ID to login to the API with. Can also be specified with the environment variable `CLOUDTEMPLE_SECRET_ID`.

### Optional

- `address` (String) The HTTP address to connect to the API. Defaults to `shiva.cloud-temple.com`. Can also be specified with the environment variable `CLOUDTEMPLE_HTTP_ADDR`.
- `client_id` (String) The client ID to login to the API with. Can also be specified with the environment variable `CLOUDTEMPLE_CLIENT_ID`.
- `scheme` (String) The URL scheme to used to connect to the API. Default to `https`. Can also be specified with the environment variable `CLOUDTEMPLE_HTTP_SCHEME`.
- `secret_id` (String, Sensitive) The secret ID to login to the API with. Can also be specified with the environment variable `CLOUDTEMPLE_SECRET_ID`.

## Logging

Expand Down
24 changes: 14 additions & 10 deletions internal/client/compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ func (c *ComputeClient) Network() *NetworkClient {
return &NetworkClient{c.c}
}

type NetworkFilter struct {
Name string `filter:"name"`
MachineManagerId string `filter:"machineManagerId"`
DatacenterId string `filter:"datacenterId"`
VirtualMachineId string `filter:"virtualMachineId"`
Type string `filter:"type"`
VirtualSwitchId string `filter:"virtualSwitchId"`
HostId string `filter:"hostId"`
HostClusterId string `filter:"hostClusterId"`
FolderId string `filter:"folderId"`
}

type Network struct {
ID string `terraform:"id"`
Name string `terraform:"name"`
Expand All @@ -22,18 +34,10 @@ type Network struct {

func (n *NetworkClient) List(
ctx context.Context,
machineManagerId string,
datacenterId string,
virtualMachineId string,
typ string,
virtualSwitchId string,
hostId string,
hostClusterId string,
folderId string,
allOptions bool) ([]*Network, error) {
filter *NetworkFilter) ([]*Network, error) {

// TODO: filters
r := n.c.newRequest("GET", "/api/compute/v1/vcenters/networks")
r.addFilter(filter)
resp, err := n.c.doRequest(ctx, r)
if err != nil {
return nil, err
Expand Down
12 changes: 9 additions & 3 deletions internal/client/compute_virtual_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ type VirtualSwitch struct {
MachineManagerID string `terraform:"machine_manager_id"`
}

type VirtualSwitchFilter struct {
Name string `filter:"name"`
MachineManagerId string `filter:"machineManagerId"`
DatacenterId string `filter:"datacenterId"`
HostClusterId string `filter:"hostClusterId"`
}

func (v *VirtualSwitchClient) List(
ctx context.Context,
machineManagerId string,
datacenterId string,
hostClusterId string) ([]*VirtualSwitch, error) {
filter *VirtualSwitchFilter) ([]*VirtualSwitch, error) {

// TODO: filters
r := v.c.newRequest("GET", "/api/compute/v1/vcenters/virtual_switchs")
r.addFilter(filter)
resp, err := v.c.doRequest(ctx, r)
if err != nil {
return nil, err
Expand Down
89 changes: 72 additions & 17 deletions internal/provider/data_source_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"fmt"

"github.com/cloud-temple/terraform-provider-cloudtemple/internal/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -12,19 +13,37 @@ func dataSourceNetwork() *schema.Resource {
return &schema.Resource{
Description: "",

ReadContext: readFullResource(func(ctx context.Context, client *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
return getBy(
ctx,
d,
"network",
func(id string) (any, error) {
return client.Compute().Network().Read(ctx, id)
},
func(d *schema.ResourceData) (any, error) {
return client.Compute().Network().List(ctx, "", "", "", "", "", "", "", "", true)
},
[]string{"name"},
)
ReadContext: readFullResource(func(ctx context.Context, c *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
name := d.Get("name").(string)
if name != "" {
networks, err := c.Compute().Network().List(ctx, &client.NetworkFilter{
Name: name,
MachineManagerId: d.Get("machine_manager_id").(string),
DatacenterId: d.Get("datacenter_id").(string),
VirtualMachineId: d.Get("virtual_machine_id").(string),
Type: d.Get("type").(string),
VirtualSwitchId: d.Get("virtual_switch_id").(string),
HostId: d.Get("host_id").(string),
FolderId: d.Get("folder_id").(string),
HostClusterId: d.Get("host_cluster_id").(string),
})
if err != nil {
return nil, fmt.Errorf("failed to find virtual network named %q: %s", name, err)
}
for _, n := range networks {
if n.Name == name {
return n, nil
}
}
return nil, fmt.Errorf("failed to find virtual network named %q", name)
}

id := d.Get("id").(string)
network, err := c.Compute().Network().Read(ctx, id)
if err == nil && network == nil {
return nil, fmt.Errorf("failed to find virtual network with id %q", id)
}
return network, err
}),

Schema: map[string]*schema.Schema{
Expand All @@ -42,16 +61,52 @@ func dataSourceNetwork() *schema.Resource {
AtLeastOneOf: []string{"id", "name"},
ConflictsWith: []string{"id"},
},
"machine_manager_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"datacenter_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"virtual_machine_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"Network", "DistributedVirtualPortgroup"}, false),
},
"virtual_switch_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"host_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"folder_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"host_cluster_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},

// Out
"moref": {
Type: schema.TypeString,
Computed: true,
},
"machine_manager_id": {
Type: schema.TypeString,
Computed: true,
},
"virtual_machines_number": {
Type: schema.TypeInt,
Computed: true,
Expand Down
14 changes: 12 additions & 2 deletions internal/provider/data_source_compute_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ func dataSourceNetworks() *schema.Resource {
return &schema.Resource{
Description: "",

ReadContext: readFullResource(func(ctx context.Context, client *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
networks, err := client.Compute().Network().List(ctx, "", "", "", "", "", "", "", "", true)
ReadContext: readFullResource(func(ctx context.Context, c *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
networks, err := c.Compute().Network().List(ctx, &client.NetworkFilter{
Name: d.Get("name").(string),
MachineManagerId: d.Get("machine_manager_id").(string),
DatacenterId: d.Get("datacenter_id").(string),
VirtualMachineId: d.Get("virtual_machine_id").(string),
Type: d.Get("type").(string),
VirtualSwitchId: d.Get("virtual_switch_id").(string),
HostId: d.Get("host_id").(string),
FolderId: d.Get("folder_id").(string),
HostClusterId: d.Get("host_cluster_id").(string),
})
return map[string]interface{}{
"id": "networks",
"networks": networks,
Expand Down
59 changes: 42 additions & 17 deletions internal/provider/data_source_compute_virtual_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"fmt"

"github.com/cloud-temple/terraform-provider-cloudtemple/internal/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -12,19 +13,32 @@ func dataSourceVirtualSwitch() *schema.Resource {
return &schema.Resource{
Description: "",

ReadContext: readFullResource(func(ctx context.Context, client *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
return getBy(
ctx,
d,
"virtual switch",
func(id string) (any, error) {
return client.Compute().VirtualSwitch().Read(ctx, id)
},
func(d *schema.ResourceData) (any, error) {
return client.Compute().VirtualSwitch().List(ctx, "", "", "")
},
[]string{"name"},
)
ReadContext: readFullResource(func(ctx context.Context, c *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
name := d.Get("name").(string)
if name != "" {
virtualSwitches, err := c.Compute().VirtualSwitch().List(ctx, &client.VirtualSwitchFilter{
Name: name,
MachineManagerId: d.Get("machine_manager_id").(string),
DatacenterId: d.Get("datacenter_id").(string),
HostClusterId: d.Get("host_cluster_id").(string),
})
if err != nil {
return nil, fmt.Errorf("failed to find virtual switch named %q: %s", name, err)
}
for _, dvs := range virtualSwitches {
if dvs.Name == name {
return dvs, nil
}
}
return nil, fmt.Errorf("failed to find virtual switch named %q", name)
}

id := d.Get("id").(string)
virtualSwitch, err := c.Compute().VirtualSwitch().Read(ctx, id)
if err == nil && virtualSwitch == nil {
return nil, fmt.Errorf("failed to find virtual switch with id %q", id)
}
return virtualSwitch, err
}),

Schema: map[string]*schema.Schema{
Expand All @@ -42,6 +56,21 @@ func dataSourceVirtualSwitch() *schema.Resource {
AtLeastOneOf: []string{"id", "name"},
ConflictsWith: []string{"id"},
},
"machine_manager_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"datacenter_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},
"host_cluster_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},

// Out
"moref": {
Expand All @@ -52,10 +81,6 @@ func dataSourceVirtualSwitch() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"machine_manager_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
9 changes: 7 additions & 2 deletions internal/provider/data_source_compute_virtual_switchs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ func dataSourceVirtualSwitchs() *schema.Resource {
return &schema.Resource{
Description: "",

ReadContext: readFullResource(func(ctx context.Context, client *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
switches, err := client.Compute().VirtualSwitch().List(ctx, "", "", "")
ReadContext: readFullResource(func(ctx context.Context, c *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) {
switches, err := c.Compute().VirtualSwitch().List(ctx, &client.VirtualSwitchFilter{
Name: d.Get("name").(string),
MachineManagerId: d.Get("machine_manager_id").(string),
DatacenterId: d.Get("datacenter_id").(string),
HostClusterId: d.Get("host_cluster_id").(string),
})
return map[string]interface{}{
"id": "virtual_switchs",
"virtual_switchs": switches,
Expand Down