Extend LogMe instance parameters (#438)
* Extend LogMe instance parameters * Update acc test * Add more field descriptions * Improve code and tests * Add more fields to acc test * Fix linter * Add float parameter
This commit is contained in:
parent
e553628b5e
commit
a08dbd8926
15 changed files with 868 additions and 200 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
|
|
@ -86,6 +87,23 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
|
|||
"plan_id": "The selected plan ID.",
|
||||
}
|
||||
|
||||
parametersDescriptions := map[string]string{
|
||||
"sgw_acl": "Comma separated list of IP networks in CIDR notation which are allowed to access this instance.",
|
||||
"enable_monitoring": "Enable monitoring.",
|
||||
"graphite": "If set, monitoring with Graphite will be enabled. Expects the host and port where the Graphite metrics should be sent to (host:port).",
|
||||
"max_disk_threshold": "The maximum disk threshold in MB. If the disk usage exceeds this threshold, the instance will be stopped.",
|
||||
"metrics_frequency": "The frequency in seconds at which metrics are emitted (in seconds).",
|
||||
"metrics_prefix": "The prefix for the metrics. Could be useful when using Graphite monitoring to prefix the metrics with a certain value, like an API key.",
|
||||
"monitoring_instance_id": "The monitoring instance ID.",
|
||||
"java_heapspace": "The amount of memory (in MB) allocated as heap by the JVM for OpenSearch.",
|
||||
"java_maxmetaspace": "The amount of memory (in MB) used by the JVM to store metadata for OpenSearch.",
|
||||
"ism_deletion_after": "Combination of an integer and a timerange when an index will be considered \"old\" and can be deleted. Possible values for the timerange are `s`, `m`, `h` and `d`.",
|
||||
"ism_job_interval": "Jitter of the execution time.",
|
||||
"syslog": "List of syslog servers to send logs to.",
|
||||
"syslog-use-udp": "Defines if syslog will use UDP. Possible values: `yes`, `no`.",
|
||||
"opensearch-tls-ciphers": "List of ciphers to use for TLS.",
|
||||
}
|
||||
|
||||
resp.Schema = schema.Schema{
|
||||
Description: descriptions["main"],
|
||||
Attributes: map[string]schema.Attribute{
|
||||
|
|
@ -128,7 +146,99 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
|
|||
"parameters": schema.SingleNestedAttribute{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"sgw_acl": schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: parametersDescriptions["sgw_acl"],
|
||||
Computed: true,
|
||||
},
|
||||
"enable_monitoring": schema.BoolAttribute{
|
||||
Description: parametersDescriptions["enable_monitoring"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tcp": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["fluentd_tcp"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["fluentd_tls"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_ciphers": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_ciphers"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_max_version": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_max_version"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_min_version": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_min_version"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_version": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_version"],
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_udp": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["fluentd_udp"],
|
||||
Computed: true,
|
||||
},
|
||||
"graphite": schema.StringAttribute{
|
||||
Description: parametersDescriptions["graphite"],
|
||||
Computed: true,
|
||||
},
|
||||
"ism_deletion_after": schema.StringAttribute{
|
||||
Description: parametersDescriptions["ism_deletion_after"],
|
||||
Computed: true,
|
||||
},
|
||||
"ism_jitter": schema.Float64Attribute{
|
||||
Description: parametersDescriptions["ism_jitter"],
|
||||
Computed: true,
|
||||
},
|
||||
"ism_job_interval": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["ism_job_interval"],
|
||||
Computed: true,
|
||||
},
|
||||
"java_heapspace": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["java_heapspace"],
|
||||
Computed: true,
|
||||
},
|
||||
"java_maxmetaspace": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["java_maxmetaspace"],
|
||||
Computed: true,
|
||||
},
|
||||
"max_disk_threshold": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["max_disk_threshold"],
|
||||
Computed: true,
|
||||
},
|
||||
"metrics_frequency": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["metrics_frequency"],
|
||||
Computed: true,
|
||||
},
|
||||
"metrics_prefix": schema.StringAttribute{
|
||||
Description: parametersDescriptions["metrics_prefix"],
|
||||
Computed: true,
|
||||
},
|
||||
"monitoring_instance_id": schema.StringAttribute{
|
||||
Description: parametersDescriptions["monitoring_instance_id"],
|
||||
Computed: true,
|
||||
},
|
||||
"opensearch_tls_ciphers": schema.ListAttribute{
|
||||
Description: parametersDescriptions["opensearch_tls_ciphers"],
|
||||
ElementType: types.StringType,
|
||||
Computed: true,
|
||||
},
|
||||
"opensearch_tls_protocols": schema.ListAttribute{
|
||||
Description: parametersDescriptions["opensearch_tls_protocols"],
|
||||
ElementType: types.StringType,
|
||||
Computed: true,
|
||||
},
|
||||
"syslog": schema.ListAttribute{
|
||||
Description: parametersDescriptions["syslog"],
|
||||
ElementType: types.StringType,
|
||||
Computed: true,
|
||||
},
|
||||
"syslog_use_udp": schema.StringAttribute{
|
||||
Description: parametersDescriptions["syslog_use_udp"],
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
Computed: true,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
|
|
@ -52,12 +54,56 @@ type Model struct {
|
|||
|
||||
// Struct corresponding to DataSourceModel.Parameters
|
||||
type parametersModel struct {
|
||||
SgwAcl types.String `tfsdk:"sgw_acl"`
|
||||
SgwAcl types.String `tfsdk:"sgw_acl"`
|
||||
EnableMonitoring types.Bool `tfsdk:"enable_monitoring"`
|
||||
FluentdTcp types.Int64 `tfsdk:"fluentd_tcp"`
|
||||
FluentdTls types.Int64 `tfsdk:"fluentd_tls"`
|
||||
FluentdTlsCiphers types.String `tfsdk:"fluentd_tls_ciphers"`
|
||||
FluentdTlsMaxVersion types.String `tfsdk:"fluentd_tls_max_version"`
|
||||
FluentdTlsMinVersion types.String `tfsdk:"fluentd_tls_min_version"`
|
||||
FluentdTlsVersion types.String `tfsdk:"fluentd_tls_version"`
|
||||
FluentdUdp types.Int64 `tfsdk:"fluentd_udp"`
|
||||
Graphite types.String `tfsdk:"graphite"`
|
||||
IsmDeletionAfter types.String `tfsdk:"ism_deletion_after"`
|
||||
IsmJitter types.Float64 `tfsdk:"ism_jitter"`
|
||||
IsmJobInterval types.Int64 `tfsdk:"ism_job_interval"`
|
||||
JavaHeapspace types.Int64 `tfsdk:"java_heapspace"`
|
||||
JavaMaxmetaspace types.Int64 `tfsdk:"java_maxmetaspace"`
|
||||
MaxDiskThreshold types.Int64 `tfsdk:"max_disk_threshold"`
|
||||
MetricsFrequency types.Int64 `tfsdk:"metrics_frequency"`
|
||||
MetricsPrefix types.String `tfsdk:"metrics_prefix"`
|
||||
MonitoringInstanceId types.String `tfsdk:"monitoring_instance_id"`
|
||||
OpensearchTlsCiphers types.List `tfsdk:"opensearch_tls_ciphers"`
|
||||
OpensearchTlsProtocols types.List `tfsdk:"opensearch_tls_protocols"`
|
||||
Syslog types.List `tfsdk:"syslog"`
|
||||
SyslogUseUdp types.String `tfsdk:"syslog_use_udp"`
|
||||
}
|
||||
|
||||
// Types corresponding to parametersModel
|
||||
var parametersTypes = map[string]attr.Type{
|
||||
"sgw_acl": basetypes.StringType{},
|
||||
"sgw_acl": basetypes.StringType{},
|
||||
"enable_monitoring": basetypes.BoolType{},
|
||||
"fluentd_tcp": basetypes.Int64Type{},
|
||||
"fluentd_tls": basetypes.Int64Type{},
|
||||
"fluentd_tls_ciphers": basetypes.StringType{},
|
||||
"fluentd_tls_max_version": basetypes.StringType{},
|
||||
"fluentd_tls_min_version": basetypes.StringType{},
|
||||
"fluentd_tls_version": basetypes.StringType{},
|
||||
"fluentd_udp": basetypes.Int64Type{},
|
||||
"graphite": basetypes.StringType{},
|
||||
"ism_deletion_after": basetypes.StringType{},
|
||||
"ism_jitter": basetypes.Float64Type{},
|
||||
"ism_job_interval": basetypes.Int64Type{},
|
||||
"java_heapspace": basetypes.Int64Type{},
|
||||
"java_maxmetaspace": basetypes.Int64Type{},
|
||||
"max_disk_threshold": basetypes.Int64Type{},
|
||||
"metrics_frequency": basetypes.Int64Type{},
|
||||
"metrics_prefix": basetypes.StringType{},
|
||||
"monitoring_instance_id": basetypes.StringType{},
|
||||
"opensearch_tls_ciphers": basetypes.ListType{ElemType: types.StringType},
|
||||
"opensearch_tls_protocols": basetypes.ListType{ElemType: types.StringType},
|
||||
"syslog": basetypes.ListType{ElemType: types.StringType},
|
||||
"syslog_use_udp": basetypes.StringType{},
|
||||
}
|
||||
|
||||
// NewInstanceResource is a helper function to simplify the provider implementation.
|
||||
|
|
@ -124,6 +170,23 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
|
|||
"plan_id": "The selected plan ID.",
|
||||
}
|
||||
|
||||
parametersDescriptions := map[string]string{
|
||||
"sgw_acl": "Comma separated list of IP networks in CIDR notation which are allowed to access this instance.",
|
||||
"enable_monitoring": "Enable monitoring.",
|
||||
"graphite": "If set, monitoring with Graphite will be enabled. Expects the host and port where the Graphite metrics should be sent to (host:port).",
|
||||
"max_disk_threshold": "The maximum disk threshold in MB. If the disk usage exceeds this threshold, the instance will be stopped.",
|
||||
"metrics_frequency": "The frequency in seconds at which metrics are emitted (in seconds).",
|
||||
"metrics_prefix": "The prefix for the metrics. Could be useful when using Graphite monitoring to prefix the metrics with a certain value, like an API key.",
|
||||
"monitoring_instance_id": "The monitoring instance ID.",
|
||||
"java_heapspace": "The amount of memory (in MB) allocated as heap by the JVM for OpenSearch.",
|
||||
"java_maxmetaspace": "The amount of memory (in MB) used by the JVM to store metadata for OpenSearch.",
|
||||
"ism_deletion_after": "Combination of an integer and a timerange when an index will be considered \"old\" and can be deleted. Possible values for the timerange are `s`, `m`, `h` and `d`.",
|
||||
"ism_job_interval": "Jitter of the execution time.",
|
||||
"syslog": "List of syslog servers to send logs to.",
|
||||
"syslog-use-udp": "Defines if syslog will use UDP. Possible values: `yes`, `no`.",
|
||||
"opensearch-tls-ciphers": "List of ciphers to use for TLS.",
|
||||
}
|
||||
|
||||
resp.Schema = schema.Schema{
|
||||
Description: descriptions["main"],
|
||||
Attributes: map[string]schema.Attribute{
|
||||
|
|
@ -183,8 +246,122 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
|
|||
"parameters": schema.SingleNestedAttribute{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"sgw_acl": schema.StringAttribute{
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Description: parametersDescriptions["sgw_acl"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"enable_monitoring": schema.BoolAttribute{
|
||||
Description: parametersDescriptions["enable_monitoring"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tcp": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["fluentd_tcp"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["fluentd_tls"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_ciphers": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_ciphers"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_max_version": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_max_version"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_min_version": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_min_version"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_tls_version": schema.StringAttribute{
|
||||
Description: parametersDescriptions["fluentd_tls_version"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"fluentd_udp": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["fluentd_udp"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"graphite": schema.StringAttribute{
|
||||
Description: parametersDescriptions["graphite"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"ism_deletion_after": schema.StringAttribute{
|
||||
Description: parametersDescriptions["ism_deletion_after"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"ism_jitter": schema.Float64Attribute{
|
||||
Description: parametersDescriptions["ism_jitter"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"ism_job_interval": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["ism_job_interval"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"java_heapspace": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["java_heapspace"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"java_maxmetaspace": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["java_maxmetaspace"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"max_disk_threshold": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["max_disk_threshold"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"metrics_frequency": schema.Int64Attribute{
|
||||
Description: parametersDescriptions["metrics_frequency"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"metrics_prefix": schema.StringAttribute{
|
||||
Description: parametersDescriptions["metrics_prefix"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"monitoring_instance_id": schema.StringAttribute{
|
||||
Description: parametersDescriptions["monitoring_instance_id"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"opensearch_tls_ciphers": schema.ListAttribute{
|
||||
Description: parametersDescriptions["opensearch_tls_ciphers"],
|
||||
ElementType: types.StringType,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"opensearch_tls_protocols": schema.ListAttribute{
|
||||
Description: parametersDescriptions["opensearch_tls_protocols"],
|
||||
ElementType: types.StringType,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"syslog": schema.ListAttribute{
|
||||
Description: parametersDescriptions["syslog"],
|
||||
ElementType: types.StringType,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"syslog_use_udp": schema.StringAttribute{
|
||||
Description: parametersDescriptions["syslog_use_udp"],
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
Optional: true,
|
||||
|
|
@ -235,8 +412,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
|||
projectId := model.ProjectId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
|
||||
var parameters = ¶metersModel{}
|
||||
var parameters *parametersModel
|
||||
if !(model.Parameters.IsNull() || model.Parameters.IsUnknown()) {
|
||||
parameters = ¶metersModel{}
|
||||
diags = model.Parameters.As(ctx, parameters, basetypes.ObjectAsOptions{})
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -264,7 +442,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
|||
}
|
||||
instanceId := *createResp.InstanceId
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx)
|
||||
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).SetTimeout(90 * time.Minute).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
|
||||
return
|
||||
|
|
@ -346,8 +524,9 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
|
|||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
||||
var parameters = ¶metersModel{}
|
||||
var parameters *parametersModel
|
||||
if !(model.Parameters.IsNull() || model.Parameters.IsUnknown()) {
|
||||
parameters = ¶metersModel{}
|
||||
diags = model.Parameters.As(ctx, parameters, basetypes.ObjectAsOptions{})
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -488,7 +667,30 @@ func mapFields(instance *logme.Instance, model *Model) error {
|
|||
func mapParameters(params map[string]interface{}) (types.Object, error) {
|
||||
attributes := map[string]attr.Value{}
|
||||
for attribute := range parametersTypes {
|
||||
valueInterface, ok := params[attribute]
|
||||
var valueInterface interface{}
|
||||
var ok bool
|
||||
|
||||
// This replacement is necessary because Terraform does not allow hyphens in attribute names
|
||||
// And the API uses hyphens in some of the attribute names, which would cause a mismatch
|
||||
// The following attributes have hyphens in the API but underscores in the schema
|
||||
hyphenAttributes := []string{
|
||||
"fluentd_tcp",
|
||||
"fluentd_tls",
|
||||
"fluentd_tls_ciphers",
|
||||
"fluentd_tls_max_version",
|
||||
"fluentd_tls_min_version",
|
||||
"fluentd_tls_version",
|
||||
"fluentd_udp",
|
||||
"opensearch_tls_ciphers",
|
||||
"opensearch_tls_protocols",
|
||||
"syslog_use_udp",
|
||||
}
|
||||
if slices.Contains(hyphenAttributes, attribute) {
|
||||
alteredAttribute := strings.ReplaceAll(attribute, "_", "-")
|
||||
valueInterface, ok = params[alteredAttribute]
|
||||
} else {
|
||||
valueInterface, ok = params[attribute]
|
||||
}
|
||||
if !ok {
|
||||
// All fields are optional, so this is ok
|
||||
// Set the value as nil, will be handled accordingly
|
||||
|
|
@ -540,6 +742,19 @@ func mapParameters(params map[string]interface{}) (types.Object, error) {
|
|||
}
|
||||
value = types.Int64Value(valueInt64)
|
||||
}
|
||||
case basetypes.Float64Type:
|
||||
if valueInterface == nil {
|
||||
value = types.Float64Null()
|
||||
} else {
|
||||
var valueFloat64 float64
|
||||
switch temp := valueInterface.(type) {
|
||||
default:
|
||||
return types.ObjectNull(parametersTypes), fmt.Errorf("found attribute '%s' of type %T, failed to assert as int", attribute, valueInterface)
|
||||
case float64:
|
||||
valueFloat64 = float64(temp)
|
||||
}
|
||||
value = types.Float64Value(valueFloat64)
|
||||
}
|
||||
case basetypes.ListType: // Assumed to be a list of strings
|
||||
if valueInterface == nil {
|
||||
value = types.ListNull(types.StringType)
|
||||
|
|
@ -584,16 +799,12 @@ func toCreatePayload(model *Model, parameters *parametersModel) (*logme.CreateIn
|
|||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
if parameters == nil {
|
||||
return &logme.CreateInstancePayload{
|
||||
InstanceName: conversion.StringValueToPointer(model.Name),
|
||||
PlanId: conversion.StringValueToPointer(model.PlanId),
|
||||
}, nil
|
||||
}
|
||||
payloadParams := &logme.InstanceParameters{}
|
||||
if parameters.SgwAcl.ValueString() != "" {
|
||||
payloadParams.SgwAcl = conversion.StringValueToPointer(parameters.SgwAcl)
|
||||
|
||||
payloadParams, err := toInstanceParams(parameters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert parameters: %w", err)
|
||||
}
|
||||
|
||||
return &logme.CreateInstancePayload{
|
||||
InstanceName: conversion.StringValueToPointer(model.Name),
|
||||
Parameters: payloadParams,
|
||||
|
|
@ -606,19 +817,63 @@ func toUpdatePayload(model *Model, parameters *parametersModel) (*logme.PartialU
|
|||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
if parameters == nil {
|
||||
return &logme.PartialUpdateInstancePayload{
|
||||
PlanId: conversion.StringValueToPointer(model.PlanId),
|
||||
}, nil
|
||||
payloadParams, err := toInstanceParams(parameters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert parameters: %w", err)
|
||||
}
|
||||
|
||||
return &logme.PartialUpdateInstancePayload{
|
||||
Parameters: &logme.InstanceParameters{
|
||||
SgwAcl: conversion.StringValueToPointer(parameters.SgwAcl),
|
||||
},
|
||||
PlanId: conversion.StringValueToPointer(model.PlanId),
|
||||
Parameters: payloadParams,
|
||||
PlanId: conversion.StringValueToPointer(model.PlanId),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func toInstanceParams(parameters *parametersModel) (*logme.InstanceParameters, error) {
|
||||
if parameters == nil {
|
||||
return nil, nil
|
||||
}
|
||||
payloadParams := &logme.InstanceParameters{}
|
||||
|
||||
payloadParams.SgwAcl = conversion.StringValueToPointer(parameters.SgwAcl)
|
||||
payloadParams.EnableMonitoring = conversion.BoolValueToPointer(parameters.EnableMonitoring)
|
||||
payloadParams.FluentdTcp = conversion.Int64ValueToPointer(parameters.FluentdTcp)
|
||||
payloadParams.FluentdTls = conversion.Int64ValueToPointer(parameters.FluentdTls)
|
||||
payloadParams.FluentdTlsCiphers = conversion.StringValueToPointer(parameters.FluentdTlsCiphers)
|
||||
payloadParams.FluentdTlsMaxVersion = conversion.StringValueToPointer(parameters.FluentdTlsMaxVersion)
|
||||
payloadParams.FluentdTlsMinVersion = conversion.StringValueToPointer(parameters.FluentdTlsMinVersion)
|
||||
payloadParams.FluentdTlsVersion = conversion.StringValueToPointer(parameters.FluentdTlsVersion)
|
||||
payloadParams.FluentdUdp = conversion.Int64ValueToPointer(parameters.FluentdUdp)
|
||||
payloadParams.Graphite = conversion.StringValueToPointer(parameters.Graphite)
|
||||
payloadParams.IsmDeletionAfter = conversion.StringValueToPointer(parameters.IsmDeletionAfter)
|
||||
payloadParams.IsmJitter = conversion.Float64ValueToPointer(parameters.IsmJitter)
|
||||
payloadParams.IsmJobInterval = conversion.Int64ValueToPointer(parameters.IsmJobInterval)
|
||||
payloadParams.JavaHeapspace = conversion.Int64ValueToPointer(parameters.JavaHeapspace)
|
||||
payloadParams.JavaMaxmetaspace = conversion.Int64ValueToPointer(parameters.JavaMaxmetaspace)
|
||||
payloadParams.MaxDiskThreshold = conversion.Int64ValueToPointer(parameters.MaxDiskThreshold)
|
||||
payloadParams.MetricsFrequency = conversion.Int64ValueToPointer(parameters.MetricsFrequency)
|
||||
payloadParams.MetricsPrefix = conversion.StringValueToPointer(parameters.MetricsPrefix)
|
||||
payloadParams.MonitoringInstanceId = conversion.StringValueToPointer(parameters.MonitoringInstanceId)
|
||||
payloadParams.SyslogUseUdp = conversion.StringValueToPointer(parameters.SyslogUseUdp)
|
||||
|
||||
var err error
|
||||
payloadParams.OpensearchTlsCiphers, err = conversion.StringListToPointer(parameters.OpensearchTlsCiphers)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert opensearch_tls_ciphers: %w", err)
|
||||
}
|
||||
|
||||
payloadParams.OpensearchTlsProtocols, err = conversion.StringListToPointer(parameters.OpensearchTlsProtocols)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert opensearch_tls_protocols: %w", err)
|
||||
}
|
||||
|
||||
payloadParams.Syslog, err = conversion.StringListToPointer(parameters.Syslog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert syslog: %w", err)
|
||||
}
|
||||
|
||||
return payloadParams, nil
|
||||
}
|
||||
|
||||
func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
|
||||
projectId := model.ProjectId.ValueString()
|
||||
res, err := r.client.ListOfferings(ctx, projectId).Execute()
|
||||
|
|
|
|||
|
|
@ -1,15 +1,104 @@
|
|||
package logme
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/logme"
|
||||
)
|
||||
|
||||
var fixtureModelParameters = types.ObjectValueMust(parametersTypes, map[string]attr.Value{
|
||||
"sgw_acl": types.StringValue("acl"),
|
||||
"enable_monitoring": types.BoolValue(true),
|
||||
"fluentd_tcp": types.Int64Value(10),
|
||||
"fluentd_tls": types.Int64Value(10),
|
||||
"fluentd_tls_ciphers": types.StringValue("ciphers"),
|
||||
"fluentd_tls_max_version": types.StringValue("max_version"),
|
||||
"fluentd_tls_min_version": types.StringValue("min_version"),
|
||||
"fluentd_tls_version": types.StringValue("version"),
|
||||
"fluentd_udp": types.Int64Value(10),
|
||||
"graphite": types.StringValue("graphite"),
|
||||
"ism_deletion_after": types.StringValue("deletion_after"),
|
||||
"ism_jitter": types.Float64Value(10.1),
|
||||
"ism_job_interval": types.Int64Value(10),
|
||||
"java_heapspace": types.Int64Value(10),
|
||||
"java_maxmetaspace": types.Int64Value(10),
|
||||
"max_disk_threshold": types.Int64Value(10),
|
||||
"metrics_frequency": types.Int64Value(10),
|
||||
"metrics_prefix": types.StringValue("prefix"),
|
||||
"monitoring_instance_id": types.StringValue("mid"),
|
||||
"opensearch_tls_ciphers": types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("ciphers"),
|
||||
types.StringValue("ciphers2"),
|
||||
}),
|
||||
"opensearch_tls_protocols": types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("protocols"),
|
||||
types.StringValue("protocols2"),
|
||||
}),
|
||||
"syslog": types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("syslog"),
|
||||
types.StringValue("syslog2"),
|
||||
}),
|
||||
"syslog_use_udp": types.StringValue("udp"),
|
||||
})
|
||||
|
||||
var fixtureNullModelParameters = types.ObjectValueMust(parametersTypes, map[string]attr.Value{
|
||||
"sgw_acl": types.StringNull(),
|
||||
"enable_monitoring": types.BoolNull(),
|
||||
"fluentd_tcp": types.Int64Null(),
|
||||
"fluentd_tls": types.Int64Null(),
|
||||
"fluentd_tls_ciphers": types.StringNull(),
|
||||
"fluentd_tls_max_version": types.StringNull(),
|
||||
"fluentd_tls_min_version": types.StringNull(),
|
||||
"fluentd_tls_version": types.StringNull(),
|
||||
"fluentd_udp": types.Int64Null(),
|
||||
"graphite": types.StringNull(),
|
||||
"ism_deletion_after": types.StringNull(),
|
||||
"ism_jitter": types.Float64Null(),
|
||||
"ism_job_interval": types.Int64Null(),
|
||||
"java_heapspace": types.Int64Null(),
|
||||
"java_maxmetaspace": types.Int64Null(),
|
||||
"max_disk_threshold": types.Int64Null(),
|
||||
"metrics_frequency": types.Int64Null(),
|
||||
"metrics_prefix": types.StringNull(),
|
||||
"monitoring_instance_id": types.StringNull(),
|
||||
"opensearch_tls_ciphers": types.ListNull(types.StringType),
|
||||
"opensearch_tls_protocols": types.ListNull(types.StringType),
|
||||
"syslog": types.ListNull(types.StringType),
|
||||
"syslog_use_udp": types.StringNull(),
|
||||
})
|
||||
|
||||
var fixtureInstanceParameters = logme.InstanceParameters{
|
||||
SgwAcl: utils.Ptr("acl"),
|
||||
EnableMonitoring: utils.Ptr(true),
|
||||
FluentdTcp: utils.Ptr(int64(10)),
|
||||
FluentdTls: utils.Ptr(int64(10)),
|
||||
FluentdTlsCiphers: utils.Ptr("ciphers"),
|
||||
FluentdTlsMaxVersion: utils.Ptr("max_version"),
|
||||
FluentdTlsMinVersion: utils.Ptr("min_version"),
|
||||
FluentdTlsVersion: utils.Ptr("version"),
|
||||
FluentdUdp: utils.Ptr(int64(10)),
|
||||
Graphite: utils.Ptr("graphite"),
|
||||
IsmDeletionAfter: utils.Ptr("deletion_after"),
|
||||
IsmJitter: utils.Ptr(10.1),
|
||||
IsmJobInterval: utils.Ptr(int64(10)),
|
||||
JavaHeapspace: utils.Ptr(int64(10)),
|
||||
JavaMaxmetaspace: utils.Ptr(int64(10)),
|
||||
MaxDiskThreshold: utils.Ptr(int64(10)),
|
||||
MetricsFrequency: utils.Ptr(int64(10)),
|
||||
MetricsPrefix: utils.Ptr("prefix"),
|
||||
MonitoringInstanceId: utils.Ptr("mid"),
|
||||
OpensearchTlsCiphers: &[]string{"ciphers", "ciphers2"},
|
||||
OpensearchTlsProtocols: &[]string{"protocols", "protocols2"},
|
||||
Syslog: &[]string{"syslog", "syslog2"},
|
||||
SyslogUseUdp: utils.Ptr("udp"),
|
||||
}
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
|
|
@ -47,7 +136,30 @@ func TestMapFields(t *testing.T) {
|
|||
Name: utils.Ptr("name"),
|
||||
CfOrganizationGuid: utils.Ptr("org"),
|
||||
Parameters: &map[string]interface{}{
|
||||
"sgw_acl": "acl",
|
||||
// Using "-" on purpose on some fields because that is the API response
|
||||
"sgw_acl": "acl",
|
||||
"enable_monitoring": true,
|
||||
"fluentd-tcp": 10,
|
||||
"fluentd-tls": 10,
|
||||
"fluentd-tls-ciphers": "ciphers",
|
||||
"fluentd-tls-max-version": "max_version",
|
||||
"fluentd-tls-min-version": "min_version",
|
||||
"fluentd-tls-version": "version",
|
||||
"fluentd-udp": 10,
|
||||
"graphite": "graphite",
|
||||
"ism_deletion_after": "deletion_after",
|
||||
"ism_jitter": 10.1,
|
||||
"ism_job_interval": 10,
|
||||
"java_heapspace": 10,
|
||||
"java_maxmetaspace": 10,
|
||||
"max_disk_threshold": 10,
|
||||
"metrics_frequency": 10,
|
||||
"metrics_prefix": "prefix",
|
||||
"monitoring_instance_id": "mid",
|
||||
"opensearch-tls-ciphers": []string{"ciphers", "ciphers2"},
|
||||
"opensearch-tls-protocols": []string{"protocols", "protocols2"},
|
||||
"syslog": []string{"syslog", "syslog2"},
|
||||
"syslog-use-udp": "udp",
|
||||
},
|
||||
},
|
||||
Model{
|
||||
|
|
@ -61,9 +173,7 @@ func TestMapFields(t *testing.T) {
|
|||
DashboardUrl: types.StringValue("dashboard"),
|
||||
ImageUrl: types.StringValue("image"),
|
||||
CfOrganizationGuid: types.StringValue("org"),
|
||||
Parameters: types.ObjectValueMust(parametersTypes, map[string]attr.Value{
|
||||
"sgw_acl": types.StringValue("acl"),
|
||||
}),
|
||||
Parameters: fixtureModelParameters,
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -125,61 +235,48 @@ func TestMapFields(t *testing.T) {
|
|||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *Model
|
||||
inputParameters *parametersModel
|
||||
expected *logme.CreateInstancePayload
|
||||
isValid bool
|
||||
description string
|
||||
input *Model
|
||||
expected *logme.CreateInstancePayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&Model{},
|
||||
¶metersModel{},
|
||||
&logme.CreateInstancePayload{
|
||||
Parameters: &logme.InstanceParameters{},
|
||||
},
|
||||
&logme.CreateInstancePayload{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
&Model{
|
||||
Name: types.StringValue("name"),
|
||||
PlanId: types.StringValue("plan"),
|
||||
},
|
||||
¶metersModel{
|
||||
SgwAcl: types.StringValue("sgw"),
|
||||
Name: types.StringValue("name"),
|
||||
PlanId: types.StringValue("plan"),
|
||||
Parameters: fixtureModelParameters,
|
||||
},
|
||||
&logme.CreateInstancePayload{
|
||||
InstanceName: utils.Ptr("name"),
|
||||
Parameters: &logme.InstanceParameters{
|
||||
SgwAcl: utils.Ptr("sgw"),
|
||||
},
|
||||
PlanId: utils.Ptr("plan"),
|
||||
PlanId: utils.Ptr("plan"),
|
||||
Parameters: &fixtureInstanceParameters,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&Model{
|
||||
Name: types.StringValue(""),
|
||||
PlanId: types.StringValue(""),
|
||||
},
|
||||
¶metersModel{
|
||||
SgwAcl: types.StringNull(),
|
||||
Name: types.StringValue(""),
|
||||
PlanId: types.StringValue(""),
|
||||
Parameters: fixtureNullModelParameters,
|
||||
},
|
||||
&logme.CreateInstancePayload{
|
||||
InstanceName: utils.Ptr(""),
|
||||
Parameters: &logme.InstanceParameters{
|
||||
SgwAcl: nil,
|
||||
},
|
||||
PlanId: utils.Ptr(""),
|
||||
PlanId: utils.Ptr(""),
|
||||
Parameters: &logme.InstanceParameters{},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_model",
|
||||
nil,
|
||||
¶metersModel{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
|
|
@ -189,7 +286,6 @@ func TestToCreatePayload(t *testing.T) {
|
|||
Name: types.StringValue("name"),
|
||||
PlanId: types.StringValue("plan"),
|
||||
},
|
||||
nil,
|
||||
&logme.CreateInstancePayload{
|
||||
InstanceName: utils.Ptr("name"),
|
||||
PlanId: utils.Ptr("plan"),
|
||||
|
|
@ -199,7 +295,17 @@ func TestToCreatePayload(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toCreatePayload(tt.input, tt.inputParameters)
|
||||
var parameters *parametersModel
|
||||
if tt.input != nil {
|
||||
if !(tt.input.Parameters.IsNull() || tt.input.Parameters.IsUnknown()) {
|
||||
parameters = ¶metersModel{}
|
||||
diags := tt.input.Parameters.As(context.Background(), parameters, basetypes.ObjectAsOptions{})
|
||||
if diags.HasError() {
|
||||
t.Fatalf("Error converting parameters: %v", diags.Errors())
|
||||
}
|
||||
}
|
||||
}
|
||||
output, err := toCreatePayload(tt.input, parameters)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -218,57 +324,44 @@ func TestToCreatePayload(t *testing.T) {
|
|||
|
||||
func TestToUpdatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *Model
|
||||
inputParameters *parametersModel
|
||||
expected *logme.PartialUpdateInstancePayload
|
||||
isValid bool
|
||||
description string
|
||||
input *Model
|
||||
expected *logme.PartialUpdateInstancePayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&Model{},
|
||||
¶metersModel{},
|
||||
&logme.PartialUpdateInstancePayload{
|
||||
Parameters: &logme.InstanceParameters{},
|
||||
},
|
||||
&logme.PartialUpdateInstancePayload{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
&Model{
|
||||
PlanId: types.StringValue("plan"),
|
||||
},
|
||||
¶metersModel{
|
||||
SgwAcl: types.StringValue("sgw"),
|
||||
PlanId: types.StringValue("plan"),
|
||||
Parameters: fixtureModelParameters,
|
||||
},
|
||||
&logme.PartialUpdateInstancePayload{
|
||||
Parameters: &logme.InstanceParameters{
|
||||
SgwAcl: utils.Ptr("sgw"),
|
||||
},
|
||||
PlanId: utils.Ptr("plan"),
|
||||
Parameters: &fixtureInstanceParameters,
|
||||
PlanId: utils.Ptr("plan"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&Model{
|
||||
PlanId: types.StringValue(""),
|
||||
},
|
||||
¶metersModel{
|
||||
SgwAcl: types.StringNull(),
|
||||
PlanId: types.StringValue(""),
|
||||
Parameters: fixtureNullModelParameters,
|
||||
},
|
||||
&logme.PartialUpdateInstancePayload{
|
||||
Parameters: &logme.InstanceParameters{
|
||||
SgwAcl: nil,
|
||||
},
|
||||
PlanId: utils.Ptr(""),
|
||||
Parameters: &logme.InstanceParameters{},
|
||||
PlanId: utils.Ptr(""),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_model",
|
||||
nil,
|
||||
¶metersModel{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
|
|
@ -277,7 +370,6 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
&Model{
|
||||
PlanId: types.StringValue("plan"),
|
||||
},
|
||||
nil,
|
||||
&logme.PartialUpdateInstancePayload{
|
||||
PlanId: utils.Ptr("plan"),
|
||||
},
|
||||
|
|
@ -286,7 +378,17 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toUpdatePayload(tt.input, tt.inputParameters)
|
||||
var parameters *parametersModel
|
||||
if tt.input != nil {
|
||||
if !(tt.input.Parameters.IsNull() || tt.input.Parameters.IsUnknown()) {
|
||||
parameters = ¶metersModel{}
|
||||
diags := tt.input.Parameters.As(context.Background(), parameters, basetypes.ObjectAsOptions{})
|
||||
if diags.HasError() {
|
||||
t.Fatalf("Error converting parameters: %v", diags.Errors())
|
||||
}
|
||||
}
|
||||
}
|
||||
output, err := toUpdatePayload(tt.input, parameters)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue