Skip to content

Commit

Permalink
refactor: r/d _vapp_isolated_network
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Nov 7, 2024
1 parent a71ac31 commit 9ea1bbe
Show file tree
Hide file tree
Showing 16 changed files with 658 additions and 507 deletions.
11 changes: 11 additions & 0 deletions .changelog/812.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
`resource/cloudavenue_vapp_org_network` - Fixed issue if `vapp_id` is provided the API return not found error.
```

```release-note:enhancement
`resource/cloudavenue_vapp_org_network` - Minor improvements in documentation.
```

```release-note:enhancement
`datasource/cloudavenue_vapp_org_network` - Minor improvements in documentation.
```
7 changes: 0 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ repos:
rev: v4.6.0
hooks:
- id: check-merge-conflict
- id: trailing-whitespace
exclude: >
(?x)^(
docs/|
templates/|
)$
- id: end-of-file-fixer
- id: forbid-submodules

## GOLANG
Expand Down
6 changes: 3 additions & 3 deletions docs/data-sources/vapp_isolated_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ output "dns" {
- `dns2` (String) The secondary DNS server IP address for the network.
- `dns_suffix` (String) The DNS suffix for the network.
- `gateway` (String) The gateway IP address for the network. This value define also the network IP range with the prefix length.
- `guest_vlan_allowed` (Boolean) Return True if Network allows guest VLAN.
- `id` (String) The ID of the network.
- `guest_vlan_allowed` (Boolean) True if Network allows guest VLAN.
- `id` (String) The ID of the isolated network.
- `netmask` (String) The netmask of the network.
- `retain_ip_mac_enabled` (Boolean) Return network resources such as IP/MAC of router will be retained across deployments.
- `retain_ip_mac_enabled` (Boolean) Specifies whether the network resources such as IP/MAC of router will be retained across deployments.
- `static_ip_pool` (Attributes Set) A set of static IP pools to be used for this network. (see [below for nested schema](#nestedatt--static_ip_pool))

<a id="nestedatt--static_ip_pool"></a>
Expand Down
16 changes: 9 additions & 7 deletions docs/resources/vapp_isolated_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ resource "cloudavenue_vapp_isolated_network" "example" {
guest_vlan_allowed = true
retain_ip_mac_enabled = true
static_ip_pool = [{
start_address = "192.168.10.51"
end_address = "192.168.10.101"
static_ip_pool = [
{
start_address = "192.168.10.51"
end_address = "192.168.10.101"
},
{
start_address = "192.168.10.10"
end_address = "192.168.10.30"
}]
}
]
}
```

Expand All @@ -53,17 +55,17 @@ resource "cloudavenue_vapp_isolated_network" "example" {
- `dns1` (String) The primary DNS server IP address for the network. Must be a valid IP with net.ParseIP.
- `dns2` (String) The secondary DNS server IP address for the network. Must be a valid IP with net.ParseIP.
- `dns_suffix` (String) The DNS suffix for the network.
- `guest_vlan_allowed` (Boolean) True if Network allows guest VLAN. Default to `false`.
- `guest_vlan_allowed` (Boolean) True if Network allows guest VLAN. Value defaults to `false`.
- `netmask` (String) (ForceNew) The netmask of the network. Must be a valid netmask. Value defaults to `255.255.255.0`.
- `retain_ip_mac_enabled` (Boolean) Specifies whether the network resources such as IP/MAC of router will be retained across deployments. Default to `false`.
- `retain_ip_mac_enabled` (Boolean) Specifies whether the network resources such as IP/MAC of router will be retained across deployments. Value defaults to `false`.
- `static_ip_pool` (Attributes Set) A set of static IP pools to be used for this network. Set must contain at least 1 elements. (see [below for nested schema](#nestedatt--static_ip_pool))
- `vapp_id` (String) (ForceNew) ID of the vApp. Ensure that one and only one attribute from this collection is set : `vapp_name`, `vapp_id`.
- `vapp_name` (String) (ForceNew) Name of the vApp. Ensure that one and only one attribute from this collection is set : `vapp_id`, `vapp_name`.
- `vdc` (String) (ForceNew) The name of vDC to use, optional if defined at provider level.

### Read-Only

- `id` (String) The ID of the network.
- `id` (String) The ID of the isolated network.

<a id="nestedatt--static_ip_pool"></a>
### Nested Schema for `static_ip_pool`
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/vapp_org_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ resource "cloudavenue_vapp_org_network" "example" {
Import is supported using the following syntax:
```shell
# if vdc is not specified, the default vdc will be used
terraform import cloudavenue_vm_disk.example vapp_name.network_name
terraform import cloudavenue_vapp_org_network.example vapp_name.network_name

# if vdc is specified, the vdc will be used
terraform import cloudavenue_vm_disk.example vdc.vapp_name.network_name
terraform import cloudavenue_vapp_org_network.example vdc.vapp_name.network_name
```
12 changes: 7 additions & 5 deletions examples/resources/cloudavenue_vapp_isolated_network/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ resource "cloudavenue_vapp_isolated_network" "example" {
guest_vlan_allowed = true
retain_ip_mac_enabled = true

static_ip_pool = [{
start_address = "192.168.10.51"
end_address = "192.168.10.101"
static_ip_pool = [
{
start_address = "192.168.10.51"
end_address = "192.168.10.101"
},
{
start_address = "192.168.10.10"
end_address = "192.168.10.30"
}]
}
}
]
}
4 changes: 2 additions & 2 deletions examples/resources/cloudavenue_vapp_org_network/import.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# if vdc is not specified, the default vdc will be used
terraform import cloudavenue_vm_disk.example vapp_name.network_name
terraform import cloudavenue_vapp_org_network.example vapp_name.network_name

# if vdc is specified, the vdc will be used
terraform import cloudavenue_vm_disk.example vdc.vapp_name.network_name
terraform import cloudavenue_vapp_org_network.example vdc.vapp_name.network_name
112 changes: 17 additions & 95 deletions internal/provider/vapp/isolated_network_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ import (
"context"
"fmt"

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

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/network"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/vapp"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/vdc"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/utils"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/uuid"
)

var (
Expand All @@ -34,25 +26,18 @@ func NewIsolatedNetworkDataSource() datasource.DataSource {

type isolatedNetworkDataSource struct {
client *client.CloudAvenue

org org.Org
vdc vdc.VDC
vapp vapp.VAPP
vdc vdc.VDC
vapp vapp.VAPP
}

// Init Initializes the data source.
func (d *isolatedNetworkDataSource) Init(ctx context.Context, dm *isolatedNetworkDataSourceModel) (diags diag.Diagnostics) {
d.org, diags = org.Init(d.client)
if diags.HasError() {
return
}

d.vdc, diags = vdc.Init(d.client, dm.VDC)
func (d *isolatedNetworkDataSource) Init(ctx context.Context, dm *isolatedNetworkModel) (diags diag.Diagnostics) {
d.vdc, diags = vdc.Init(d.client, dm.VDC.StringValue)
if diags.HasError() {
return
}

d.vapp, diags = vapp.Init(d.client, d.vdc, dm.VAppID, dm.VAppName)
d.vapp, diags = vapp.Init(d.client, d.vdc, dm.VAppID.StringValue, dm.VAppName.StringValue)

return
}
Expand All @@ -62,7 +47,7 @@ func (d *isolatedNetworkDataSource) Metadata(ctx context.Context, req datasource
}

func (d *isolatedNetworkDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = network.GetSchema(network.SetIsolatedVapp()).GetDataSource(ctx)
resp.Schema = isolatedNetworkSchema().GetDataSource(ctx)
}

func (d *isolatedNetworkDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand All @@ -87,96 +72,33 @@ func (d *isolatedNetworkDataSource) Configure(ctx context.Context, req datasourc

func (d *isolatedNetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_vapp_isolated_network", d.client.GetOrgName(), metrics.Read)()

var (
config = &isolatedNetworkDataSourceModel{}
diag diag.Diagnostics
vAppNetwork = govcdtypes.VAppNetworkConfiguration{}
)
config := &isolatedNetworkModel{}

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}

// Init the resource
// Init datasource
resp.Diagnostics.Append(d.Init(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}

// Get vApp Network information
vAppNetworkConfig, err := d.vapp.GetNetworkConfig()
if err != nil {
resp.Diagnostics.AddError("Error retrieving vApp network config", err.Error())
return
}

for _, networkConfig := range vAppNetworkConfig.NetworkConfig {
if networkConfig.NetworkName == config.Name.ValueString() {
vAppNetwork = networkConfig
}
s := &isolatedNetworkResource{
client: d.client,
vdc: d.vdc,
vapp: d.vapp,
}

if vAppNetwork == (govcdtypes.VAppNetworkConfiguration{}) {
resp.State.RemoveResource(ctx)
return
}

// Get UUID.
networkID, err := govcd.GetUuidFromHref(vAppNetwork.Link.HREF, false)
if err != nil {
resp.Diagnostics.AddError("Error on getting vApp network ID", err.Error())
// Read data
data, _, diags := s.read(ctx, config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

plan := &isolatedNetworkDataSourceModel{
ID: utils.StringValueOrNull(uuid.Normalize(uuid.Network, networkID).String()),
VDC: utils.StringValueOrNull(d.vdc.GetName()),
Name: utils.StringValueOrNull(vAppNetwork.NetworkName),
Description: utils.StringValueOrNull(vAppNetwork.Description),
VAppName: utils.StringValueOrNull(config.VAppName.ValueString()),
VAppID: utils.StringValueOrNull(config.VAppID.ValueString()),
Netmask: types.StringNull(),
Gateway: types.StringNull(),
DNS1: types.StringNull(),
DNS2: types.StringNull(),
DNSSuffix: types.StringNull(),
GuestVLANAllowed: types.BoolValue(*vAppNetwork.Configuration.GuestVlanAllowed),
RetainIPMacEnabled: types.BoolValue(*vAppNetwork.Configuration.RetainNetInfoAcrossDeployments),
}

// Get IP Scopes
if len(vAppNetwork.Configuration.IPScopes.IPScope) > 0 {
plan.Netmask = utils.StringValueOrNull(vAppNetwork.Configuration.IPScopes.IPScope[0].Netmask)
plan.Gateway = utils.StringValueOrNull(vAppNetwork.Configuration.IPScopes.IPScope[0].Gateway)
plan.DNS1 = utils.StringValueOrNull(vAppNetwork.Configuration.IPScopes.IPScope[0].DNS1)
plan.DNS2 = utils.StringValueOrNull(vAppNetwork.Configuration.IPScopes.IPScope[0].DNS2)
plan.DNSSuffix = utils.StringValueOrNull(vAppNetwork.Configuration.IPScopes.IPScope[0].DNSSuffix)
}

// Loop on static_ip_pool if it is not nil
staticIPRanges := make([]staticIPPoolModel, 0)
plan.StaticIPPool = types.SetNull(types.ObjectType{AttrTypes: staticIPPoolModelAttrTypes})
if vAppNetwork.Configuration.IPScopes.IPScope[0].IPRanges != nil {
for _, staticIPRange := range vAppNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange {
staticIPRanges = append(staticIPRanges, staticIPPoolModel{
StartAddress: utils.StringValueOrNull(staticIPRange.StartAddress),
EndAddress: utils.StringValueOrNull(staticIPRange.EndAddress),
})
}

plan.StaticIPPool, diag = types.SetValueFrom(ctx, types.ObjectType{AttrTypes: staticIPPoolModelAttrTypes}, staticIPRanges)
resp.Diagnostics.Append(diag...)
if resp.Diagnostics.HasError() {
return
}
}

// Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading

0 comments on commit 9ea1bbe

Please sign in to comment.