diff --git a/.github/workflows/lint-test-code.yaml b/.github/workflows/lint-test-code.yaml index f0c7281..6870e33 100644 --- a/.github/workflows/lint-test-code.yaml +++ b/.github/workflows/lint-test-code.yaml @@ -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: | diff --git a/Justfile b/Justfile index e9f23c4..1a0b239 100644 --- a/Justfile +++ b/Justfile @@ -61,6 +61,7 @@ lint: set -e set -o pipefail + echo "gofmt -d ." output=$(gofmt -d .) if [ -n "$output" ]; then echo "$output" @@ -68,17 +69,22 @@ lint: 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: diff --git a/go.mod b/go.mod index 2b5dc69..7bd92fc 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4d0d3d0..6b3cc6b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/provider/resources/resource_agent.go b/internal/provider/resources/resource_agent.go index f38209c..098c200 100644 --- a/internal/provider/resources/resource_agent.go +++ b/internal/provider/resources/resource_agent.go @@ -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" @@ -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)..., + ), + }, }, }, } diff --git a/internal/provider/resources/resource_sensor.go b/internal/provider/resources/resource_sensor.go index e8ae6a6..3bfe1aa 100644 --- a/internal/provider/resources/resource_sensor.go +++ b/internal/provider/resources/resource_sensor.go @@ -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" @@ -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)..., + ), + }, }, }, } diff --git a/internal/provider/util/string.go b/internal/provider/util/string.go new file mode 100644 index 0000000..563cc26 --- /dev/null +++ b/internal/provider/util/string.go @@ -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 +} diff --git a/test/mocked/resources/agent_test.go b/test/mocked/resources/agent_test.go index 98d1f09..ca644e3 100644 --- a/test/mocked/resources/agent_test.go +++ b/test/mocked/resources/agent_test.go @@ -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() diff --git a/test/mocked/resources/sensor_test.go b/test/mocked/resources/sensor_test.go index ffa260c..b5bef02 100644 --- a/test/mocked/resources/sensor_test.go +++ b/test/mocked/resources/sensor_test.go @@ -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()