Skip to content
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

feat(vpc): add custom route resource #2702

Merged
merged 10 commits into from
Sep 13, 2024
6 changes: 3 additions & 3 deletions docs/guides/backend_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page_title: "Using Backend Guide"

This page describes how to configure a backend by adding the backend block to your configuration with the Terraform Scaleway Provider.

Terraform provides the option to set up a [“backend”](https://www.terraform.io/language/settings/backends/configuration) of the `state` data files.
Terraform provides the option to set up a [“backend”](https://developer.hashicorp.com/terraform/language/backend) of the `state` data files.

This option allows you to handle the state and the way certain operations are executed.

Expand Down Expand Up @@ -70,7 +70,7 @@ export PG_CONN_STR=postgres://<user>:<pass>@localhost:<port>/terraform_backend?s

## Secrets

Hashicorp offers several methods to keep your secrets. Please check the Terraform [partial configuration](https://www.terraform.io/language/settings/backends/configuration#partial-configuration) for this topic.
Hashicorp offers several methods to keep your secrets. Please check the Terraform [partial configuration](https://developer.hashicorp.com/terraform/language/backend#partial-configuration) for this topic.

## Create your infrastructure with the Scaleway provider

Expand Down Expand Up @@ -161,7 +161,7 @@ $ terraform init -backend-config="conn_str=${PG_CONN_STR}" -migrate-state

## What about locking?

Most of the remote [backends](https://www.terraform.io/language/settings/backends/configuration#available-backends) natively support locking. To run terraform apply, Terraform will automatically acquire a lock;
Most of the remote [backends](https://developer.hashicorp.com/terraform/language/backend#backend-types) natively support locking. To run terraform apply, Terraform will automatically acquire a lock;
if someone else is already running apply, they will already have the lock, and you will have to wait.
You can run apply with the `-lock-timeout=<TIME>` parameter to tell Terraform to wait up to TIME for a lock to be released (e.g., `-lock-timeout=10m` will wait for 10 minutes).

Expand Down
4 changes: 3 additions & 1 deletion docs/resources/vpc_private_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

- `id` - The ID of the Private Network.
- `created_at` - The date and time of the creation of the Private Network (RFC 3339 format).
- `updated_at` - The date and time of the creation of the Private Network (RFC 3339 format).
- `ipv4_subnet` - The IPv4 subnet associated with the Private Network.
- `subnet` - The subnet CIDR.
- `id` - The subnet ID.
Expand All @@ -84,5 +86,5 @@ In addition to all arguments above, the following attributes are exported:
Private Networks can be imported using `{region}/{id}`, e.g.

```bash
terraform import scaleway_vpc_private_network.vpc_demo fr-par/11111111-1111-1111-1111-111111111111
terraform import scaleway_vpc_private_network.main fr-par/11111111-1111-1111-1111-111111111111
```
77 changes: 77 additions & 0 deletions docs/resources/vpc_route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
subcategory: "VPC"
page_title: "Scaleway: scaleway_vpc_route"
---

# Resource: scaleway_vpc_route

Creates and manages Scaleway VPC Routes.
For more information, see [the main documentation](https://www.scaleway.com/en/docs/network/vpc/concepts/).

## Example Usage

### Basic

```terraform
resource "scaleway_vpc" "vpc01" {
name = "tf-vpc-vpn"
}

resource "scaleway_vpc_private_network" "pn01" {
name = "tf-pn-vpn"
ipv4_subnet {
subnet = "172.16.64.0/22"
}
vpc_id = scaleway_vpc.vpc01.id
}

resource "scaleway_instance_server" "server01" {
name = "tf-server-vpn"
type = "PLAY2-MICRO"
image = "openvpn"
}

resource "scaleway_instance_private_nic" "pnic01" {
private_network_id = scaleway_vpc_private_network.pn01.id
server_id = scaleway_instance_server.server01.id
}

resource "scaleway_vpc_route" "rt01" {
vpc_id = scaleway_vpc.vpc01.id
description = "tf-route-vpn"
tags = ["tf", "route"]
destination = "10.0.0.0/24"
nexthop_resource_id = scaleway_instance_private_nic.pnic01.id
}
```

## Argument Reference

The following arguments are supported:

- `vpc_id` - (Required) The VPC ID the route belongs to.
- `description` - (Optional) The route description.
- `tags` - (Optional) The tags to associate with the route.
- `destination` - (Optional) The destination of the route.
- `nexthop_resource_id` - (Optional) The ID of the nexthop resource.
- `nexthop_private_network_id` - (Optional) The ID of the nexthop private network.
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the route.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the Project the route is associated with.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- `id` - The ID of the route.
- `created_at` - The date and time of the creation of the route (RFC 3339 format).
- `updated_at` - The date and time of the creation of the route (RFC 3339 format).

~> **Important:** routes' IDs are [regional](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-111111111111

## Import

Routes can be imported using `{region}/{id}`, e.g.

```bash
terraform import scaleway_vpc_route.main fr-par/11111111-1111-1111-1111-111111111111
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func Provider(config *Config) plugin.ProviderFunc {
"scaleway_vpc_public_gateway_ip": vpcgw.ResourceIP(),
"scaleway_vpc_public_gateway_ip_reverse_dns": vpcgw.ResourceIPReverseDNS(),
"scaleway_vpc_public_gateway_pat_rule": vpcgw.ResourcePATRule(),
"scaleway_vpc_route": vpc.ResourceRoute(),
"scaleway_webhosting": webhosting.ResourceWebhosting(),
},

Expand Down
11 changes: 9 additions & 2 deletions internal/services/mnq/helpers_mnq_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func NewSQSClient(httpClient *http.Client, region string, endpoint string, acces
return sqs.New(s), nil
}

func NATSClientWithRegion(d *schema.ResourceData, m interface{}) (nats.JetStreamContext, scw.Region, error) { //nolint:ireturn
func NATSClientWithRegion( //nolint:ireturn
d *schema.ResourceData,
m interface{},
) (nats.JetStreamContext, scw.Region, error) {
region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, "", err
Expand All @@ -84,7 +87,11 @@ func NATSClientWithRegion(d *schema.ResourceData, m interface{}) (nats.JetStream
return js, region, err
}

func newNATSJetStreamClient(region string, endpoint string, credentials string) (nats.JetStreamContext, error) { //nolint:ireturn
func newNATSJetStreamClient( //nolint:ireturn
region string,
endpoint string,
credentials string,
) (nats.JetStreamContext, error) {
jwt, seed, err := splitNATSJWTAndSeed(credentials)
if err != nil {
return nil, err
Expand Down
39 changes: 39 additions & 0 deletions internal/services/vpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-cty/cty"
Expand Down Expand Up @@ -94,3 +95,41 @@ func vpcPrivateNetworkUpgradeV1ZonalToRegionalID(element string) (string, error)

return fmt.Sprintf("%s/%s", fetchRegion.String(), id), nil
}

func vpcRouteExpandResourceID(id string) (string, error) {
parts := strings.Split(id, "/")
partCount := len(parts)

switch partCount {
case 1:
return id, nil
case 2:
_, ID, err := locality.ParseLocalizedID(id)
if err != nil {
return "", fmt.Errorf("failed to parse localized ID: %w", err)
}
return ID, nil
case 3:
// Parse as a nested ID and return the outerID
_, _, ID, err := locality.ParseLocalizedNestedID(id)
if err != nil {
return "", fmt.Errorf("failed to parse nested ID: %w", err)
}
return ID, nil
default:
return "", fmt.Errorf("unrecognized ID format: %s", id)
}
}

func diffSuppressFuncRouteResourceID(_, oldValue, newValue string, _ *schema.ResourceData) bool {
oldResourceID, err := vpcRouteExpandResourceID(oldValue)
if err != nil {
return false
}
newResourceID, err := vpcRouteExpandResourceID(newValue)
if err != nil {
return false
}

return oldResourceID == newResourceID
}
Loading
Loading