diff --git a/docs/data-sources/network_isolated.md b/docs/data-sources/network_isolated.md index a3e214bd..67f6e0cd 100644 --- a/docs/data-sources/network_isolated.md +++ b/docs/data-sources/network_isolated.md @@ -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" } ``` @@ -23,11 +23,11 @@ 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 @@ -35,9 +35,9 @@ data "cloudavenue_network_isolated" "example" { - `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)) @@ -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. - diff --git a/examples/data-sources/cloudavenue_network_isolated/data-source.tf b/examples/data-sources/cloudavenue_network_isolated/data-source.tf index dddce922..5d4a472b 100644 --- a/examples/data-sources/cloudavenue_network_isolated/data-source.tf +++ b/examples/data-sources/cloudavenue_network_isolated/data-source.tf @@ -1,3 +1,4 @@ data "cloudavenue_network_isolated" "example" { name = "net-isolated" + vdc = "VDC_Test" } \ No newline at end of file diff --git a/internal/provider/common/network/schema.go b/internal/provider/common/network/schema.go index 20af5731..4cc384c3 100644 --- a/internal/provider/common/network/schema.go +++ b/internal/provider/common/network/schema.go @@ -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" @@ -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{ @@ -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{ @@ -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{ @@ -139,6 +135,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema { stringplanmodifier.RequiresReplace(), }, }, + DataSource: &schemaD.StringAttribute{ + Computed: true, + }, }, "prefix_length": superschema.Int64Attribute{ Common: &schemaR.Int64Attribute{ @@ -153,6 +152,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema { int64planmodifier.RequiresReplace(), }, }, + DataSource: &schemaD.Int64Attribute{ + Computed: true, + }, }, "dns1": superschema.StringAttribute{ Common: &schemaR.StringAttribute{ @@ -164,6 +166,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema { fstringvalidator.IsIP(), }, }, + DataSource: &schemaD.StringAttribute{ + Computed: true, + }, }, "dns2": superschema.StringAttribute{ Common: &schemaR.StringAttribute{ @@ -175,6 +180,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema { fstringvalidator.IsIP(), }, }, + DataSource: &schemaD.StringAttribute{ + Computed: true, + }, }, "dns_suffix": superschema.StringAttribute{ Common: &schemaR.StringAttribute{ @@ -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{ @@ -205,6 +219,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema { fstringvalidator.IsIP(), }, }, + DataSource: &schemaD.StringAttribute{ + Computed: true, + }, }, "end_address": superschema.StringAttribute{ Common: &schemaR.StringAttribute{ @@ -216,6 +233,9 @@ func GetSchema(opts ...networkSchemaOpts) superschema.Schema { fstringvalidator.IsIP(), }, }, + DataSource: &schemaD.StringAttribute{ + Computed: true, + }, }, }, }, @@ -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: diff --git a/internal/provider/network/common_network.go b/internal/provider/network/common_network.go index 37b74d9b..dc5cc1d8 100644 --- a/internal/provider/network/common_network.go +++ b/internal/provider/network/common_network.go @@ -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) { diff --git a/internal/provider/network/isolated_datasource.go b/internal/provider/network/isolated_datasource.go index 8ecbc093..ddbd4662 100644 --- a/internal/provider/network/isolated_datasource.go +++ b/internal/provider/network/isolated_datasource.go @@ -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 ( @@ -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) { @@ -130,7 +56,7 @@ 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)...) @@ -138,12 +64,6 @@ func (d *networkIsolatedDataSource) Read(ctx context.Context, req datasource.Rea 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 { @@ -159,10 +79,10 @@ 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), }) @@ -170,21 +90,21 @@ func (d *networkIsolatedDataSource) Read(ctx context.Context, req datasource.Rea } // 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 @@ -192,7 +112,4 @@ func (d *networkIsolatedDataSource) Read(ctx context.Context, req datasource.Rea // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - if resp.Diagnostics.HasError() { - return - } } diff --git a/internal/provider/network/isolated_resource.go b/internal/provider/network/isolated_resource.go index e78eb9eb..09ffa676 100644 --- a/internal/provider/network/isolated_resource.go +++ b/internal/provider/network/isolated_resource.go @@ -44,19 +44,6 @@ type networkIsolatedResource struct { network network.Kind } -type networkIsolatedResourceModel 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"` -} - func (r *networkIsolatedResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { configVDC := &types.String{} req.Config.GetAttribute(ctx, path.Root("vdc"), configVDC) @@ -79,7 +66,7 @@ func (r *networkIsolatedResource) Metadata(_ context.Context, req resource.Metad } // Init resource used to initialize the resource. -func (r *networkIsolatedResource) Init(_ context.Context, rm *networkIsolatedResourceModel) (diags diag.Diagnostics) { +func (r *networkIsolatedResource) Init(_ context.Context, rm *networkIsolatedModel) (diags diag.Diagnostics) { // Set Network Type r.network.TypeOfNetwork = network.ISOLATED // Init Org @@ -120,7 +107,7 @@ func (r *networkIsolatedResource) Configure(ctx context.Context, req resource.Co // Create creates the resource and sets the initial Terraform state. func (r *networkIsolatedResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { // Retrieve values from plan - plan := &networkIsolatedResourceModel{} + plan := &networkIsolatedModel{} resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...) if resp.Diagnostics.HasError() { return @@ -172,7 +159,7 @@ func (r *networkIsolatedResource) Create(ctx context.Context, req resource.Creat // Read refreshes the Terraform state with the latest data. func (r *networkIsolatedResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { // Get current state - state := &networkIsolatedResourceModel{} + state := &networkIsolatedModel{} resp.Diagnostics.Append(req.State.Get(ctx, state)...) if resp.Diagnostics.HasError() { return @@ -215,7 +202,7 @@ func (r *networkIsolatedResource) Read(ctx context.Context, req resource.ReadReq } // Set Plan updated - plan := &networkIsolatedResourceModel{ + plan := &networkIsolatedModel{ ID: types.StringValue(orgNetwork.OpenApiOrgVdcNetwork.ID), Name: types.StringValue(orgNetwork.OpenApiOrgVdcNetwork.Name), Description: types.StringValue(orgNetwork.OpenApiOrgVdcNetwork.Description), @@ -245,9 +232,9 @@ func (r *networkIsolatedResource) Read(ctx context.Context, req resource.ReadReq // Update updates the resource and sets the updated Terraform state on success. func (r *networkIsolatedResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // Get current state - plan := &networkIsolatedResourceModel{} + plan := &networkIsolatedModel{} resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...) - state := &networkIsolatedResourceModel{} + state := &networkIsolatedModel{} resp.Diagnostics.Append(req.State.Get(ctx, state)...) if resp.Diagnostics.HasError() { return @@ -307,7 +294,7 @@ func (r *networkIsolatedResource) Update(ctx context.Context, req resource.Updat // Delete deletes the resource and removes the Terraform state on success. func (r *networkIsolatedResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { // Get current state - state := &networkIsolatedResourceModel{} + state := &networkIsolatedModel{} resp.Diagnostics.Append(req.State.Get(ctx, state)...) if resp.Diagnostics.HasError() { return @@ -393,7 +380,7 @@ func (r *networkIsolatedResource) ImportState(ctx context.Context, req resource. // Set state to fully populated data // Set Plan updated - plan := &networkIsolatedResourceModel{ + plan := &networkIsolatedModel{ ID: types.StringValue(orgNetwork.OpenApiOrgVdcNetwork.ID), Name: types.StringValue(orgNetwork.OpenApiOrgVdcNetwork.Name), Description: types.StringValue(orgNetwork.OpenApiOrgVdcNetwork.Description), @@ -422,7 +409,7 @@ func (r *networkIsolatedResource) ImportState(ctx context.Context, req resource. func (r *networkIsolatedResource) SetNetworkAPIObject(ctx context.Context, plan any) (*govcdtypes.OpenApiOrgVdcNetwork, diag.Diagnostics) { d := diag.Diagnostics{} - p, ok := plan.(*networkIsolatedResourceModel) + p, ok := plan.(*networkIsolatedModel) if !ok { d.AddError("Error", "Error converting plan to network isolated resource model") return nil, d diff --git a/internal/tests/network/isolated_datasource_test.go b/internal/tests/network/isolated_datasource_test.go index c1108667..177ceb82 100644 --- a/internal/tests/network/isolated_datasource_test.go +++ b/internal/tests/network/isolated_datasource_test.go @@ -12,6 +12,7 @@ import ( const testAccNetworkIsolatedDataSourceConfig = ` data "cloudavenue_network_isolated" "example" { name = "net-isolated" + vdc = "VDC_Test" } ` diff --git a/templates/data-sources/network_isolated.md.tmpl b/templates/data-sources/network_isolated.md.tmpl new file mode 100644 index 00000000..041dffe7 --- /dev/null +++ b/templates/data-sources/network_isolated.md.tmpl @@ -0,0 +1,25 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "Network" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# {{.Name}} ({{.Type}}) + +{{ .Description | trimspace }} + +{{ if .HasExample -}} +## Example Usage + +{{ tffile .ExampleFile }} +{{- end }} + +{{ .SchemaMarkdown | trimspace }} + +{{ if .HasImport -}} +## Import + +Import is supported using the following syntax: +{{ codefile "shell" .ImportFile }} +{{- end }} \ No newline at end of file