-
Notifications
You must be signed in to change notification settings - Fork 63
feature(vpn): onboard vpn gateway status endpoint #1515
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_vpn_gateway_status Data Source - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| VPN Gateway Status data source schema. Uses the default_region specified in the provider configuration as a fallback in case no region is defined on datasource level. | ||
| --- | ||
|
|
||
| # stackit_vpn_gateway_status (Data Source) | ||
|
|
||
| VPN Gateway Status data source schema. Uses the `default_region` specified in the provider configuration as a fallback in case no `region` is defined on datasource level. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "stackit_vpn_gateway_status" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| gateway_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `gateway_id` (String) The server-generated UUID of the VPN gateway. | ||
| - `project_id` (String) STACKIT project ID associated with the VPN gateway. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `display_name` (String) A user-friendly name for the VPN gateway. | ||
| - `error_message` (String) A descriptive message provided when the gateway is in an error state. | ||
| - `id` (String) Terraform's internal resource identifier. Structured as "`project_id`,`region`,`gateway_id`". | ||
| - `region` (String) STACKIT region name the resource is located in. If not defined, the provider region is used. | ||
| - `state` (String) The current life cycle state of the gateway. Possible values are: `PENDING`, `READY`, `ERROR`, `DELETING`. | ||
| - `tunnels` (Attributes List) List of the VPN tunnels in the gateway. (see [below for nested schema](#nestedatt--tunnels)) | ||
|
|
||
| <a id="nestedatt--tunnels"></a> | ||
| ### Nested Schema for `tunnels` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `instance_state` (String) The current life cycle state of the tunnel. Possible values are: `PENDING`, `READY`, `ERROR`, `DELETING`. | ||
|
Manuelvaas marked this conversation as resolved.
|
||
| - `internal_next_hop_ip` (String) The IPv4 address of the endpoint in the SNA. | ||
| - `name` (String) The name of the VPN tunnel. Possible values are: `tunnel1`, `tunnel2`. | ||
| - `public_ip` (String) The public IPv4 address of this endpoint. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| data "stackit_vpn_gateway_status" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| gateway_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,270 @@ | ||||||
| package gateway_status | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "net/http" | ||||||
|
|
||||||
| "github.com/hashicorp/terraform-plugin-framework/attr" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||||||
| "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" | ||||||
| sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||||||
| vpn "github.com/stackitcloud/stackit-sdk-go/services/vpn/v1api" | ||||||
|
|
||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/vpn/utils" | ||||||
|
|
||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||||||
| tfutils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||||||
| ) | ||||||
|
|
||||||
| var ( | ||||||
| _ datasource.DataSource = &vpnGatewayStatusDataSource{} | ||||||
| _ datasource.DataSourceWithConfigure = &vpnGatewayStatusDataSource{} | ||||||
|
|
||||||
| tunnelNames = sdkUtils.EnumSliceToStringSlice(vpn.AllowedVPNTunnelsNameEnumValues) | ||||||
|
Manuelvaas marked this conversation as resolved.
|
||||||
| ) | ||||||
|
|
||||||
| type vpnGatewayStatusDataSource struct { | ||||||
| client *vpn.APIClient | ||||||
| providerData core.ProviderData | ||||||
| } | ||||||
|
|
||||||
| type Model struct { | ||||||
| Id types.String `tfsdk:"id"` // needed by TF | ||||||
| GatewayId types.String `tfsdk:"gateway_id"` | ||||||
| ProjectId types.String `tfsdk:"project_id"` | ||||||
| Region types.String `tfsdk:"region"` | ||||||
| DisplayName types.String `tfsdk:"display_name"` | ||||||
| Tunnels types.List `tfsdk:"tunnels"` | ||||||
| } | ||||||
|
|
||||||
| type Tunnel struct { | ||||||
| InternalNextHopIP types.String `tfsdk:"internal_next_hop_ip"` | ||||||
| Name types.String `tfsdk:"name"` | ||||||
| PublicIP types.String `tfsdk:"public_ip"` | ||||||
| } | ||||||
|
|
||||||
| var tunnelsType = map[string]attr.Type{ | ||||||
| "internal_next_hop_ip": basetypes.StringType{}, | ||||||
| "name": basetypes.StringType{}, | ||||||
| "public_ip": basetypes.StringType{}, | ||||||
| } | ||||||
|
|
||||||
| func NewVPNGatewayStatusDataSource() datasource.DataSource { | ||||||
| return &vpnGatewayStatusDataSource{} | ||||||
| } | ||||||
|
|
||||||
| func (d *vpnGatewayStatusDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||||||
| var ok bool | ||||||
| d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||||||
| if !ok { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| d.client = utils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
| tflog.Info(ctx, "VPN client configured") | ||||||
| } | ||||||
|
|
||||||
| func (d *vpnGatewayStatusDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||||||
| resp.TypeName = req.ProviderTypeName + "_vpn_gateway_status" | ||||||
| } | ||||||
|
|
||||||
| var schemaDescriptions = map[string]string{ | ||||||
| "id": "Terraform's internal resource identifier. Structured as \"`project_id`,`region`,`gateway_id`\".", | ||||||
| "gateway_id": "The server-generated UUID of the VPN gateway.", | ||||||
| "project_id": "STACKIT project ID associated with the VPN gateway.", | ||||||
| "region": "STACKIT region name the resource is located in. If not defined, the provider region is used.", | ||||||
| "display_name": "A user-friendly name for the VPN gateway.", | ||||||
| "tunnels": "List of the VPN tunnels in the gateway.", | ||||||
| "tunnel_internal_next_hop_ip": "The IPv4 address of the endpoint in the SNA.", | ||||||
| "tunnel_name": fmt.Sprintf("The name of the VPN tunnel. %s", tfutils.FormatPossibleValues(tunnelNames...)), | ||||||
| "tunnel_public_ip": "The public IPv4 address of this endpoint.", | ||||||
| } | ||||||
|
|
||||||
| func (d *vpnGatewayStatusDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||||||
| resp.Schema = schema.Schema{ | ||||||
| Description: fmt.Sprintf("VPN Gateway Status data source schema. %s", core.DatasourceRegionFallbackDocstring), | ||||||
| Attributes: map[string]schema.Attribute{ | ||||||
| "id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["id"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "project_id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["project_id"], | ||||||
| Required: true, | ||||||
| Validators: []validator.String{ | ||||||
| validate.UUID(), | ||||||
| validate.NoSeparator(), | ||||||
| }, | ||||||
| }, | ||||||
| "region": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["region"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "gateway_id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["gateway_id"], | ||||||
| Required: true, | ||||||
| Validators: []validator.String{ | ||||||
| validate.UUID(), | ||||||
| validate.NoSeparator(), | ||||||
| }, | ||||||
| }, | ||||||
| "display_name": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["display_name"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "tunnels": schema.ListNestedAttribute{ | ||||||
| Description: schemaDescriptions["tunnels"], | ||||||
| Computed: true, | ||||||
| NestedObject: schema.NestedAttributeObject{ | ||||||
| Attributes: map[string]schema.Attribute{ | ||||||
| "internal_next_hop_ip": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["tunnel_internal_next_hop_ip"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "name": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["tunnel_name"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "public_ip": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["tunnel_public_ip"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| }, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (d *vpnGatewayStatusDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform | ||||||
| var model Model | ||||||
| diags := req.Config.Get(ctx, &model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| ctx = core.InitProviderContext(ctx) | ||||||
|
|
||||||
| projectId := model.ProjectId.ValueString() | ||||||
| region := d.providerData.GetRegionWithOverride(model.Region) | ||||||
| gatewayId := model.GatewayId.ValueString() | ||||||
|
|
||||||
| ctx = tflog.SetField(ctx, "project_id", projectId) | ||||||
| ctx = tflog.SetField(ctx, "region", region) | ||||||
| ctx = tflog.SetField(ctx, "gateway_id", gatewayId) | ||||||
|
|
||||||
| gatewayResponse, err := d.client.DefaultAPI.GetGatewayStatus(ctx, projectId, region, gatewayId).Execute() | ||||||
| if err != nil { | ||||||
| var oapiErr *oapierror.GenericOpenAPIError | ||||||
| ok := errors.As(err, &oapiErr) | ||||||
| if ok && oapiErr.StatusCode == http.StatusNotFound { | ||||||
| resp.State.RemoveResource(ctx) | ||||||
| return | ||||||
| } | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading VPN gateway", fmt.Sprintf("Calling API: %v", err)) | ||||||
| return | ||||||
| } | ||||||
| ctx = core.LogResponse(ctx) | ||||||
|
|
||||||
| err = mapFields(ctx, gatewayResponse, &model, region) | ||||||
| if err != nil { | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading VPN gateway", fmt.Sprintf("Processing response: %v", err)) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| diags = resp.State.Set(ctx, model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
| tflog.Info(ctx, "VPN gateway read", map[string]any{ | ||||||
| "gateway_id": gatewayId, | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| func mapFields(ctx context.Context, gatewayStatus *vpn.GatewayStatusResponse, model *Model, region string) error { | ||||||
| if gatewayStatus == nil { | ||||||
| return fmt.Errorf("response input is nil") | ||||||
| } | ||||||
| if model == nil { | ||||||
| return fmt.Errorf("model input is nil") | ||||||
| } | ||||||
|
|
||||||
| var gatewayId string | ||||||
| if model.GatewayId.ValueString() != "" { | ||||||
| gatewayId = model.GatewayId.ValueString() | ||||||
| } else if gatewayStatus.Id != nil { | ||||||
| gatewayId = *gatewayStatus.Id | ||||||
| } else { | ||||||
| return fmt.Errorf("gateway id not present") | ||||||
| } | ||||||
|
|
||||||
| model.Id = tfutils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, gatewayId) | ||||||
| model.GatewayId = types.StringValue(gatewayId) | ||||||
| model.Region = types.StringValue(region) | ||||||
|
|
||||||
| if gatewayStatus.DisplayName != nil { | ||||||
| model.DisplayName = types.StringValue(*gatewayStatus.DisplayName) | ||||||
| } | ||||||
|
|
||||||
| if err := mapTunnels(ctx, gatewayStatus, model); err != nil { | ||||||
|
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
|
||||||
| return fmt.Errorf("map tunnels: %w", err) | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func mapTunnels(ctx context.Context, gatewayStatus *vpn.GatewayStatusResponse, model *Model) error { | ||||||
|
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
there's no need to pass the whole response to this one 😉
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. You could even go further. If you don't pass the model and instead return the ListValue, you save a lot of boilerplate code in your unit test of this function. You just pass the []vpn.VPNTunnels and check for the expected result without having to define an expected model etc. func mapTunnels(ctx context.Context, vpnTunnels []vpn.VPNTunnels) (*basetypes.ListValue, error) {
tunnels := []attr.Value{}
for _, tunnelItem := range vpnTunnels {
tunnel := Tunnel{}
if tunnelItem.InternalNextHopIP != nil {
tunnel.InternalNextHopIP = types.StringValue(string(*tunnelItem.InternalNextHopIP))
}
if tunnelItem.Name != nil {
tunnel.Name = types.StringValue(string(*tunnelItem.Name))
}
if tunnelItem.PublicIP != nil {
tunnel.PublicIP = types.StringValue(string(*tunnelItem.PublicIP))
}
tunnelValue, diags := types.ObjectValueFrom(ctx, tunnelsType, tunnel)
if diags.HasError() {
return nil, fmt.Errorf("mapping tunnel: %w", core.DiagsToError(diags))
}
tunnels = append(tunnels, tunnelValue)
}
tfTunnels, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: tunnelsType}, tunnels)
if diags.HasError() {
return nil, fmt.Errorf("mapping tunnels: %w", core.DiagsToError(diags))
}
return &tfTunnels, nil
} |
||||||
| if gatewayStatus == nil { | ||||||
| return fmt.Errorf("gatewayStatus is nil") | ||||||
| } | ||||||
| if model == nil { | ||||||
| return fmt.Errorf("model is nil") | ||||||
| } | ||||||
|
|
||||||
| tunnels := []attr.Value{} | ||||||
|
|
||||||
| for _, tunnelItem := range gatewayStatus.Tunnels { | ||||||
| tunnel := Tunnel{} | ||||||
|
|
||||||
| if tunnelItem.InternalNextHopIP != nil { | ||||||
| tunnel.InternalNextHopIP = types.StringValue(string(*tunnelItem.InternalNextHopIP)) | ||||||
| } | ||||||
| if tunnelItem.Name != nil { | ||||||
| tunnel.Name = types.StringValue(string(*tunnelItem.Name)) | ||||||
| } | ||||||
| if tunnelItem.PublicIP != nil { | ||||||
| tunnel.PublicIP = types.StringValue(string(*tunnelItem.PublicIP)) | ||||||
| } | ||||||
|
|
||||||
| tunnelValue, diags := types.ObjectValueFrom(ctx, tunnelsType, tunnel) | ||||||
| if diags.HasError() { | ||||||
| return fmt.Errorf("mapping tunnel: %w", core.DiagsToError(diags)) | ||||||
| } | ||||||
|
|
||||||
| tunnels = append(tunnels, tunnelValue) | ||||||
| } | ||||||
|
|
||||||
| var diags diag.Diagnostics | ||||||
| model.Tunnels, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: tunnelsType}, tunnels) | ||||||
| if diags.HasError() { | ||||||
| return fmt.Errorf("mapping tunnels: %w", core.DiagsToError(diags)) | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.