Skip to content

Commit

Permalink
Merge pull request #2090 from terraform-providers/paddy_1292
Browse files Browse the repository at this point in the history
Check for project deletion when refreshing/deleting project_service.
  • Loading branch information
paddycarver authored Sep 26, 2018
2 parents 8c0bc93 + 791c508 commit a395142
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
20 changes: 20 additions & 0 deletions google/resource_google_project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ func resourceGoogleProjectServiceRead(d *schema.ResourceData, meta interface{})
return err
}

project, err := config.clientResourceManager.Projects.Get(id.project).Do()
if err != nil {
return handleNotFoundError(err, d, id.project)
}
if project.LifecycleState == "DELETE_REQUESTED" {
log.Printf("[WARN] Removing %s from state, its project is deleted", id.terraformId())
d.SetId("")
return nil
}

services, err := getApiServices(id.project, config, map[string]struct{}{})
if err != nil {
return err
Expand Down Expand Up @@ -100,6 +110,16 @@ func resourceGoogleProjectServiceDelete(d *schema.ResourceData, meta interface{}
return err
}

project, err := config.clientResourceManager.Projects.Get(id.project).Do()
if err != nil {
return handleNotFoundError(err, d, id.project)
}
if project.LifecycleState == "DELETE_REQUESTED" {
log.Printf("[WARN] Removing %s from state, its project is deleted", id.terraformId())
d.SetId("")
return nil
}

if err = disableService(id.service, id.project, config); err != nil {
return fmt.Errorf("Error disabling service: %s", err)
}
Expand Down
55 changes: 55 additions & 0 deletions google/resource_google_project_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ func TestAccProjectService_basic(t *testing.T) {
})
}

func TestAccProjectService_handleNotFound(t *testing.T) {
t.Parallel()

org := getTestOrgFromEnv(t)
pid := "terraform-" + acctest.RandString(10)
service := "iam.googleapis.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccProjectService_handleNotFound(service, pid, pname, org),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectService([]string{service}, pid, true),
),
},
// Delete the project, implicitly deletes service, expect the plan to want to create the service again
resource.TestStep{
Config: testAccProjectService_handleNotFoundNoProject(service, pid),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckProjectService(services []string, pid string, expectEnabled bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
Expand Down Expand Up @@ -132,3 +157,33 @@ resource "google_project_service" "test2" {
}
`, pid, name, org, services[0], services[1])
}

func testAccProjectService_handleNotFound(service, pid, name, org string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
project_id = "%s"
name = "%s"
org_id = "%s"
}
// by passing through locals, we break the dependency chain
// see terraform-provider-google#1292
locals {
project_id = "${google_project.acceptance.project_id}"
}
resource "google_project_service" "test" {
project = "${local.project_id}"
service = "%s"
}
`, pid, name, org, service)
}

func testAccProjectService_handleNotFoundNoProject(service, pid string) string {
return fmt.Sprintf(`
resource "google_project_service" "test" {
project = "%s"
service = "%s"
}
`, pid, service)
}

0 comments on commit a395142

Please sign in to comment.