Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cw scrape job: fix modifyplan when creating a new resource #1804

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()))
}
Expand Down
18 changes: 18 additions & 0 deletions internal/resources/cloudprovider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/provider/configure_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading