Only check project if it's not present on the resource. (#1125)

This commit is contained in:
Nathan McKinley 2018-02-22 15:31:55 -08:00 committed by GitHub
parent e1d5543362
commit 151e83aa26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 8 deletions

View File

@ -406,17 +406,24 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
return handleNotFoundError(err, d, fmt.Sprintf("Storage Bucket %q", d.Get("name").(string)))
}
log.Printf("[DEBUG] Read bucket %v at location %v\n\n", res.Name, res.SelfLink)
// We need to get the project associated with this bucket because otherwise import
// won't work properly. That means we need to call the projects.get API with the
// project number, to get the project ID - there's no project ID field in the
// resource response.
log.Println("[TRACE] Fetching project ID.")
proj, err := config.clientCompute.Projects.Get(strconv.FormatUint(res.ProjectNumber, 10)).Do()
if err != nil {
return err
// resource response. However, this requires a call to the Compute API, which
// would otherwise not be required for this resource. So, we're going to
// intentionally check whether the project is set *on the resource*. If it is,
// we will not try to fetch the project name. If it is not, either because
// the user intends to use the default provider project, or because the resource
// is currently being imported, we will read it from the API.
if _, ok := d.GetOk("project"); !ok {
log.Printf("[DEBUG] Read bucket %v at location %v\n\n", res.Name, res.SelfLink)
proj, err := config.clientCompute.Projects.Get(strconv.FormatUint(res.ProjectNumber, 10)).Do()
if err != nil {
return err
}
log.Printf("[DEBUG] Bucket %v is in project number %v, which is project ID %s.\n", res.Name, res.ProjectNumber, proj.Name)
d.Set("project", proj.Name)
}
log.Printf("[DEBUG] Bucket %v is in project number %v, which is project ID %s.\n", res.Name, res.ProjectNumber, proj.Name)
// Update the bucket ID according to the resource ID
d.Set("self_link", res.SelfLink)
@ -428,7 +435,6 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("versioning", flattenBucketVersioning(res.Versioning))
d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle))
d.Set("labels", res.Labels)
d.Set("project", proj.Name)
d.SetId(res.Id)
return nil
}

View File

@ -34,6 +34,8 @@ func TestAccStorageBucket_basic(t *testing.T) {
"google_storage_bucket.bucket", "location", "US"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "force_destroy", "false"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
),
},
resource.TestStep{
@ -86,6 +88,8 @@ func TestAccStorageBucket_customAttributes(t *testing.T) {
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "location", "EU"),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "force_destroy", "true"),
),
@ -116,6 +120,8 @@ func TestAccStorageBucket_lifecycleRules(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "lifecycle_rule.#", "2"),
resource.TestCheckResourceAttr(
@ -164,6 +170,8 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
),
@ -173,6 +181,8 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
),
@ -182,6 +192,8 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
resource.TestCheckResourceAttr(
@ -223,6 +235,8 @@ func TestAccStorageBucket_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "location", "US"),
resource.TestCheckResourceAttr(
@ -234,6 +248,8 @@ func TestAccStorageBucket_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "location", "EU"),
resource.TestCheckResourceAttr(
@ -245,6 +261,8 @@ func TestAccStorageBucket_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageBucketExists(
"google_storage_bucket.bucket", bucketName, &bucket),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "location", "EU"),
resource.TestCheckResourceAttr(