github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/import_aws_s3_bucket.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/aws/awserr"
     6  	"github.com/aws/aws-sdk-go/service/s3"
     7  	"github.com/hashicorp/errwrap"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceAwsS3BucketImportState(
    12  	d *schema.ResourceData,
    13  	meta interface{}) ([]*schema.ResourceData, error) {
    14  
    15  	results := make([]*schema.ResourceData, 1, 1)
    16  	results[0] = d
    17  
    18  	conn := meta.(*AWSClient).s3conn
    19  	pol, err := conn.GetBucketPolicy(&s3.GetBucketPolicyInput{
    20  		Bucket: aws.String(d.Id()),
    21  	})
    22  	if err != nil {
    23  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchBucketPolicy" {
    24  			// Bucket without policy
    25  			return results, nil
    26  		}
    27  		return nil, errwrap.Wrapf("Error importing AWS S3 bucket policy: {{err}}", err)
    28  	}
    29  
    30  	policy := resourceAwsS3BucketPolicy()
    31  	pData := policy.Data(nil)
    32  	pData.SetId(d.Id())
    33  	pData.SetType("aws_s3_bucket_policy")
    34  	pData.Set("bucket", d.Id())
    35  	pData.Set("policy", pol)
    36  	results = append(results, pData)
    37  
    38  	return results, nil
    39  }