Skip to content

Commit

Permalink
refactor(network): include supershema
Browse files Browse the repository at this point in the history
  • Loading branch information
David MICHENEAU committed Apr 3, 2023
1 parent 4a8e2e5 commit 3bbf555
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 139 deletions.
19 changes: 9 additions & 10 deletions docs/data-sources/network_isolated.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "cloudavenue_network_isolated Data Source - cloudavenue"
subcategory: ""
subcategory: "Network"
description: |-
The network_isolated datasource allows you to manage a ...
Provides a Cloud Avenue VDC isolated Network data source to read data or reference existing network.
---

# cloudavenue_network_isolated (Data Source)

The network_isolated datasource allows you to manage a ...
Provides a Cloud Avenue VDC isolated Network data source to read data or reference existing network.

## Example Usage

```terraform
data "cloudavenue_network_isolated" "example" {
name = "net-isolated"
vdc = "VDC_Test"
}
```

Expand All @@ -23,21 +23,21 @@ data "cloudavenue_network_isolated" "example" {

### Required

- `name` (String) A unique name for this network
- `name` (String) The name of the network. This value must be unique within the `VDC` or `VDC Group` that owns the network.

### Optional

- `vdc` (String) The name of VDC to use, optional if defined at provider level
- `vdc` (String) The name of vDC to use, optional if defined at provider level.

### Read-Only

- `description` (String) A description of the network.
- `dns1` (String) The primary DNS server IP address for the network.
- `dns2` (String) The secondary DNS server IP address for the network.
- `dns_suffix` (String) The DNS suffix for the network.
- `gateway` (String) (Force replacement) The gateway IP address for the network. This value define also the network IP range with the prefix length.
- `id` (String) The ID of this resource.
- `prefix_length` (Number) (Force replacement) The prefix length for the network. This value must be a valid prefix length for the network IP range.(e.g. 24 for netmask 255.255.255.0)
- `gateway` (String) The gateway IP address for the network. This value define also the network IP range with the prefix length.
- `id` (String) The ID of the network.
- `prefix_length` (Number) The prefix length for the network. This value must be a valid prefix length for the network IP range. (e.g. /24 for netmask 255.255.255.0).
- `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 All @@ -48,4 +48,3 @@ Read-Only:
- `end_address` (String) The end address of the IP pool. This value must be a valid IP address in the network IP range.
- `start_address` (String) The start address of the IP pool. This value must be a valid IP address in the network IP range.


Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data "cloudavenue_network_isolated" "example" {
name = "net-isolated"
vdc = "VDC_Test"
}
44 changes: 32 additions & 12 deletions internal/provider/common/network/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
Expand Down Expand Up @@ -92,12 +93,6 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {

// _schema := superschema.Schema{}
_schema := superschema.Schema{
// Common: superschema.SchemaDetails{
// MarkdownDescription: "Provides a Cloud Avenue VDC isolated Network ",
// },
// Resource: superschema.SchemaDetails{
// MarkdownDescription: "resource. This can be used to create, modify, and delete VDC isolated networks.",
// },
Attributes: map[string]superschema.Attribute{
"id": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -113,9 +108,7 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
"name": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The name of the network. This value must be unique within the `VDC` or `VDC Group` that owns the network.",
},
Resource: &schemaR.StringAttribute{
Required: true,
Required: true,
},
},
"description": superschema.StringAttribute{
Expand All @@ -125,6 +118,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
Resource: &schemaR.StringAttribute{
Optional: true,
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"gateway": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -139,6 +135,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
stringplanmodifier.RequiresReplace(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"prefix_length": superschema.Int64Attribute{
Common: &schemaR.Int64Attribute{
Expand All @@ -153,6 +152,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
int64planmodifier.RequiresReplace(),
},
},
DataSource: &schemaD.Int64Attribute{
Computed: true,
},
},
"dns1": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -164,6 +166,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
fstringvalidator.IsIP(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"dns2": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -175,6 +180,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
fstringvalidator.IsIP(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"dns_suffix": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -183,17 +191,23 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
Resource: &schemaR.StringAttribute{
Optional: true,
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"static_ip_pool": superschema.SetNestedAttribute{
Common: &schemaR.SetNestedAttribute{
Optional: true,
MarkdownDescription: "A set of static IP pools to be used for this network.",
},
Resource: &schemaR.SetNestedAttribute{
Optional: true,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
},
},
DataSource: &schemaD.SetNestedAttribute{
Computed: true,
},
Attributes: map[string]superschema.Attribute{
"start_address": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -205,6 +219,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
fstringvalidator.IsIP(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
"end_address": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
Expand All @@ -216,6 +233,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {
fstringvalidator.IsIP(),
},
},
DataSource: &schemaD.StringAttribute{
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -276,8 +296,8 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema {

case ISOLATED:
// Add isolated network specific attributes to the schema
_schema.Common.MarkdownDescription = "Provides a Cloud Avenue VDC isolated Network."
_schema.Resource.MarkdownDescription = "This can be used to create, modify, and delete VDC isolated networks."
_schema.Resource.MarkdownDescription = "Provides a Cloud Avenue VDC isolated Network. This can be used to create, modify, and delete VDC isolated networks."
_schema.DataSource.MarkdownDescription = "Provides a Cloud Avenue VDC isolated Network data source to read data or reference existing network."
_schema.Attributes["vdc"] = vdc.SuperSchema()

case ISOLATEDVAPP:
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/network/common_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ var staticIPPoolAttrTypes = map[string]attr.Type{
"end_address": types.StringType,
}

type networkIsolatedModel struct {
ID types.String `tfsdk:"id"`
VDC types.String `tfsdk:"vdc"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Gateway types.String `tfsdk:"gateway"`
PrefixLength types.Int64 `tfsdk:"prefix_length"`
DNS1 types.String `tfsdk:"dns1"`
DNS2 types.String `tfsdk:"dns2"`
DNSSuffix types.String `tfsdk:"dns_suffix"`
StaticIPPool types.Set `tfsdk:"static_ip_pool"`
}

var networkMutexKV = mutex.NewKV()

func GetParentEdgeGatewayID(org org.Org, edgeGatewayID string) (*string, diag.Diagnostic) {
Expand Down
107 changes: 12 additions & 95 deletions internal/provider/network/isolated_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"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/provider/common/network"
)

var (
Expand All @@ -26,87 +27,12 @@ type networkIsolatedDataSource struct {
client *client.CloudAvenue
}

type networkIsolatedDataSourceModel struct {
ID types.String `tfsdk:"id"`
VDC types.String `tfsdk:"vdc"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Gateway types.String `tfsdk:"gateway"`
PrefixLength types.Int64 `tfsdk:"prefix_length"`
PrimaryDNS types.String `tfsdk:"dns1"`
SecondaryDNS types.String `tfsdk:"dns2"`
SuffixDNS types.String `tfsdk:"dns_suffix"`
StaticIPPool types.Set `tfsdk:"static_ip_pool"`
}

type staticIPPoolDataSourceModel struct {
StartAddress types.String `tfsdk:"start_address"`
EndAddress types.String `tfsdk:"end_address"`
}

func (d *networkIsolatedDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + categoryName + "_isolated"
}

func (d *networkIsolatedDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "The network_isolated datasource allows you to manage a ...",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"vdc": schema.StringAttribute{
Computed: true,
Optional: true,
MarkdownDescription: "The name of VDC to use, optional if defined at provider level",
},
"name": schema.StringAttribute{
Required: true,
MarkdownDescription: "A unique name for this network",
},
"description": schema.StringAttribute{
Computed: true,
MarkdownDescription: "A description of the network.",
},
"gateway": schema.StringAttribute{
Computed: true,
MarkdownDescription: "(Force replacement) The gateway IP address for the network. This value define also the network IP range with the prefix length.",
},
"prefix_length": schema.Int64Attribute{
Computed: true,
MarkdownDescription: "(Force replacement) The prefix length for the network. This value must be a valid prefix length for the network IP range.(e.g. 24 for netmask 255.255.255.0)",
},
"dns1": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The primary DNS server IP address for the network.",
},
"dns2": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The secondary DNS server IP address for the network.",
},
"dns_suffix": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The DNS suffix for the network.",
},
"static_ip_pool": schema.SetNestedAttribute{
Computed: true,
MarkdownDescription: "A set of static IP pools to be used for this network.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"start_address": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The start address of the IP pool. This value must be a valid IP address in the network IP range.",
},
"end_address": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The end address of the IP pool. This value must be a valid IP address in the network IP range.",
},
},
},
},
},
}
resp.Schema = network.GetSchema(network.SetIsolated()).GetDataSource(ctx)
}

func (d *networkIsolatedDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand All @@ -130,20 +56,14 @@ func (d *networkIsolatedDataSource) Configure(ctx context.Context, req datasourc
}

func (d *networkIsolatedDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data networkIsolatedDataSourceModel
var data networkIsolatedModel

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

// Get the organization
// if _, err := d.client.GetOrg(); err != nil {
// resp.Diagnostics.AddError("Unable to get organization", err.Error())
// return
//}

// Define VDC or VDCGroup
vdcOrVDCGroup, err := d.client.GetVDCOrVDCGroup(data.VDC.ValueString())
if err != nil {
Expand All @@ -159,40 +79,37 @@ func (d *networkIsolatedDataSource) Read(ctx context.Context, req datasource.Rea
}

// Get network static IP pools
ipPools := []staticIPPoolDataSourceModel{}
ipPools := []staticIPPool{}
if len(network.OpenApiOrgVdcNetwork.Subnets.Values[0].IPRanges.Values) > 0 {
for _, ipRange := range network.OpenApiOrgVdcNetwork.Subnets.Values[0].IPRanges.Values {
ipPools = append(ipPools, staticIPPoolDataSourceModel{
ipPools = append(ipPools, staticIPPool{
StartAddress: types.StringValue(ipRange.StartAddress),
EndAddress: types.StringValue(ipRange.EndAddress),
})
}
}

// Set Plan updated
data = networkIsolatedDataSourceModel{
data = networkIsolatedModel{
ID: types.StringValue(network.OpenApiOrgVdcNetwork.ID),
Name: types.StringValue(network.OpenApiOrgVdcNetwork.Name),
Description: types.StringValue(network.OpenApiOrgVdcNetwork.Description),
VDC: types.StringValue(vdcOrVDCGroup.GetName()),
Gateway: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].Gateway),
PrefixLength: types.Int64Value(int64(network.OpenApiOrgVdcNetwork.Subnets.Values[0].PrefixLength)),
PrimaryDNS: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].DNSServer1),
SecondaryDNS: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].DNSServer2),
SuffixDNS: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].DNSSuffix),
DNS1: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].DNSServer1),
DNS2: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].DNSServer2),
DNSSuffix: types.StringValue(network.OpenApiOrgVdcNetwork.Subnets.Values[0].DNSSuffix),
}

// Set static IP pools
var diags diag.Diagnostics
data.StaticIPPool, diags = types.SetValueFrom(ctx, types.ObjectType{AttrTypes: staticIPPoolResourceModelAttrTypes}, ipPools)
data.StaticIPPool, diags = types.SetValueFrom(ctx, types.ObjectType{AttrTypes: staticIPPoolAttrTypes}, ipPools)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

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

0 comments on commit 3bbf555

Please sign in to comment.