Skip to content

Commit bfc5afc

Browse files
move deleted project check into project service read (#3350) (#1937)
* move deleted project check into project service read * compile Signed-off-by: Modular Magician <[email protected]>
1 parent 1851ad1 commit bfc5afc

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

.changelog/3350.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
* serviceusage: fixed issue where `google_project_services` attempted to read a project before enabling the API that allows that read
3+
```

google-beta/resource_google_project.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -573,22 +573,9 @@ func doEnableServicesRequest(services []string, project string, config *Config,
573573
// forms of the service. LIST responses are expected to return only the old or
574574
// new form, but we'll always return both.
575575
func listCurrentlyEnabledServices(project string, config *Config, timeout time.Duration) (map[string]struct{}, error) {
576-
// Verify project for services still exists
577-
p, err := config.clientResourceManager.Projects.Get(project).Do()
578-
if err != nil {
579-
return nil, err
580-
}
581-
if p.LifecycleState == "DELETE_REQUESTED" {
582-
// Construct a 404 error for handleNotFoundError
583-
return nil, &googleapi.Error{
584-
Code: 404,
585-
Message: "Project deletion was requested",
586-
}
587-
}
588-
589576
log.Printf("[DEBUG] Listing enabled services for project %s", project)
590577
apiServices := make(map[string]struct{})
591-
err = retryTimeDuration(func() error {
578+
err := retryTimeDuration(func() error {
592579
ctx := context.Background()
593580
return config.clientServiceUsage.Services.
594581
List(fmt.Sprintf("projects/%s", project)).

google-beta/resource_google_project_service.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
"google.golang.org/api/googleapi"
1011
"google.golang.org/api/serviceusage/v1"
1112
)
1213

@@ -154,14 +155,27 @@ func resourceGoogleProjectServiceRead(d *schema.ResourceData, meta interface{})
154155
if err != nil {
155156
return err
156157
}
157-
srv := d.Get("service").(string)
158+
159+
// Verify project for services still exists
160+
p, err := config.clientResourceManager.Projects.Get(project).Do()
161+
if err != nil {
162+
return err
163+
}
164+
if p.LifecycleState == "DELETE_REQUESTED" {
165+
// Construct a 404 error for handleNotFoundError
166+
return &googleapi.Error{
167+
Code: 404,
168+
Message: "Project deletion was requested",
169+
}
170+
}
158171

159172
servicesRaw, err := BatchRequestReadServices(project, d, config)
160173
if err != nil {
161174
return handleNotFoundError(err, d, fmt.Sprintf("Project Service %s", d.Id()))
162175
}
163176
servicesList := servicesRaw.(map[string]struct{})
164177

178+
srv := d.Get("service").(string)
165179
if _, ok := servicesList[srv]; ok {
166180
d.Set("project", project)
167181
d.Set("service", srv)

0 commit comments

Comments
 (0)