Skip to content

Commit f9a895e

Browse files
mawasilemattdot
andauthored
adding additional check for default currency request (#374)
Co-authored-by: Matt Dotson <mattdot@users.noreply.github.com>
1 parent a89f1af commit f9a895e

File tree

10 files changed

+29
-3
lines changed

10 files changed

+29
-3
lines changed
-4.33 KB
Binary file not shown.

internal/powerplatform/api/api_client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func NewApiClientBase(config *config.ProviderConfig, baseAuth *Auth) *ApiClient
4141
}
4242

4343
func TryGetScopeFromURL(url string, cloudConfig config.ProviderConfigUrls) (string, error) {
44+
45+
4446
switch {
4547
case strings.LastIndex(url, cloudConfig.BapiUrl) != -1,
4648
strings.LastIndex(url, cloudConfig.PowerAppsUrl) != -1:

internal/powerplatform/helpers/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
ERROR_OBJECT_NOT_FOUND ErrorCode = "OBJECT_NOT_FOUND"
1515
ERROR_UNEXPECTED_HTTP_RETURN_CODE ErrorCode = "UNEXPECTED_HTTP_RETURN_CODE"
1616
ERROR_INCORRECT_URL_FORMAT ErrorCode = "INCORRECT_URL_FORMAT"
17+
ERROR_ENVIRONMENT_URL_NOT_FOUND ErrorCode = "ENVIRONMENT_URL_NOT_FOUND"
1718
)
1819

1920
type providerError struct {

internal/powerplatform/services/authorization/api_user.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ func (client *UserClient) GetEnvironmentUrlById(ctx context.Context, environment
247247
return "", err
248248
}
249249
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
250+
if environmentUrl == "" {
251+
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
252+
}
250253
return environmentUrl, nil
251254
}
252255

internal/powerplatform/services/data_record/api_data_record.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ func (client *DataRecordClient) GetEnvironmentUrlById(ctx context.Context, envir
9090
return "", err
9191
}
9292
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
93+
if environmentUrl == "" {
94+
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
95+
}
9396
return environmentUrl, nil
9497
}
9598

internal/powerplatform/services/environment/api_environment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ func (client *EnvironmentClient) GetEnvironmentUrlById(ctx context.Context, envi
220220
return "", err
221221
}
222222
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
223+
if environmentUrl == "" {
224+
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
225+
}
223226
return environmentUrl, nil
224227
}
225228

internal/powerplatform/services/environment/datasource_environments.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/types"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
1414
api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
15+
helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
1516
)
1617

1718
var (
@@ -196,10 +197,13 @@ func (d *EnvironmentsDataSource) Read(ctx context.Context, req datasource.ReadRe
196197
currencyCode := ""
197198
defaultCurrency, err := d.EnvironmentClient.GetDefaultCurrencyForEnvironment(ctx, env.Name)
198199
if err != nil {
199-
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", env.Name), err.Error())
200+
if helpers.Code(err) != helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND {
201+
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", env.Name), err.Error())
202+
}
200203
} else {
201204
currencyCode = defaultCurrency.IsoCurrencyCode
202205
}
206+
203207
env, err := ConvertSourceModelFromEnvironmentDto(env, &currencyCode, nil, nil)
204208
if err != nil {
205209
resp.Diagnostics.AddError(fmt.Sprintf("Error when converting environment %s", env.DisplayName), err.Error())

internal/powerplatform/services/environment/resource_environment.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hashicorp/terraform-plugin-log/tflog"
2020

2121
api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
22+
helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
2223
powerplatform_helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
2324
powerplatform_modifiers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/modifiers"
2425
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
298299
currencyCode := ""
299300
defaultCurrency, err := r.EnvironmentClient.GetDefaultCurrencyForEnvironment(ctx, envDto.Name)
300301
if err != nil {
301-
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", envDto.Name), err.Error())
302+
if helpers.Code(err) != helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND {
303+
resp.Diagnostics.AddWarning(fmt.Sprintf("Error when reading default currency for environment %s", envDto.Name), err.Error())
304+
}
305+
302306
if !state.Dataverse.IsNull() && !state.Dataverse.IsUnknown() {
303307
var dataverseSourceModel DataverseSourceModel
304308
state.Dataverse.As(ctx, &dataverseSourceModel, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})

internal/powerplatform/services/environment_settings/api_environment_settings.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
api "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/api"
14+
powerplatform_helpers "github.com/microsoft/terraform-provider-power-platform/internal/powerplatform/helpers"
1415
)
1516

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

35-
3636
func (client *EnvironmentSettingsClient) GetEnvironmentSettings(ctx context.Context, environmentId string) (*EnvironmentSettingsDto, error) {
3737
environmentUrl, err := client.GetEnvironmentUrlById(ctx, environmentId)
3838
if err != nil {
@@ -84,6 +84,9 @@ func (client *EnvironmentSettingsClient) GetEnvironmentUrlById(ctx context.Conte
8484
return "", err
8585
}
8686
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
87+
if environmentUrl == "" {
88+
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
89+
}
8790
return environmentUrl, nil
8891
}
8992

internal/powerplatform/services/solution/api_solution.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ func (client *SolutionClient) GetEnvironmentUrlById(ctx context.Context, environ
287287
return "", err
288288
}
289289
environmentUrl := strings.TrimSuffix(env.Properties.LinkedEnvironmentMetadata.InstanceURL, "/")
290+
if environmentUrl == "" {
291+
return "", powerplatform_helpers.WrapIntoProviderError(nil, powerplatform_helpers.ERROR_ENVIRONMENT_URL_NOT_FOUND, "environment url not found, please check if the environment has dataverse linked")
292+
}
290293
return environmentUrl, nil
291294
}
292295

0 commit comments

Comments
 (0)