Skip to content

Commit

Permalink
adding additional check for default currency request (#374)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Dotson <[email protected]>
  • Loading branch information
mawasile and mattdot authored Jul 24, 2024
1 parent a89f1af commit f9a895e
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 3 deletions.
Binary file not shown.
2 changes: 2 additions & 0 deletions internal/powerplatform/api/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func NewApiClientBase(config *config.ProviderConfig, baseAuth *Auth) *ApiClient
}

func TryGetScopeFromURL(url string, cloudConfig config.ProviderConfigUrls) (string, error) {


switch {
case strings.LastIndex(url, cloudConfig.BapiUrl) != -1,
strings.LastIndex(url, cloudConfig.PowerAppsUrl) != -1:
Expand Down
1 change: 1 addition & 0 deletions internal/powerplatform/helpers/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
ERROR_OBJECT_NOT_FOUND ErrorCode = "OBJECT_NOT_FOUND"
ERROR_UNEXPECTED_HTTP_RETURN_CODE ErrorCode = "UNEXPECTED_HTTP_RETURN_CODE"
ERROR_INCORRECT_URL_FORMAT ErrorCode = "INCORRECT_URL_FORMAT"
ERROR_ENVIRONMENT_URL_NOT_FOUND ErrorCode = "ENVIRONMENT_URL_NOT_FOUND"
)

type providerError struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/powerplatform/services/authorization/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ func (client *UserClient) GetEnvironmentUrlById(ctx context.Context, environment
return "", err
}
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
if environmentUrl == "" {
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
}
return environmentUrl, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (client *DataRecordClient) GetEnvironmentUrlById(ctx context.Context, envir
return "", err
}
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
if environmentUrl == "" {
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
}
return environmentUrl, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func (client *EnvironmentClient) GetEnvironmentUrlById(ctx context.Context, envi
return "", err
}
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
if environmentUrl == "" {
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
}
return environmentUrl, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
)

var (
Expand Down Expand Up @@ -196,10 +197,13 @@ func (d *EnvironmentsDataSource) Read(ctx context.Context, req datasource.ReadRe
currencyCode := ""
defaultCurrency, err := d.EnvironmentClient.GetDefaultCurrencyForEnvironment(ctx, env.Name)
if err != nil {
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", env.Name), err.Error())
if helpers.Code(err) != helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND {
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", env.Name), err.Error())
}
} else {
currencyCode = defaultCurrency.IsoCurrencyCode
}

env, err := ConvertSourceModelFromEnvironmentDto(env, &currencyCode, nil, nil)
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Error when converting environment %s", env.DisplayName), err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"

api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
powerplatform_helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
powerplatform_modifiers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/modifiers"
licensing "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/services/licensing"
Expand Down Expand Up @@ -298,7 +299,10 @@ func (r *EnvironmentResource) Read(ctx context.Context, req resource.ReadRequest
currencyCode := ""
defaultCurrency, err := r.EnvironmentClient.GetDefaultCurrencyForEnvironment(ctx, envDto.Name)
if err != nil {
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", envDto.Name), err.Error())
if helpers.Code(err) != helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND {
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", envDto.Name), err.Error())
}

if !state.Dataverse.IsNull() && !state.Dataverse.IsUnknown() {
var dataverseSourceModel DataverseSourceModel
state.Dataverse.As(ctx, &dataverseSourceModel, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
powerplatform_helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
)

func NewEnvironmentSettingsClient(api *api.ApiClient) EnvironmentSettingsClient {
Expand All @@ -32,7 +33,6 @@ func (client *EnvironmentSettingsClient) DataverseExists(ctx context.Context, en
return env.Properties.LinkedEnvironmentMetadata.InstanceURL != "", nil
}


func (client *EnvironmentSettingsClient) GetEnvironmentSettings(ctx context.Context, environmentId string) (*EnvironmentSettingsDto, error) {
environmentUrl, err := client.GetEnvironmentUrlById(ctx, environmentId)
if err != nil {
Expand Down Expand Up @@ -84,6 +84,9 @@ func (client *EnvironmentSettingsClient) GetEnvironmentUrlById(ctx context.Conte
return "", err
}
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
if environmentUrl == "" {
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
}
return environmentUrl, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/powerplatform/services/solution/api_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ func (client *SolutionClient) GetEnvironmentUrlById(ctx context.Context, environ
return "", err
}
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
if environmentUrl == "" {
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
}
return environmentUrl, nil
}

Expand Down

0 comments on commit f9a895e

Please sign in to comment.