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

fix(object): import bucket by name #343

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions scaleway/helper_storage_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func getS3ClientWithRegion(d *schema.ResourceData, m interface{}) (*s3.S3, scw.R
return meta.s3Client, region, err
}

// getS3ClientWithRegion returns a new S3 client with the correct region and id extracted from the resource data.
func getS3ClientWithRegionAndID(m interface{}, id string) (*s3.S3, scw.Region, string, error) {
// getS3ClientWithRegion returns a new S3 client with the correct region and name extracted from the resource data.
func getS3ClientWithRegionAndName(m interface{}, name string) (*s3.S3, scw.Region, string, error) {
meta := m.(*Meta)

region, id, err := parseRegionalID(id)
region, name, err := parseRegionalID(name)
if err != nil {
return nil, "", id, err
return nil, "", name, err
}

if region != meta.DefaultRegion {
Expand All @@ -48,11 +48,11 @@ func getS3ClientWithRegionAndID(m interface{}, id string) (*s3.S3, scw.Region, s

err := newMeta.bootstrapS3Client()
if err != nil {
return nil, "", id, err
return nil, "", name, err
}
return newMeta.s3Client, region, id, nil
return newMeta.s3Client, region, name, nil
}

return meta.s3Client, region, id, err
return meta.s3Client, region, name, err

}
29 changes: 18 additions & 11 deletions scaleway/resource_object_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func resourceScalewayObjectBucket() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -33,13 +32,10 @@ func resourceScalewayObjectBucket() *schema.Resource {
Default: "private",
Description: "ACL of the bucket: either 'public-read' or 'private'.",
ValidateFunc: validation.StringInSlice([]string{
"private",
"public-read",
"public-read-write",
"authenticated-read",
"bucket-owner-read",
"bucket-owner-full-control",
"log-delivery-write",
s3.ObjectCannedACLPrivate,
s3.ObjectCannedACLPublicRead,
s3.ObjectCannedACLPublicReadWrite,
s3.ObjectCannedACLAuthenticatedRead,
}, false),
},
"region": regionSchema(),
Expand Down Expand Up @@ -67,11 +63,22 @@ func resourceScalewayObjectBucketCreate(d *schema.ResourceData, m interface{}) e
}

func resourceScalewayObjectBucketRead(d *schema.ResourceData, m interface{}) error {
s3Client, _, bucketName, err := getS3ClientWithRegionAndID(m, d.Id())
s3Client, _, bucketName, err := getS3ClientWithRegionAndName(m, d.Id())
if err != nil {
return err
}

d.Set("name", bucketName)

// We do not read `acl` attribute because it could be impossible to find
// the right canned ACL from a complex ACL object.
//
// Known issue:
// Import a bucket (eg. terraform import scaleway_object_bucket.x fr-par/x)
// will always trigger a diff (eg. terraform plan) on acl attribute because
// we do not read it and it has a "private" default value.
// AWS has the same issue: https://github.com/terraform-providers/terraform-provider-aws/issues/6193

_, err = s3Client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(bucketName),
})
Expand All @@ -88,7 +95,7 @@ func resourceScalewayObjectBucketRead(d *schema.ResourceData, m interface{}) err
}

func resourceScalewayObjectBucketUpdate(d *schema.ResourceData, m interface{}) error {
s3Client, _, bucketName, err := getS3ClientWithRegionAndID(m, d.Id())
s3Client, _, bucketName, err := getS3ClientWithRegionAndName(m, d.Id())
if err != nil {
return err
}
Expand All @@ -110,7 +117,7 @@ func resourceScalewayObjectBucketUpdate(d *schema.ResourceData, m interface{}) e
}

func resourceScalewayObjectBucketDelete(d *schema.ResourceData, m interface{}) error {
s3Client, _, bucketName, err := getS3ClientWithRegionAndID(m, d.Id())
s3Client, _, bucketName, err := getS3ClientWithRegionAndName(m, d.Id())
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions website/docs/r/object_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ The following arguments are supported:

In addition to all above arguments, the following attribute is exported:

* `id` - The ID of the bucket.
* `id` - The unique name of the bucket.

## Import

Buckets can be imported using the `{region}/{id}` identifier, e.g.
Buckets can be imported using the `{region}/{bucketName}` identifier, e.g.

```
$ terraform import scaleway_object_bucket.some_bucket fr-par/11111111-1111-1111-1111-111111111111
```bash
$ terraform import scaleway_object_bucket.some_bucket fr-par/some-bucket
```