Skip to content

Commit e120765

Browse files
authored
Merge branch 'master' into fix/add_optionnal_field_project_id
2 parents 53358c2 + 42bab1d commit e120765

36 files changed

+29894
-17853
lines changed

docs/resources/mongodb_instance.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ resource "scaleway_mongodb_instance" "main" {
4949
}
5050
```
5151

52+
### Private Network and Public Network
53+
54+
```terraform
55+
resource scaleway_vpc_private_network pn01 {
56+
name = "my_private_network"
57+
region = "fr-par"
58+
}
59+
60+
resource "scaleway_mongodb_instance" "main" {
61+
name = "test-mongodb-basic1"
62+
version = "7.0.12"
63+
node_type = "MGDB-PLAY2-NANO"
64+
node_number = 1
65+
user_name = "my_initial_user"
66+
password = "thiZ_is_v&ry_s3cret"
67+
volume_size_in_gb = 5
68+
69+
private_network {
70+
pn_id = "${scaleway_vpc_private_network.pn02.id}"
71+
}
72+
73+
public_network {}
74+
75+
}
76+
```
77+
5278

5379
### Restore From Snapshot
5480

@@ -77,7 +103,9 @@ The following arguments are supported:
77103
- `snapshot_id` - (Optional) Snapshot ID to restore the MongoDB® instance from.
78104
- `private_network` - (Optional) Private Network endpoints of the Database Instance.
79105
- `pn_id` - (Required) The ID of the Private Network.
80-
- `public_network` - (Optional) Public network specs details.
106+
- `public_network` - (Optional) Public network endpoint configuration (no arguments).
107+
~> **Important** If neither private_network nor public_network is specified, a public network endpoint is created by default.
108+
81109

82110
## Attributes Reference
83111

@@ -91,6 +119,10 @@ In addition to all arguments above, the following attributes are exported:
91119
- `ips` - List of IP addresses for your endpoint.
92120
- `port` - TCP port of the endpoint.
93121
- `dns_records` - List of DNS records for your endpoint.
122+
- `public_network` - Private Network endpoints of the Database Instance.
123+
- `id` - The ID of the endpoint.
124+
- `port` - TCP port of the endpoint.
125+
- `dns_records` - List of DNS records for your endpoint.
94126

95127
## Import
96128

internal/services/baremetal/server.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument
273273
"private_ips": {
274274
Type: schema.TypeList,
275275
Computed: true,
276+
Optional: true,
276277
Description: "List of private IPv4 and IPv6 addresses associated with the resource",
277278
Elem: &schema.Resource{
278279
Schema: map[string]*schema.Schema{
@@ -534,6 +535,7 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface
534535
privateNetworkIDs = append(privateNetworkIDs, pn.PrivateNetworkID)
535536
}
536537

538+
// Read private IPs if possible
537539
allPrivateIPs := make([]map[string]interface{}, 0, listPrivateNetworks.TotalCount)
538540
diags := diag.Diagnostics{}
539541

@@ -542,25 +544,29 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface
542544
opts := &ipam.GetResourcePrivateIPsOptions{
543545
ResourceType: &resourceType,
544546
PrivateNetworkID: &privateNetworkID,
547+
ProjectID: &server.ProjectID,
545548
}
546549

547550
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, pnRegion, opts)
548-
if err != nil {
549-
if !httperrors.Is403(err) {
550-
return diag.FromErr(err)
551-
}
552551

552+
switch {
553+
case err == nil:
554+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
555+
case httperrors.Is403(err):
556+
return append(diags, diag.Diagnostic{
557+
Severity: diag.Warning,
558+
Summary: "Unauthorized to read server's private IPs, please check your IAM permissions",
559+
Detail: err.Error(),
560+
AttributePath: cty.GetAttrPath("private_ips"),
561+
})
562+
default:
553563
diags = append(diags, diag.Diagnostic{
554564
Severity: diag.Warning,
555-
Summary: err.Error(),
556-
Detail: "Got 403 while reading private IPs from IPAM API, please check your IAM permissions",
565+
Summary: fmt.Sprintf("Unable to get private IPs for server %s (pn_id: %s)", server.ID, privateNetworkID),
566+
Detail: err.Error(),
557567
AttributePath: cty.GetAttrPath("private_ips"),
558568
})
559569
}
560-
561-
if privateIPs != nil {
562-
allPrivateIPs = append(allPrivateIPs, privateIPs...)
563-
}
564570
}
565571

566572
_ = d.Set("private_ips", allPrivateIPs)

internal/services/baremetal/testdata/server-create-server-with-private-network.cassette.yaml

Lines changed: 671 additions & 622 deletions
Large diffs are not rendered by default.

internal/services/block/snapshot.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1314
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1415
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1516
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
@@ -41,10 +42,11 @@ func ResourceSnapshot() *schema.Resource {
4142
},
4243
"volume_id": {
4344
Type: schema.TypeString,
44-
Required: true,
45+
Optional: true,
4546
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
4647
Description: "ID of the volume from which creates a snapshot",
4748
DiffSuppressFunc: dsf.Locality,
49+
ConflictsWith: []string{"import"},
4850
},
4951
"tags": {
5052
Type: schema.TypeList,
@@ -54,6 +56,34 @@ func ResourceSnapshot() *schema.Resource {
5456
Optional: true,
5557
Description: "The tags associated with the snapshot",
5658
},
59+
"import": {
60+
Type: schema.TypeList,
61+
ForceNew: true,
62+
MaxItems: 1,
63+
Elem: &schema.Resource{
64+
Schema: map[string]*schema.Schema{
65+
"bucket": {
66+
Type: schema.TypeString,
67+
Required: true,
68+
ForceNew: true,
69+
Description: "Bucket containing qcow",
70+
DiffSuppressFunc: dsf.Locality,
71+
StateFunc: func(i interface{}) string {
72+
return regional.ExpandID(i.(string)).ID
73+
},
74+
},
75+
"key": {
76+
Type: schema.TypeString,
77+
Required: true,
78+
ForceNew: true,
79+
Description: "Key of the qcow file in the specified bucket",
80+
},
81+
},
82+
},
83+
Optional: true,
84+
Description: "Import snapshot from a qcow",
85+
ConflictsWith: []string{"volume_id"},
86+
},
5787
"zone": zonal.Schema(),
5888
"project_id": account.ProjectIDSchema(),
5989
},
@@ -66,15 +96,33 @@ func ResourceBlockSnapshotCreate(ctx context.Context, d *schema.ResourceData, m
6696
return diag.FromErr(err)
6797
}
6898

69-
snapshot, err := api.CreateSnapshot(&block.CreateSnapshotRequest{
70-
Zone: zone,
71-
ProjectID: d.Get("project_id").(string),
72-
Name: types.ExpandOrGenerateString(d.Get("name").(string), "snapshot"),
73-
VolumeID: locality.ExpandID(d.Get("volume_id")),
74-
Tags: types.ExpandStrings(d.Get("tags")),
75-
}, scw.WithContext(ctx))
76-
if err != nil {
77-
return diag.FromErr(err)
99+
var snapshot *block.Snapshot
100+
101+
if _, isImported := d.GetOk("import"); !isImported {
102+
snapshot, err = api.CreateSnapshot(&block.CreateSnapshotRequest{
103+
Zone: zone,
104+
ProjectID: d.Get("project_id").(string),
105+
Name: types.ExpandOrGenerateString(d.Get("name").(string), "snapshot"),
106+
VolumeID: locality.ExpandID(d.Get("volume_id")),
107+
Tags: types.ExpandStrings(d.Get("tags")),
108+
}, scw.WithContext(ctx))
109+
if err != nil {
110+
return diag.FromErr(err)
111+
}
112+
} else {
113+
req := &block.ImportSnapshotFromObjectStorageRequest{
114+
Zone: zone,
115+
ProjectID: d.Get("project_id").(string),
116+
Name: types.ExpandOrGenerateString(d.Get("name"), "snapshot"),
117+
Bucket: regional.ExpandID(d.Get("import.0.bucket")).ID,
118+
Key: d.Get("import.0.key").(string),
119+
Tags: types.ExpandStrings(d.Get("tags")),
120+
}
121+
122+
snapshot, err = api.ImportSnapshotFromObjectStorage(req, scw.WithContext(ctx))
123+
if err != nil {
124+
return diag.FromErr(err)
125+
}
78126
}
79127

80128
d.SetId(zonal.NewIDString(zone, snapshot.ID))

internal/services/block/snapshot_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package block_test
22

33
import (
4+
"fmt"
45
"testing"
56

7+
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
79
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
810
blocktestfuncs "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/block/testfuncs"
11+
objectchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/object/testfuncs"
912
)
1013

1114
func TestAccSnapshot_Basic(t *testing.T) {
@@ -38,3 +41,47 @@ func TestAccSnapshot_Basic(t *testing.T) {
3841
},
3942
})
4043
}
44+
45+
func TestAccSnapshot_FromS3(t *testing.T) {
46+
tt := acctest.NewTestTools(t)
47+
defer tt.Cleanup()
48+
49+
bucketName := sdkacctest.RandomWithPrefix("test-acc-scaleway-block-snapshot")
50+
resource.ParallelTest(t, resource.TestCase{
51+
PreCheck: func() { acctest.PreCheck(t) },
52+
ProviderFactories: tt.ProviderFactories,
53+
CheckDestroy: resource.ComposeTestCheckFunc(
54+
blocktestfuncs.IsSnapshotDestroyed(tt),
55+
objectchecks.IsObjectDestroyed(tt),
56+
objectchecks.IsBucketDestroyed(tt),
57+
),
58+
Steps: []resource.TestStep{
59+
{
60+
Config: fmt.Sprintf(`
61+
resource "scaleway_object_bucket" "snapshot-bucket" {
62+
name = "%s"
63+
}
64+
65+
resource "scaleway_object" "qcow-object" {
66+
bucket = scaleway_object_bucket.snapshot-bucket.name
67+
key = "test-acc-block-snapshot.qcow2"
68+
file = "testfixtures/small_image.qcow2"
69+
}
70+
71+
resource "scaleway_block_snapshot" "qcow-block-snapshot" {
72+
name = "test-acc-block-snapshot-qcow2"
73+
import {
74+
bucket = scaleway_object.qcow-object.bucket
75+
key = scaleway_object.qcow-object.key
76+
}
77+
}
78+
`, bucketName),
79+
Check: resource.ComposeTestCheckFunc(
80+
blocktestfuncs.IsSnapshotPresent(tt, "scaleway_block_snapshot.qcow-block-snapshot"),
81+
acctest.CheckResourceAttrUUID("scaleway_block_snapshot.qcow-block-snapshot", "id"),
82+
resource.TestCheckResourceAttr("scaleway_block_snapshot.qcow-block-snapshot", "name", "test-acc-block-snapshot-qcow2"),
83+
),
84+
},
85+
},
86+
})
87+
}

0 commit comments

Comments
 (0)