Skip to content

Commit

Permalink
chore(edgegw): Add schema/types (#404)
Browse files Browse the repository at this point in the history
Co-authored-by: DUBREUIL Valentin <valentin.dubreuil@orange.com>
  • Loading branch information
valentin-dubreuil and DUBREUIL Valentin authored Jul 24, 2023
1 parent 427f579 commit 0522a0a
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 207 deletions.
112 changes: 1 addition & 111 deletions internal/provider/edgegw/app_port_profile_resource.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package network provides a Terraform resource.
package edgegw

import (
Expand All @@ -12,20 +11,10 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"

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

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"

superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
Expand Down Expand Up @@ -55,21 +44,6 @@ type portProfilesResource struct {
org org.Org
}

type portProfilesResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
VDC types.String `tfsdk:"vdc"`
Description types.String `tfsdk:"description"`
AppPorts types.List `tfsdk:"app_ports"`
}

type portProfilesResourceModelAppPorts []portProfilesResourceModelAppPort

type portProfilesResourceModelAppPort struct {
Protocol types.String `tfsdk:"protocol"`
Ports types.Set `tfsdk:"ports"`
}

func (rm *portProfilesResourceModel) AppPortsFromPlan(ctx context.Context) (appPorts portProfilesResourceModelAppPorts, diags diag.Diagnostics) {
appPorts = make([]portProfilesResourceModelAppPort, 0)
diags.Append(rm.AppPorts.ElementsAs(ctx, &appPorts, false)...)
Expand Down Expand Up @@ -159,7 +133,7 @@ func (r *portProfilesResource) Metadata(_ context.Context, req resource.Metadata

// Schema defines the schema for the resource.
func (r *portProfilesResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = r.superSchema(ctx).GetResource(ctx)
resp.Schema = portProfilesSchema(ctx).GetResource(ctx)
}

func (r *portProfilesResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down Expand Up @@ -456,87 +430,3 @@ func (r *portProfilesResource) ImportState(ctx context.Context, req resource.Imp
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("vdc"), vdcID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[1])...)
}

func (r *portProfilesResource) superSchema(_ context.Context) superschema.Schema {
return superschema.Schema{
Resource: superschema.SchemaDetails{
MarkdownDescription: "Provides a NSX-T App Port Profile resource",
},
Attributes: map[string]superschema.Attribute{
"id": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The ID of the VM.",
Computed: true,
},
Resource: &schemaR.StringAttribute{
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
"name": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Application Port Profile name.",
},
Resource: &schemaR.StringAttribute{
Required: true,
},
},
"description": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Application Port Profile description.",
},
Resource: &schemaR.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
"vdc": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "ID of VDC or VDC Group",
},
Resource: &schemaR.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
},
},
"app_ports": superschema.ListNestedAttribute{
Common: &schemaR.ListNestedAttribute{
MarkdownDescription: "List of application ports.",
Required: true,
},
Attributes: map[string]superschema.Attribute{
"ports": superschema.SetAttribute{
Common: &schemaR.SetAttribute{
MarkdownDescription: "Set of ports or ranges.",
Computed: true,
},
Resource: &schemaR.SetAttribute{
Optional: true,
ElementType: types.StringType,
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
},
},
"protocol": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Protocol.",
},
Resource: &schemaR.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("ICMPv4", "ICMPv6", "TCP", "UDP"),
},
},
},
},
},
},
}
}
102 changes: 102 additions & 0 deletions internal/provider/edgegw/app_port_profile_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package edgegw

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

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

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"

superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"
)

func portProfilesSchema(_ context.Context) superschema.Schema {
return superschema.Schema{
Resource: superschema.SchemaDetails{
MarkdownDescription: "Provides a NSX-T App Port Profile resource",
},
Attributes: map[string]superschema.Attribute{
"id": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "The ID of the VM.",
Computed: true,
},
Resource: &schemaR.StringAttribute{
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
"name": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Application Port Profile name.",
},
Resource: &schemaR.StringAttribute{
Required: true,
},
},
"description": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Application Port Profile description.",
},
Resource: &schemaR.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
"vdc": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "ID of VDC or VDC Group",
},
Resource: &schemaR.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
},
},
"app_ports": superschema.ListNestedAttribute{
Common: &schemaR.ListNestedAttribute{
MarkdownDescription: "List of application ports.",
Required: true,
},
Attributes: map[string]superschema.Attribute{
"ports": superschema.SetAttribute{
Common: &schemaR.SetAttribute{
MarkdownDescription: "Set of ports or ranges.",
Computed: true,
},
Resource: &schemaR.SetAttribute{
Optional: true,
ElementType: types.StringType,
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
},
},
"protocol": superschema.StringAttribute{
Common: &schemaR.StringAttribute{
MarkdownDescription: "Protocol.",
},
Resource: &schemaR.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("ICMPv4", "ICMPv6", "TCP", "UDP"),
},
},
},
},
},
},
}
}
18 changes: 18 additions & 0 deletions internal/provider/edgegw/app_port_profile_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edgegw

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

type portProfilesResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
VDC types.String `tfsdk:"vdc"`
Description types.String `tfsdk:"description"`
AppPorts types.List `tfsdk:"app_ports"`
}

type portProfilesResourceModelAppPorts []portProfilesResourceModelAppPort

type portProfilesResourceModelAppPort struct {
Protocol types.String `tfsdk:"protocol"`
Ports types.Set `tfsdk:"ports"`
}
24 changes: 0 additions & 24 deletions internal/provider/edgegw/edgegateway_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"regexp"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -15,32 +14,9 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"

superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"
)

type edgeGatewaysResourceModel struct {
Timeouts timeouts.Value `tfsdk:"timeouts"`
ID types.String `tfsdk:"id"`
Tier0VrfID types.String `tfsdk:"tier0_vrf_name"`
Name types.String `tfsdk:"name"`
OwnerType types.String `tfsdk:"owner_type"`
OwnerName types.String `tfsdk:"owner_name"`
Description types.String `tfsdk:"description"`
EnableLoadBalancing types.Bool `tfsdk:"lb_enabled"`
}

type edgeGatewayDataSourceModel struct {
ID types.String `tfsdk:"id"`
Tier0VrfID types.String `tfsdk:"tier0_vrf_name"`
Name types.String `tfsdk:"name"`
OwnerType types.String `tfsdk:"owner_type"`
OwnerName types.String `tfsdk:"owner_name"`
Description types.String `tfsdk:"description"`
EnableLoadBalancing types.Bool `tfsdk:"lb_enabled"`
}

/*
edgegwSchema
Expand Down
28 changes: 28 additions & 0 deletions internal/provider/edgegw/edgegateway_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edgegw

import (
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
)

type edgeGatewaysResourceModel struct {
Timeouts timeouts.Value `tfsdk:"timeouts"`
ID types.String `tfsdk:"id"`
Tier0VrfID types.String `tfsdk:"tier0_vrf_name"`
Name types.String `tfsdk:"name"`
OwnerType types.String `tfsdk:"owner_type"`
OwnerName types.String `tfsdk:"owner_name"`
Description types.String `tfsdk:"description"`
EnableLoadBalancing types.Bool `tfsdk:"lb_enabled"`
}

type edgeGatewayDataSourceModel struct {
ID types.String `tfsdk:"id"`
Tier0VrfID types.String `tfsdk:"tier0_vrf_name"`
Name types.String `tfsdk:"name"`
OwnerType types.String `tfsdk:"owner_type"`
OwnerName types.String `tfsdk:"owner_name"`
Description types.String `tfsdk:"description"`
EnableLoadBalancing types.Bool `tfsdk:"lb_enabled"`
}
Loading

0 comments on commit 0522a0a

Please sign in to comment.