Skip to content

Commit

Permalink
feat: acceptance test | resource | agents
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Nov 13, 2024
1 parent af5f89c commit a48cc4e
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 10 deletions.
2 changes: 2 additions & 0 deletions internal/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func (r *agentResource) Schema(
},
"notes": schema.StringAttribute{
Optional: true,
Computed: true,
},
"pcap_mode": schema.StringAttribute{
Optional: true,
Computed: true,
},
},
}
Expand Down
5 changes: 0 additions & 5 deletions internal/provider/resources/sensor_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ type sensorGroupAssignmentResourceModel struct {
GroupID types.String `tfsdk:"group_id"`
}

type SensorGroupAssignmentRequestModel struct {
GroupUID string // <group_uid:str>,
SensorUID string // <sensor_uid:str>
}

func NewSensorGroupAssignmentResource() resource.Resource {
return &sensorGroupAssignmentResource{}
}
Expand Down
6 changes: 5 additions & 1 deletion test/live/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package config
// Configuration-API Acceptance Testing (844457745a1111ef880836000a52e73e)
// And therefore the client_id and client_secret used for the acceptance tests must match this
// customer.
const AgentUid = "8260f349-5c73-394a-b786-57985d001763"
const AgentPermanentUid = "8260f349-5c73-394a-b786-57985d001763"
const AgentCreateSerial = "56fb38331f19d278"
const CustomerUid = "9c16d493-7649-40bc-975b-07422d227c0b"
const GroupUidRoot = "07422d227c0b"
const WiredNetworkUid = "ethernet-0ee5b46c2ef0"
const WiredNetworkName = "tf-provider-acceptance-tests-ethernet-0"
Expand All @@ -13,3 +15,5 @@ const WirelessNetworkName = "tf-provider-acceptance-tests-ssid-0"
const ServiceTestUid = "6f81e43d-76f1-4a15-aafe-4ce2371d918a"
const ServiceTestName = "tf-provider-acceptance-test-0"
const SensorUid = "4b031caf-cea8-411d-8928-79f518163dae"

const DeviceGatewayHost = "https://device-gateway.staging.capedev.io"
2 changes: 1 addition & 1 deletion test/live/datasources/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAgentGroupAssignmentDataSource(t *testing.T) {
data "uxi_agent" "my_agent" {
filter = {
agent_id = "` + config.AgentUid + `"
agent_id = "` + config.AgentPermanentUid + `"
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/live/datasources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

func TestAgentDataSource(t *testing.T) {
agent := util.GetAgentProperties(config.AgentUid)
agent := util.GetAgentProperties(config.AgentPermanentUid)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: provider.ProviderConfig + `
data "uxi_agent" "my_agent" {
filter = {
agent_id = "` + config.AgentUid + `"
agent_id = "` + config.AgentPermanentUid + `"
}
}
`,
Expand Down
90 changes: 90 additions & 0 deletions test/live/resources/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package resource_test

import (
"os"
"regexp"
"testing"

"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/config"
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/provider"
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/util"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAgentResource(t *testing.T) {
const (
agentName = "tf_provider_acceptance_test_agent_resource"
agentNameUpdated = agentName + "_updated"
)

// we provision an agent here so that we have something to delete later on
agentUid, err := util.ProvisionAgent{
CustomerUid: config.CustomerUid,
ProvisionToken: os.Getenv("UXI_PROVISION_TOKEN"),
DeviceSerial: config.AgentCreateSerial,
DeviceGatewayHost: config.DeviceGatewayHost,
}.Provision()
if err != nil {
panic(err)
}

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Creating an agent is not allowed
{
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "` + agentName + `"
}`,

ExpectError: regexp.MustCompile(
`creating an agent is not supported; agents can only be imported`,
),
},
// Importing an agent
{
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "` + agentName + `"
notes = ""
pcap_mode = "light"
}
import {
to = uxi_agent.my_agent
id = "` + agentUid + `"
}`,

Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", agentName),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", ""),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "id", agentUid),
),
},
// ImportState testing
{
ResourceName: "uxi_agent.my_agent",
ImportState: true,
ImportStateVerify: true,
},
// Update testing
{
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "` + agentNameUpdated + `"
notes = "notes"
pcap_mode = "off"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "id", agentUid),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", agentNameUpdated),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "notes"),
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "off"),
),
},
// Delete testing happen automatically
},
})
}
85 changes: 84 additions & 1 deletion test/live/util/agent.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package util

import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"net/http"

config_api_client "github.com/aruba-uxi/terraform-provider-hpeuxi/pkg/config-api-client"
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/config"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/nbio/st"
)
Expand All @@ -25,7 +32,7 @@ func GetAgentProperties(id string) config_api_client.AgentItem {

func CheckStateAgainstAgent(t st.Fatalf, agent config_api_client.AgentItem) resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.uxi_agent.my_agent", "id", config.AgentUid),
resource.TestCheckResourceAttr("data.uxi_agent.my_agent", "id", config.AgentPermanentUid),
resource.TestCheckResourceAttr("data.uxi_agent.my_agent", "serial", agent.Serial),
TestOptionalValue(t, "data.uxi_agent.my_agent", "model_number", agent.ModelNumber.Get()),
resource.TestCheckResourceAttrWith(
Expand All @@ -52,3 +59,79 @@ func CheckStateAgainstAgent(t st.Fatalf, agent config_api_client.AgentItem) reso
TestOptionalValue(t, "data.uxi_agent.my_agent", "pcap_mode", agent.PcapMode.Get()),
)
}

type ProvisionAgent struct {
CustomerUid string
ProvisionToken string
DeviceSerial string
DeviceGatewayHost string
}

type provisionAgentRequest struct {
Uid string `json:"uid"`
CustomerUid string `json:"customer_uid"`
ProvisionToken string `json:"provision_token"`
PlatformName string `json:"platform_name"`
DeviceSerial string `json:"device_serial"`
}

func (p ProvisionAgent) Provision() (string, error) {
url := p.DeviceGatewayHost + "/provision-zebra-device"
uid, err := p.generateUid()
if err != nil {
return uid, err
}

request := provisionAgentRequest{
Uid: uid,
CustomerUid: p.CustomerUid,
ProvisionToken: p.ProvisionToken,
PlatformName: "zebra",
DeviceSerial: p.DeviceSerial,
}
jsonData, err := json.Marshal(request)
if err != nil {
return uid, err
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return uid, err
}

req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return uid, err
}
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusCreated {
return uid, fmt.Errorf(
"unexpected status code returned: %d\nresponse: %s",
resp.StatusCode,
string(body),
)
}

return uid, nil
}

func (p ProvisionAgent) generateUid() (string, error) {
// Create an MD5 hash of the serial string
hasher := md5.New()
hasher.Write([]byte(p.DeviceSerial))
digest := hasher.Sum(nil)

// Use the first 16 bytes of the digest to create a UUID v3
uuid, err := uuid.FromBytes(digest[:16])
if err != nil {
return "", err
}
uuid[6] = (uuid[6] & 0x0f) | 0x30 // Set the version to 3 (MD5-based UUID)
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Set the variant to RFC 4122

return uuid.String(), nil
}

0 comments on commit a48cc4e

Please sign in to comment.