-
Notifications
You must be signed in to change notification settings - Fork 13
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: env0_gcp_credentials - apply after import will delete+create #380
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,6 +1,8 @@ | ||
package env0 | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
|
@@ -14,6 +16,7 @@ func TestUnitGcpCredentialsResource(t *testing.T) { | |
|
||
resourceType := "env0_gcp_credentials" | ||
resourceName := "test" | ||
resourceNameImport := resourceType + "." + resourceName | ||
accessor := resourceAccessor(resourceType, resourceName) | ||
|
||
gcpCredentialResource := map[string]interface{}{ | ||
|
@@ -23,7 +26,7 @@ func TestUnitGcpCredentialsResource(t *testing.T) { | |
} | ||
|
||
updateGcpCredentialResource := map[string]interface{}{ | ||
"name": "testUpdate", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug in test. |
||
"name": "update", | ||
"project_id": "333_update", | ||
"service_account_key": "444_update", | ||
} | ||
|
@@ -47,14 +50,21 @@ func TestUnitGcpCredentialsResource(t *testing.T) { | |
} | ||
|
||
returnValues := client.Credentials{ | ||
Id: "id", | ||
Id: "f595c4b6-0a24-4c22-89f7-7030045de300", | ||
Name: "test", | ||
OrganizationId: "id", | ||
Type: string(client.GcpServiceAccountCredentialsType), | ||
} | ||
|
||
otherTypeReturnValues := client.Credentials{ | ||
Id: "f595c4b6-0a24-4c22-89f7-7030045de30a", | ||
Name: "test", | ||
OrganizationId: "id", | ||
Type: "AZURE_....", | ||
} | ||
|
||
updateReturnValues := client.Credentials{ | ||
Id: "id2", | ||
Id: "f595c4b6-0a24-4c22-89f7-7030045de311", | ||
Name: "update", | ||
OrganizationId: "id", | ||
Type: string(client.GcpServiceAccountCredentialsType), | ||
|
@@ -165,4 +175,97 @@ func TestUnitGcpCredentialsResource(t *testing.T) { | |
) | ||
}) | ||
}) | ||
|
||
t.Run("import by name", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, gcpCredentialResource), | ||
}, | ||
{ | ||
ResourceName: resourceNameImport, | ||
ImportState: true, | ||
ImportStateId: gcpCredentialResource["name"].(string), | ||
ImportStateVerify: false, | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().GcpCredentialsCreate(gcpCredCreatePayload).Times(1).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentials(returnValues.Id).Times(2).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentialsList().Times(1).Return([]client.Credentials{otherTypeReturnValues, returnValues}, nil) | ||
mock.EXPECT().CloudCredentialsDelete(returnValues.Id).Times(1).Return(nil) | ||
}) | ||
}) | ||
|
||
t.Run("import by id", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, gcpCredentialResource), | ||
}, | ||
{ | ||
ResourceName: resourceNameImport, | ||
ImportState: true, | ||
ImportStateId: returnValues.Id, | ||
ImportStateVerify: false, | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().GcpCredentialsCreate(gcpCredCreatePayload).Times(1).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentials(returnValues.Id).Times(3).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentialsDelete(returnValues.Id).Times(1).Return(nil) | ||
}) | ||
}) | ||
|
||
t.Run("import by id not found", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, gcpCredentialResource), | ||
}, | ||
{ | ||
ResourceName: resourceNameImport, | ||
ImportState: true, | ||
ImportStateId: otherTypeReturnValues.Id, | ||
ImportStateVerify: true, | ||
ExpectError: regexp.MustCompile(fmt.Sprintf("gcp credentials resource with id %v not found", otherTypeReturnValues.Id)), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().GcpCredentialsCreate(gcpCredCreatePayload).Times(1).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentials(returnValues.Id).Times(1).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentials(otherTypeReturnValues.Id).Times(1).Return(client.Credentials{}, &client.NotFoundError{}) | ||
mock.EXPECT().CloudCredentialsDelete(returnValues.Id).Times(1).Return(nil) | ||
}) | ||
}) | ||
|
||
t.Run("import by name not found", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, gcpCredentialResource), | ||
}, | ||
{ | ||
ResourceName: resourceNameImport, | ||
ImportState: true, | ||
ImportStateId: gcpCredentialResource["name"].(string), | ||
ImportStateVerify: true, | ||
ExpectError: regexp.MustCompile(fmt.Sprintf("credentials with name %v not found", gcpCredentialResource["name"].(string))), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().GcpCredentialsCreate(gcpCredCreatePayload).Times(1).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentials(returnValues.Id).Times(1).Return(returnValues, nil) | ||
mock.EXPECT().CloudCredentialsList().Times(1).Return([]client.Credentials{otherTypeReturnValues}, nil) | ||
mock.EXPECT().CloudCredentialsDelete(returnValues.Id).Times(1).Return(nil) | ||
}) | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
terraform import env0_gcp_credentials.by_id d31a6b30-5f69-4d24-937c-22322754934e | ||
terraform import env0_gcp_credentials.by_name "credentials name" |
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.
Just asking, what changed that we now need it than before?
Is
import
flow will not work without it?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.
@razbensimon ,
The writeResourceData (and readReasourceData) is a utility function that reads all the fields in the interface and updates the schema via
set
calls.This is future proof: in case you add more fields to the schema/api - no additional coding is needed (other than updating the schema).
There is a potential bug here that drifting is not detected (E.g., if someone changes the name in the UX). Once I added this code, it caught a bug in the tests as well (so I fixed the tests as well).
Import flow will work without it, and I just fixed bugs I encountered on the way.