Make google resource storage bucket importable (#14455)

This commit is contained in:
emily 2017-05-15 09:38:32 -07:00 committed by Paul Stack
parent 6d0786cccb
commit 2ad2af0c09
2 changed files with 35 additions and 8 deletions

View File

@ -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
}

View File

@ -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())