Skip to content

Commit

Permalink
t # This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
add default value to schema
  • Loading branch information
aayushrangwala committed Mar 15, 2024
1 parent b6c792e commit 69cafa4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
42 changes: 12 additions & 30 deletions internal/resources/metal/vlan/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,22 @@ type DataSourceModel struct {

func (m *DataSourceModel) parse(vlan *packngo.VirtualNetwork) diag.Diagnostics {
m.ID = types.StringValue(vlan.ID)
m.VlanID = types.StringValue(vlan.ID)
m.Description = types.StringValue(vlan.Description)
m.Vxlan = types.Int64Value(int64(vlan.VXLAN))
m.Facility = types.StringNull()

if vlan.Project.ID != "" {
m.ProjectID = types.StringValue(vlan.Project.ID)
}

m.VlanID = types.StringValue(vlan.ID)
m.Description = types.StringValue(vlan.Description)
m.Vxlan = types.Int64Value(int64(vlan.VXLAN))

var metroCode, facilityCode types.String
if vlan.Facility != nil {
facilityCode = types.StringValue(vlan.Facility.Code)
metroCode = types.StringValue(strings.ToLower(vlan.Facility.Metro.Code))
}
// version of this resource. StateFunc doesn't exist in terraform and it requires implementation
// of bespoke logic before storing state. To ensure backward compatibility we ignore lower/upper
// case diff for now, but we may want to require input upper case
if !strings.EqualFold(m.Facility.ValueString(), facilityCode.ValueString()) {
m.Facility = facilityCode
m.Facility = types.StringValue(strings.ToLower(vlan.Facility.Code))
m.Metro = types.StringValue(strings.ToLower(vlan.Facility.Metro.Code))
}

if vlan.Metro != nil {
metroCode = types.StringValue(strings.ToLower(vlan.Metro.Code))
}
if !strings.EqualFold(m.Metro.ValueString(), metroCode.ValueString()) {
m.Metro = metroCode
m.Metro = types.StringValue(strings.ToLower(vlan.Metro.Code))
}

deviceIds := make([]types.String, 0, len(vlan.Instances))
Expand All @@ -70,28 +60,20 @@ func (m *ResourceModel) parse(vlan *packngo.VirtualNetwork) diag.Diagnostics {
m.ID = types.StringValue(vlan.ID)
m.Description = types.StringValue(vlan.Description)
m.Vxlan = types.Int64Value(int64(vlan.VXLAN))
m.Facility = types.StringNull()

if vlan.Project.ID != "" {
m.ProjectID = types.StringValue(vlan.Project.ID)
}

var metroCode, facilityCode types.String
if vlan.Facility != nil {
facilityCode = types.StringValue(vlan.Facility.Code)
metroCode = types.StringValue(strings.ToLower(vlan.Facility.Metro.Code))
}
// version of this resource. StateFunc doesn't exist in terraform and it requires implementation
// of bespoke logic before storing state. To ensure backward compatibility we ignore lower/upper
// case diff for now, but we may want to require input upper case
if !strings.EqualFold(m.Facility.ValueString(), facilityCode.ValueString()) {
m.Facility = facilityCode
m.Facility = types.StringValue(strings.ToLower(vlan.Facility.Code))
m.Metro = types.StringValue(strings.ToLower(vlan.Facility.Metro.Code))
}

if vlan.Metro != nil {
metroCode = types.StringValue(strings.ToLower(vlan.Metro.Code))
}
if !strings.EqualFold(m.Metro.ValueString(), metroCode.ValueString()) {
m.Metro = metroCode
m.Metro = types.StringValue(strings.ToLower(vlan.Metro.Code))
}

return nil
}
7 changes: 6 additions & 1 deletion internal/resources/metal/vlan/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package vlan

import (
"context"
equinixplanmodifiers "github.com/equinix/terraform-provider-equinix/internal/planmodifiers"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

equinixplanmodifiers "github.com/equinix/terraform-provider-equinix/internal/planmodifiers"
)

func resourceSchema(ctx context.Context) schema.Schema {
Expand Down Expand Up @@ -40,13 +43,15 @@ func resourceSchema(ctx context.Context) schema.Schema {
Description: "Facility where to create the VLAN",
DeprecationMessage: "Use metro instead of facility. For more information, read the migration guide: https://registry.terraform.io/providers/equinix/equinix/latest/docs/guides/migration_guide_facilities_to_metros_devices",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ConflictsWith(path.MatchRoot("metro")),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
Default: stringdefault.StaticString(""),
},
"metro": schema.StringAttribute{
Description: "Metro in which to create the VLAN",
Expand Down

0 comments on commit 69cafa4

Please sign in to comment.