mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-04 17:51:11 +00:00
Only check project if it's not present on the resource. (#1125)
This commit is contained in:
parent
e1d5543362
commit
151e83aa26
@ -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)))
|
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
|
// 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
|
// 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
|
// project number, to get the project ID - there's no project ID field in the
|
||||||
// resource response.
|
// resource response. However, this requires a call to the Compute API, which
|
||||||
log.Println("[TRACE] Fetching project ID.")
|
// 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()
|
proj, err := config.clientCompute.Projects.Get(strconv.FormatUint(res.ProjectNumber, 10)).Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Printf("[DEBUG] Bucket %v is in project number %v, which is project ID %s.\n", res.Name, res.ProjectNumber, proj.Name)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
// Update the bucket ID according to the resource ID
|
// Update the bucket ID according to the resource ID
|
||||||
d.Set("self_link", res.SelfLink)
|
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("versioning", flattenBucketVersioning(res.Versioning))
|
||||||
d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle))
|
d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle))
|
||||||
d.Set("labels", res.Labels)
|
d.Set("labels", res.Labels)
|
||||||
d.Set("project", proj.Name)
|
|
||||||
d.SetId(res.Id)
|
d.SetId(res.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,8 @@ func TestAccStorageBucket_basic(t *testing.T) {
|
|||||||
"google_storage_bucket.bucket", "location", "US"),
|
"google_storage_bucket.bucket", "location", "US"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "force_destroy", "false"),
|
"google_storage_bucket.bucket", "force_destroy", "false"),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
@ -86,6 +88,8 @@ func TestAccStorageBucket_customAttributes(t *testing.T) {
|
|||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "EU"),
|
"google_storage_bucket.bucket", "location", "EU"),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "force_destroy", "true"),
|
"google_storage_bucket.bucket", "force_destroy", "true"),
|
||||||
),
|
),
|
||||||
@ -116,6 +120,8 @@ func TestAccStorageBucket_lifecycleRules(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "lifecycle_rule.#", "2"),
|
"google_storage_bucket.bucket", "lifecycle_rule.#", "2"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
@ -164,6 +170,8 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
|
"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
|
||||||
),
|
),
|
||||||
@ -173,6 +181,8 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
|
"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
|
||||||
),
|
),
|
||||||
@ -182,6 +192,8 @@ func TestAccStorageBucket_storageClass(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
|
"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
@ -223,6 +235,8 @@ func TestAccStorageBucket_update(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "US"),
|
"google_storage_bucket.bucket", "location", "US"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
@ -234,6 +248,8 @@ func TestAccStorageBucket_update(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "EU"),
|
"google_storage_bucket.bucket", "location", "EU"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
@ -245,6 +261,8 @@ func TestAccStorageBucket_update(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName, &bucket),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"google_storage_bucket.bucket", "project", getTestProjectFromEnv()),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "EU"),
|
"google_storage_bucket.bucket", "location", "EU"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
Loading…
Reference in New Issue
Block a user