Skip to content
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_aws_credentials - apply after import will delete+create #371

Merged
merged 2 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Check [resource_module.go](./env0/resource_module.go) that uses the utilities vs

Pay attention to the following caveats:
* The utilities leverage golang reflection. And work well for most simple types. Complex types may need additional code to be implemented.
* The golang fields are in CamalCase, while the terraform fields are in snake_case. They must match. E.g., ProjectName (golang) == project_name (Terraform).
* The golang fields are in CamalCase, while the terraform fields are in snake_case. They must match. E.g., ProjectName (golang) == project_name (Terraform). To override the default CamalCase to snake_case conversion you may use the tag `tfschema`.

### Handling drifts

Expand Down
8 changes: 2 additions & 6 deletions client/cloud_credentials.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package client

import (
"fmt"
)

type AwsCredentialsType string
type GcpCredentialsType string
type AzureCredentialsType string
Expand Down Expand Up @@ -37,7 +33,7 @@ type AwsCredentialsCreatePayload struct {
}

type AwsCredentialsValuePayload struct {
RoleArn string `json:"roleArn"`
RoleArn string `json:"roleArn" tfschema:"arn"`
ExternalId string `json:"externalId"`
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Expand Down Expand Up @@ -89,7 +85,7 @@ func (client *ApiClient) CloudCredentials(id string) (Credentials, error) {
}
}

return Credentials{}, fmt.Errorf("CloudCredentials: [%s] not found ", id)
return Credentials{}, &NotFoundError{}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to handle drift later.

}

func (client *ApiClient) CloudCredentialsList() ([]Credentials, error) {
Expand Down
7 changes: 7 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

type NotFoundError struct{}

func (e *NotFoundError) Error() string {
return "not found"
}
47 changes: 47 additions & 0 deletions env0/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package env0

import (
"fmt"
"log"
"strings"

"github.com/env0/terraform-provider-env0/client"
"github.com/google/uuid"
)

func getCredentialsByName(name string, prefix string, meta interface{}) (client.Credentials, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common helper functions for credentials (can be used by GCP and Azure in the future). (In case import is added to them as well).

apiClient := meta.(client.ApiClientInterface)

credentialsList, err := apiClient.CloudCredentialsList()
if err != nil {
return client.Credentials{}, err
}

var foundCredentials []client.Credentials
for _, credentials := range credentialsList {
if credentials.Name == name && strings.HasPrefix(credentials.Type, prefix) {
foundCredentials = append(foundCredentials, credentials)
}
}

if len(foundCredentials) == 0 {
return client.Credentials{}, fmt.Errorf("credentials with name %v not found", name)
}

if len(foundCredentials) > 1 {
return client.Credentials{}, fmt.Errorf("found multiple credentials with name: %s. Use id instead or make sure credential names are unique %v", name, foundCredentials)
}

return foundCredentials[0], nil
}

func getCredentials(id string, prefix string, meta interface{}) (client.Credentials, error) {
_, err := uuid.Parse(id)
if err == nil {
log.Println("[INFO] Resolving credentials by id: ", id)
return meta.(client.ApiClientInterface).CloudCredentials(id)
} else {
log.Println("[INFO] Resolving credentials by name: ", id)
return getCredentialsByName(id, prefix, meta)
}
}
2 changes: 1 addition & 1 deletion env0/data_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func dataNotificationRead(ctx context.Context, d *schema.ResourceData, meta inte
}

if err := writeResourceData(notification, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
return diag.Errorf("schema resource data serialization failed: %v", err)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a few places with the same bug...

}

return nil
Expand Down
15 changes: 14 additions & 1 deletion env0/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ package env0
import (
"log"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceGetFailure(resourceName string, d *schema.ResourceData, err error) diag.Diagnostics {
func driftDetected(d *schema.ResourceData, err error) bool {
if frerr, ok := err.(*http.FailedResponseError); ok && frerr.NotFound() {
return true
}

if _, ok := err.(*client.NotFoundError); ok {
return true
}

return false
}

func ResourceGetFailure(resourceName string, d *schema.ResourceData, err error) diag.Diagnostics {
if driftDetected(d, err) {
log.Printf("[WARN] Drift Detected: Terraform will remove %s from state", d.Id())
d.SetId("")
return nil
Expand Down
4 changes: 2 additions & 2 deletions env0/resource_agent_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func resourceAgentProjectAssignmentRead(ctx context.Context, d *schema.ResourceD
}

if err := writeResourceData(assignment, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
return diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
Expand Down Expand Up @@ -189,7 +189,7 @@ func resourceAgentProjectAssignmentImport(ctx context.Context, d *schema.Resourc
}

if err := writeResourceData(assignment, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
return nil, fmt.Errorf("schema resource data serialization failed: %v", err)
}

return []*schema.ResourceData{d}, nil
Expand Down
4 changes: 2 additions & 2 deletions env0/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func resourceApiKeyRead(ctx context.Context, d *schema.ResourceData, meta interf
}

if err := writeResourceData(apiKey, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
return diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
Expand Down Expand Up @@ -138,7 +138,7 @@ func resourceApiKeyImport(ctx context.Context, d *schema.ResourceData, meta inte
}

if err := writeResourceData(apiKey, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
return nil, fmt.Errorf("schema resource data serialization failed: %v", err)
}

return []*schema.ResourceData{d}, nil
Expand Down
56 changes: 40 additions & 16 deletions env0/resource_aws_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env0

import (
"context"
"fmt"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -14,6 +15,8 @@ func resourceAwsCredentials() *schema.Resource {
ReadContext: resourceAwsCredentialsRead,
DeleteContext: resourceAwsCredentialsDelete,

Importer: &schema.ResourceImporter{StateContext: resourceAwsCredentialsImport},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomerHeber if we're already at it, can we perhaps also support import for all other credentials type? I'd do it in a separate PR tho

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping the issue open.


Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -27,7 +30,6 @@ func resourceAwsCredentials() *schema.Resource {
Optional: true,
ForceNew: true,
ConflictsWith: []string{"access_key_id"},
ExactlyOneOf: []string{"access_key_id"},
Copy link
Collaborator Author

@TomerHeber TomerHeber May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "ExactlyOneOf" cannot be enforced in the schema level. (Will fail on import).
(Enforced in the code level).

},
"external_id": {
Type: schema.TypeString,
Expand All @@ -46,7 +48,6 @@ func resourceAwsCredentials() *schema.Resource {
ForceNew: true,
ConflictsWith: []string{"arn", "external_id"},
RequiredWith: []string{"secret_access_key"},
ExactlyOneOf: []string{"arn"},
},
"secret_access_key": {
Type: schema.TypeString,
Expand All @@ -62,29 +63,32 @@ func resourceAwsCredentials() *schema.Resource {
}

func resourceAwsCredentialsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
_, accessKeyExist := d.GetOk("access_key_id")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enforcing the exactlyOnce in the code level only for creation (not for read/import).

_, arnExist := d.GetOk("arn")
if !accessKeyExist && !arnExist {
// Due to "import" must be inforced here and not in the schema level.
// This fields are only available during creation (will not be returned in read or import).
return diag.Errorf("one of `access_key_id,arn` must be specified")
}

apiClient := meta.(client.ApiClientInterface)

value := client.AwsCredentialsValuePayload{}
requestType := client.AwsAssumedRoleCredentialsType
if arn, ok := d.GetOk("arn"); ok {
value.RoleArn = arn.(string)
requestType = client.AwsAssumedRoleCredentialsType
}
if externalId, ok := d.GetOk("external_id"); ok {
value.ExternalId = externalId.(string)
if err := readResourceData(&value, d); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some small refactoring (less lines of code).

return diag.Errorf("schema resource data deserialization failed: %v", err)
}
if accessKeyId, ok := d.GetOk("access_key_id"); ok {
value.AccessKeyId = accessKeyId.(string)

requestType := client.AwsAssumedRoleCredentialsType
if _, ok := d.GetOk("access_key_id"); ok {
requestType = client.AwsAccessKeysCredentialsType
}
if secretAccessKey, ok := d.GetOk("secret_access_key"); ok {
value.SecretAccessKey = secretAccessKey.(string)
}

request := client.AwsCredentialsCreatePayload{
Name: d.Get("name").(string),
Value: value,
Type: requestType,
}

credentials, err := apiClient.AwsCredentialsCreate(request)
if err != nil {
return diag.Errorf("could not create credentials key: %v", err)
Expand All @@ -98,11 +102,15 @@ func resourceAwsCredentialsCreate(ctx context.Context, d *schema.ResourceData, m
func resourceAwsCredentialsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

id := d.Id()
_, err := apiClient.CloudCredentials(id)
credentials, err := apiClient.CloudCredentials(d.Id())
if err != nil {
return ResourceGetFailure("aws credentials", d, err)
}

if err := writeResourceData(&credentials, d); err != nil {
return diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
}

Expand All @@ -116,3 +124,19 @@ func resourceAwsCredentialsDelete(ctx context.Context, d *schema.ResourceData, m
}
return nil
}

func resourceAwsCredentialsImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
credentials, err := getCredentials(d.Id(), "AWS_", meta)
if err != nil {
if _, ok := err.(*client.NotFoundError); ok {
return nil, fmt.Errorf("aws credentials resource with id %v not found", d.Id())
}
return nil, err
}

if err := writeResourceData(&credentials, d); err != nil {
return nil, fmt.Errorf("schema resource data serialization failed: %v", err)
}

return []*schema.ResourceData{d}, nil
}
Loading