Skip to content

Commit

Permalink
test(object): website config: all tests OK on nl-ams
Browse files Browse the repository at this point in the history
  • Loading branch information
Mia-Cross committed Dec 1, 2023
1 parent 859aac5 commit 3acb3c3
Show file tree
Hide file tree
Showing 7 changed files with 1,995 additions and 825 deletions.
6 changes: 3 additions & 3 deletions docs/resources/object_bucket_website_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource "scaleway_object_bucket" "main" {
}
resource "scaleway_object_bucket_website_configuration" "main" {
bucket = scaleway_object_bucket.main.name
bucket = scaleway_object_bucket.main.id
index_document {
suffix = "index.html"
}
Expand Down Expand Up @@ -55,7 +55,7 @@ resource "scaleway_object_bucket_policy" "main" {
}
resource "scaleway_object_bucket_website_configuration" "main" {
bucket = scaleway_object_bucket.main.name
bucket = scaleway_object_bucket.main.id
index_document {
suffix = "index.html"
}
Expand All @@ -69,7 +69,7 @@ The following arguments are supported:
* `bucket` - (Required, Forces new resource) The name of the bucket.
* `index_document` - (Required) The name of the index document for the website [detailed below](#index_document).
* `error_document` - (Optional) The name of the error document for the website [detailed below](#error_document).
* `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the bucket is associated with.
* `project_id` - (Defaults to [provider](../index.md#arguments-reference) `project_id`) The ID of the project the bucket is associated with.

## index_document

Expand Down
33 changes: 22 additions & 11 deletions scaleway/resource_object_bucket_website_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func ResourceBucketWebsiteConfiguration() *schema.Resource {

Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 63),
Description: "The bucket name.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 63),
Description: "The bucket's name or regional ID.",
DiffSuppressFunc: diffSuppressFuncLocality,
},
"index_document": {
Type: schema.TypeList,
Expand Down Expand Up @@ -84,7 +85,17 @@ func resourceBucketWebsiteConfigurationCreate(ctx context.Context, d *schema.Res
return diag.FromErr(err)
}

bucket := expandID(d.Get("bucket").(string))
regionalID := expandRegionalID(d.Get("bucket"))
bucket := regionalID.ID
bucketRegion := regionalID.Region

if bucketRegion != "" && bucketRegion != region {
conn, err = s3ClientForceRegion(d, meta, bucketRegion.String())
if err != nil {
return diag.FromErr(err)
}
region = bucketRegion
}

websiteConfig := &s3.WebsiteConfiguration{
IndexDocument: expandBucketWebsiteConfigurationIndexDocument(d.Get("index_document").([]interface{})),
Expand All @@ -98,11 +109,11 @@ func resourceBucketWebsiteConfigurationCreate(ctx context.Context, d *schema.Res
Bucket: scw.StringPtr(bucket),
})
if err != nil {
if s3err, ok := err.(awserr.Error); ok && s3err.Code() == s3.ErrCodeNoSuchBucket {
tflog.Error(ctx, fmt.Sprintf("Bucket %q was not found - removing from state!", bucket))
d.SetId("")
return nil
}
//if s3err, ok := err.(awserr.Error); ok && s3err.Code() == s3.ErrCodeNoSuchBucket {
// tflog.Error(ctx, fmt.Sprintf("Bucket %q was not found - removing from state!", bucket))
// d.SetId("")
// return nil
//}
return diag.FromErr(fmt.Errorf("couldn't read bucket: %s", err))
}

Expand Down
99 changes: 88 additions & 11 deletions scaleway/resource_object_bucket_website_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scaleway

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -40,12 +41,12 @@ func TestAccScalewayObjectBucketWebsiteConfiguration_Basic(t *testing.T) {
region = %[2]q
acl = "public-read"
tags = {
TestName = "TestAccSCW_WebsiteConfig_basic"
TestName = "TestAccScalewayObjectBucketWebsiteConfiguration_Basic"
}
}
resource "scaleway_object_bucket_website_configuration" "test" {
bucket = scaleway_object_bucket.test.name
bucket = scaleway_object_bucket.test.id
index_document {
suffix = "index.html"
}
Expand Down Expand Up @@ -93,12 +94,12 @@ func TestAccScalewayObjectBucketWebsiteConfiguration_WithPolicy(t *testing.T) {
region = %[2]q
acl = "public-read"
tags = {
TestName = "TestAccSCW_WebsiteConfig_basic"
TestName = "TestAccScalewayObjectBucketWebsiteConfiguration_WithPolicy"
}
}
resource "scaleway_object_bucket_policy" "main" {
bucket = scaleway_object_bucket.test.name
bucket = scaleway_object_bucket.test.id
policy = jsonencode(
{
"Version" = "2012-10-17",
Expand All @@ -120,7 +121,7 @@ func TestAccScalewayObjectBucketWebsiteConfiguration_WithPolicy(t *testing.T) {
}
resource "scaleway_object_bucket_website_configuration" "test" {
bucket = scaleway_object_bucket.test.name
bucket = scaleway_object_bucket.test.id
index_document {
suffix = "index.html"
}
Expand Down Expand Up @@ -169,12 +170,12 @@ func TestAccScalewayObjectBucketWebsiteConfiguration_Update(t *testing.T) {
region = %[2]q
acl = "public-read"
tags = {
TestName = "TestAccSCW_WebsiteConfig_basic"
TestName = "TestAccScalewayObjectBucketWebsiteConfiguration_Update"
}
}
resource "scaleway_object_bucket_website_configuration" "test" {
bucket = scaleway_object_bucket.test.name
bucket = scaleway_object_bucket.test.id
index_document {
suffix = "index.html"
}
Expand All @@ -192,12 +193,12 @@ func TestAccScalewayObjectBucketWebsiteConfiguration_Update(t *testing.T) {
region = %[2]q
acl = "public-read"
tags = {
TestName = "TestAccSCW_WebsiteConfig_basic"
TestName = "TestAccScalewayObjectBucketWebsiteConfiguration_Update"
}
}
resource "scaleway_object_bucket_website_configuration" "test" {
bucket = scaleway_object_bucket.test.name
bucket = scaleway_object_bucket.test.id
index_document {
suffix = "index.html"
}
Expand Down Expand Up @@ -226,6 +227,64 @@ func TestAccScalewayObjectBucketWebsiteConfiguration_Update(t *testing.T) {
})
}

func TestAccScalewayObjectBucketWebsiteConfiguration_WithBucketName(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(ResourcePrefix)
resourceName := resourceTestName

tt := NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: ErrorCheck(t, EndpointsID),
ProviderFactories: tt.ProviderFactories,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckScalewayObjectBucketWebsiteConfigurationDestroy(tt),
testAccCheckScalewayObjectBucketDestroy(tt),
),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "scaleway_object_bucket" "test" {
name = %[1]q
region = %[2]q
acl = "public-read"
}
resource "scaleway_object_bucket_website_configuration" "test" {
bucket = scaleway_object_bucket.test.name
index_document {
suffix = "index.html"
}
}
`, rName, objectTestsMainRegion),
ExpectError: regexp.MustCompile("couldn't read bucket: NoSuchBucket: The specified bucket does not exist"),
},
{
Config: fmt.Sprintf(`
resource "scaleway_object_bucket" "test" {
name = %[1]q
region = %[2]q
acl = "public-read"
}
resource "scaleway_object_bucket_website_configuration" "test" {
bucket = scaleway_object_bucket.test.name
region = %[2]q
index_document {
suffix = "index.html"
}
}
`, rName, objectTestsMainRegion),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayObjectBucketExistsForceRegion(tt, "scaleway_object_bucket.test", true),
testAccCheckScalewayObjectBucketWebsiteConfigurationExists(tt, resourceName),
),
},
},
})
}

func testAccCheckScalewayObjectBucketWebsiteConfigurationDestroy(tt *TestTools) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn, err := newS3ClientFromMeta(tt.Meta)
Expand All @@ -238,7 +297,16 @@ func testAccCheckScalewayObjectBucketWebsiteConfigurationDestroy(tt *TestTools)
continue
}

bucket := expandID(rs.Primary.ID)
regionalID := expandRegionalID(rs.Primary.ID)
bucket := regionalID.ID
bucketRegion := regionalID.Region

if bucketRegion != "" && bucketRegion.String() != *conn.Config.Region {
conn, err = newS3ClientFromMetaForceRegion(tt.Meta, bucketRegion.String())
if err != nil {
return err
}
}

input := &s3.GetBucketWebsiteInput{
Bucket: aws.String(bucket),
Expand Down Expand Up @@ -284,7 +352,16 @@ func testAccCheckScalewayObjectBucketWebsiteConfigurationExists(tt *TestTools, r
return fmt.Errorf("resource (%s) ID not set", resourceName)
}

bucket := expandID(rs.Primary.ID)
regionalID := expandRegionalID(rs.Primary.ID)
bucket := regionalID.ID
bucketRegion := regionalID.Region

if bucketRegion != "" && bucketRegion.String() != *conn.Config.Region {
conn, err = newS3ClientFromMetaForceRegion(tt.Meta, bucketRegion.String())
if err != nil {
return err
}
}

input := &s3.GetBucketWebsiteInput{
Bucket: aws.String(bucket),
Expand Down
Loading

0 comments on commit 3acb3c3

Please sign in to comment.