Skip to content

Commit c09b302

Browse files
fix: derive the project attribute from resource or compute api or fallback to provider (#12305)
Co-authored-by: Chris Hawk <[email protected]>
1 parent e7cf93b commit c09b302

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

mmv1/third_party/terraform/services/storage/resource_storage_bucket.go.tmpl

+7-9
Original file line numberDiff line numberDiff line change
@@ -1845,18 +1845,16 @@ func setStorageBucket(d *schema.ResourceData, config *transport_tpg.Config, res
18451845
// block, or the resource or an environment variable, we use the compute API to lookup the projectID
18461846
// from the projectNumber which is included in the bucket API response
18471847
if d.Get("project") == "" {
1848-
project, _ := tpgresource.GetProject(d, config)
1849-
if err := d.Set("project", project); err != nil {
1850-
return fmt.Errorf("Error setting project: %s", err)
1851-
}
1852-
}
1853-
if d.Get("project") == "" {
1848+
projectName, _ := tpgresource.GetProject(d, config)
18541849
proj, err := config.NewComputeClient(userAgent).Projects.Get(strconv.FormatUint(res.ProjectNumber, 10)).Do()
18551850
if err != nil {
1856-
return err
1851+
log.Printf("[ERROR] Missing Compute API permissions, fallback to provider/resource default")
1852+
}
1853+
1854+
if proj != nil && projectName != "" && projectName != proj.Name {
1855+
projectName = proj.Name
18571856
}
1858-
log.Printf("[DEBUG] Bucket %v is in project number %v, which is project ID %s.\n", res.Name, res.ProjectNumber, proj.Name)
1859-
if err := d.Set("project", proj.Name); err != nil {
1857+
if err := d.Set("project", projectName); err != nil {
18601858
return fmt.Errorf("Error setting project: %s", err)
18611859
}
18621860
}

mmv1/third_party/terraform/website/docs/d/storage_bucket.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The following arguments are supported:
2626

2727
* `name` - (Required) The name of the bucket.
2828

29-
* `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. If no value is supplied in the configuration or through provider defaults then the data source will use the Compute API to find the project id that corresponds to the project number returned from the Storage API. Supplying a value for `project` doesn't influence retrieving data about the bucket but it can be used to prevent use of the Compute API. If you do provide a `project` value ensure that it is the correct value for that bucket; the data source will not check that the project id and project number match.
29+
* `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided then the data source will use the Compute API to find the project id that corresponds to the project number returned from the Storage API, and if no Compute API permissions are available or if the Compute API is disabled it defaults to the provider value. Supplying a value for `project` doesn't influence retrieving data about the bucket but it can be used to prevent use of the Compute API. If you do provide a `project` value ensure that it is the correct value for that bucket; the data source will not check that the project id and project number match.
3030

3131
## Attributes Reference
3232

0 commit comments

Comments
 (0)