diff --git a/resource_storage_bucket.go b/resource_storage_bucket.go index 1d660324..2640a1cc 100644 --- a/resource_storage_bucket.go +++ b/resource_storage_bucket.go @@ -19,6 +19,9 @@ func resourceStorageBucket() *schema.Resource { Read: resourceStorageBucketRead, Update: resourceStorageBucketUpdate, Delete: resourceStorageBucketDelete, + Importer: &schema.ResourceImporter{ + State: resourceStorageBucketStateImporter, + }, Schema: map[string]*schema.Schema{ "name": &schema.Schema{ @@ -154,12 +157,8 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error log.Printf("[DEBUG] Created bucket %v at location %v\n\n", res.Name, res.SelfLink) - // Assign the bucket ID as the resource ID - d.Set("self_link", res.SelfLink) - d.Set("url", fmt.Sprintf("gs://%s", bucket)) d.SetId(res.Id) - - return nil + return resourceStorageBucketRead(d, meta) } func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error { @@ -228,8 +227,10 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error { // Update the bucket ID according to the resource ID d.Set("self_link", res.SelfLink) + d.Set("url", fmt.Sprintf("gs://%s", bucket)) + d.Set("storage_class", res.StorageClass) + d.Set("location", res.Location) d.SetId(res.Id) - return nil } @@ -289,3 +290,8 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error return nil } + +func resourceStorageBucketStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + d.Set("name", d.Id()) + return []*schema.ResourceData{d}, nil +} diff --git a/resource_storage_bucket_test.go b/resource_storage_bucket_test.go index 417164be..b40cabde 100644 --- a/resource_storage_bucket_test.go +++ b/resource_storage_bucket_test.go @@ -86,14 +86,14 @@ func TestAccStorageStorageClass(t *testing.T) { ), }, { - Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "REGIONAL", "us-central1"), + Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "REGIONAL", "US-CENTRAL1"), Check: resource.ComposeTestCheckFunc( testAccCheckCloudStorageBucketExists( "google_storage_bucket.bucket", bucketName), resource.TestCheckResourceAttr( "google_storage_bucket.bucket", "storage_class", "REGIONAL"), resource.TestCheckResourceAttr( - "google_storage_bucket.bucket", "location", "us-central1"), + "google_storage_bucket.bucket", "location", "US-CENTRAL1"), ), }, }, @@ -136,6 +136,27 @@ func TestAccStorageBucketUpdate(t *testing.T) { }) } +func TestAccStorageBucketImport(t *testing.T) { + bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccGoogleStorageDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testGoogleStorageBucketsReaderDefaults(bucketName), + }, + resource.TestStep{ + ResourceName: "google_storage_bucket.bucket", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, + }, + }, + }) +} + func TestAccStorageForceDestroy(t *testing.T) { bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())