Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provider | enum validators for pcap_mode #156

Merged
merged 24 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0058b72
feat: enum validators
1riatsila1 Dec 9, 2024
4ea3eb2
Add tests
1riatsila1 Dec 9, 2024
e3958fe
remove comments
1riatsila1 Dec 9, 2024
0bbfae7
increase go lint deadline
1riatsila1 Dec 9, 2024
2ea5cbd
revert deadline
1riatsila1 Dec 9, 2024
de6d1ce
try this?
1riatsila1 Dec 9, 2024
67c0b74
more specific output
1riatsila1 Dec 9, 2024
d230070
try bigger runner
1riatsila1 Dec 9, 2024
4bbd2c2
try with exit flags
1riatsila1 Dec 9, 2024
57118d9
try with double pipe gone
1riatsila1 Dec 9, 2024
62c87de
try pinning ubuntu
1riatsila1 Dec 9, 2024
be06fee
that did not work
1riatsila1 Dec 9, 2024
9d0a24a
Try installing dependencies first
1riatsila1 Dec 9, 2024
4b89d18
check package files before doing lint
1riatsila1 Dec 9, 2024
98b9a94
Merge remote-tracking branch 'origin/main' into ay/feat/provider/enum…
1riatsila1 Dec 9, 2024
d660549
try use the large runner again
1riatsila1 Dec 9, 2024
a641fd1
Merge remote-tracking branch 'origin/main' into ay/feat/provider/enum…
1riatsila1 Dec 9, 2024
5e61817
Try with a lower version of the validators package
1riatsila1 Dec 9, 2024
f8a2db4
also rm the runner tag until it is avaliable to us
1riatsila1 Dec 9, 2024
bec345b
restore original lint deadline
1riatsila1 Dec 9, 2024
62b11be
Merge branch 'main' into ay/feat/provider/enum-validators
1riatsila1 Dec 9, 2024
1e2458d
test typo
1riatsila1 Dec 9, 2024
c1a09c2
Merge branch 'ay/feat/provider/enum-validators' of github.com:aruba-u…
1riatsila1 Dec 9, 2024
daa6fc5
Make tests more brief
1riatsila1 Dec 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected] . --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/[email protected] 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
Loading