From 5b044d275fa7a2648f406444126e168df979b554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ruben=20H=C3=B6nle?= Date: Wed, 21 May 2025 16:17:00 +0200 Subject: [PATCH] chore(docs): add possible values for security group rule protocol (#857) relates to #816 --- docs/resources/security_group_rule.md | 2 +- .../iaas/securitygrouprule/resource.go | 14 +++++--- stackit/internal/utils/utils.go | 9 +++++ stackit/internal/utils/utils_test.go | 36 +++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/docs/resources/security_group_rule.md b/docs/resources/security_group_rule.md index 0354cd84..bf789bf6 100644 --- a/docs/resources/security_group_rule.md +++ b/docs/resources/security_group_rule.md @@ -74,5 +74,5 @@ Required: Optional: -- `name` (String) The protocol name which the rule should match. Either `name` or `number` must be provided. +- `name` (String) The protocol name which the rule should match. Either `name` or `number` must be provided. Possible values are: `ah`, `dccp`, `egp`, `esp`, `gre`, `icmp`, `igmp`, `ipip`, `ipv6-encap`, `ipv6-frag`, `ipv6-icmp`, `ipv6-nonxt`, `ipv6-opts`, `ipv6-route`, `ospf`, `pgm`, `rsvp`, `sctp`, `tcp`, `udp`, `udplite`, `vrrp`. - `number` (Number) The protocol number which the rule should match. Either `name` or `number` must be provided. diff --git a/stackit/internal/services/iaas/securitygrouprule/resource.go b/stackit/internal/services/iaas/securitygrouprule/resource.go index ecb06f84..683f1089 100644 --- a/stackit/internal/services/iaas/securitygrouprule/resource.go +++ b/stackit/internal/services/iaas/securitygrouprule/resource.go @@ -34,10 +34,14 @@ import ( // Ensure the implementation satisfies the expected interfaces. var ( - _ resource.Resource = &securityGroupRuleResource{} - _ resource.ResourceWithConfigure = &securityGroupRuleResource{} - _ resource.ResourceWithImportState = &securityGroupRuleResource{} - icmpProtocols = []string{"icmp", "ipv6-icmp"} + _ resource.Resource = &securityGroupRuleResource{} + _ resource.ResourceWithConfigure = &securityGroupRuleResource{} + _ resource.ResourceWithImportState = &securityGroupRuleResource{} + icmpProtocols = []string{"icmp", "ipv6-icmp"} + protocolsPossibleValues = []string{ + "ah", "dccp", "egp", "esp", "gre", "icmp", "igmp", "ipip", "ipv6-encap", "ipv6-frag", "ipv6-icmp", + "ipv6-nonxt", "ipv6-opts", "ipv6-route", "ospf", "pgm", "rsvp", "sctp", "tcp", "udp", "udplite", "vrrp", + } ) type Model struct { @@ -329,7 +333,7 @@ func (r *securityGroupRuleResource) Schema(_ context.Context, _ resource.SchemaR }, Attributes: map[string]schema.Attribute{ "name": schema.StringAttribute{ - Description: "The protocol name which the rule should match. Either `name` or `number` must be provided.", + Description: fmt.Sprintf("The protocol name which the rule should match. Either `name` or `number` must be provided. %s", utils.FormatPossibleValues(protocolsPossibleValues)), Optional: true, Computed: true, Validators: []validator.String{ diff --git a/stackit/internal/utils/utils.go b/stackit/internal/utils/utils.go index 330abfa2..04c409eb 100644 --- a/stackit/internal/utils/utils.go +++ b/stackit/internal/utils/utils.go @@ -144,3 +144,12 @@ func LogError(ctx context.Context, inputDiags *diag.Diagnostics, err error, summ } core.LogAndAddError(ctx, inputDiags, summary, description) } + +// FormatPossibleValues formats a slice into a comma-separated-list for usage in the provider docs +func FormatPossibleValues(values []string) string { + var formattedValues []string + for _, value := range values { + formattedValues = append(formattedValues, fmt.Sprintf("`%v`", value)) + } + return fmt.Sprintf("Possible values are: %s.", strings.Join(formattedValues, ", ")) +} diff --git a/stackit/internal/utils/utils_test.go b/stackit/internal/utils/utils_test.go index 3ab01e58..a1be01b2 100644 --- a/stackit/internal/utils/utils_test.go +++ b/stackit/internal/utils/utils_test.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "testing" "github.com/google/go-cmp/cmp" @@ -268,3 +269,38 @@ func TestIsLegacyProjectRole(t *testing.T) { }) } } + +func TestFormatPossibleValues(t *testing.T) { + gotPrefix := "Possible values are:" + + type args struct { + values []string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "single string value", + args: args{ + values: []string{"foo"}, + }, + want: fmt.Sprintf("%s `foo`.", gotPrefix), + }, + { + name: "multiple string value", + args: args{ + values: []string{"foo", "bar", "trololol"}, + }, + want: fmt.Sprintf("%s `foo`, `bar`, `trololol`.", gotPrefix), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FormatPossibleValues(tt.args.values); got != tt.want { + t.Errorf("FormatPossibleValues() = %v, want %v", got, tt.want) + } + }) + } +}