Skip to content

Commit

Permalink
Merge branch 'master' into feat/cockpit-add-support-for-alert-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jremy42 committed May 21, 2024
2 parents 6789a95 + d2e9182 commit 1ed7b93
Show file tree
Hide file tree
Showing 49 changed files with 28,034 additions and 27,146 deletions.
39 changes: 36 additions & 3 deletions docs/resources/vpc_public_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ For more information, see [the documentation](https://www.scaleway.com/en/develo

## Example Usage

### Basic

```terraform
resource "scaleway_vpc_public_gateway" "main" {
name = "public_gateway_demo"
Expand All @@ -18,6 +20,36 @@ resource "scaleway_vpc_public_gateway" "main" {
}
```

### With bastion

```terraform
resource "scaleway_iam_ssh_key" "key1" {
name = "key1"
public_key = file("~/.ssh/id_rsa.pub")
}
resource "scaleway_iam_ssh_key" "key2" {
name = "key2"
public_key = file("~/.ssh/another_key.pub")}
# Use a local variable to compute a hash of the SSH keys
locals {
ssh_keys_hash = sha256(join(",", [
scaleway_iam_ssh_key.key1.public_key,
scaleway_iam_ssh_key.key2.public_key,
]))
}
resource "scaleway_vpc_public_gateway" "main" {
name = "public_gateway_demo"
type = "VPC-GW-S"
tags = ["demo", "terraform"]
bastion_enabled = true
bastion_port = 61000
refresh_ssh_keys = local.ssh_keys_hash
}
```

## Argument Reference

The following arguments are supported:
Expand All @@ -28,10 +60,11 @@ The following arguments are supported:
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the public gateway should be created.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the public gateway is associated with.
- `upstream_dns_servers` - (Optional) override the gateway's default recursive DNS servers, if DNS features are enabled.
- `ip_id` - (Optional) attach an existing flexible IP to the gateway
- `bastion_enabled` - (Optional) Enable SSH bastion on the gateway
- `ip_id` - (Optional) attach an existing flexible IP to the gateway.
- `bastion_enabled` - (Optional) Enable SSH bastion on the gateway.
- `bastion_port` - (Optional) The port on which the SSH bastion will listen.
- `enable_smtp` - (Optional) Enable SMTP on the gateway
- `enable_smtp` - (Optional) Enable SMTP on the gateway.
- `refresh_ssh_keys` - (Optional) Trigger a refresh of the SSH keys on the public gateway by changing this field's value.

## Attributes Reference

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/nats-io/jwt/v2 v2.5.6
github.com/nats-io/nats.go v1.34.1
github.com/robfig/cron/v3 v3.0.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240416090022-eaa650293b32
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.22.0
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXq
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240416090022-eaa650293b32 h1:VaTPo4ij4NRWcRRtxpYo+JlB3nwgAjIIUAePKVU2zaI=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240416090022-eaa650293b32/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470 h1:jOsaUs0Mpg/2UCEcB/z3YpyG1UtgsjOPSKCRaOdMntg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
Expand Down
95 changes: 95 additions & 0 deletions internal/services/instance/helpers_instance_block.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package instance

import (
"errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
Expand All @@ -14,6 +16,99 @@ type BlockAndInstanceAPI struct {
blockAPI *block.API
}

type GetUnknownVolumeRequest struct {
VolumeID string
Zone scw.Zone
}

type UnknownVolume struct {
Zone scw.Zone
ID string
Name string
Size scw.Size
ServerID *string

// IsBlockVolume is true if volume is managed by block API
IsBlockVolume bool

InstanceVolumeType instance.VolumeVolumeType
}

// VolumeTemplateUpdate return a VolumeServerTemplate for an UpdateServer request
func (volume *UnknownVolume) VolumeTemplateUpdate() *instance.VolumeServerTemplate {
template := &instance.VolumeServerTemplate{
ID: scw.StringPtr(volume.ID),
Name: &volume.Name, // name is ignored by the API, any name will work here
}
if volume.IsBlockVolume {
template.Name = nil
template.VolumeType = volume.InstanceVolumeType
}

return template
}

// IsLocal returns true if the volume is a local volume
func (volume *UnknownVolume) IsLocal() bool {
return !volume.IsBlockVolume && volume.InstanceVolumeType == instance.VolumeVolumeTypeLSSD
}

// IsAttached returns true if the volume is attached to a server
func (volume *UnknownVolume) IsAttached() bool {
return volume.ServerID != nil && *volume.ServerID != ""
}

func (api *BlockAndInstanceAPI) GetUnknownVolume(req *GetUnknownVolumeRequest, opts ...scw.RequestOption) (*UnknownVolume, error) {
getVolumeResponse, err := api.API.GetVolume(&instance.GetVolumeRequest{
Zone: req.Zone,
VolumeID: req.VolumeID,
}, opts...)
notFoundErr := &scw.ResourceNotFoundError{}
if err != nil && !errors.As(err, &notFoundErr) {
return nil, err
}

if getVolumeResponse != nil {
vol := &UnknownVolume{
Zone: getVolumeResponse.Volume.Zone,
ID: getVolumeResponse.Volume.ID,
Name: getVolumeResponse.Volume.Name,
Size: getVolumeResponse.Volume.Size,
IsBlockVolume: false,
InstanceVolumeType: getVolumeResponse.Volume.VolumeType,
}
if getVolumeResponse.Volume.Server != nil {
vol.ServerID = &getVolumeResponse.Volume.Server.ID
}

return vol, nil
}

blockVolume, err := api.blockAPI.GetVolume(&block.GetVolumeRequest{
Zone: req.Zone,
VolumeID: req.VolumeID,
}, opts...)
if err != nil {
return nil, err
}

vol := &UnknownVolume{
Zone: blockVolume.Zone,
ID: blockVolume.ID,
Name: blockVolume.Name,
Size: blockVolume.Size,
IsBlockVolume: true,
InstanceVolumeType: instance.VolumeVolumeTypeSbsVolume,
}
for _, ref := range blockVolume.References {
if ref.ProductResourceType == "instance_server" {
vol.ServerID = &ref.ProductResourceID
}
}

return vol, nil
}

// newAPIWithZone returns a new instance API and the zone for a Create request
func instanceAndBlockAPIWithZone(d *schema.ResourceData, m interface{}) (*BlockAndInstanceAPI, scw.Zone, error) {
instanceAPI := instance.NewAPI(meta.ExtractScwClient(m))
Expand Down
30 changes: 11 additions & 19 deletions internal/services/instance/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,27 +830,19 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m

for i, volumeID := range raw.([]interface{}) {
volumeHasChange := d.HasChange("additional_volume_ids." + strconv.Itoa(i))
// local volumes can only be added when the instanceSDK is stopped
if volumeHasChange && !isStopped {
volumeResp, err := api.API.GetVolume(&instanceSDK.GetVolumeRequest{
Zone: zone,
VolumeID: zonal.ExpandID(volumeID).ID,
})
if err != nil {
return diag.FromErr(fmt.Errorf("failed to get updated volume: %w", err))
}

// We must be able to tell whether a volume is already present in the server or not
if volumeResp.Volume.Server != nil {
if volumeResp.Volume.VolumeType == instanceSDK.VolumeVolumeTypeLSSD && volumeResp.Volume.Server.ID != "" {
return diag.FromErr(errors.New("instanceSDK must be stopped to change local volumes"))
}
}
volume, err := api.GetUnknownVolume(&GetUnknownVolumeRequest{
VolumeID: zonal.ExpandID(volumeID).ID,
Zone: zone,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(fmt.Errorf("failed to get updated volume: %w", err))
}
volumes[strconv.Itoa(i+1)] = &instanceSDK.VolumeServerTemplate{
ID: scw.StringPtr(zonal.ExpandID(volumeID).ID),
Name: scw.StringPtr(types.NewRandomName("vol")), // name is ignored by the API, any name will work here

// local volumes can only be added when the server is stopped
if volumeHasChange && !isStopped && volume.IsLocal() && volume.IsAttached() {
return diag.FromErr(errors.New("instanceSDK must be stopped to change local volumes"))
}
volumes[strconv.Itoa(i+1)] = volume.VolumeTemplateUpdate()
}

serverShouldUpdate = true
Expand Down
18 changes: 18 additions & 0 deletions internal/services/instance/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,24 @@ func TestAccServer_BlockExternal(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_instance_server.main", "additional_volume_ids.#", "0"),
),
},
{
Config: `
resource "scaleway_block_volume" "volume" {
iops = 5000
size_in_gb = 10
}
resource "scaleway_instance_server" "main" {
image = "ubuntu_jammy"
type = "PLAY2-PICO"
additional_volume_ids = [scaleway_block_volume.volume.id]
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_instance_server.main", "type", "PLAY2-PICO"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "additional_volume_ids.#", "1"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "additional_volume_ids.0", "scaleway_block_volume.volume", "id"),
),
},
},
})
}
Expand Down
6,410 changes: 3,815 additions & 2,595 deletions internal/services/instance/testdata/server-additional-volumes.cassette.yaml

Large diffs are not rendered by default.

Loading

0 comments on commit 1ed7b93

Please sign in to comment.