-
Notifications
You must be signed in to change notification settings - Fork 89
Feat/add vpn network config #1763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SerseusWasTaken
wants to merge
2
commits into
main
Choose a base branch
from
feat/add-vpn-network-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |||||
| "net/http" | ||||||
| "regexp" | ||||||
| "strings" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" | ||||||
| "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" | ||||||
|
|
@@ -20,6 +21,7 @@ import ( | |||||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||||||
| "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-log/tflog" | ||||||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||||||
| vpn "github.com/stackitcloud/stackit-sdk-go/services/vpn/v1api" | ||||||
|
|
@@ -34,6 +36,11 @@ import ( | |||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||||||
| ) | ||||||
|
|
||||||
| const ( | ||||||
| updateGatewayAttempts = 6 | ||||||
| updateGatewayRetryDelay = 10 * time.Second | ||||||
| ) | ||||||
|
|
||||||
| var ( | ||||||
| _ resource.Resource = &gatewayResource{} | ||||||
| _ resource.ResourceWithConfigure = &gatewayResource{} | ||||||
|
|
@@ -54,6 +61,16 @@ type BGPGatewayConfigModel struct { | |||||
| OverrideAdvertisedRoutes types.List `tfsdk:"override_advertised_routes"` | ||||||
| } | ||||||
|
|
||||||
| type NetworkConfigModel struct { | ||||||
| PredefinedNetworkPrefix types.String `tfsdk:"predefined_network_prefix"` | ||||||
| RoutingTableId types.String `tfsdk:"routing_table_id"` | ||||||
| } | ||||||
|
|
||||||
| var networkConfigTypes = map[string]attr.Type{ | ||||||
| "predefined_network_prefix": basetypes.StringType{}, | ||||||
| "routing_table_id": basetypes.StringType{}, | ||||||
| } | ||||||
|
|
||||||
| type Model struct { | ||||||
| Id types.String `tfsdk:"id"` // needed by TF | ||||||
| GatewayId types.String `tfsdk:"gateway_id"` | ||||||
|
|
@@ -64,6 +81,7 @@ type Model struct { | |||||
| RoutingType types.String `tfsdk:"routing_type"` | ||||||
| AvailabilityZones *AvailabilityZonesModel `tfsdk:"availability_zones"` | ||||||
| Bgp *BGPGatewayConfigModel `tfsdk:"bgp"` | ||||||
| NetworkConfig types.Object `tfsdk:"network_config"` | ||||||
| Labels types.Map `tfsdk:"labels"` | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -81,7 +99,10 @@ var schemaDescriptions = map[string]string{ | |||||
| "bgp": fmt.Sprintf("BGP configuration. Only applicable when routing_type is %s.", vpn.ROUTINGTYPE_BGP_ROUTE_BASED), | ||||||
| "bgp_local_asn": "Local ASN for BGP (private ASN range, 64512-4294967294).", | ||||||
| "bgp_override_advertised_routes": "List of IPv4 CIDRs to advertise via BGP. If omitted, SNA network ranges are advertised.", | ||||||
| "labels": "Map of custom labels (key-value string pairs).", | ||||||
| "network_config": "Network configuration for the VPN gateway.", | ||||||
| "network_config_predefined_network_prefix": "The IPv4 network prefix (CIDR notation) allocated for the VPN gateway. Must have a prefix length of /28 or larger. Cannot be changed after the gateway is created.", | ||||||
| "network_config_routing_table_id": "Custom routing table ID for the VPN gateway. If omitted, a default routing table is assigned.", | ||||||
| "labels": "Map of custom labels (key-value string pairs).", | ||||||
| } | ||||||
|
|
||||||
| type gatewayResource struct { | ||||||
|
|
@@ -215,6 +236,34 @@ func (r *gatewayResource) Schema(_ context.Context, _ resource.SchemaRequest, re | |||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| "network_config": schema.SingleNestedAttribute{ | ||||||
| Description: schemaDescriptions["network_config"], | ||||||
| Optional: true, | ||||||
| Attributes: map[string]schema.Attribute{ | ||||||
| "predefined_network_prefix": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["network_config_predefined_network_prefix"], | ||||||
| Optional: true, | ||||||
| Validators: []validator.String{ | ||||||
| validate.CIDR(), | ||||||
| }, | ||||||
| PlanModifiers: []planmodifier.String{ | ||||||
| stringplanmodifier.RequiresReplace(), | ||||||
| }, | ||||||
| }, | ||||||
| "routing_table_id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["network_config_routing_table_id"], | ||||||
| Optional: true, | ||||||
| // Computed: true, | ||||||
| Validators: []validator.String{ | ||||||
| validate.UUID(), | ||||||
| validate.NoSeparator(), | ||||||
| }, | ||||||
| PlanModifiers: []planmodifier.String{ | ||||||
| stringplanmodifier.UseStateForUnknown(), | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| "labels": schema.MapAttribute{ | ||||||
| Description: schemaDescriptions["labels"], | ||||||
| Optional: true, | ||||||
|
|
@@ -431,7 +480,13 @@ func (r *gatewayResource) Update(ctx context.Context, req resource.UpdateRequest | |||||
| return | ||||||
| } | ||||||
|
|
||||||
| _, err = r.client.DefaultAPI.UpdateGateway(ctx, projectId, region, gatewayId).UpdateGatewayPayload(*payload).Execute() | ||||||
| retryConfig := tfutils.RetryConfig{ | ||||||
| Attempts: updateGatewayAttempts, | ||||||
| Delay: updateGatewayRetryDelay, | ||||||
| RetryStatusCodes: []int{http.StatusConflict}, | ||||||
| } | ||||||
|
|
||||||
| _, err = tfutils.RetryRequest(ctx, r.client.DefaultAPI.UpdateGateway(ctx, projectId, region, gatewayId).UpdateGatewayPayload(*payload).Execute, retryConfig) | ||||||
| if err != nil { | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating VPN gateway", err.Error()) | ||||||
| return | ||||||
|
|
@@ -525,6 +580,14 @@ func toCreatePayload(ctx context.Context, model *Model) (*vpn.CreateGatewayPaylo | |||||
| payload.Bgp = bgpConfig | ||||||
| } | ||||||
|
|
||||||
| if !tfutils.IsUndefined(model.NetworkConfig) { | ||||||
| networkConfig, err := getNetworkConfigPayload(ctx, model) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| payload.NetworkConfig = &networkConfig | ||||||
| } | ||||||
|
|
||||||
| labels, err := tfutils.LabelsToPayload(ctx, model.Labels) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
|
|
@@ -566,6 +629,14 @@ func toUpdatePayload(ctx context.Context, model *Model) (*vpn.UpdateGatewayPaylo | |||||
| payload.Bgp = bgpConfig | ||||||
| } | ||||||
|
|
||||||
| if !tfutils.IsUndefined(model.NetworkConfig) { | ||||||
| networkConfig, err := getNetworkConfigPayload(ctx, model) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| payload.NetworkConfig = &networkConfig | ||||||
| } | ||||||
|
|
||||||
| labels, err := tfutils.LabelsToPayload(ctx, model.Labels) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
|
|
@@ -575,6 +646,25 @@ func toUpdatePayload(ctx context.Context, model *Model) (*vpn.UpdateGatewayPaylo | |||||
| return payload, nil | ||||||
| } | ||||||
|
|
||||||
| func getNetworkConfigPayload(ctx context.Context, model *Model) (vpn.NetworkConfig, error) { | ||||||
| var networkConfigModel NetworkConfigModel | ||||||
| diags := model.NetworkConfig.As(ctx, &networkConfigModel, basetypes.ObjectAsOptions{}) | ||||||
| if diags.HasError() { | ||||||
| return vpn.NetworkConfig{}, core.DiagsToError(diags) | ||||||
| } | ||||||
| networkConfig := vpn.NetworkConfig{} | ||||||
|
|
||||||
| if !tfutils.IsUndefined(networkConfigModel.PredefinedNetworkPrefix) { | ||||||
| networkConfig.PredefinedNetworkPrefix = networkConfigModel.PredefinedNetworkPrefix.ValueStringPointer() | ||||||
| } | ||||||
|
|
||||||
| if !tfutils.IsUndefined(networkConfigModel.RoutingTableId) { | ||||||
| networkConfig.RoutingTableId = networkConfigModel.RoutingTableId.ValueStringPointer() | ||||||
| } | ||||||
|
|
||||||
| return networkConfig, nil | ||||||
| } | ||||||
|
|
||||||
| func mapFields(ctx context.Context, gateway *vpn.GatewayResponse, model *Model, region string) error { | ||||||
| if gateway == nil { | ||||||
| return fmt.Errorf("response input is nil") | ||||||
|
|
@@ -617,6 +707,29 @@ func mapFields(ctx context.Context, gateway *vpn.GatewayResponse, model *Model, | |||||
| model.Bgp = bgpModel | ||||||
| } | ||||||
|
|
||||||
| if gateway.NetworkConfig == nil { | ||||||
| model.NetworkConfig = types.ObjectNull(networkConfigTypes) | ||||||
| } else { | ||||||
| predefinedNetworkPrefix := types.StringNull() | ||||||
| if gateway.NetworkConfig.PredefinedNetworkPrefix != nil { | ||||||
| predefinedNetworkPrefix = types.StringValue(*gateway.NetworkConfig.PredefinedNetworkPrefix) | ||||||
| } | ||||||
|
|
||||||
| routingTableId := types.StringNull() | ||||||
| if gateway.NetworkConfig.RoutingTableId != nil { | ||||||
| routingTableId = types.StringValue(*gateway.NetworkConfig.RoutingTableId) | ||||||
| } | ||||||
|
|
||||||
| networkConfigObject, diags := types.ObjectValue(networkConfigTypes, map[string]attr.Value{ | ||||||
| "predefined_network_prefix": predefinedNetworkPrefix, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same should be possible for the routing table id. Saves you a lot of code and especially the two if conditions above. |
||||||
| "routing_table_id": routingTableId, | ||||||
| }) | ||||||
| if diags.HasError() { | ||||||
| return fmt.Errorf("mapping network config: %w", core.DiagsToError(diags)) | ||||||
| } | ||||||
| model.NetworkConfig = networkConfigObject | ||||||
| } | ||||||
|
|
||||||
| labels, err := tfutils.MapLabels(ctx, gateway.Labels, model.Labels) | ||||||
| if err != nil { | ||||||
| return fmt.Errorf("mapping labels: %w", err) | ||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove commented code