Skip to content

Commit

Permalink
added import and added tests. fixed errors handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Aug 5, 2024
1 parent 6858a25 commit d84aeb1
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 139 deletions.
32 changes: 29 additions & 3 deletions env0/cloud_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/env0/terraform-provider-env0/client"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -29,10 +30,35 @@ func getCloudConfigurationFromSchema(d *schema.ResourceData, provider string) (i
return configuration, nil
}

func getCloudConfigurationByNameFromApi(apiClient client.ApiClientInterface, name string) (*client.CloudAccount, error) {
cloudAccounts, err := apiClient.CloudAccounts()
if err != nil {
return nil, err
}

for i, cloudAccount := range cloudAccounts {
if cloudAccount.Name == name {
return &cloudAccounts[i], nil
}
}

return nil, fmt.Errorf("cloud configuration called '%s' was not found", name)
}

func getCloudConfigurationFromApi(apiClient client.ApiClientInterface, id string) (*client.CloudAccount, error) {
cloudAccount, err := apiClient.CloudAccount(id)
var err error
var cloudAccount *client.CloudAccount

if _, parseErr := uuid.Parse(id); parseErr != nil {
// Get by name (used by import).
cloudAccount, err = getCloudConfigurationByNameFromApi(apiClient, id)
} else {
// Get by id.
cloudAccount, err = apiClient.CloudAccount(id)
}

if err != nil {
return nil, fmt.Errorf("failed to get clound configuration with id '%s': %w", id, err)
return nil, fmt.Errorf("failed to get clound configuration: %w", err)
}

var configuration interface{}
Expand Down Expand Up @@ -152,7 +178,7 @@ func importCloudConfiguration(ctx context.Context, d *schema.ResourceData, meta

cloudAccount, err := getCloudConfigurationFromApi(apiClient, d.Id())
if err != nil {
return nil, fmt.Errorf("failed to get cloud configuration with id '%s'", d.Id())
return nil, err
}

if err := writeResourceData(cloudAccount, d); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions env0/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env0

import (
"context"
"errors"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
Expand All @@ -11,15 +12,14 @@ import (
)

func driftDetected(err error) bool {
if frerr, ok := err.(*http.FailedResponseError); ok && frerr.NotFound() {
var failedResponseError *http.FailedResponseError
if errors.As(err, &failedResponseError) && failedResponseError.NotFound() {
return true
}

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

return false
return errors.As(err, &notfoundError)
}

func ResourceGetFailure(ctx context.Context, resourceName string, d *schema.ResourceData, err error) diag.Diagnostics {
Expand Down
Loading

0 comments on commit d84aeb1

Please sign in to comment.