diff --git a/internal/resources/cloudprovider/resource_aws_cloudwatch_scrape_job.go b/internal/resources/cloudprovider/resource_aws_cloudwatch_scrape_job.go index 77f3f4b8e..527dc3a1f 100644 --- a/internal/resources/cloudprovider/resource_aws_cloudwatch_scrape_job.go +++ b/internal/resources/cloudprovider/resource_aws_cloudwatch_scrape_job.go @@ -310,7 +310,8 @@ func (r *resourceAWSCloudWatchScrapeJob) Read(ctx context.Context, req resource. } func (r *resourceAWSCloudWatchScrapeJob) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { - var stateData awsCWScrapeJobTFModel + // This must be a pointer because ModifyPlan is called even on resource creation, when no state exists yet. + var stateData *awsCWScrapeJobTFModel diags := req.State.Get(ctx, &stateData) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -327,10 +328,10 @@ func (r *resourceAWSCloudWatchScrapeJob) ModifyPlan(ctx context.Context, req res // This helps reduce the occurrences of Unknown states for the disabled_reason attribute // by tying it to how the enabled attribute will change. switch { - case !stateData.Enabled.ValueBool() && planData.Enabled.ValueBool(): - resp.Plan.SetAttribute(ctx, path.Root("disabled_reason"), basetypes.NewStringValue("")) - case stateData.Enabled.ValueBool() && !planData.Enabled.ValueBool(): + case (stateData == nil || stateData.Enabled.ValueBool()) && !planData.Enabled.ValueBool(): resp.Plan.SetAttribute(ctx, path.Root("disabled_reason"), basetypes.NewStringUnknown()) + case (stateData == nil || !stateData.Enabled.ValueBool()) && planData.Enabled.ValueBool(): + resp.Plan.SetAttribute(ctx, path.Root("disabled_reason"), basetypes.NewStringValue("")) default: resp.Plan.SetAttribute(ctx, path.Root("disabled_reason"), basetypes.NewStringValue(stateData.DisabledReason.ValueString())) } diff --git a/internal/resources/cloudprovider/resources.go b/internal/resources/cloudprovider/resources.go index 282358d28..92dd09c81 100644 --- a/internal/resources/cloudprovider/resources.go +++ b/internal/resources/cloudprovider/resources.go @@ -32,6 +32,15 @@ func withClientForResource(req resource.ConfigureRequest, resp *resource.Configu return nil, fmt.Errorf("unexpected Resource Configure Type: %T, expected *common.Client", req.ProviderData) } + if client.CloudProviderAPI == nil { + resp.Diagnostics.AddError( + "The Grafana Provider is missing a configuration for the Cloud Provider API.", + "Please ensure that cloud_provider_url and cloud_provider_access_token are set in the provider configuration.", + ) + + return nil, fmt.Errorf("CloudProviderAPI is nil") + } + return client.CloudProviderAPI, nil } @@ -47,5 +56,14 @@ func withClientForDataSource(req datasource.ConfigureRequest, resp *datasource.C return nil, fmt.Errorf("unexpected DataSource Configure Type: %T, expected *common.Client", req.ProviderData) } + if client.CloudProviderAPI == nil { + resp.Diagnostics.AddError( + "The Grafana Provider is missing a configuration for the Cloud Provider API.", + "Please ensure that cloud_provider_url and cloud_provider_access_token are set in the provider configuration.", + ) + + return nil, fmt.Errorf("CloudProviderAPI is nil") + } + return client.CloudProviderAPI, nil } diff --git a/pkg/provider/configure_clients.go b/pkg/provider/configure_clients.go index 686490839..bdf51c19b 100644 --- a/pkg/provider/configure_clients.go +++ b/pkg/provider/configure_clients.go @@ -59,7 +59,7 @@ func CreateClients(providerConfig ProviderConfig) (*common.Client, error) { onCallClient.UserAgent = providerConfig.UserAgent.ValueString() c.OnCallClient = onCallClient } - if !providerConfig.CloudProviderURL.IsNull() && !providerConfig.CloudProviderAccessToken.IsNull() { + if !providerConfig.CloudProviderAccessToken.IsNull() { if err := createCloudProviderClient(c, providerConfig); err != nil { return nil, err }