-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle resources deleted outside TF provider
- Loading branch information
Showing
17 changed files
with
469 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/humanitec/humanitec-go-autogen" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAccResourceAccountResource(t *testing.T) { | ||
id := fmt.Sprintf("gcp-test-%d", time.Now().UnixNano()) | ||
email := fmt.Sprintf("gpc-myemail-%[email protected]", time.Now().UnixNano()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: testAccResourceAccountResource(id, "gcp-test-1"), | ||
Config: testAccResourceAccountResource(id, "gcp-test-1", email), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("humanitec_resource_account.gcp_test", "id", id), | ||
resource.TestCheckResourceAttr("humanitec_resource_account.gcp_test", "name", "gcp-test-1"), | ||
|
@@ -32,7 +38,7 @@ func TestAccResourceAccountResource(t *testing.T) { | |
}, | ||
// Update and Read testing | ||
{ | ||
Config: testAccResourceAccountResource(id, "gcp-test-2"), | ||
Config: testAccResourceAccountResource(id, "gcp-test-2", email), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("humanitec_resource_account.gcp_test", "name", "gcp-test-2"), | ||
), | ||
|
@@ -42,13 +48,65 @@ func TestAccResourceAccountResource(t *testing.T) { | |
}) | ||
} | ||
|
||
func testAccResourceAccountResource(id, name string) string { | ||
func TestAccResourceAccountResource_DeletedManually(t *testing.T) { | ||
assert := assert.New(t) | ||
ctx := context.Background() | ||
id := fmt.Sprintf("gcp-test-%d", time.Now().UnixNano()) | ||
email := fmt.Sprintf("gpc-myemail-%[email protected]", time.Now().UnixNano()) | ||
|
||
orgID := os.Getenv("HUMANITEC_ORG") | ||
token := os.Getenv("HUMANITEC_TOKEN") | ||
apiHost := os.Getenv("HUMANITEC_HOST") | ||
if apiHost == "" { | ||
apiHost = humanitec.DefaultAPIHost | ||
} | ||
|
||
var client *humanitec.Client | ||
var err error | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
|
||
client, err = NewHumanitecClient(apiHost, token, "test", nil) | ||
assert.NoError(err) | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccResourceAccountResource(id, "gcp-test-2", email), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("humanitec_resource_account.gcp_test", "id", id), | ||
func(_ *terraform.State) error { | ||
// Manually delete the resource account via the API | ||
resp, err := client.DeleteResourceAccountWithResponse(ctx, orgID, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode() != 204 { | ||
return fmt.Errorf("expected status code 204, got %d, body: %s", resp.StatusCode(), string(resp.Body)) | ||
} | ||
|
||
return nil | ||
}, | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccResourceAccountResource(id, name, email string) string { | ||
return fmt.Sprintf(` | ||
resource "humanitec_resource_account" "gcp_test" { | ||
id = "%s" | ||
name = "%s" | ||
type = "gcp" | ||
credentials = "{}" | ||
credentials = jsonencode({ | ||
client_email = "%s" | ||
private_key = "mykey" | ||
}) | ||
} | ||
`, id, name) | ||
`, id, name, email) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.