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
10 changes: 10 additions & 0 deletions stackit/internal/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ func Int64ValueToPointer(s basetypes.Int64Value) *int64 {
return &value
}

// Float32ValueToPointer converts basetypes.Float32Value to a pointer to float32.
// It returns nil if the value is null or unknown.
func Float32ValueToPointer(s basetypes.Float32Value) *float32 {
if s.IsNull() || s.IsUnknown() {
return nil
}
value := s.ValueFloat32()
return &value
}

// Float64ValueToPointer converts basetypes.Float64Value to a pointer to float64.
// It returns nil if the value is null or unknown.
func Float64ValueToPointer(s basetypes.Float64Value) *float64 {
Expand Down
8 changes: 4 additions & 4 deletions stackit/internal/services/logme/credential/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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/logme"
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
)

// Ensure the implementation satisfies the expected interfaces.
Expand All @@ -31,7 +31,7 @@ func NewCredentialDataSource() datasource.DataSource {

// credentialDataSource is the data source implementation.
type credentialDataSource struct {
client *logme.APIClient
client *logmeSdk.APIClient
}

// Metadata returns the data source type name.
Expand Down Expand Up @@ -102,7 +102,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 @@ -134,7 +134,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
46 changes: 21 additions & 25 deletions stackit/internal/services/logme/credential/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ 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/logme"
"github.com/stackitcloud/stackit-sdk-go/services/logme/wait"

logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
"github.com/stackitcloud/stackit-sdk-go/services/logme/v1api/wait"
)

// Ensure the implementation satisfies the expected interfaces.
Expand All @@ -40,7 +41,7 @@ type Model struct {
ProjectId types.String `tfsdk:"project_id"`
Host types.String `tfsdk:"host"`
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 All @@ -52,7 +53,7 @@ func NewCredentialResource() resource.Resource {

// credentialResource is the resource implementation.
type credentialResource struct {
client *logme.APIClient
client *logmeSdk.APIClient
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -137,7 +138,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 @@ -168,19 +169,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 @@ -191,7 +188,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 @@ -229,7 +226,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 @@ -283,14 +280,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 @@ -318,7 +315,7 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
tflog.Info(ctx, "LogMe credential state imported")
}

func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
func mapFields(credentialsResp *logmeSdk.CredentialsResponse, model *Model) error {
if credentialsResp == nil {
return fmt.Errorf("response input is nil")
}
Expand All @@ -333,20 +330,19 @@ func mapFields(credentialsResp *logme.CredentialsResponse, model *Model) error {
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")
}

model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.InstanceId.ValueString(), credentialId)
model.CredentialId = types.StringValue(credentialId)
if credentials != nil {
model.Host = types.StringPointerValue(credentials.Host)
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.Host = types.StringValue(credentials.Host)
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
}
60 changes: 30 additions & 30 deletions stackit/internal/services/logme/credential/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/logme"
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
)

func TestMapFields(t *testing.T) {
tests := []struct {
description string
input *logme.CredentialsResponse
input *logmeSdk.CredentialsResponse
expected Model
isValid bool
}{
{
"default_values",
&logme.CredentialsResponse{
Id: new("cid"),
Raw: &logme.RawCredentials{},
&logmeSdk.CredentialsResponse{
Id: "cid",
Raw: &logmeSdk.RawCredentials{},
},
Model{
Id: types.StringValue("pid,iid,cid"),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Host: types.StringNull(),
Password: types.StringNull(),
Port: types.Int64Null(),
Host: types.StringValue(""),
Password: types.StringValue(""),
Port: types.Int32Null(),
Uri: types.StringNull(),
Username: types.StringNull(),
Username: types.StringValue(""),
},
true,
},
{
"simple_values",
&logme.CredentialsResponse{
Id: new("cid"),
Raw: &logme.RawCredentials{
Credentials: &logme.Credentials{
Host: new("host"),
Password: new("password"),
Port: new(int64(1234)),
&logmeSdk.CredentialsResponse{
Id: "cid",
Raw: &logmeSdk.RawCredentials{
Credentials: logmeSdk.Credentials{
Host: "host",
Password: "password",
Port: new(int32(1234)),
Uri: new("uri"),
Username: new("username"),
Username: "username",
},
},
},
Expand All @@ -55,23 +55,23 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"),
Password: types.StringValue("password"),
Port: types.Int64Value(1234),
Port: types.Int32Value(1234),
Uri: types.StringValue("uri"),
Username: types.StringValue("username"),
},
true,
},
{
"null_fields_and_int_conversions",
&logme.CredentialsResponse{
Id: new("cid"),
Raw: &logme.RawCredentials{
Credentials: &logme.Credentials{
Host: new(""),
Password: new(""),
Port: new(int64(2123456789)),
&logmeSdk.CredentialsResponse{
Id: "cid",
Raw: &logmeSdk.RawCredentials{
Credentials: logmeSdk.Credentials{
Host: "",
Password: "",
Port: new(int32(2123456789)),
Uri: nil,
Username: new(""),
Username: "",
},
},
},
Expand All @@ -82,7 +82,7 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
Host: types.StringValue(""),
Password: types.StringValue(""),
Port: types.Int64Value(2123456789),
Port: types.Int32Value(2123456789),
Uri: types.StringNull(),
Username: types.StringValue(""),
},
Expand All @@ -96,14 +96,14 @@ func TestMapFields(t *testing.T) {
},
{
"no_resource_id",
&logme.CredentialsResponse{},
&logmeSdk.CredentialsResponse{},
Model{},
false,
},
{
"nil_raw_credential",
&logme.CredentialsResponse{
Id: new("cid"),
&logmeSdk.CredentialsResponse{
Id: "cid",
},
Model{},
false,
Expand Down
24 changes: 12 additions & 12 deletions stackit/internal/services/logme/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/logme"
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
)

// Ensure the implementation satisfies the expected interfaces.
Expand All @@ -32,7 +32,7 @@ func NewInstanceDataSource() datasource.DataSource {

// instanceDataSource is the data source implementation.
type instanceDataSource struct {
client *logme.APIClient
client *logmeSdk.APIClient
}

// Metadata returns the data source type name.
Expand Down Expand Up @@ -133,11 +133,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Description: parametersDescriptions["enable_monitoring"],
Computed: true,
},
"fluentd_tcp": schema.Int64Attribute{
"fluentd_tcp": schema.Int32Attribute{
Description: parametersDescriptions["fluentd_tcp"],
Computed: true,
},
"fluentd_tls": schema.Int64Attribute{
"fluentd_tls": schema.Int32Attribute{
Description: parametersDescriptions["fluentd_tls"],
Computed: true,
},
Expand All @@ -157,7 +157,7 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Description: parametersDescriptions["fluentd_tls_version"],
Computed: true,
},
"fluentd_udp": schema.Int64Attribute{
"fluentd_udp": schema.Int32Attribute{
Description: parametersDescriptions["fluentd_udp"],
Computed: true,
},
Expand All @@ -169,27 +169,27 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Description: parametersDescriptions["ism_deletion_after"],
Computed: true,
},
"ism_jitter": schema.Float64Attribute{
"ism_jitter": schema.Float32Attribute{
Description: parametersDescriptions["ism_jitter"],
Computed: true,
},
"ism_job_interval": schema.Int64Attribute{
"ism_job_interval": schema.Int32Attribute{
Description: parametersDescriptions["ism_job_interval"],
Computed: true,
},
"java_heapspace": schema.Int64Attribute{
"java_heapspace": schema.Int32Attribute{
Description: parametersDescriptions["java_heapspace"],
Computed: true,
},
"java_maxmetaspace": schema.Int64Attribute{
"java_maxmetaspace": schema.Int32Attribute{
Description: parametersDescriptions["java_maxmetaspace"],
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 @@ -254,7 +254,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