Skip to content

Commit

Permalink
provider/aws: Added s3 bucket region attribute management
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninir committed Dec 2, 2016
1 parent ecfc547 commit 03f1425
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 81 deletions.
111 changes: 63 additions & 48 deletions builtin/providers/aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,80 @@ func resourceAwsS3Bucket() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"bucket": &schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"arn": &schema.Schema{
"arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"acl": &schema.Schema{
"acl": {
Type: schema.TypeString,
Default: "private",
Optional: true,
},

"policy": &schema.Schema{
"policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
},

"cors_rule": &schema.Schema{
"cors_rule": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_headers": &schema.Schema{
"allowed_headers": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"allowed_methods": &schema.Schema{
"allowed_methods": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"allowed_origins": &schema.Schema{
"allowed_origins": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"expose_headers": &schema.Schema{
"expose_headers": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"max_age_seconds": &schema.Schema{
"max_age_seconds": {
Type: schema.TypeInt,
Optional: true,
},
},
},
},

"website": &schema.Schema{
"website": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index_document": &schema.Schema{
"index_document": {
Type: schema.TypeString,
Optional: true,
},

"error_document": &schema.Schema{
"error_document": {
Type: schema.TypeString,
Optional: true,
},

"redirect_all_requests_to": &schema.Schema{
"redirect_all_requests_to": {
Type: schema.TypeString,
ConflictsWith: []string{
"website.0.index_document",
Expand All @@ -111,7 +111,7 @@ func resourceAwsS3Bucket() *schema.Resource {
Optional: true,
},

"routing_rules": &schema.Schema{
"routing_rules": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateJsonString,
Expand All @@ -124,34 +124,34 @@ func resourceAwsS3Bucket() *schema.Resource {
},
},

"hosted_zone_id": &schema.Schema{
"hosted_zone_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"region": &schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"website_endpoint": &schema.Schema{
"website_endpoint": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"website_domain": &schema.Schema{
"website_domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"versioning": &schema.Schema{
"versioning": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": &schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Expand All @@ -167,16 +167,16 @@ func resourceAwsS3Bucket() *schema.Resource {
},
},

"logging": &schema.Schema{
"logging": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_bucket": &schema.Schema{
"target_bucket": {
Type: schema.TypeString,
Required: true,
},
"target_prefix": &schema.Schema{
"target_prefix": {
Type: schema.TypeString,
Optional: true,
},
Expand All @@ -191,98 +191,98 @@ func resourceAwsS3Bucket() *schema.Resource {
},
},

"lifecycle_rule": &schema.Schema{
"lifecycle_rule": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateS3BucketLifecycleRuleId,
},
"prefix": &schema.Schema{
"prefix": {
Type: schema.TypeString,
Required: true,
},
"enabled": &schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
},
"abort_incomplete_multipart_upload_days": &schema.Schema{
"abort_incomplete_multipart_upload_days": {
Type: schema.TypeInt,
Optional: true,
},
"expiration": &schema.Schema{
"expiration": {
Type: schema.TypeSet,
Optional: true,
Set: expirationHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"date": &schema.Schema{
"date": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateS3BucketLifecycleTimestamp,
},
"days": &schema.Schema{
"days": {
Type: schema.TypeInt,
Optional: true,
},
"expired_object_delete_marker": &schema.Schema{
"expired_object_delete_marker": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
"noncurrent_version_expiration": &schema.Schema{
"noncurrent_version_expiration": {
Type: schema.TypeSet,
Optional: true,
Set: expirationHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": &schema.Schema{
"days": {
Type: schema.TypeInt,
Optional: true,
},
},
},
},
"transition": &schema.Schema{
"transition": {
Type: schema.TypeSet,
Optional: true,
Set: transitionHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"date": &schema.Schema{
"date": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateS3BucketLifecycleTimestamp,
},
"days": &schema.Schema{
"days": {
Type: schema.TypeInt,
Optional: true,
},
"storage_class": &schema.Schema{
"storage_class": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateS3BucketLifecycleStorageClass,
},
},
},
},
"noncurrent_version_transition": &schema.Schema{
"noncurrent_version_transition": {
Type: schema.TypeSet,
Optional: true,
Set: transitionHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": &schema.Schema{
"days": {
Type: schema.TypeInt,
Optional: true,
},
"storage_class": &schema.Schema{
"storage_class": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateS3BucketLifecycleStorageClass,
Expand All @@ -294,20 +294,20 @@ func resourceAwsS3Bucket() *schema.Resource {
},
},

"force_destroy": &schema.Schema{
"force_destroy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"acceleration_status": &schema.Schema{
"acceleration_status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateS3BucketAccelerationStatus,
},

"request_payer": &schema.Schema{
"request_payer": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand All @@ -321,7 +321,6 @@ func resourceAwsS3Bucket() *schema.Resource {

func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
awsRegion := meta.(*AWSClient).region

// Get the bucket and acl
bucket := d.Get("bucket").(string)
Expand All @@ -334,6 +333,14 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
ACL: aws.String(acl),
}

var awsRegion string
if region, ok := d.GetOk("region"); ok {
awsRegion = region.(string)
} else {
awsRegion = meta.(*AWSClient).region
}
log.Printf("[DEBUG] S3 bucket create: %s, using region: %s", bucket, awsRegion)

// Special case us-east-1 region and do not set the LocationConstraint.
// See "Request Elements: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
if awsRegion != "us-east-1" {
Expand Down Expand Up @@ -589,7 +596,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}
}

//read the acceleration status
// Read the acceleration status
accelerate, err := s3conn.GetBucketAccelerateConfiguration(&s3.GetBucketAccelerateConfigurationInput{
Bucket: aws.String(d.Id()),
})
Expand All @@ -600,7 +607,15 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "UnsupportedArgument" {
return err
}
log.Printf("[WARN] S3 bucket: %s, the S3 Transfer Acceleration is not supported in the region: %s", d.Id(), meta.(*AWSClient).region)

var awsRegion string
if region, ok := d.GetOk("region"); ok {
awsRegion = region.(string)
} else {
awsRegion = meta.(*AWSClient).region
}

log.Printf("[WARN] S3 bucket: %s, the S3 Transfer Acceleration is not supported in the region: %s", d.Id(), awsRegion)
} else {
log.Printf("[DEBUG] S3 bucket: %s, read Acceleration: %v", d.Id(), accelerate)
d.Set("acceleration_status", accelerate.Status)
Expand Down
Loading

0 comments on commit 03f1425

Please sign in to comment.