Skip to content

Commit

Permalink
feat: provider | enum validators for pcap_mode (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Dec 9, 2024
1 parent 385cfd9 commit 19c3dc4
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint-test-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run Code Linting Test
run: just lint

- name: Check package files up to date
run: |
just tidy
git diff --exit-code -- go.* pkg/config-api-client/go.*
git diff --exit-code -- go.* pkg/config-api-client/go.* || exit 1
- name: Run Code Linting Test
run: just lint

- name: Run Tests
run: |
Expand Down
6 changes: 6 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,30 @@ lint:
set -e
set -o pipefail
echo "gofmt -d ."
output=$(gofmt -d .)
if [ -n "$output" ]; then
echo "$output"
echo "Error: (gofmt) formatting required" >&2
exit 1
fi

echo "golines . --dry-run"
output=$(go run github.com/segmentio/golines@v0.12.2 . --dry-run)
echo output
if [ -n "$output" ]; then
echo "$output"
echo "Error: (golines) formatting required" >&2
exit 1
fi

echo "golangci-lint run"
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.1 run

echo "python -m tools.lint-attribution lint"
python -m tools.lint-attribution lint

echo "terraform fmt -recursive -check"
terraform fmt -recursive -check

fmt:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/uuid v1.6.0
github.com/h2non/gock v1.2.0
github.com/hashicorp/terraform-plugin-framework v1.10.0
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
github.com/hashicorp/terraform-plugin-framework v1.10.0 h1:xXhICE2Fns1RYZxEQebwkB2+kXouLC932Li9qelozrc=
github.com/hashicorp/terraform-plugin-framework v1.10.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0 h1:DKb1bX7/EPZUTW6F5zdwJzS/EZ/ycVD6JAW5RYOj4f8=
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0/go.mod h1:dzxOiHh7O9CAwc6p8N4mR1H++LtRkl+u+21YNiBVNno=
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resources/resource_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/aruba-uxi/terraform-provider-hpeuxi/internal/provider/util"
Expand Down Expand Up @@ -95,6 +97,11 @@ func (r *agentResource) Schema(
Description: "The packet capture mode of the agent.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf(
util.ConvertToStrings(config_api_client.AllowedAgentPcapModeEnumValues)...,
),
},
},
},
}
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resources/resource_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package resources
import (
"context"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/aruba-uxi/terraform-provider-hpeuxi/internal/provider/util"
Expand Down Expand Up @@ -113,6 +115,11 @@ func (r *sensorResource) Schema(
Optional: true,
// computed because goes from nil -> "light" when sensor becomes configured
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf(
util.ConvertToStrings(config_api_client.AllowedSensorPcapModeEnumValues)...,
),
},
},
},
}
Expand Down
14 changes: 14 additions & 0 deletions internal/provider/util/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright 2024 Hewlett Packard Enterprise Development LP.
*/

package util

func ConvertToStrings[T ~string](input []T) []string {
result := make([]string, len(input))
for i, v := range input {
result[i] = string(v)
}

return result
}
25 changes: 25 additions & 0 deletions test/mocked/resources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ func TestAgentResource(t *testing.T) {
mockOAuth.Mock.Disable()
}

func Test_AgentResource_WithInvalidPcapMode_ShouldFail(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// agent with invalid pcap_mode
{
Config: provider.ProviderConfig + `
resource "hpeuxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "invalid_pcap_mode"
}`,
ExpectError: regexp.MustCompile(
`(?s)Attribute pcap_mode value must be one of: \["light" "full" "off"\], got:\s*"invalid_pcap_mode"`,
),
},
},
})

mockOAuth.Mock.Disable()
}

func TestAgentResourceTooManyRequestsHandling(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()
Expand Down
26 changes: 26 additions & 0 deletions test/mocked/resources/sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ func TestSensorResource(t *testing.T) {
mockOAuth.Mock.Disable()
}

func Test_SensorResource_WithInvalidPcapMode_ShouldFail(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// sensor with invalid pcap_mode
{
Config: provider.ProviderConfig + `
resource "hpeuxi_sensor" "my_sensor" {
name = "name"
address_note = "address_note"
notes = "notes"
pcap_mode = "invalid_pcap_mode"
}`,
ExpectError: regexp.MustCompile(
`(?s)Attribute pcap_mode value must be one of: \["light" "full" "off"\], got:\s*"invalid_pcap_mode"`,
),
},
},
})

mockOAuth.Mock.Disable()
}

func TestSensorResourceTooManyRequestsHandling(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()
Expand Down

0 comments on commit 19c3dc4

Please sign in to comment.