Skip to content

Commit

Permalink
fix: add diversity zone support to mcr, add location details to produ…
Browse files Browse the repository at this point in the history
…cts, fix vxc update, fix tests
  • Loading branch information
MegaportPhilipBrowne committed May 28, 2024
1 parent 97e376b commit c296b5a
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 36 deletions.
60 changes: 58 additions & 2 deletions internal/provider/lag_port_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -58,8 +59,9 @@ type lagPortResourceModel struct {
LagCount types.Int64 `tfsdk:"lag_count"`
LagPortUIDs types.List `tfsdk:"lag_port_uids"`

AttributeTags types.Object `tfsdk:"attribute_tags"`
Resources types.Object `tfsdk:"resources"`
AttributeTags types.Object `tfsdk:"attribute_tags"`
Resources types.Object `tfsdk:"resources"`
LocationDetails types.Object `tfsdk:"location_details"`
}

func (orm *lagPortResourceModel) fromAPIPort(ctx context.Context, p *megaport.Port) diag.Diagnostics {
Expand Down Expand Up @@ -153,6 +155,18 @@ func (orm *lagPortResourceModel) fromAPIPort(ctx context.Context, p *megaport.Po
diags = append(diags, resourcesDiags...)
orm.Resources = resourcesObject

if p.LocationDetails != nil {
locationDetailsModel := &productLocationDetailsModel{
Name: types.StringValue(p.LocationDetails.Name),
City: types.StringValue(p.LocationDetails.City),
Metro: types.StringValue(p.LocationDetails.Metro),
Country: types.StringValue(p.LocationDetails.Country),
}
locationDetailsObject, locationDetailsDiags := types.ObjectValueFrom(ctx, productLocationDetailsAttrs, locationDetailsModel)
diags = append(diags, locationDetailsDiags...)
orm.LocationDetails = locationDetailsObject
}

return diags
}

Expand Down Expand Up @@ -317,6 +331,48 @@ func (r *lagPortResource) Schema(_ context.Context, _ resource.SchemaRequest, re
listplanmodifier.UseStateForUnknown(),
},
},
"location_details": schema.SingleNestedAttribute{
Description: "The location details of the product.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"city": schema.StringAttribute{
Description: "The city of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"metro": schema.StringAttribute{
Description: "The metro of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"country": schema.StringAttribute{
Description: "The country of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
"attribute_tags": schema.SingleNestedAttribute{
Description: "The attribute tags of the product.",
Optional: true,
Expand Down
74 changes: 66 additions & 8 deletions internal/provider/mcr_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"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/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -70,13 +71,14 @@ type mcrResourceModel struct {
ASN types.Int64 `tfsdk:"asn"`
DiversityZone types.String `tfsdk:"diversity_zone"`

Virtual types.Bool `tfsdk:"virtual"`
BuyoutPort types.Bool `tfsdk:"buyout_port"`
Locked types.Bool `tfsdk:"locked"`
AdminLocked types.Bool `tfsdk:"admin_locked"`
Cancelable types.Bool `tfsdk:"cancelable"`
AttributeTags types.Map `tfsdk:"attribute_tags"`
VirtualRouter types.Object `tfsdk:"virtual_router"`
Virtual types.Bool `tfsdk:"virtual"`
BuyoutPort types.Bool `tfsdk:"buyout_port"`
Locked types.Bool `tfsdk:"locked"`
AdminLocked types.Bool `tfsdk:"admin_locked"`
Cancelable types.Bool `tfsdk:"cancelable"`
AttributeTags types.Map `tfsdk:"attribute_tags"`
VirtualRouter types.Object `tfsdk:"virtual_router"`
LocationDetails types.Object `tfsdk:"location_details"`

PrefixFilterList types.Object `tfsdk:"prefix_filter_list"`
}
Expand Down Expand Up @@ -185,6 +187,19 @@ func (orm *mcrResourceModel) fromAPIMCR(ctx context.Context, m *megaport.MCR) di
virtualRouter, virtualRouterDiags := types.ObjectValueFrom(ctx, virtualRouterAttributes, virtualRouterModel)
apiDiags = append(apiDiags, virtualRouterDiags...)
orm.VirtualRouter = virtualRouter

if m.LocationDetails != nil {
locationDetailsModel := &productLocationDetailsModel{
Name: types.StringValue(m.LocationDetails.Name),
City: types.StringValue(m.LocationDetails.City),
Metro: types.StringValue(m.LocationDetails.Metro),
Country: types.StringValue(m.LocationDetails.Country),
}
locationDetails, locationDetailsDiags := types.ObjectValueFrom(ctx, productLocationDetailsAttrs, locationDetailsModel)
apiDiags = append(apiDiags, locationDetailsDiags...)
orm.LocationDetails = locationDetails
}

return apiDiags
}

Expand Down Expand Up @@ -343,7 +358,8 @@ func (r *mcrResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
},
"marketplace_visibility": schema.BoolAttribute{
Description: "Whether the product is visible in the Marketplace.",
Required: true,
Optional: true,
Computed: true,
},
"asn": schema.Int64Attribute{
Description: "ASN in the MCR order configuration.",
Expand Down Expand Up @@ -386,6 +402,48 @@ func (r *mcrResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
Description: "Attribute tags of the product.",
Computed: true,
},
"location_details": schema.SingleNestedAttribute{
Description: "The location details of the product.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"city": schema.StringAttribute{
Description: "The city of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"metro": schema.StringAttribute{
Description: "The metro of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"country": schema.StringAttribute{
Description: "The country of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
"prefix_filter_list": schema.SingleNestedAttribute{
Description: "Prefix filter list associated with the product.",
Optional: true,
Expand Down
56 changes: 56 additions & 0 deletions internal/provider/mve_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type mveResourceModel struct {
NetworkInterfaces types.List `tfsdk:"vnics"`
AttributeTags types.Map `tfsdk:"attribute_tags"`
Resources types.Object `tfsdk:"resources"`
LocationDetails types.Object `tfsdk:"location_details"`
}

// mveNetworkInterfaceModel represents a vNIC.
Expand Down Expand Up @@ -295,6 +296,19 @@ func (orm *mveResourceModel) fromAPIMVE(ctx context.Context, p *megaport.MVE) di
apiDiags = append(apiDiags, resourcesDiags...)
orm.Resources = resourcesObj
}

if p.LocationDetails != nil {
locationDetailsModel := &productLocationDetailsModel{
Name: types.StringValue(p.LocationDetails.Name),
City: types.StringValue(p.LocationDetails.City),
Metro: types.StringValue(p.LocationDetails.Metro),
Country: types.StringValue(p.LocationDetails.Country),
}
locationDetailsObject, locationDetailsDiags := types.ObjectValueFrom(ctx, productLocationDetailsAttrs, locationDetailsModel)
apiDiags = append(apiDiags, locationDetailsDiags...)
orm.LocationDetails = locationDetailsObject
}

return apiDiags
}

Expand Down Expand Up @@ -637,6 +651,48 @@ func (r *mveResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
listplanmodifier.UseStateForUnknown(),
},
},
"location_details": schema.SingleNestedAttribute{
Description: "The location details of the product.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"city": schema.StringAttribute{
Description: "The city of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"metro": schema.StringAttribute{
Description: "The metro of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"country": schema.StringAttribute{
Description: "The country of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
"vendor_config": schema.SingleNestedAttribute{
Description: "The vendor configuration of the MVE.",
Required: true,
Expand Down
74 changes: 72 additions & 2 deletions internal/provider/single_port_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"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/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -65,6 +66,13 @@ var (
"resource_type": types.StringType,
"up": types.Int64Type,
}

productLocationDetailsAttrs = map[string]attr.Type{
"name": types.StringType,
"city": types.StringType,
"metro": types.StringType,
"country": types.StringType,
}
)

// singlePortResourceModel maps the resource schema data.
Expand Down Expand Up @@ -96,8 +104,9 @@ type singlePortResourceModel struct {
Cancelable types.Bool `tfsdk:"cancelable"`
DiversityZone types.String `tfsdk:"diversity_zone"`

AttributeTags types.Object `tfsdk:"attribute_tags"`
Resources types.Object `tfsdk:"resources"`
AttributeTags types.Object `tfsdk:"attribute_tags"`
Resources types.Object `tfsdk:"resources"`
LocationDetails types.Object `tfsdk:"location_details"`
}

type portAttributeTagsModel struct {
Expand Down Expand Up @@ -145,6 +154,13 @@ type portInterfaceModel struct {
Up types.Int64 `tfsdk:"up"`
}

type productLocationDetailsModel struct {
Name types.String `tfsdk:"name"`
City types.String `tfsdk:"city"`
Metro types.String `tfsdk:"metro"`
Country types.String `tfsdk:"country"`
}

func (orm *singlePortResourceModel) fromAPIPort(ctx context.Context, p *megaport.Port) diag.Diagnostics {
diags := diag.Diagnostics{}
orm.UID = types.StringValue(p.UID)
Expand Down Expand Up @@ -232,6 +248,18 @@ func (orm *singlePortResourceModel) fromAPIPort(ctx context.Context, p *megaport
diags = append(diags, resourcesDiags...)
orm.Resources = resourcesObject

if p.LocationDetails != nil {
locationDetailsModel := &productLocationDetailsModel{
Name: types.StringValue(p.LocationDetails.Name),
City: types.StringValue(p.LocationDetails.City),
Metro: types.StringValue(p.LocationDetails.Metro),
Country: types.StringValue(p.LocationDetails.Country),
}
locationDetailsObject, locationDetailsDiags := types.ObjectValueFrom(ctx, productLocationDetailsAttrs, locationDetailsModel)
diags = append(diags, locationDetailsDiags...)
orm.LocationDetails = locationDetailsObject
}

return diags
}

Expand Down Expand Up @@ -469,6 +497,48 @@ func (r *portResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
},
},
"location_details": schema.SingleNestedAttribute{
Description: "The location details of the product.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"city": schema.StringAttribute{
Description: "The city of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"metro": schema.StringAttribute{
Description: "The metro of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"country": schema.StringAttribute{
Description: "The country of the location.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
"resources": schema.SingleNestedAttribute{
Description: "Resources attached to port.",
Optional: true,
Expand Down
Loading

0 comments on commit c296b5a

Please sign in to comment.