From 3fb28d12482222fc46565034bdedfd3e97e49b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Tue, 9 Jul 2024 13:14:38 +0100 Subject: [PATCH] Document possible values of schema fields (#455) * Document possible values of schema fields * Change from possible to supported --- docs/resources/dns_zone.md | 2 +- docs/resources/loadbalancer.md | 4 +-- docs/resources/mongodbflex_user.md | 2 +- docs/resources/postgresflex_user.md | 2 +- .../internal/services/dns/zone/resource.go | 6 ++-- .../loadbalancer/loadbalancer/resource.go | 16 +++++---- .../services/mongodbflex/user/resource.go | 7 ++-- .../services/postgresflex/user/resource.go | 5 +++ stackit/internal/utils/utils.go | 14 ++++++++ stackit/internal/utils/utils_test.go | 33 +++++++++++++++++++ 10 files changed, 76 insertions(+), 15 deletions(-) diff --git a/docs/resources/dns_zone.md b/docs/resources/dns_zone.md index b65ed39f..9a5f6488 100644 --- a/docs/resources/dns_zone.md +++ b/docs/resources/dns_zone.md @@ -47,7 +47,7 @@ resource "stackit_dns_zone" "example" { - `primaries` (List of String) Primary name server for secondary zone. E.g. ["1.2.3.4"] - `refresh_time` (Number) Refresh time. E.g. 3600 - `retry_time` (Number) Retry time. E.g. 600 -- `type` (String) Zone type. Defaults to `primary` +- `type` (String) Zone type. Defaults to `primary`. Supported values are: `primary`, `secondary`. ### Read-Only diff --git a/docs/resources/loadbalancer.md b/docs/resources/loadbalancer.md index c94a92e3..ad84605c 100644 --- a/docs/resources/loadbalancer.md +++ b/docs/resources/loadbalancer.md @@ -203,7 +203,7 @@ Optional: - `display_name` (String) - `port` (Number) Port number where we listen for traffic. -- `protocol` (String) Protocol is the highest network protocol we understand to load balance. +- `protocol` (String) Protocol is the highest network protocol we understand to load balance. Supported values are: `PROTOCOL_UNSPECIFIED`, `PROTOCOL_TCP`, `PROTOCOL_UDP`, `PROTOCOL_TCP_PROXY`, `PROTOCOL_TLS_PASSTHROUGH`. - `server_name_indicators` (Attributes List) A list of domain names to match in order to pass TLS traffic to the target pool in the current listener (see [below for nested schema](#nestedatt--listeners--server_name_indicators)) - `target_pool` (String) Reference target pool by target pool name. @@ -225,7 +225,7 @@ Required: Optional: -- `role` (String) The role defines how the load balancer is using the network. +- `role` (String) The role defines how the load balancer is using the network. Supported values are: `ROLE_UNSPECIFIED`, `ROLE_LISTENERS_AND_TARGETS`, `ROLE_LISTENERS`, `ROLE_TARGETS`. diff --git a/docs/resources/mongodbflex_user.md b/docs/resources/mongodbflex_user.md index 1318bb16..b3afef14 100644 --- a/docs/resources/mongodbflex_user.md +++ b/docs/resources/mongodbflex_user.md @@ -30,7 +30,7 @@ resource "stackit_mongodbflex_user" "example" { - `database` (String) - `instance_id` (String) ID of the MongoDB Flex instance. - `project_id` (String) STACKIT project ID to which the instance is associated. -- `roles` (Set of String) Database access levels for the user. +- `roles` (Set of String) Database access levels for the user. Supported values are: `read`, `readWrite`. ### Optional diff --git a/docs/resources/postgresflex_user.md b/docs/resources/postgresflex_user.md index e272837c..01346ac3 100644 --- a/docs/resources/postgresflex_user.md +++ b/docs/resources/postgresflex_user.md @@ -28,7 +28,7 @@ resource "stackit_postgresflex_user" "example" { - `instance_id` (String) ID of the PostgresFlex instance. - `project_id` (String) STACKIT project ID to which the instance is associated. -- `roles` (Set of String) +- `roles` (Set of String) Database access levels for the user. Supported values are: `login`, `createdb`. - `username` (String) ### Read-Only diff --git a/stackit/internal/services/dns/zone/resource.go b/stackit/internal/services/dns/zone/resource.go index 47f127fc..f4271e74 100644 --- a/stackit/internal/services/dns/zone/resource.go +++ b/stackit/internal/services/dns/zone/resource.go @@ -114,6 +114,8 @@ func (r *zoneResource) Configure(ctx context.Context, req resource.ConfigureRequ // Schema defines the schema for the resource. func (r *zoneResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + primaryOptions := []string{"primary", "secondary"} + resp.Schema = schema.Schema{ Description: "DNS Zone resource schema.", Attributes: map[string]schema.Attribute{ @@ -254,12 +256,12 @@ func (r *zoneResource) Schema(_ context.Context, _ resource.SchemaRequest, resp }, }, "type": schema.StringAttribute{ - Description: "Zone type. Defaults to `primary`", + Description: "Zone type. Defaults to `primary`. " + utils.SupportedValuesDocumentation(primaryOptions), Optional: true, Computed: true, Default: stringdefault.StaticString("primary"), Validators: []validator.String{ - stringvalidator.OneOf("primary", "secondary"), + stringvalidator.OneOf(primaryOptions...), }, }, "primary_name_server": schema.StringAttribute{ diff --git a/stackit/internal/services/loadbalancer/loadbalancer/resource.go b/stackit/internal/services/loadbalancer/loadbalancer/resource.go index ce16592e..312f2e62 100644 --- a/stackit/internal/services/loadbalancer/loadbalancer/resource.go +++ b/stackit/internal/services/loadbalancer/loadbalancer/resource.go @@ -28,11 +28,12 @@ import ( "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/core/utils" + sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/wait" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" "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" ) @@ -219,6 +220,9 @@ func (r *loadBalancerResource) Configure(ctx context.Context, req resource.Confi // Schema defines the schema for the resource. func (r *loadBalancerResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + protocolOptions := []string{"PROTOCOL_UNSPECIFIED", "PROTOCOL_TCP", "PROTOCOL_UDP", "PROTOCOL_TCP_PROXY", "PROTOCOL_TLS_PASSTHROUGH"} + roleOptions := []string{"ROLE_UNSPECIFIED", "ROLE_LISTENERS_AND_TARGETS", "ROLE_LISTENERS", "ROLE_TARGETS"} + descriptions := map[string]string{ "main": "Load Balancer resource schema.", "id": "Terraform's internal resource ID. It is structured as \"`project_id`\",\"`name`\".", @@ -226,12 +230,12 @@ func (r *loadBalancerResource) Schema(_ context.Context, _ resource.SchemaReques "external_address": "External Load Balancer IP address where this Load Balancer is exposed.", "listeners": "List of all listeners which will accept traffic. Limited to 20.", "port": "Port number where we listen for traffic.", - "protocol": "Protocol is the highest network protocol we understand to load balance.", + "protocol": "Protocol is the highest network protocol we understand to load balance. " + utils.SupportedValuesDocumentation(protocolOptions), "target_pool": "Reference target pool by target pool name.", "name": "Load balancer name.", "networks": "List of networks that listeners and targets reside in.", "network_id": "Openstack network ID.", - "role": "The role defines how the load balancer is using the network.", + "role": "The role defines how the load balancer is using the network. " + utils.SupportedValuesDocumentation(roleOptions), "options": "Defines any optional functionality you want to have enabled on your load balancer.", "acl": "Load Balancer is accessible only from an IP address in this range.", "private_network_only": "If true, Load Balancer is accessible only via a private network IP address.", @@ -350,7 +354,7 @@ The example below uses OpenStack to create the network, router, a public IP addr stringplanmodifier.UseStateForUnknown(), }, Validators: []validator.String{ - stringvalidator.OneOf("PROTOCOL_UNSPECIFIED", "PROTOCOL_TCP", "PROTOCOL_UDP", "PROTOCOL_TCP_PROXY", "PROTOCOL_TLS_PASSTHROUGH"), + stringvalidator.OneOf(protocolOptions...), }, }, "server_name_indicators": schema.ListNestedAttribute{ @@ -420,7 +424,7 @@ The example below uses OpenStack to create the network, router, a public IP addr stringplanmodifier.UseStateForUnknown(), }, Validators: []validator.String{ - stringvalidator.OneOf("ROLE_UNSPECIFIED", "ROLE_LISTENERS_AND_TARGETS", "ROLE_LISTENERS", "ROLE_TARGETS"), + stringvalidator.OneOf(roleOptions...), }, }, }, @@ -669,7 +673,7 @@ func (r *loadBalancerResource) Update(ctx context.Context, req resource.UpdateRe ctx = tflog.SetField(ctx, "target_pool_name", targetPoolName) // Generate API request body from model - payload, err := toTargetPoolUpdatePayload(ctx, utils.Ptr(targetPoolModel)) + payload, err := toTargetPoolUpdatePayload(ctx, sdkUtils.Ptr(targetPoolModel)) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating load balancer", fmt.Sprintf("Creating API payload for target pool: %v", err)) return diff --git a/stackit/internal/services/mongodbflex/user/resource.go b/stackit/internal/services/mongodbflex/user/resource.go index 3f907600..e18fef55 100644 --- a/stackit/internal/services/mongodbflex/user/resource.go +++ b/stackit/internal/services/mongodbflex/user/resource.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" "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" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -99,13 +100,15 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ // Schema defines the schema for the resource. func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + rolesOptions := []string{"read", "readWrite"} + descriptions := map[string]string{ "main": "MongoDB Flex user resource schema. Must have a `region` specified in the provider configuration.", "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".", "user_id": "User ID.", "instance_id": "ID of the MongoDB Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", - "roles": "Database access levels for the user.", + "roles": "Database access levels for the user. " + utils.SupportedValuesDocumentation(rolesOptions), } resp.Schema = schema.Schema{ @@ -166,7 +169,7 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp Required: true, Validators: []validator.Set{ setvalidator.ValueStringsAre( - stringvalidator.OneOf("read", "readWrite"), + stringvalidator.OneOf(rolesOptions...), ), }, }, diff --git a/stackit/internal/services/postgresflex/user/resource.go b/stackit/internal/services/postgresflex/user/resource.go index 594f5f25..ba4bd023 100644 --- a/stackit/internal/services/postgresflex/user/resource.go +++ b/stackit/internal/services/postgresflex/user/resource.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" "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" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -100,12 +101,15 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ // Schema defines the schema for the resource. func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + rolesOptions := []string{"login", "createdb"} + descriptions := map[string]string{ "main": "PostgresFlex user resource schema. Must have a `region` specified in the provider configuration.", "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".", "user_id": "User ID.", "instance_id": "ID of the PostgresFlex instance.", "project_id": "STACKIT project ID to which the instance is associated.", + "roles": "Database access levels for the user. " + utils.SupportedValuesDocumentation(rolesOptions), } resp.Schema = schema.Schema{ @@ -159,6 +163,7 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp }, }, "roles": schema.SetAttribute{ + Description: descriptions["roles"], ElementType: types.StringType, Required: true, PlanModifiers: []planmodifier.Set{ diff --git a/stackit/internal/utils/utils.go b/stackit/internal/utils/utils.go index 38664b67..346c63d2 100644 --- a/stackit/internal/utils/utils.go +++ b/stackit/internal/utils/utils.go @@ -76,3 +76,17 @@ func SimplifyBackupSchedule(schedule string) string { }) return simplifiedSchedule } + +func SupportedValuesDocumentation(values []string) string { + if len(values) == 0 { + return "" + } + return "Supported values are: " + strings.Join(quoteValues(values), ", ") + "." +} + +func quoteValues(values []string) []string { + for i, value := range values { + values[i] = fmt.Sprintf("`%s`", value) + } + return values +} diff --git a/stackit/internal/utils/utils_test.go b/stackit/internal/utils/utils_test.go index a62e9651..77a1e32f 100644 --- a/stackit/internal/utils/utils_test.go +++ b/stackit/internal/utils/utils_test.go @@ -192,3 +192,36 @@ func TestSimplifyBackupSchedule(t *testing.T) { }) } } + +func TestSupportedValuesDocumentation(t *testing.T) { + tests := []struct { + description string + values []string + expected string + }{ + { + "empty values", + []string{}, + "", + }, + { + "single value", + []string{"value"}, + "Supported values are: `value`.", + }, + { + "multiple values", + []string{"value1", "value2", "value3"}, + "Supported values are: `value1`, `value2`, `value3`.", + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + output := SupportedValuesDocumentation(tt.values) + if output != tt.expected { + t.Fatalf("Data does not match: %s", output) + } + }) + } +}