Skip to content
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
6 changes: 3 additions & 3 deletions stackit/internal/services/mariadb/credential/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *credentialDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Computed: true,
Sensitive: true,
},
"port": schema.Int64Attribute{
"port": schema.Int32Attribute{
Computed: true,
},
"uri": schema.StringAttribute{
Expand Down Expand Up @@ -142,7 +142,7 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credential_id", credentialId)

recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
58 changes: 27 additions & 31 deletions stackit/internal/services/mariadb/credential/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
"github.com/stackitcloud/stackit-sdk-go/services/mariadb/wait"
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
"github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api/wait"
)

// Ensure the implementation satisfies the expected interfaces.
Expand All @@ -41,7 +41,7 @@ type Model struct {
Hosts types.List `tfsdk:"hosts"`
Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"`
Port types.Int64 `tfsdk:"port"`
Port types.Int32 `tfsdk:"port"`
Uri types.String `tfsdk:"uri"`
Username types.String `tfsdk:"username"`
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest,
Computed: true,
Sensitive: true,
},
"port": schema.Int64Attribute{
"port": schema.Int32Attribute{
Computed: true,
},
"uri": schema.StringAttribute{
Expand Down Expand Up @@ -176,19 +176,15 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
ctx = tflog.SetField(ctx, "instance_id", instanceId)

// Create new recordset
credentialsResp, err := r.client.CreateCredentials(ctx, projectId, instanceId).Execute()
credentialsResp, err := r.client.DefaultAPI.CreateCredentials(ctx, projectId, instanceId).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Calling API: %v", err))
return
}

ctx = core.LogResponse(ctx)

if credentialsResp.Id == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", "Got empty credential id")
return
}
credentialId := *credentialsResp.Id
credentialId := credentialsResp.Id
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
"project_id": projectId,
Expand All @@ -199,7 +195,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
return
}

waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
waitResp, err := wait.CreateCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Instance creation waiting: %v", err))
return
Expand Down Expand Up @@ -237,7 +233,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "credential_id", credentialId)

recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
recordSetResp, err := r.client.DefaultAPI.GetCredentials(ctx, projectId, instanceId, credentialId).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 {
Expand Down Expand Up @@ -291,14 +287,14 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
ctx = tflog.SetField(ctx, "credential_id", credentialId)

// Delete existing record set
err := r.client.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
err := r.client.DefaultAPI.DeleteCredentials(ctx, projectId, instanceId, credentialId).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
}

ctx = core.LogResponse(ctx)

_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client, projectId, instanceId, credentialId).WaitWithContext(ctx)
_, err = wait.DeleteCredentialsWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, credentialId).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Instance deletion waiting: %v", err))
return
Expand Down Expand Up @@ -341,8 +337,8 @@ func mapFields(ctx context.Context, credentialsResp *mariadb.CredentialsResponse
var credentialId string
if model.CredentialId.ValueString() != "" {
credentialId = model.CredentialId.ValueString()
} else if credentialsResp.Id != nil {
credentialId = *credentialsResp.Id
} else if credentialsResp.Id != "" {
credentialId = credentialsResp.Id
} else {
return fmt.Errorf("credentials id not present")
}
Expand All @@ -360,25 +356,25 @@ func mapFields(ctx context.Context, credentialsResp *mariadb.CredentialsResponse

model.Hosts = types.ListNull(types.StringType)
model.CredentialId = types.StringValue(credentialId)
if credentials != nil {
if credentials.Hosts != nil {
respHosts := *credentials.Hosts

reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)
if credentials.Hosts != nil {
respHosts := credentials.Hosts

hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
if diags.HasError() {
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
}
reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)

model.Hosts = hostsTF
hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
if diags.HasError() {
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
}
model.Host = types.StringPointerValue(credentials.Host)
model.Name = types.StringPointerValue(credentials.Name)
model.Password = types.StringPointerValue(credentials.Password)
model.Port = types.Int64PointerValue(credentials.Port)
model.Uri = types.StringPointerValue(credentials.Uri)
model.Username = types.StringPointerValue(credentials.Username)

model.Hosts = hostsTF
}
model.Host = types.StringValue(credentials.Host)
model.Name = types.StringPointerValue(credentials.Name)
model.Password = types.StringValue(credentials.Password)
model.Port = types.Int32PointerValue(credentials.Port)
model.Uri = types.StringPointerValue(credentials.Uri)
model.Username = types.StringValue(credentials.Username)

return nil
}
62 changes: 31 additions & 31 deletions stackit/internal/services/mariadb/credential/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
)

func TestMapFields(t *testing.T) {
Expand All @@ -25,21 +25,21 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
},
&mariadb.CredentialsResponse{
Id: new("cid"),
Id: "cid",
Raw: &mariadb.RawCredentials{},
},
Model{
Id: types.StringValue("pid,iid,cid"),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Host: types.StringNull(),
Host: types.StringValue(""),
Hosts: types.ListNull(types.StringType),
Name: types.StringNull(),
Password: types.StringNull(),
Port: types.Int64Null(),
Password: types.StringValue(""),
Port: types.Int32Null(),
Uri: types.StringNull(),
Username: types.StringNull(),
Username: types.StringValue(""),
},
true,
},
Expand All @@ -50,19 +50,19 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
},
&mariadb.CredentialsResponse{
Id: new("cid"),
Id: "cid",
Raw: &mariadb.RawCredentials{
Credentials: &mariadb.Credentials{
Host: new("host"),
Hosts: &[]string{
Credentials: mariadb.Credentials{
Host: "host",
Hosts: []string{
"host_1",
"",
},
Name: new("name"),
Password: new("password"),
Port: new(int64(1234)),
Password: "password",
Port: new(int32(1234)),
Uri: new("uri"),
Username: new("username"),
Username: "username",
},
},
},
Expand All @@ -78,7 +78,7 @@ func TestMapFields(t *testing.T) {
}),
Name: types.StringValue("name"),
Password: types.StringValue("password"),
Port: types.Int64Value(1234),
Port: types.Int32Value(1234),
Uri: types.StringValue("uri"),
Username: types.StringValue("username"),
},
Expand All @@ -96,20 +96,20 @@ func TestMapFields(t *testing.T) {
}),
},
&mariadb.CredentialsResponse{
Id: new("cid"),
Id: "cid",
Raw: &mariadb.RawCredentials{
Credentials: &mariadb.Credentials{
Host: new("host"),
Hosts: &[]string{
Credentials: mariadb.Credentials{
Host: "host",
Hosts: []string{
"",
"host_1",
"host_2",
},
Name: new("name"),
Password: new("password"),
Port: new(int64(1234)),
Password: "password",
Port: new(int32(1234)),
Uri: new("uri"),
Username: new("username"),
Username: "username",
},
},
},
Expand All @@ -126,7 +126,7 @@ func TestMapFields(t *testing.T) {
}),
Name: types.StringValue("name"),
Password: types.StringValue("password"),
Port: types.Int64Value(1234),
Port: types.Int32Value(1234),
Uri: types.StringValue("uri"),
Username: types.StringValue("username"),
},
Expand All @@ -139,16 +139,16 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
},
&mariadb.CredentialsResponse{
Id: new("cid"),
Id: "cid",
Raw: &mariadb.RawCredentials{
Credentials: &mariadb.Credentials{
Host: new(""),
Hosts: &[]string{},
Credentials: mariadb.Credentials{
Host: "",
Hosts: []string{},
Name: nil,
Password: new(""),
Port: new(int64(2123456789)),
Password: "",
Port: new(int32(2123456789)),
Uri: nil,
Username: new(""),
Username: "",
},
},
},
Expand All @@ -161,7 +161,7 @@ func TestMapFields(t *testing.T) {
Hosts: types.ListValueMust(types.StringType, []attr.Value{}),
Name: types.StringNull(),
Password: types.StringValue(""),
Port: types.Int64Value(2123456789),
Port: types.Int32Value(2123456789),
Uri: types.StringNull(),
Username: types.StringValue(""),
},
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
},
&mariadb.CredentialsResponse{
Id: new("cid"),
Id: "cid",
},
Model{},
false,
Expand Down
8 changes: 4 additions & 4 deletions stackit/internal/services/mariadb/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -131,11 +131,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Description: parametersDescriptions["graphite"],
Computed: true,
},
"max_disk_threshold": schema.Int64Attribute{
"max_disk_threshold": schema.Int32Attribute{
Description: parametersDescriptions["max_disk_threshold"],
Computed: true,
},
"metrics_frequency": schema.Int64Attribute{
"metrics_frequency": schema.Int32Attribute{
Description: parametersDescriptions["metrics_frequency"],
Computed: true,
},
Expand Down Expand Up @@ -190,7 +190,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId)

instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, instanceId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
Loading
Loading