-
Notifications
You must be signed in to change notification settings - Fork 16
Open
Labels
bugSomething isn't workingSomething isn't workingwaiting-responseIssues or pull requests waiting for an external responseIssues or pull requests waiting for an external response
Description
terraform-plugin-testing version
github.com/hashicorp/terraform-plugin-testing v1.11.0
Relevant provider source code
Custom provider function
package provider
import (
"cmp"
"context"
"github.com/Dynatrace/terraform-provider-zendesk/internal/models"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/types"
"slices"
)
var _ function.Function = &SortCustomFieldOptions{}
type SortCustomFieldOptions struct{}
func NewSortCustomFieldOptions() function.Function {
return &SortCustomFieldOptions{}
}
func (s *SortCustomFieldOptions) Metadata(ctx context.Context, request function.MetadataRequest, response *function.MetadataResponse) {
response.Name = "sort_custom_field_options"
}
func (s *SortCustomFieldOptions) Definition(ctx context.Context, request function.DefinitionRequest, response *function.DefinitionResponse) {
response.Definition = function.Definition{
Summary: "Sort custom field options",
Description: "Sort custom field options by display name",
Parameters: []function.Parameter{
function.ListParameter{
Name: "custom_field_options",
Description: "Custom field options to sort",
ElementType: types.ObjectType{AttrTypes: models.CustomFieldOptionResourceModel{}.AttributeTypes()},
},
},
Return: function.ListReturn{ElementType: types.ObjectType{AttrTypes: models.CustomFieldOptionResourceModel{}.AttributeTypes()}},
}
}
func (s *SortCustomFieldOptions) Run(ctx context.Context, request function.RunRequest, response *function.RunResponse) {
var customFieldOptionsList types.List
response.Error = function.ConcatFuncErrors(response.Error, request.Arguments.Get(ctx, &customFieldOptionsList))
var customFieldOptions []models.CustomFieldOptionResourceModel
diags := customFieldOptionsList.ElementsAs(ctx, &customFieldOptions, true)
if diags.HasError() {
response.Error = function.ConcatFuncErrors(response.Error, function.NewFuncError("Error converting list to custom field options"))
return
}
slices.SortFunc(customFieldOptions, func(a, b models.CustomFieldOptionResourceModel) int {
return cmp.Compare(a.Name.ValueString(), b.Name.ValueString())
})
customFieldOptionsList, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.CustomFieldOptionResourceModel{}.AttributeTypes()}, customFieldOptions)
if diags.HasError() {
response.Error = function.ConcatFuncErrors(response.Error, function.NewFuncError("Error converting custom field options to list"))
return
}
response.Error = function.ConcatFuncErrors(response.Error, response.Result.Set(ctx, customFieldOptionsList))
}
Function in Provider def
func (p *ZendeskProvider) Functions(ctx context.Context) []func() function.Function {
return []func() function.Function{
NewSortCustomFieldOptions,
}
}
func TestSortCustomFieldOptionsValid(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
ConfigFile: config.TestNameFile("main.tf"),
},
},
})
}
Terraform Configuration Files
locals {
cfos=[
{name:"T",value:"T"},
{name:"H",value:"H"},
{name:"D",value:"D"},
{name:"L",value:"L"}
]
}
output "test" {
value = provider::zendesk::sort_custom_field_options(local.cfos)
}
Expected Behavior
Actual Behavior
Steps to Reproduce
References
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingwaiting-responseIssues or pull requests waiting for an external responseIssues or pull requests waiting for an external response