fix(STACKITTPR-168): improve error messages (#762)
* remove deprecated argus resources * improve error messages
This commit is contained in:
parent
1c02c5eb67
commit
d6749b6ce3
76 changed files with 600 additions and 8403 deletions
|
|
@ -15,10 +15,9 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/observability"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
argusCredentialResource "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/argus/credential"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
||||
|
|
@ -26,7 +25,6 @@ import (
|
|||
var (
|
||||
_ resource.Resource = &credentialResource{}
|
||||
_ resource.ResourceWithConfigure = &credentialResource{}
|
||||
_ resource.ResourceWithMoveState = &credentialResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
|
|
@ -88,40 +86,6 @@ func (r *credentialResource) Configure(ctx context.Context, req resource.Configu
|
|||
tflog.Info(ctx, "Observability credential client configured")
|
||||
}
|
||||
|
||||
func (r *credentialResource) MoveState(_ context.Context) []resource.StateMover {
|
||||
return []resource.StateMover{
|
||||
{
|
||||
SourceSchema: &argusCredentialResource.Schema,
|
||||
StateMover: func(ctx context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
|
||||
if req.SourceTypeName != "stackit_argus_credential" {
|
||||
return
|
||||
}
|
||||
|
||||
// Checks source provider
|
||||
if !strings.HasSuffix(req.SourceProviderAddress, "stackitcloud/stackit") {
|
||||
return
|
||||
}
|
||||
|
||||
var sourceStateData argusCredentialResource.Model
|
||||
resp.Diagnostics.Append(req.SourceState.Get(ctx, &sourceStateData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
targetStateData := Model{
|
||||
Id: sourceStateData.Id,
|
||||
ProjectId: sourceStateData.ProjectId,
|
||||
InstanceId: sourceStateData.InstanceId,
|
||||
Username: sourceStateData.Username,
|
||||
Password: sourceStateData.Password,
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.TargetState.Set(ctx, targetStateData)...)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Observability credential resource schema. Must have a `region` specified in the provider configuration.",
|
||||
|
|
@ -246,12 +210,17 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
|
|||
userName := model.Username.ValueString()
|
||||
_, err := r.client.GetCredentials(ctx, instanceId, projectId, userName).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading credential",
|
||||
fmt.Sprintf("Credential with username %q or instance with ID %q does not exist in project %q.", userName, instanceId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
diags = resp.State.Set(ctx, model)
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/observability"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/observability/wait"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
||||
|
|
@ -384,11 +384,17 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
|
|||
instanceId := model.InstanceId.ValueString()
|
||||
instanceResp, err := d.client.GetInstance(ctx, instanceId, projectId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading instance",
|
||||
fmt.Sprintf("Instance with ID %q does not exist in project %q.", instanceId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
if instanceResp != nil && instanceResp.Status != nil && *instanceResp.Status == wait.DeleteSuccess {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ import (
|
|||
"github.com/stackitcloud/stackit-sdk-go/services/observability/wait"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
argusInstanceResource "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/argus/instance"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
||||
|
|
@ -44,7 +43,6 @@ var (
|
|||
_ resource.Resource = &instanceResource{}
|
||||
_ resource.ResourceWithConfigure = &instanceResource{}
|
||||
_ resource.ResourceWithImportState = &instanceResource{}
|
||||
_ resource.ResourceWithMoveState = &instanceResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
|
|
@ -377,64 +375,6 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure
|
|||
tflog.Info(ctx, "Observability instance client configured")
|
||||
}
|
||||
|
||||
// MoveState moves the state of a `stackit_argus_instance` resource to a `stackit_observability_instance` resource.
|
||||
func (r *instanceResource) MoveState(_ context.Context) []resource.StateMover {
|
||||
return []resource.StateMover{
|
||||
{
|
||||
SourceSchema: &argusInstanceResource.Schema,
|
||||
StateMover: func(ctx context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
|
||||
if req.SourceTypeName != "stackit_argus_instance" {
|
||||
return
|
||||
}
|
||||
|
||||
// Checks source provider
|
||||
if !strings.HasSuffix(req.SourceProviderAddress, "stackitcloud/stackit") {
|
||||
return
|
||||
}
|
||||
|
||||
var sourceStateData argusInstanceResource.Model
|
||||
resp.Diagnostics.Append(req.SourceState.Get(ctx, &sourceStateData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
targetStateData := Model{
|
||||
Id: sourceStateData.Id,
|
||||
ProjectId: sourceStateData.ProjectId,
|
||||
InstanceId: sourceStateData.InstanceId,
|
||||
Name: sourceStateData.Name,
|
||||
PlanName: sourceStateData.PlanName,
|
||||
PlanId: sourceStateData.PlanId,
|
||||
Parameters: sourceStateData.Parameters,
|
||||
DashboardURL: sourceStateData.DashboardURL,
|
||||
IsUpdatable: sourceStateData.IsUpdatable,
|
||||
GrafanaURL: sourceStateData.GrafanaURL,
|
||||
GrafanaPublicReadAccess: sourceStateData.GrafanaPublicReadAccess,
|
||||
GrafanaInitialAdminPassword: sourceStateData.GrafanaInitialAdminPassword,
|
||||
GrafanaInitialAdminUser: sourceStateData.GrafanaInitialAdminUser,
|
||||
MetricsRetentionDays: sourceStateData.MetricsRetentionDays,
|
||||
MetricsRetentionDays5mDownsampling: sourceStateData.MetricsRetentionDays5mDownsampling,
|
||||
MetricsRetentionDays1hDownsampling: sourceStateData.MetricsRetentionDays1hDownsampling,
|
||||
MetricsURL: sourceStateData.MetricsURL,
|
||||
MetricsPushURL: sourceStateData.MetricsPushURL,
|
||||
TargetsURL: sourceStateData.TargetsURL,
|
||||
AlertingURL: sourceStateData.AlertingURL,
|
||||
LogsURL: sourceStateData.LogsURL,
|
||||
LogsPushURL: sourceStateData.LogsPushURL,
|
||||
JaegerTracesURL: sourceStateData.JaegerTracesURL,
|
||||
JaegerUIURL: sourceStateData.JaegerUIURL,
|
||||
OtlpTracesURL: sourceStateData.OtlpTracesURL,
|
||||
ZipkinSpansURL: sourceStateData.ZipkinSpansURL,
|
||||
ACL: sourceStateData.ACL,
|
||||
AlertConfig: sourceStateData.AlertConfig,
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.TargetState.Set(ctx, targetStateData)...)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Schema defines the schema for the resource.
|
||||
func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/observability"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
||||
|
|
@ -214,11 +214,17 @@ func (d *scrapeConfigDataSource) Read(ctx context.Context, req datasource.ReadRe
|
|||
|
||||
scResp, err := d.client.GetScrapeConfig(ctx, instanceId, scName, projectId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Unable to read scrape config", err.Error())
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading scrape config",
|
||||
fmt.Sprintf("Scrape config with name %q or instance with ID %q does not exist in project %q.", scName, instanceId, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ import (
|
|||
"github.com/stackitcloud/stackit-sdk-go/services/observability/wait"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
argusScrapeConfigResource "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/argus/scrapeconfig"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
||||
|
|
@ -49,7 +48,6 @@ var (
|
|||
_ resource.Resource = &scrapeConfigResource{}
|
||||
_ resource.ResourceWithConfigure = &scrapeConfigResource{}
|
||||
_ resource.ResourceWithImportState = &scrapeConfigResource{}
|
||||
_ resource.ResourceWithMoveState = &scrapeConfigResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
|
|
@ -151,47 +149,6 @@ func (r *scrapeConfigResource) Configure(ctx context.Context, req resource.Confi
|
|||
tflog.Info(ctx, "Observability scrape config client configured")
|
||||
}
|
||||
|
||||
func (r *scrapeConfigResource) MoveState(_ context.Context) []resource.StateMover {
|
||||
return []resource.StateMover{
|
||||
{
|
||||
SourceSchema: &argusScrapeConfigResource.Schema,
|
||||
StateMover: func(ctx context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
|
||||
if req.SourceTypeName != "stackit_argus_scrapeconfig" {
|
||||
return
|
||||
}
|
||||
|
||||
// Checks source provider
|
||||
if !strings.HasSuffix(req.SourceProviderAddress, "stackitcloud/stackit") {
|
||||
return
|
||||
}
|
||||
|
||||
var sourceStateData argusScrapeConfigResource.Model
|
||||
resp.Diagnostics.Append(req.SourceState.Get(ctx, &sourceStateData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
targetStateData := Model{
|
||||
Id: sourceStateData.Id,
|
||||
ProjectId: sourceStateData.ProjectId,
|
||||
InstanceId: sourceStateData.InstanceId,
|
||||
Name: sourceStateData.Name,
|
||||
MetricsPath: sourceStateData.MetricsPath,
|
||||
Scheme: sourceStateData.Scheme,
|
||||
ScrapeInterval: sourceStateData.ScrapeInterval,
|
||||
ScrapeTimeout: sourceStateData.ScrapeTimeout,
|
||||
SampleLimit: sourceStateData.SampleLimit,
|
||||
SAML2: sourceStateData.SAML2,
|
||||
BasicAuth: sourceStateData.BasicAuth,
|
||||
Targets: sourceStateData.Targets,
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.TargetState.Set(ctx, targetStateData)...)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Schema defines the schema for the resource.
|
||||
func (r *scrapeConfigResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue