-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle resources deleted outside TF provider #97
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I wonder if we should rather just say "The resource class (%s) does not exist"?
How do we know the resource actually ever existed? espescially if someone is using terraform state importing to add a resource they created by hand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case here is that the resource is in the TF state but we can't read it because we don't find it anymore.
I think it would be helpful to see this message in that case, no?