Skip to content

Commit

Permalink
feat: implement 'is_http_2xx_status_code'
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Mar 13, 2024
1 parent 3e7f50d commit 6661ca3
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/functions/is_http_2xx_status_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "is_http_2xx_status_code function - terraform-provider-assert"
subcategory: ""
description: |-
Checks whether the HTTP status code is a valid 2xx status code
---

# function: is_http_2xx_status_code

Checks whether the HTTP status code is a valid 2xx status code



## Signature

<!-- signature generated by tfplugindocs -->
```text
is_http_2xx_status_code(status_code number) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `status_code` (Number) The HTTP status code

73 changes: 73 additions & 0 deletions internal/provider/is_http_2xx_status_code_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/function"
)

var (
_ function.Function = IsHTTP2XXStatusCodeFunction{}
)

func NewIsHTTP2XXStatusCodeFunction() function.Function {
return IsHTTP2XXStatusCodeFunction{}
}

type IsHTTP2XXStatusCodeFunction struct{}

func (r IsHTTP2XXStatusCodeFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "is_http_2xx_status_code"
}

func (r IsHTTP2XXStatusCodeFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Checks whether the HTTP status code is a valid 2xx status code",
MarkdownDescription: "Checks whether the HTTP status code is a valid 2xx status code",
Parameters: []function.Parameter{
function.Int64Parameter{
AllowNullValue: false,
AllowUnknownValues: true,
Description: "The HTTP status code",
Name: "status_code",
},
},
Return: function.BoolReturn{},
}
}

func (r IsHTTP2XXStatusCodeFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var statusCode int

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &statusCode))

if resp.Error != nil {
return
}

resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, is2xxStatusCode(statusCode)))
}

// isValid2xxStatusCode checks if an HTTP status code is within the 2xx range
func is2xxStatusCode(statusCode int) bool {
switch statusCode {
case
http.StatusOK,
http.StatusCreated,
http.StatusAccepted,
http.StatusNonAuthoritativeInfo,
http.StatusNoContent,
http.StatusResetContent,
http.StatusPartialContent,
http.StatusMultiStatus,
http.StatusAlreadyReported,
http.StatusIMUsed:
return true
default:
return false
}
}
81 changes: 81 additions & 0 deletions internal/provider/is_http_2xx_status_code_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestIsHTTP2XXStatusCodeFunction_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::is_http_2xx_status_code(200)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestIsHTTP2XXStatusCodeFunction_httpCreated(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
locals {
http_created = 201
}
output "test" {
value = provider::assert::is_http_2xx_status_code(local.http_created)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestIsHTTP2XXStatusCodeFunction_imATeaPot(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
locals {
im_a_teapot = 418
}
output "test" {
value = provider::assert::is_http_2xx_status_code(local.im_a_teapot)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
},
})
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (p *AssertProvider) Functions(ctx context.Context) []func() function.Functi
NewNotNullFunction,
NewIsNullFunction,
NewWithinRangeFunction,
NewIsHTTP2XXStatusCodeFunction,
}
}

Expand Down

0 comments on commit 6661ca3

Please sign in to comment.