parent
460c18c202
commit
53a3697850
124 changed files with 8342 additions and 6042 deletions
|
|
@ -31,7 +31,8 @@ func NewNetworkAreaRouteDataSource() datasource.DataSource {
|
|||
|
||||
// networkDataSource is the data source implementation.
|
||||
type networkAreaRouteDataSource struct {
|
||||
client *iaas.APIClient
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
|
|
@ -40,12 +41,13 @@ func (d *networkAreaRouteDataSource) Metadata(_ context.Context, req datasource.
|
|||
}
|
||||
|
||||
func (d *networkAreaRouteDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
var ok bool
|
||||
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -61,7 +63,7 @@ func (d *networkAreaRouteDataSource) Schema(_ context.Context, _ datasource.Sche
|
|||
MarkdownDescription: description,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: "Terraform's internal data source ID. It is structured as \"`organization_id`,`network_area_id`,`network_area_route_id`\".",
|
||||
Description: "Terraform's internal data source ID. It is structured as \"`organization_id`,`region`,`network_area_id`,`network_area_route_id`\".",
|
||||
Computed: true,
|
||||
},
|
||||
"organization_id": schema.StringAttribute{
|
||||
|
|
@ -80,6 +82,11 @@ func (d *networkAreaRouteDataSource) Schema(_ context.Context, _ datasource.Sche
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Description: "The resource region. If not defined, the provider region is used.",
|
||||
// the region cannot be found, so it has to be passed
|
||||
Optional: true,
|
||||
},
|
||||
"network_area_route_id": schema.StringAttribute{
|
||||
Description: "The network area route ID.",
|
||||
Required: true,
|
||||
|
|
@ -88,13 +95,33 @@ func (d *networkAreaRouteDataSource) Schema(_ context.Context, _ datasource.Sche
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"next_hop": schema.StringAttribute{
|
||||
Description: "The IP address of the routing system, that will route the prefix configured. Should be a valid IPv4 address.",
|
||||
"destination": schema.SingleNestedAttribute{
|
||||
Description: "Destination of the route.",
|
||||
Computed: true,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"type": schema.StringAttribute{
|
||||
Description: fmt.Sprintf("CIDRV type. %s", utils.FormatPossibleValues("cidrv4", "cidrv6")),
|
||||
Computed: true,
|
||||
},
|
||||
"value": schema.StringAttribute{
|
||||
Description: "An CIDR string.",
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
"prefix": schema.StringAttribute{
|
||||
Description: "The network, that is reachable though the Next Hop. Should use CIDR notation.",
|
||||
"next_hop": schema.SingleNestedAttribute{
|
||||
Description: "Next hop destination.",
|
||||
Computed: true,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"type": schema.StringAttribute{
|
||||
Description: "Type of the next hop. " + utils.FormatPossibleValues("blackhole", "internet", "ipv4", "ipv6"),
|
||||
Computed: true,
|
||||
},
|
||||
"value": schema.StringAttribute{
|
||||
Description: "Either IPv4 or IPv6 (not set for blackhole and internet).",
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
"labels": schema.MapAttribute{
|
||||
Description: "Labels are key-value string pairs which can be attached to a resource container",
|
||||
|
|
@ -107,23 +134,26 @@ func (d *networkAreaRouteDataSource) Schema(_ context.Context, _ datasource.Sche
|
|||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (d *networkAreaRouteDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model ModelV1
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
organizationId := model.OrganizationId.ValueString()
|
||||
networkAreaId := model.NetworkAreaId.ValueString()
|
||||
region := d.providerData.GetRegionWithOverride(model.Region)
|
||||
networkAreaRouteId := model.NetworkAreaRouteId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "network_area_route_id", networkAreaRouteId)
|
||||
|
||||
networkAreaRouteResp, err := d.client.GetNetworkAreaRoute(ctx, organizationId, networkAreaId, networkAreaRouteId).Execute()
|
||||
networkAreaRouteResp, err := d.client.GetNetworkAreaRoute(ctx, organizationId, networkAreaId, region, networkAreaRouteId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
|
|
@ -141,11 +171,12 @@ func (d *networkAreaRouteDataSource) Read(ctx context.Context, req datasource.Re
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
err = mapFields(ctx, networkAreaRouteResp, &model)
|
||||
err = mapFields(ctx, networkAreaRouteResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network area route", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||
|
|
@ -27,13 +28,28 @@ import (
|
|||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ resource.Resource = &networkAreaRouteResource{}
|
||||
_ resource.ResourceWithConfigure = &networkAreaRouteResource{}
|
||||
_ resource.ResourceWithImportState = &networkAreaRouteResource{}
|
||||
_ resource.Resource = &networkAreaRouteResource{}
|
||||
_ resource.ResourceWithConfigure = &networkAreaRouteResource{}
|
||||
_ resource.ResourceWithImportState = &networkAreaRouteResource{}
|
||||
_ resource.ResourceWithModifyPlan = &networkAreaRouteResource{}
|
||||
_ resource.ResourceWithUpgradeState = &networkAreaRouteResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
// ModelV1 is the currently used model
|
||||
type ModelV1 struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
OrganizationId types.String `tfsdk:"organization_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
NetworkAreaId types.String `tfsdk:"network_area_id"`
|
||||
NetworkAreaRouteId types.String `tfsdk:"network_area_route_id"`
|
||||
NextHop *NexthopModelV1 `tfsdk:"next_hop"`
|
||||
Destination *DestinationModelV1 `tfsdk:"destination"`
|
||||
Labels types.Map `tfsdk:"labels"`
|
||||
}
|
||||
|
||||
// ModelV0 is the old model (only needed for state upgrade)
|
||||
type ModelV0 struct {
|
||||
Id types.String `tfsdk:"id"`
|
||||
OrganizationId types.String `tfsdk:"organization_id"`
|
||||
NetworkAreaId types.String `tfsdk:"network_area_id"`
|
||||
NetworkAreaRouteId types.String `tfsdk:"network_area_route_id"`
|
||||
|
|
@ -42,6 +58,18 @@ type Model struct {
|
|||
Labels types.Map `tfsdk:"labels"`
|
||||
}
|
||||
|
||||
// DestinationModelV1 maps the route destination data
|
||||
type DestinationModelV1 struct {
|
||||
Type types.String `tfsdk:"type"`
|
||||
Value types.String `tfsdk:"value"`
|
||||
}
|
||||
|
||||
// NexthopModelV1 maps the route nexthop data
|
||||
type NexthopModelV1 struct {
|
||||
Type types.String `tfsdk:"type"`
|
||||
Value types.String `tfsdk:"value"`
|
||||
}
|
||||
|
||||
// NewNetworkAreaRouteResource is a helper function to simplify the provider implementation.
|
||||
func NewNetworkAreaRouteResource() resource.Resource {
|
||||
return &networkAreaRouteResource{}
|
||||
|
|
@ -49,7 +77,8 @@ func NewNetworkAreaRouteResource() resource.Resource {
|
|||
|
||||
// networkResource is the resource implementation.
|
||||
type networkAreaRouteResource struct {
|
||||
client *iaas.APIClient
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the resource type name.
|
||||
|
|
@ -57,14 +86,45 @@ func (r *networkAreaRouteResource) Metadata(_ context.Context, req resource.Meta
|
|||
resp.TypeName = req.ProviderTypeName + "_network_area_route"
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *networkAreaRouteResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel ModelV1
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
}
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var planModel ModelV1
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the resource.
|
||||
func (r *networkAreaRouteResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -78,9 +138,10 @@ func (r *networkAreaRouteResource) Schema(_ context.Context, _ resource.SchemaRe
|
|||
resp.Schema = schema.Schema{
|
||||
Description: description,
|
||||
MarkdownDescription: description,
|
||||
Version: 1,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`organization_id`,`network_area_id`,`network_area_route_id`\".",
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`organization_id`,`network_area_id`,`region`,`network_area_route_id`\".",
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.UseStateForUnknown(),
|
||||
|
|
@ -97,6 +158,15 @@ func (r *networkAreaRouteResource) Schema(_ context.Context, _ resource.SchemaRe
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Description: "The resource region. If not defined, the provider region is used.",
|
||||
Optional: true,
|
||||
// must be computed to allow for storing the override value from the provider
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"network_area_id": schema.StringAttribute{
|
||||
Description: "The network area ID to which the network area route is associated.",
|
||||
Required: true,
|
||||
|
|
@ -121,24 +191,50 @@ func (r *networkAreaRouteResource) Schema(_ context.Context, _ resource.SchemaRe
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"next_hop": schema.StringAttribute{
|
||||
Description: "The IP address of the routing system, that will route the prefix configured. Should be a valid IPv4 address.",
|
||||
"next_hop": schema.SingleNestedAttribute{
|
||||
Description: "Next hop destination.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
Validators: []validator.String{
|
||||
validate.IP(false),
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"type": schema.StringAttribute{
|
||||
Description: fmt.Sprintf("Type of the next hop. %s %s", utils.FormatPossibleValues("blackhole", "internet", "ipv4", "ipv6"), "Only `ipv4` supported currently."),
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"value": schema.StringAttribute{
|
||||
Description: "Either IPv4 or IPv6 (not set for blackhole and internet). Only IPv4 supported currently.",
|
||||
Optional: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
Validators: []validator.String{
|
||||
validate.IP(false),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"prefix": schema.StringAttribute{
|
||||
Description: "The network, that is reachable though the Next Hop. Should use CIDR notation.",
|
||||
"destination": schema.SingleNestedAttribute{
|
||||
Description: "Destination of the route.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
Validators: []validator.String{
|
||||
validate.CIDR(),
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"type": schema.StringAttribute{
|
||||
Description: fmt.Sprintf("CIDRV type. %s %s", utils.FormatPossibleValues("cidrv4", "cidrv6"), "Only `cidrv4` is supported currently."),
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"value": schema.StringAttribute{
|
||||
Description: "An CIDR string.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
Validators: []validator.String{
|
||||
validate.CIDR(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"labels": schema.MapAttribute{
|
||||
|
|
@ -150,10 +246,91 @@ func (r *networkAreaRouteResource) Schema(_ context.Context, _ resource.SchemaRe
|
|||
}
|
||||
}
|
||||
|
||||
func (r *networkAreaRouteResource) UpgradeState(_ context.Context) map[int64]resource.StateUpgrader {
|
||||
return map[int64]resource.StateUpgrader{
|
||||
0: {
|
||||
// This handles moving from version 0 to 1
|
||||
PriorSchema: &schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"organization_id": schema.StringAttribute{
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"network_area_id": schema.StringAttribute{
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"network_area_route_id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"next_hop": schema.StringAttribute{
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.IP(false),
|
||||
},
|
||||
},
|
||||
"prefix": schema.StringAttribute{
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.CIDR(),
|
||||
},
|
||||
},
|
||||
"labels": schema.MapAttribute{
|
||||
ElementType: types.StringType,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
StateUpgrader: func(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
|
||||
var priorStateData ModelV0
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &priorStateData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
nexthopValue := priorStateData.NextHop.ValueString()
|
||||
prefixValue := priorStateData.Prefix.ValueString()
|
||||
|
||||
newStateData := ModelV1{
|
||||
Id: priorStateData.Id,
|
||||
OrganizationId: priorStateData.OrganizationId,
|
||||
NetworkAreaId: priorStateData.NetworkAreaId,
|
||||
NetworkAreaRouteId: priorStateData.NetworkAreaRouteId,
|
||||
Labels: priorStateData.Labels,
|
||||
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv4"),
|
||||
Value: types.StringValue(nexthopValue),
|
||||
},
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv4"),
|
||||
Value: types.StringValue(prefixValue),
|
||||
},
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, newStateData)...)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Create creates the resource and sets the initial Terraform state.
|
||||
func (r *networkAreaRouteResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model Model
|
||||
var model ModelV1
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -163,8 +340,10 @@ func (r *networkAreaRouteResource) Create(ctx context.Context, req resource.Crea
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
organizationId := model.OrganizationId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
networkAreaId := model.NetworkAreaId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
|
||||
|
||||
// Generate API request body from model
|
||||
|
|
@ -175,7 +354,7 @@ func (r *networkAreaRouteResource) Create(ctx context.Context, req resource.Crea
|
|||
}
|
||||
|
||||
// Create new network area route
|
||||
routes, err := r.client.CreateNetworkAreaRoute(ctx, organizationId, networkAreaId).CreateNetworkAreaRoutePayload(*payload).Execute()
|
||||
routes, err := r.client.CreateNetworkAreaRoute(ctx, organizationId, networkAreaId, region).CreateNetworkAreaRoutePayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network area route", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -196,12 +375,12 @@ func (r *networkAreaRouteResource) Create(ctx context.Context, req resource.Crea
|
|||
// Gets the route ID from the first element, routes.Items[0]
|
||||
routeItems := *routes.Items
|
||||
route := routeItems[0]
|
||||
routeId := *route.RouteId
|
||||
routeId := *route.Id
|
||||
|
||||
ctx = tflog.SetField(ctx, "network_area_route_id", routeId)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, &route, &model)
|
||||
err = mapFields(ctx, &route, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating network area route.", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -217,7 +396,7 @@ func (r *networkAreaRouteResource) Create(ctx context.Context, req resource.Crea
|
|||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *networkAreaRouteResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model ModelV1
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -225,15 +404,17 @@ func (r *networkAreaRouteResource) Read(ctx context.Context, req resource.ReadRe
|
|||
}
|
||||
organizationId := model.OrganizationId.ValueString()
|
||||
networkAreaId := model.NetworkAreaId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
networkAreaRouteId := model.NetworkAreaRouteId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "network_area_route_id", networkAreaRouteId)
|
||||
|
||||
networkAreaRouteResp, err := r.client.GetNetworkAreaRoute(ctx, organizationId, networkAreaId, networkAreaRouteId).Execute()
|
||||
networkAreaRouteResp, err := r.client.GetNetworkAreaRoute(ctx, organizationId, networkAreaId, region, networkAreaRouteId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
|
|
@ -247,7 +428,7 @@ func (r *networkAreaRouteResource) Read(ctx context.Context, req resource.ReadRe
|
|||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, networkAreaRouteResp, &model)
|
||||
err = mapFields(ctx, networkAreaRouteResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading network area route", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -264,7 +445,7 @@ func (r *networkAreaRouteResource) Read(ctx context.Context, req resource.ReadRe
|
|||
// Delete deletes the resource and removes the Terraform state on success.
|
||||
func (r *networkAreaRouteResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from state
|
||||
var model Model
|
||||
var model ModelV1
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -273,16 +454,18 @@ func (r *networkAreaRouteResource) Delete(ctx context.Context, req resource.Dele
|
|||
|
||||
organizationId := model.OrganizationId.ValueString()
|
||||
networkAreaId := model.NetworkAreaId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
networkAreaRouteId := model.NetworkAreaRouteId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "network_area_route_id", networkAreaRouteId)
|
||||
|
||||
// Delete existing network
|
||||
err := r.client.DeleteNetworkAreaRoute(ctx, organizationId, networkAreaId, networkAreaRouteId).Execute()
|
||||
err := r.client.DeleteNetworkAreaRoute(ctx, organizationId, networkAreaId, region, networkAreaRouteId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting network area route", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -296,7 +479,7 @@ func (r *networkAreaRouteResource) Delete(ctx context.Context, req resource.Dele
|
|||
// Update updates the resource and sets the updated Terraform state on success.
|
||||
func (r *networkAreaRouteResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model Model
|
||||
var model ModelV1
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -305,16 +488,18 @@ func (r *networkAreaRouteResource) Update(ctx context.Context, req resource.Upda
|
|||
|
||||
organizationId := model.OrganizationId.ValueString()
|
||||
networkAreaId := model.NetworkAreaId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
networkAreaRouteId := model.NetworkAreaRouteId.ValueString()
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "network_area_route_id", networkAreaRouteId)
|
||||
|
||||
// Retrieve values from state
|
||||
var stateModel Model
|
||||
var stateModel ModelV1
|
||||
diags = req.State.Get(ctx, &stateModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -328,7 +513,7 @@ func (r *networkAreaRouteResource) Update(ctx context.Context, req resource.Upda
|
|||
return
|
||||
}
|
||||
// Update existing network area route
|
||||
networkAreaRouteResp, err := r.client.UpdateNetworkAreaRoute(ctx, organizationId, networkAreaId, networkAreaRouteId).UpdateNetworkAreaRoutePayload(*payload).Execute()
|
||||
networkAreaRouteResp, err := r.client.UpdateNetworkAreaRoute(ctx, organizationId, networkAreaId, region, networkAreaRouteId).UpdateNetworkAreaRoutePayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network area route", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -336,7 +521,7 @@ func (r *networkAreaRouteResource) Update(ctx context.Context, req resource.Upda
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
err = mapFields(ctx, networkAreaRouteResp, &model)
|
||||
err = mapFields(ctx, networkAreaRouteResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating network area route", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -354,28 +539,25 @@ func (r *networkAreaRouteResource) Update(ctx context.Context, req resource.Upda
|
|||
func (r *networkAreaRouteResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
"Error importing network area route",
|
||||
fmt.Sprintf("Expected import identifier with format: [organization_id],[network_area_id],[network_area_route_id] Got: %q", req.ID),
|
||||
fmt.Sprintf("Expected import identifier with format: [organization_id],[network_area_id],[region],[network_area_route_id] Got: %q", req.ID),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
organizationId := idParts[0]
|
||||
networkAreaId := idParts[1]
|
||||
networkAreaRouteId := idParts[2]
|
||||
ctx = tflog.SetField(ctx, "organization_id", organizationId)
|
||||
ctx = tflog.SetField(ctx, "network_area_id", networkAreaId)
|
||||
ctx = tflog.SetField(ctx, "network_area_route_id", networkAreaRouteId)
|
||||
utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
|
||||
"organization_id": idParts[0],
|
||||
"network_area_id": idParts[1],
|
||||
"region": idParts[2],
|
||||
"network_area_route_id": idParts[3],
|
||||
})
|
||||
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization_id"), organizationId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_area_id"), networkAreaId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_area_route_id"), networkAreaRouteId)...)
|
||||
tflog.Info(ctx, "Network area route state imported")
|
||||
}
|
||||
|
||||
func mapFields(ctx context.Context, networkAreaRoute *iaas.Route, model *Model) error {
|
||||
func mapFields(ctx context.Context, networkAreaRoute *iaas.Route, model *ModelV1, region string) error {
|
||||
if networkAreaRoute == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
|
|
@ -386,13 +568,14 @@ func mapFields(ctx context.Context, networkAreaRoute *iaas.Route, model *Model)
|
|||
var networkAreaRouteId string
|
||||
if model.NetworkAreaRouteId.ValueString() != "" {
|
||||
networkAreaRouteId = model.NetworkAreaRouteId.ValueString()
|
||||
} else if networkAreaRoute.RouteId != nil {
|
||||
networkAreaRouteId = *networkAreaRoute.RouteId
|
||||
} else if networkAreaRoute.Id != nil {
|
||||
networkAreaRouteId = *networkAreaRoute.Id
|
||||
} else {
|
||||
return fmt.Errorf("network area route id not present")
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(model.OrganizationId.ValueString(), model.NetworkAreaId.ValueString(), networkAreaRouteId)
|
||||
model.Id = utils.BuildInternalTerraformId(model.OrganizationId.ValueString(), model.NetworkAreaId.ValueString(), region, networkAreaRouteId)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkAreaRoute.Labels, model.Labels)
|
||||
if err != nil {
|
||||
|
|
@ -400,13 +583,22 @@ func mapFields(ctx context.Context, networkAreaRoute *iaas.Route, model *Model)
|
|||
}
|
||||
|
||||
model.NetworkAreaRouteId = types.StringValue(networkAreaRouteId)
|
||||
model.NextHop = types.StringPointerValue(networkAreaRoute.Nexthop)
|
||||
model.Prefix = types.StringPointerValue(networkAreaRoute.Prefix)
|
||||
model.Labels = labels
|
||||
|
||||
model.NextHop, err = mapRouteNextHop(networkAreaRoute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.Destination, err = mapRouteDestination(networkAreaRoute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNetworkAreaRoutePayload, error) {
|
||||
func toCreatePayload(ctx context.Context, model *ModelV1) (*iaas.CreateNetworkAreaRoutePayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
|
@ -416,18 +608,28 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNetworkArea
|
|||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
nextHopPayload, err := toNextHopPayload(model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
destinationPayload, err := toDestinationPayload(model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &iaas.CreateNetworkAreaRoutePayload{
|
||||
Ipv4: &[]iaas.Route{
|
||||
Items: &[]iaas.Route{
|
||||
{
|
||||
Prefix: conversion.StringValueToPointer(model.Prefix),
|
||||
Nexthop: conversion.StringValueToPointer(model.NextHop),
|
||||
Labels: &labels,
|
||||
Destination: destinationPayload,
|
||||
Labels: &labels,
|
||||
Nexthop: nextHopPayload,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map) (*iaas.UpdateNetworkAreaRoutePayload, error) {
|
||||
func toUpdatePayload(ctx context.Context, model *ModelV1, currentLabels types.Map) (*iaas.UpdateNetworkAreaRoutePayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
|
@ -441,3 +643,97 @@ func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map)
|
|||
Labels: &labels,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func toNextHopPayload(model *ModelV1) (*iaas.RouteNexthop, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
} else if model.NextHop == nil {
|
||||
return nil, fmt.Errorf("nexthop is nil in model")
|
||||
}
|
||||
|
||||
switch model.NextHop.Type.ValueString() {
|
||||
case "blackhole":
|
||||
return sdkUtils.Ptr(iaas.NexthopBlackholeAsRouteNexthop(iaas.NewNexthopBlackhole("blackhole"))), nil
|
||||
case "internet":
|
||||
return sdkUtils.Ptr(iaas.NexthopInternetAsRouteNexthop(iaas.NewNexthopInternet("internet"))), nil
|
||||
case "ipv4":
|
||||
return sdkUtils.Ptr(iaas.NexthopIPv4AsRouteNexthop(iaas.NewNexthopIPv4("ipv4", model.NextHop.Value.ValueString()))), nil
|
||||
case "ipv6":
|
||||
return sdkUtils.Ptr(iaas.NexthopIPv6AsRouteNexthop(iaas.NewNexthopIPv6("ipv6", model.NextHop.Value.ValueString()))), nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown nexthop type: %s", model.NextHop.Type.ValueString())
|
||||
}
|
||||
|
||||
func toDestinationPayload(model *ModelV1) (*iaas.RouteDestination, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
} else if model.Destination == nil {
|
||||
return nil, fmt.Errorf("destination is nil in model")
|
||||
}
|
||||
|
||||
switch model.Destination.Type.ValueString() {
|
||||
case "cidrv4":
|
||||
return sdkUtils.Ptr(iaas.DestinationCIDRv4AsRouteDestination(iaas.NewDestinationCIDRv4("cidrv4", model.Destination.Value.ValueString()))), nil
|
||||
case "cidrv6":
|
||||
return sdkUtils.Ptr(iaas.DestinationCIDRv6AsRouteDestination(iaas.NewDestinationCIDRv6("cidrv6", model.Destination.Value.ValueString()))), nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown destination type: %s", model.Destination.Type.ValueString())
|
||||
}
|
||||
|
||||
func mapRouteNextHop(routeResp *iaas.Route) (*NexthopModelV1, error) {
|
||||
if routeResp.Nexthop == nil {
|
||||
return &NexthopModelV1{
|
||||
Type: types.StringNull(),
|
||||
Value: types.StringNull(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
switch i := routeResp.Nexthop.GetActualInstance().(type) {
|
||||
case *iaas.NexthopIPv4:
|
||||
return &NexthopModelV1{
|
||||
Type: types.StringPointerValue(i.Type),
|
||||
Value: types.StringPointerValue(i.Value),
|
||||
}, nil
|
||||
case *iaas.NexthopIPv6:
|
||||
return &NexthopModelV1{
|
||||
Type: types.StringPointerValue(i.Type),
|
||||
Value: types.StringPointerValue(i.Value),
|
||||
}, nil
|
||||
case *iaas.NexthopBlackhole:
|
||||
return &NexthopModelV1{
|
||||
Type: types.StringPointerValue(i.Type),
|
||||
Value: types.StringNull(),
|
||||
}, nil
|
||||
case *iaas.NexthopInternet:
|
||||
return &NexthopModelV1{
|
||||
Type: types.StringPointerValue(i.Type),
|
||||
Value: types.StringNull(),
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected nexthop type: %T", i)
|
||||
}
|
||||
}
|
||||
|
||||
func mapRouteDestination(routeResp *iaas.Route) (*DestinationModelV1, error) {
|
||||
if routeResp.Destination == nil {
|
||||
return &DestinationModelV1{
|
||||
Type: types.StringNull(),
|
||||
Value: types.StringNull(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
switch i := routeResp.Destination.GetActualInstance().(type) {
|
||||
case *iaas.DestinationCIDRv4:
|
||||
return &DestinationModelV1{
|
||||
Type: types.StringPointerValue(i.Type),
|
||||
Value: types.StringPointerValue(i.Value),
|
||||
}, nil
|
||||
case *iaas.DestinationCIDRv6:
|
||||
return &DestinationModelV1{
|
||||
Type: types.StringPointerValue(i.Type),
|
||||
Value: types.StringPointerValue(i.Value),
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected Destionation type: %T", i)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,100 +2,133 @@ package networkarearoute
|
|||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
type args struct {
|
||||
state ModelV1
|
||||
input *iaas.Route
|
||||
region string
|
||||
}
|
||||
tests := []struct {
|
||||
description string
|
||||
state Model
|
||||
input *iaas.Route
|
||||
expected Model
|
||||
args args
|
||||
expected ModelV1
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"id_ok",
|
||||
Model{
|
||||
description: "id_ok",
|
||||
args: args{
|
||||
state: ModelV1{
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
NetworkAreaRouteId: types.StringValue("narid"),
|
||||
},
|
||||
input: &iaas.Route{},
|
||||
region: "eu01",
|
||||
},
|
||||
expected: ModelV1{
|
||||
Id: types.StringValue("oid,naid,eu01,narid"),
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
NetworkAreaRouteId: types.StringValue("narid"),
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringNull(),
|
||||
Value: types.StringNull(),
|
||||
},
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringNull(),
|
||||
Value: types.StringNull(),
|
||||
},
|
||||
Labels: types.MapNull(types.StringType),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
&iaas.Route{},
|
||||
Model{
|
||||
Id: types.StringValue("oid,naid,narid"),
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
NetworkAreaRouteId: types.StringValue("narid"),
|
||||
Prefix: types.StringNull(),
|
||||
NextHop: types.StringNull(),
|
||||
Labels: types.MapNull(types.StringType),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"values_ok",
|
||||
Model{
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
NetworkAreaRouteId: types.StringValue("narid"),
|
||||
},
|
||||
&iaas.Route{
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
Nexthop: utils.Ptr("hop"),
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
description: "values_ok",
|
||||
args: args{
|
||||
state: ModelV1{
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
NetworkAreaRouteId: types.StringValue("narid"),
|
||||
Region: types.StringValue("eu01"),
|
||||
},
|
||||
input: &iaas.Route{
|
||||
Destination: &iaas.RouteDestination{
|
||||
DestinationCIDRv4: &iaas.DestinationCIDRv4{
|
||||
Type: utils.Ptr("cidrv4"),
|
||||
Value: utils.Ptr("prefix"),
|
||||
},
|
||||
DestinationCIDRv6: nil,
|
||||
},
|
||||
Nexthop: &iaas.RouteNexthop{
|
||||
NexthopIPv4: &iaas.NexthopIPv4{
|
||||
Type: utils.Ptr("ipv4"),
|
||||
Value: utils.Ptr("hop"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
},
|
||||
region: "eu02",
|
||||
},
|
||||
Model{
|
||||
Id: types.StringValue("oid,naid,narid"),
|
||||
expected: ModelV1{
|
||||
Id: types.StringValue("oid,naid,eu02,narid"),
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
NetworkAreaRouteId: types.StringValue("narid"),
|
||||
Prefix: types.StringValue("prefix"),
|
||||
NextHop: types.StringValue("hop"),
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv4"),
|
||||
Value: types.StringValue("prefix"),
|
||||
},
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv4"),
|
||||
Value: types.StringValue("hop"),
|
||||
},
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
Region: types.StringValue("eu02"),
|
||||
},
|
||||
true,
|
||||
isValid: true,
|
||||
},
|
||||
{
|
||||
"response_fields_nil_fail",
|
||||
Model{},
|
||||
&iaas.Route{
|
||||
Prefix: nil,
|
||||
Nexthop: nil,
|
||||
description: "response_fields_nil_fail",
|
||||
args: args{
|
||||
input: &iaas.Route{
|
||||
Destination: nil,
|
||||
Nexthop: nil,
|
||||
},
|
||||
},
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"response_nil_fail",
|
||||
Model{},
|
||||
nil,
|
||||
Model{},
|
||||
false,
|
||||
description: "response_nil_fail",
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
Model{
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
description: "no_resource_id",
|
||||
args: args{
|
||||
state: ModelV1{
|
||||
OrganizationId: types.StringValue("oid"),
|
||||
NetworkAreaId: types.StringValue("naid"),
|
||||
},
|
||||
input: &iaas.Route{},
|
||||
},
|
||||
&iaas.Route{},
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
err := mapFields(context.Background(), tt.input, &tt.state)
|
||||
err := mapFields(context.Background(), tt.args.input, &tt.args.state, tt.args.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -103,7 +136,7 @@ func TestMapFields(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(tt.state, tt.expected)
|
||||
diff := cmp.Diff(tt.args.state, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
|
@ -115,24 +148,41 @@ func TestMapFields(t *testing.T) {
|
|||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *Model
|
||||
input *ModelV1
|
||||
expected *iaas.CreateNetworkAreaRoutePayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
description: "default_ok",
|
||||
input: &Model{
|
||||
Prefix: types.StringValue("prefix"),
|
||||
NextHop: types.StringValue("hop"),
|
||||
input: &ModelV1{
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv4"),
|
||||
Value: types.StringValue("prefix"),
|
||||
},
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv4"),
|
||||
Value: types.StringValue("hop"),
|
||||
},
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
},
|
||||
expected: &iaas.CreateNetworkAreaRoutePayload{
|
||||
Ipv4: &[]iaas.Route{
|
||||
Items: &[]iaas.Route{
|
||||
{
|
||||
Prefix: utils.Ptr("prefix"),
|
||||
Nexthop: utils.Ptr("hop"),
|
||||
Destination: &iaas.RouteDestination{
|
||||
DestinationCIDRv4: &iaas.DestinationCIDRv4{
|
||||
Type: utils.Ptr("cidrv4"),
|
||||
Value: utils.Ptr("prefix"),
|
||||
},
|
||||
DestinationCIDRv6: nil,
|
||||
},
|
||||
Nexthop: &iaas.RouteNexthop{
|
||||
NexthopIPv4: &iaas.NexthopIPv4{
|
||||
Type: utils.Ptr("ipv4"),
|
||||
Value: utils.Ptr("hop"),
|
||||
},
|
||||
},
|
||||
Labels: &map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
|
|
@ -164,13 +214,13 @@ func TestToCreatePayload(t *testing.T) {
|
|||
func TestToUpdatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *Model
|
||||
input *ModelV1
|
||||
expected *iaas.UpdateNetworkAreaRoutePayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_ok",
|
||||
&Model{
|
||||
&ModelV1{
|
||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key1": types.StringValue("value1"),
|
||||
"key2": types.StringValue("value2"),
|
||||
|
|
@ -203,3 +253,371 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToNextHopPayload(t *testing.T) {
|
||||
type args struct {
|
||||
model *ModelV1
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *iaas.RouteNexthop
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "ipv4",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv4"),
|
||||
Value: types.StringValue("10.20.30.40"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &iaas.RouteNexthop{
|
||||
NexthopIPv4: &iaas.NexthopIPv4{
|
||||
Type: utils.Ptr("ipv4"),
|
||||
Value: utils.Ptr("10.20.30.40"),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "ipv6",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv6"),
|
||||
Value: types.StringValue("2001:db8:85a3:0:0:8a2e:370:7334"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &iaas.RouteNexthop{
|
||||
NexthopIPv6: &iaas.NexthopIPv6{
|
||||
Type: utils.Ptr("ipv6"),
|
||||
Value: utils.Ptr("2001:db8:85a3:0:0:8a2e:370:7334"),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "internet",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("internet"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &iaas.RouteNexthop{
|
||||
NexthopInternet: &iaas.NexthopInternet{
|
||||
Type: utils.Ptr("internet"),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "blackhole",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("blackhole"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &iaas.RouteNexthop{
|
||||
NexthopBlackhole: &iaas.NexthopBlackhole{
|
||||
Type: utils.Ptr("blackhole"),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid type",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
NextHop: &NexthopModelV1{
|
||||
Type: types.StringValue("foobar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "model is nil",
|
||||
args: args{
|
||||
model: nil,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "nexthop in model is nil",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
NextHop: nil,
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := toNextHopPayload(tt.args.model)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("toNextHopPayload() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("toNextHopPayload() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToDestinationPayload(t *testing.T) {
|
||||
type args struct {
|
||||
model *ModelV1
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *iaas.RouteDestination
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "cidrv4",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv4"),
|
||||
Value: types.StringValue("192.168.1.0/24"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &iaas.RouteDestination{
|
||||
DestinationCIDRv4: &iaas.DestinationCIDRv4{
|
||||
Type: utils.Ptr("cidrv4"),
|
||||
Value: utils.Ptr("192.168.1.0/24"),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "cidrv6",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv6"),
|
||||
Value: types.StringValue("2001:db8:1234::/48"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &iaas.RouteDestination{
|
||||
DestinationCIDRv6: &iaas.DestinationCIDRv6{
|
||||
Type: utils.Ptr("cidrv6"),
|
||||
Value: utils.Ptr("2001:db8:1234::/48"),
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid type",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
Destination: &DestinationModelV1{
|
||||
Type: types.StringValue("foobar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "model is nil",
|
||||
args: args{
|
||||
model: nil,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "destination in model is nil",
|
||||
args: args{
|
||||
model: &ModelV1{
|
||||
Destination: nil,
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := toDestinationPayload(tt.args.model)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("toDestinationPayload() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("toDestinationPayload() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRouteNextHop(t *testing.T) {
|
||||
type args struct {
|
||||
routeResp *iaas.Route
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *NexthopModelV1
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "ipv4",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Nexthop: &iaas.RouteNexthop{
|
||||
NexthopIPv4: &iaas.NexthopIPv4{
|
||||
Type: utils.Ptr("ipv4"),
|
||||
Value: utils.Ptr("192.168.1.0/24"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv4"),
|
||||
Value: types.StringValue("192.168.1.0/24"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ipv6",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Nexthop: &iaas.RouteNexthop{
|
||||
NexthopIPv4: &iaas.NexthopIPv4{
|
||||
Type: utils.Ptr("ipv6"),
|
||||
Value: utils.Ptr("2001:db8:85a3:0:0:8a2e:370:7334"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &NexthopModelV1{
|
||||
Type: types.StringValue("ipv6"),
|
||||
Value: types.StringValue("2001:db8:85a3:0:0:8a2e:370:7334"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "blackhole",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Nexthop: &iaas.RouteNexthop{
|
||||
NexthopBlackhole: &iaas.NexthopBlackhole{
|
||||
Type: utils.Ptr("blackhole"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &NexthopModelV1{
|
||||
Type: types.StringValue("blackhole"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "internet",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Nexthop: &iaas.RouteNexthop{
|
||||
NexthopInternet: &iaas.NexthopInternet{
|
||||
Type: utils.Ptr("internet"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &NexthopModelV1{
|
||||
Type: types.StringValue("internet"),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := mapRouteNextHop(tt.args.routeResp)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("mapRouteNextHop() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("mapRouteNextHop() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRouteDestination(t *testing.T) {
|
||||
type args struct {
|
||||
routeResp *iaas.Route
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *DestinationModelV1
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "cidrv4",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Destination: &iaas.RouteDestination{
|
||||
DestinationCIDRv4: &iaas.DestinationCIDRv4{
|
||||
Type: utils.Ptr("cidrv4"),
|
||||
Value: utils.Ptr("192.168.1.0/24"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv4"),
|
||||
Value: types.StringValue("192.168.1.0/24"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cidrv6",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Destination: &iaas.RouteDestination{
|
||||
DestinationCIDRv4: &iaas.DestinationCIDRv4{
|
||||
Type: utils.Ptr("cidrv6"),
|
||||
Value: utils.Ptr("2001:db8:1234::/48"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &DestinationModelV1{
|
||||
Type: types.StringValue("cidrv6"),
|
||||
Value: types.StringValue("2001:db8:1234::/48"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "destination in API response is nil",
|
||||
args: args{
|
||||
routeResp: &iaas.Route{
|
||||
Destination: nil,
|
||||
},
|
||||
},
|
||||
want: &DestinationModelV1{
|
||||
Type: types.StringNull(),
|
||||
Value: types.StringNull(),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := mapRouteDestination(tt.args.routeResp)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("mapRouteDestination() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("mapRouteDestination() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue