diff --git a/examples/data-sources/powerplatform_environments/cert.pkcs12 b/examples/data-sources/powerplatform_environments/cert.pkcs12 deleted file mode 100644 index a70a7085..00000000 Binary files a/examples/data-sources/powerplatform_environments/cert.pkcs12 and /dev/null differ diff --git a/internal/powerplatform/api/api_client.go b/internal/powerplatform/api/api_client.go index 898aa36c..fe31a894 100644 --- a/internal/powerplatform/api/api_client.go +++ b/internal/powerplatform/api/api_client.go @@ -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: diff --git a/internal/powerplatform/helpers/error.go b/internal/powerplatform/helpers/error.go index 3ccacb29..7dd7aa9d 100644 --- a/internal/powerplatform/helpers/error.go +++ b/internal/powerplatform/helpers/error.go @@ -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 { diff --git a/internal/powerplatform/services/authorization/api_user.go b/internal/powerplatform/services/authorization/api_user.go index 083bb108..a6b4b155 100644 --- a/internal/powerplatform/services/authorization/api_user.go +++ b/internal/powerplatform/services/authorization/api_user.go @@ -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 } diff --git a/internal/powerplatform/services/data_record/api_data_record.go b/internal/powerplatform/services/data_record/api_data_record.go index 8a1eecfa..5c0a42b7 100644 --- a/internal/powerplatform/services/data_record/api_data_record.go +++ b/internal/powerplatform/services/data_record/api_data_record.go @@ -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 } diff --git a/internal/powerplatform/services/environment/api_environment.go b/internal/powerplatform/services/environment/api_environment.go index 64602702..9342570a 100644 --- a/internal/powerplatform/services/environment/api_environment.go +++ b/internal/powerplatform/services/environment/api_environment.go @@ -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 } diff --git a/internal/powerplatform/services/environment/datasource_environments.go b/internal/powerplatform/services/environment/datasource_environments.go index c293161d..ceffdfbe 100644 --- a/internal/powerplatform/services/environment/datasource_environments.go +++ b/internal/powerplatform/services/environment/datasource_environments.go @@ -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 ( @@ -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, ¤cyCode, nil, nil) if err != nil { resp.Diagnostics.AddError(fmt.Sprintf("Error when converting environment %s", env.DisplayName), err.Error()) diff --git a/internal/powerplatform/services/environment/resource_environment.go b/internal/powerplatform/services/environment/resource_environment.go index 9c765df6..9d99ad37 100644 --- a/internal/powerplatform/services/environment/resource_environment.go +++ b/internal/powerplatform/services/environment/resource_environment.go @@ -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" @@ -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}) diff --git a/internal/powerplatform/services/environment_settings/api_environment_settings.go b/internal/powerplatform/services/environment_settings/api_environment_settings.go index 57ad7038..2bdb6eab 100644 --- a/internal/powerplatform/services/environment_settings/api_environment_settings.go +++ b/internal/powerplatform/services/environment_settings/api_environment_settings.go @@ -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 { @@ -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 { @@ -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 } diff --git a/internal/powerplatform/services/solution/api_solution.go b/internal/powerplatform/services/solution/api_solution.go index 7fa71c87..f2e192d6 100644 --- a/internal/powerplatform/services/solution/api_solution.go +++ b/internal/powerplatform/services/solution/api_solution.go @@ -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 }