From 002a307beb722e5123ed1e0d17a623afc5c6434f Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 19 Sep 2024 10:14:59 +0200 Subject: [PATCH] refactor: neaten up test directory --- .../test/data-sources/group_test.go | 90 ++++ .../sensor_group_assignment_test.go | 46 ++ .../test/data-sources/wired_group_test.go | 52 +++ .../test/data-sources/wireless_group_test.go | 53 +++ .../test/provider/provider.go | 26 ++ .../resources/agent_group_assignment_test.go | 169 ++++++++ .../test/resources/agent_test.go | 87 ++++ .../test/resources/group_test.go | 146 +++++++ .../network_group_assignment_test.go | 403 ++++++++++++++++++ .../resources/sensor_group_assignment_test.go | 232 ++++++++++ .../test/resources/sensor_test.go | 114 +++++ .../test/resources/service_test.go | 91 ++++ .../service_test_group_assignment_test.go | 196 +++++++++ .../test/resources/wired_network_test.go | 121 ++++++ .../test/resources/wireless_network_test.go | 120 ++++++ pkg/config-api-provider/test/util/utils.go | 243 +++++++++++ 16 files changed, 2189 insertions(+) create mode 100644 pkg/config-api-provider/test/data-sources/group_test.go create mode 100644 pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go create mode 100644 pkg/config-api-provider/test/data-sources/wired_group_test.go create mode 100644 pkg/config-api-provider/test/data-sources/wireless_group_test.go create mode 100644 pkg/config-api-provider/test/provider/provider.go create mode 100644 pkg/config-api-provider/test/resources/agent_group_assignment_test.go create mode 100644 pkg/config-api-provider/test/resources/agent_test.go create mode 100644 pkg/config-api-provider/test/resources/group_test.go create mode 100644 pkg/config-api-provider/test/resources/network_group_assignment_test.go create mode 100644 pkg/config-api-provider/test/resources/sensor_group_assignment_test.go create mode 100644 pkg/config-api-provider/test/resources/sensor_test.go create mode 100644 pkg/config-api-provider/test/resources/service_test.go create mode 100644 pkg/config-api-provider/test/resources/service_test_group_assignment_test.go create mode 100644 pkg/config-api-provider/test/resources/wired_network_test.go create mode 100644 pkg/config-api-provider/test/resources/wireless_network_test.go create mode 100644 pkg/config-api-provider/test/util/utils.go diff --git a/pkg/config-api-provider/test/data-sources/group_test.go b/pkg/config-api-provider/test/data-sources/group_test.go new file mode 100644 index 00000000..f23244e9 --- /dev/null +++ b/pkg/config-api-provider/test/data-sources/group_test.go @@ -0,0 +1,90 @@ +package data_source + +import ( + "regexp" + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestGroupDataSource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Test no filters set + { + Config: provider.ProviderConfig + ` + data "uxi_group" "my_group" { + filter = {} + } + `, + ExpectError: regexp.MustCompile(`either filter.group_id must be set or 'filter.is_root = true' is required`), + }, + // Test too many filters set + { + Config: provider.ProviderConfig + ` + data "uxi_group" "my_group" { + filter = { + is_root = true + group_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`group_id and 'is_root = true' cannot both be set`), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_root_group.my_root_group", "id", "mock_uid"), + ), + }, + // Test Read, is_root not set + { + PreConfig: func() { + util.MockGetGroup( + "uid", + util.GenerateGroupPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseModel("uid", "", ""))}), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_group" "my_group" { + filter = { + group_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_group.my_group", "id", "uid"), + ), + }, + // Test Read, is_root is false + { + PreConfig: func() { + util.MockGetGroup( + "uid", + util.GenerateGroupPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseModel("uid", "", ""))}), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_group" "my_group" { + filter = { + is_root = false + group_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_group.my_group", "id", "uid"), + ), + }, + // TODO: Test retrieving the root group + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go new file mode 100644 index 00000000..3d147043 --- /dev/null +++ b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go @@ -0,0 +1,46 @@ +package data_source_test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "testing" +) + +func TestSensorGroupAssignmentDataSource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: func() { + util.MockGetSensorGroupAssignment( + "uid", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("uid", "")), + }), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + filter = { + sensor_group_assignment_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "id", "uid"), + resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "group_id", "group_uid"), + resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "sensor_id", "sensor_uid"), + ), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/wired_group_test.go b/pkg/config-api-provider/test/data-sources/wired_group_test.go new file mode 100644 index 00000000..2fa33816 --- /dev/null +++ b/pkg/config-api-provider/test/data-sources/wired_group_test.go @@ -0,0 +1,52 @@ +package data_source_test + +import ( + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestWiredNetworkDataSource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_wired_network" "my_wired_network" { + filter = { + wired_network_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "id", "uid"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "alias", "alias"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "ip_version", "ip_version"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "security", "security"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "dns_lookup_domain", "dns_lookup_domain"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "disable_edns", "false"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "use_dns64", "false"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "external_connectivity", "false"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "vlan_id", "123"), + ), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/wireless_group_test.go b/pkg/config-api-provider/test/data-sources/wireless_group_test.go new file mode 100644 index 00000000..50a61d2c --- /dev/null +++ b/pkg/config-api-provider/test/data-sources/wireless_group_test.go @@ -0,0 +1,53 @@ +package data_source_test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "testing" + + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestWirelessNetworkDataSource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_wireless_network" "my_wireless_network" { + filter = { + wireless_network_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "id", "uid"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "ssid", "ssid"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "alias", "alias"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "ip_version", "ip_version"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "security", "security"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "hidden", "false"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "band_locking", "band_locking"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "dns_lookup_domain", "dns_lookup_domain"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "disable_edns", "false"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "use_dns64", "false"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "external_connectivity", "false"), + ), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/provider/provider.go b/pkg/config-api-provider/test/provider/provider.go new file mode 100644 index 00000000..dc04bb91 --- /dev/null +++ b/pkg/config-api-provider/test/provider/provider.go @@ -0,0 +1,26 @@ +package provider + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider" + "github.com/hashicorp/terraform-plugin-framework/providerserver" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" +) + +const ( + ProviderConfig = `provider "uxi" { + host = "test.api.capenetworks.com" + client_id = "client_id" + client_secret = "client_secret" + token_url = "https://test.sso.common.cloud.hpe.com/as/token.oauth2" + }` +) + +var ( + // TestAccProtoV6ProviderFactories are used to instantiate a provider during + // acceptance testing. The factory function will be invoked for every Terraform + // CLI command executed to create a provider server to which the CLI can + // reattach. + TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ + "uxi": providerserver.NewProtocol6WithError(provider.New("test")()), + } +) diff --git a/pkg/config-api-provider/test/resources/agent_group_assignment_test.go b/pkg/config-api-provider/test/resources/agent_group_assignment_test.go new file mode 100644 index 00000000..d2116f3c --- /dev/null +++ b/pkg/config-api-provider/test/resources/agent_group_assignment_test.go @@ -0,0 +1,169 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAgentGroupAssignmentResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a agent group assignment + { + PreConfig: func() { + // required for agent import + resources.GetAgent = func(uid string) resources.AgentResponseModel { + return util.GenerateAgentResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 1, + ) + + // required for agent group assignment create + agentGroupAssignmentResponse := util.GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid", "") + resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel { + return agentGroupAssignmentResponse + } + resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel { + return agentGroupAssignmentResponse + } + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_agent" "my_agent" { + name = "name" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_agent.my_agent + id = "agent_uid" + } + + resource "uxi_agent_group_assignment" "my_agent_group_assignment" { + agent_id = uxi_agent.my_agent.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "agent_id", "agent_uid"), + resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "group_id", "group_uid"), + resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "id", "agent_group_assignment_uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_agent_group_assignment.my_agent_group_assignment", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + resources.GetAgent = func(uid string) resources.AgentResponseModel { + if uid == "agent_uid" { + return util.GenerateAgentResponseModel(uid, "") + } else { + return util.GenerateAgentResponseModel(uid, "_2") + } + } + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 2, + ) + + // required for creating another group + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + + // required for agent group assignment create + resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel { + if uid == "agent_group_assignment_uid" { + return util.GenerateAgentGroupAssignmentResponse(uid, "") + } else { + return util.GenerateAgentGroupAssignmentResponse(uid, "_2") + } + } + resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel { + return util.GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid_2", "_2") + } + }, + Config: provider.ProviderConfig + ` + // the original resources + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_agent" "my_agent" { + name = "name" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_agent.my_agent + id = "agent_uid" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "name_2" + parent_group_id = "parent_uid_2" + } + + resource "uxi_agent" "my_agent_2" { + name = "name_2" + notes = "notes_2" + pcap_mode = "light_2" + } + + import { + to = uxi_agent.my_agent_2 + id = "agent_uid_2" + } + + // the assignment update, updated from agent/group to agent_2/group_2 + resource "uxi_agent_group_assignment" "my_agent_group_assignment" { + agent_id = uxi_agent.my_agent_2.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "agent_id", "agent_uid_2"), + resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "group_id", "group_uid_2"), + resource.TestCheckResourceAttr("uxi_agent_group_assignment.my_agent_group_assignment", "id", "agent_group_assignment_uid_2"), + ), + }, + // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/agent_test.go b/pkg/config-api-provider/test/resources/agent_test.go new file mode 100644 index 00000000..5db7bc45 --- /dev/null +++ b/pkg/config-api-provider/test/resources/agent_test.go @@ -0,0 +1,87 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "regexp" + "testing" +) + +func TestAgentResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + 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 = "name" + notes = "note" + pcap_mode = "light" + }`, + + ExpectError: regexp.MustCompile(`creating an agent is not supported; agents can only be imported`), + }, + // Importing an agent + { + PreConfig: func() { + resources.GetAgent = func(uid string) resources.AgentResponseModel { + return util.GenerateAgentResponseModel(uid, "") + } + }, + Config: provider.ProviderConfig + ` + resource "uxi_agent" "my_agent" { + name = "name" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_agent.my_agent + id = "uid" + }`, + + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "name"), + resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "notes"), + resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light"), + resource.TestCheckResourceAttr("uxi_agent.my_agent", "id", "uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_agent.my_agent", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + resources.GetAgent = func(uid string) resources.AgentResponseModel { + return util.GenerateAgentResponseModel(uid, "_2") + } + }, + Config: provider.ProviderConfig + ` + resource "uxi_agent" "my_agent" { + name = "name_2" + notes = "notes_2" + pcap_mode = "light_2" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_agent.my_agent", "name", "name_2"), + resource.TestCheckResourceAttr("uxi_agent.my_agent", "notes", "notes_2"), + resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light_2"), + ), + }, + // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/group_test.go b/pkg/config-api-provider/test/resources/group_test.go new file mode 100644 index 00000000..9f153c85 --- /dev/null +++ b/pkg/config-api-provider/test/resources/group_test.go @@ -0,0 +1,146 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "regexp" + "testing" +) + +type Fetcher interface { + FetchData() ([]byte, error) +} + +func TestGroupResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create and Read testing + { + PreConfig: func() { + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), 1) + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_group.my_group", "name", "name"), + resource.TestCheckResourceAttr("uxi_group.my_group", "parent_group_id", "parent_uid"), + resource.TestCheckResourceAttr("uxi_group.my_group", "id", "uid"), + ), + }, + // ImportState testing + { + PreConfig: func() { + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), + }), + 1, + ) + }, + ResourceName: "uxi_group.my_group", + ImportState: true, + ImportStateVerify: true, + }, + // Update that does not trigger a recreate + { + PreConfig: func() { + resources.UpdateGroup = func(request resources.GroupUpdateRequestModel) resources.GroupResponseModel { + return util.GenerateGroupResponseModel("uid", "_2", "") + } + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("uid", "_2", "")), + }), + 2, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name_2" + parent_group_id = "parent_uid" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_group.my_group", "name", "name_2"), + resource.TestCheckResourceAttr("uxi_group.my_group", "parent_group_id", "parent_uid"), + resource.TestCheckResourceAttr("uxi_group.my_group", "id", "uid"), + ), + Destroy: false, + }, + // Update that does trigger a recreate + { + PreConfig: func() { + // existing group + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), + }), + 1, + ) + // new group (replacement) + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("new_uid", "", "_2")), 1) + util.MockGetGroup("new_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("new_uid", "", "_2")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid_2" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_group.my_group", "name", "name"), + resource.TestCheckResourceAttr("uxi_group.my_group", "parent_group_id", "parent_uid_2"), + resource.TestCheckResourceAttr("uxi_group.my_group", "id", "new_uid"), + ), + }, + // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} + +func TestRootGroupResource(t *testing.T) { + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Importing the root group does not work + { + PreConfig: func() { + resources.GetRootGroupUID = func() string { return "my_root_group_uid" } + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_root_group" { + name = "name" + parent_group_id = "some_random_string" + } + + import { + to = uxi_group.my_root_group + id = "my_root_group_uid" + }`, + ExpectError: regexp.MustCompile(`the root node cannot be used as a resource`), + }, + }, + }) +} diff --git a/pkg/config-api-provider/test/resources/network_group_assignment_test.go b/pkg/config-api-provider/test/resources/network_group_assignment_test.go new file mode 100644 index 00000000..64c1d4f4 --- /dev/null +++ b/pkg/config-api-provider/test/resources/network_group_assignment_test.go @@ -0,0 +1,403 @@ +package test + +import ( + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestNetworkGroupAssignmentResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + // Test Wired Network Group Assignment + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a network group assignment + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 2, + ) + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 2, + ) + + // required for network group assignment create + resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + } + resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + } + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wired_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_network + id = "network_uid" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "group_id", "group_uid"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "id", "network_group_assignment_uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_network_group_assignment.my_network_group_assignment", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid_2", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid_2", "_2")}), + 2, + ) + util.MockGetWiredNetwork( + "network_uid", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 2, + ) + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 3, + ) + + // required for creating another group + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + + // required for network group assignment create + resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") + } + resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { + if uid == "network_group_assignment_uid" { + return util.GenerateNetworkGroupAssignmentResponse(uid, "") + } else { + return util.GenerateNetworkGroupAssignmentResponse(uid, "_2") + } + } + }, + Config: provider.ProviderConfig + ` + // the original resources + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wired_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_network + id = "network_uid" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "name_2" + parent_group_id = "parent_uid_2" + } + + resource "uxi_wired_network" "my_network_2" { + alias = "alias_2" + } + + import { + to = uxi_wired_network.my_network_2 + id = "network_uid_2" + } + + // the assignment update, updated from network/group to network_2/group_2 + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network_2.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid_2"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "group_id", "group_uid_2"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "id", "network_group_assignment_uid_2"), + ), + }, + // Remove networks from state + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid_2", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid_2", "_2")}), + 1, + ) + util.MockGetWiredNetwork( + "network_uid", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 2, + ) + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_wired_network.my_network + + lifecycle { + destroy = false + } + } + + removed { + from = uxi_wired_network.my_network_2 + + lifecycle { + destroy = false + } + }`, + }, + // Delete testing automatically occurs in TestCase + }, + }) + + // Test Wireless Network Group Assignment + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a network group assignment + { + PreConfig: func() { + // required for network import + util.MockGetWirelessNetwork( + "network_uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid", "")}), + 2, + ) + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 1, + ) + + // required for network group assignment create + resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + } + resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { + return util.GenerateNetworkGroupAssignmentResponse(uid, "") + } + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wireless_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wireless_network.my_network + id = "network_uid" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wireless_network.my_network.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "group_id", "group_uid"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "id", "network_group_assignment_uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_network_group_assignment.my_network_group_assignment", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "network_uid_2", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid_2", "_2")}), + 2, + ) + util.MockGetWirelessNetwork( + "network_uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid", "")}), + 2, + ) + + // required for creating another group + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 3, + ) + + // required for network group assignment create + resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { + if uid == "network_group_assignment_uid" { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + } else { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") + } + } + resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") + } + }, + Config: provider.ProviderConfig + ` + // the original resources + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wireless_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wireless_network.my_network + id = "network_uid" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "name_2" + parent_group_id = "parent_uid_2" + } + + resource "uxi_wireless_network" "my_network_2" { + alias = "alias_2" + } + + import { + to = uxi_wireless_network.my_network_2 + id = "network_uid_2" + } + + // the assignment update, updated from network/group to network_2/group_2 + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wireless_network.my_network_2.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid_2"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "group_id", "group_uid_2"), + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "id", "network_group_assignment_uid_2"), + ), + }, + // Remove networks from state + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "network_uid_2", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid_2", "_2")}), + 1, + ) + util.MockGetWirelessNetwork( + "network_uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid", "")}), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 2, + ) + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_wireless_network.my_network + + lifecycle { + destroy = false + } + } + + removed { + from = uxi_wireless_network.my_network_2 + + lifecycle { + destroy = false + } + }`, + }, + // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go new file mode 100644 index 00000000..3005721f --- /dev/null +++ b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go @@ -0,0 +1,232 @@ +package test + +import ( + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestSensorGroupAssignmentResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a sensor group assignment + { + PreConfig: func() { + // required for sensor import + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 1, + ) + + // required for sensor group assignment create + resources.CreateSensorGroupAssignment = func(request resources.SensorGroupAssignmentRequestModel) resources.SensorGroupAssignmentResponseModel { + return util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "") + } + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + }), + 1, + ) + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "sensor_uid" + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "sensor_id", "sensor_uid"), + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "group_id", "group_uid"), + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "id", "sensor_group_assignment_uid"), + ), + }, + // ImportState testing + { + PreConfig: func() { + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + }), + 1, + ) + }, + ResourceName: "uxi_sensor_group_assignment.my_sensor_group_assignment", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + resources.GetSensor = func(uid string) resources.SensorResponseModel { + if uid == "sensor_uid" { + return util.GenerateSensorResponseModel("sensor_uid", "") + } else { + return util.GenerateSensorResponseModel("sensor_uid", "_2") + } + } + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 2, + ) + + // required for creating another group + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + + // required for sensor group assignment create + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + }), + 1, + ) + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid_2", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2")), + }), + 1, + ) + + resources.CreateSensorGroupAssignment = func(request resources.SensorGroupAssignmentRequestModel) resources.SensorGroupAssignmentResponseModel { + return util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2") + } + }, + Config: provider.ProviderConfig + ` + // the original resources + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "sensor_uid" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "name_2" + parent_group_id = "parent_uid_2" + } + + resource "uxi_sensor" "my_sensor_2" { + name = "name_2" + address_note = "address_note_2" + notes = "notes_2" + pcap_mode = "light_2" + } + + import { + to = uxi_sensor.my_sensor_2 + id = "sensor_uid_2" + } + + // the assignment update, updated from sensor/group to sensor_2/group_2 + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor_2.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "sensor_id", "sensor_uid_2"), + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "group_id", "group_uid_2"), + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "id", "sensor_group_assignment_uid_2"), + ), + }, + // Remove sensors from state + { + PreConfig: func() { + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 1, + ) + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_sensor.my_sensor + + lifecycle { + destroy = false + } + } + + removed { + from = uxi_sensor.my_sensor_2 + + lifecycle { + destroy = false + } + }`, + }, + // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/sensor_test.go b/pkg/config-api-provider/test/resources/sensor_test.go new file mode 100644 index 00000000..6e456c64 --- /dev/null +++ b/pkg/config-api-provider/test/resources/sensor_test.go @@ -0,0 +1,114 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "regexp" + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" +) + +func TestSensorResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + // Creating a sensor is not allowed + { + Config: provider.ProviderConfig + ` + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "note" + pcap_mode = "light" + }`, + + ExpectError: regexp.MustCompile(`creating a sensor is not supported; sensors can only be imported`), + }, + // Importing a sensor + { + PreConfig: func() { + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "") + } + }, + Config: provider.ProviderConfig + ` + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "uid" + }`, + + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "name"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "address_note", "address_note"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "notes", "notes"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "id", "uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_sensor.my_sensor", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "_2") + } + }, + Config: provider.ProviderConfig + ` + resource "uxi_sensor" "my_sensor" { + name = "name_2" + address_note = "address_note_2" + notes = "notes_2" + pcap_mode = "light_2" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "name_2"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "address_note", "address_note_2"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "notes", "notes_2"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light_2"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "id", "uid"), + ), + }, + // Deleting a sensor is not allowed + { + Config: provider.ProviderConfig + ``, + ExpectError: regexp.MustCompile(`deleting a sensor is not supported; sensors can only removed from state`), + }, + // Remove sensor from state + { + Config: provider.ProviderConfig + ` + removed { + from = uxi_sensor.my_sensor + + lifecycle { + destroy = false + } + }`, + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/service_test.go b/pkg/config-api-provider/test/resources/service_test.go new file mode 100644 index 00000000..0149e88e --- /dev/null +++ b/pkg/config-api-provider/test/resources/service_test.go @@ -0,0 +1,91 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "regexp" + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" +) + +func TestServiceTestResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + // Creating a service_test is not allowed + { + Config: provider.ProviderConfig + ` + resource "uxi_service_test" "my_service_test" { + title = "title" + }`, + + ExpectError: regexp.MustCompile(`(?s)creating a service_test is not supported; service_tests can only be\s*imported`), + }, + // Importing a service_test + { + PreConfig: func() { + resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { + return util.GenerateServiceTestResponseModel(uid, "") + } + }, + Config: provider.ProviderConfig + ` + resource "uxi_service_test" "my_service_test" { + title = "title" + } + + import { + to = uxi_service_test.my_service_test + id = "uid" + }`, + + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_service_test.my_service_test", "title", "title"), + resource.TestCheckResourceAttr("uxi_service_test.my_service_test", "id", "uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_service_test.my_service_test", + ImportState: true, + ImportStateVerify: true, + }, + // Updating a service_test is not allowed + { + Config: provider.ProviderConfig + ` + resource "uxi_service_test" "my_service_test" { + title = "updated_title" + }`, + ExpectError: regexp.MustCompile(`(?s)updating a service_test is not supported; service_tests can only be updated\s*through the dashboard`), + }, + // Deleting a service_test is not allowed + { + Config: provider.ProviderConfig + ``, + ExpectError: regexp.MustCompile(`(?s)deleting a service_test is not supported; service_tests can only removed from\s*state`), + }, + // Remove service_test from state + { + Config: provider.ProviderConfig + ` + removed { + from = uxi_service_test.my_service_test + + lifecycle { + destroy = false + } + }`, + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go new file mode 100644 index 00000000..57bd71c4 --- /dev/null +++ b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go @@ -0,0 +1,196 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestServiceTestGroupAssignmentResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a serviceTest group assignment + { + PreConfig: func() { + // required for serviceTest import + resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { + return util.GenerateServiceTestResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 1, + ) + + // required for serviceTest group assignment create + serviceTestGroupAssignmentResponse := util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") + resources.CreateServiceTestGroupAssignment = func(request resources.ServiceTestGroupAssignmentRequestModel) resources.ServiceTestGroupAssignmentResponseModel { + return serviceTestGroupAssignmentResponse + } + resources.GetServiceTestGroupAssignment = func(uid string) resources.ServiceTestGroupAssignmentResponseModel { + return serviceTestGroupAssignmentResponse + } + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_service_test" "my_service_test" { + title = "title" + } + + import { + to = uxi_service_test.my_service_test + id = "service_test_uid" + } + + resource "uxi_service_test_group_assignment" "my_service_test_group_assignment" { + service_test_id = uxi_service_test.my_service_test.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "service_test_id", "service_test_uid"), + resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "group_id", "group_uid"), + resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "id", "service_test_group_assignment_uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_service_test_group_assignment.my_service_test_group_assignment", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { + if uid == "service_test_uid" { + return util.GenerateServiceTestResponseModel("service_test_uid", "") + } else { + return util.GenerateServiceTestResponseModel("service_test_uid", "_2") + } + } + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 3, + ) + + // required for creating another group + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + + // required for serviceTest group assignment create + resources.GetServiceTestGroupAssignment = func(uid string) resources.ServiceTestGroupAssignmentResponseModel { + if uid == "service_test_group_assignment_uid" { + return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") + } else { + return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") + } + } + resources.CreateServiceTestGroupAssignment = func(request resources.ServiceTestGroupAssignmentRequestModel) resources.ServiceTestGroupAssignmentResponseModel { + return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") + } + }, + Config: provider.ProviderConfig + ` + // the original resources + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_service_test" "my_service_test" { + title = "title" + } + + import { + to = uxi_service_test.my_service_test + id = "service_test_uid" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "name_2" + parent_group_id = "parent_uid_2" + } + + resource "uxi_service_test" "my_service_test_2" { + title = "title_2" + } + + import { + to = uxi_service_test.my_service_test_2 + id = "service_test_uid_2" + } + + // the assignment update, updated from service_test/group to service_test_2/group_2 + resource "uxi_service_test_group_assignment" "my_service_test_group_assignment" { + service_test_id = uxi_service_test.my_service_test_2.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "service_test_id", "service_test_uid_2"), + resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "group_id", "group_uid_2"), + resource.TestCheckResourceAttr("uxi_service_test_group_assignment.my_service_test_group_assignment", "id", "service_test_group_assignment_uid_2"), + ), + }, + // Remove serviceTests from state + { + PreConfig: func() { + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), + }), + 1, + ) + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_service_test.my_service_test + + lifecycle { + destroy = false + } + } + + removed { + from = uxi_service_test.my_service_test_2 + + lifecycle { + destroy = false + } + }`, + }, + // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/wired_network_test.go b/pkg/config-api-provider/test/resources/wired_network_test.go new file mode 100644 index 00000000..b5b5d815 --- /dev/null +++ b/pkg/config-api-provider/test/resources/wired_network_test.go @@ -0,0 +1,121 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "regexp" + "testing" + + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" +) + +func TestWiredNetworkResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + // Creating a wired_network is not allowed + { + Config: provider.ProviderConfig + ` + resource "uxi_wired_network" "my_wired_network" { + alias = "alias" + }`, + + ExpectError: regexp.MustCompile(`(?s)creating a wired_network is not supported; wired_networks can only be\s*imported`), + }, + // Importing a wired_network + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, + ), + 2, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wired_network" "my_wired_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_wired_network + id = "uid" + }`, + + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_wired_network.my_wired_network", "alias", "alias"), + resource.TestCheckResourceAttr("uxi_wired_network.my_wired_network", "id", "uid"), + ), + }, + // ImportState testing + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, + ), + 1, + ) + }, + ResourceName: "uxi_wired_network.my_wired_network", + ImportState: true, + ImportStateVerify: true, + }, + // Updating a wired_network is not allowed + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, + ), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wired_network" "my_wired_network" { + alias = "updated_alias" + }`, + ExpectError: regexp.MustCompile(`(?s)updating a wired_network is not supported; wired_networks can only be updated\s*through the dashboard`), + }, + // Deleting a wired_network is not allowed + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, + ), + 2, + ) + }, + Config: provider.ProviderConfig + ``, + ExpectError: regexp.MustCompile(`(?s)deleting a wired_network is not supported; wired_networks can only removed\s*from state`), + }, + // Remove wired_network from state + { + Config: provider.ProviderConfig + ` + removed { + from = uxi_wired_network.my_wired_network + + lifecycle { + destroy = false + } + }`, + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/wireless_network_test.go b/pkg/config-api-provider/test/resources/wireless_network_test.go new file mode 100644 index 00000000..dddf913b --- /dev/null +++ b/pkg/config-api-provider/test/resources/wireless_network_test.go @@ -0,0 +1,120 @@ +package test + +import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "regexp" + "testing" + + "github.com/h2non/gock" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" +) + +func TestWirelessNetworkResource(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + // Creating a wireless_network is not allowed + { + Config: provider.ProviderConfig + ` + resource "uxi_wireless_network" "my_wireless_network" { + alias = "alias" + }`, + + ExpectError: regexp.MustCompile(`(?s)creating a wireless_network is not supported; wireless_networks can only be\s*imported`), + }, + // Importing a wireless_network + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 2, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wireless_network" "my_wireless_network" { + alias = "alias" + } + + import { + to = uxi_wireless_network.my_wireless_network + id = "uid" + }`, + + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_wireless_network.my_wireless_network", "alias", "alias"), + resource.TestCheckResourceAttr("uxi_wireless_network.my_wireless_network", "id", "uid"), + ), + }, + // ImportState testing + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 1, + ) + }, + ResourceName: "uxi_wireless_network.my_wireless_network", + ImportState: true, + ImportStateVerify: true, + }, + // Updating a wireless_network is not allowed + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wireless_network" "my_wireless_network" { + alias = "updated_alias" + }`, + ExpectError: regexp.MustCompile(`(?s)updating a wireless_network is not supported; wireless_networks can only be\s*updated through the dashboard`), + }, + // Deleting a wireless_network is not allowed + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 1, + ) + }, + Config: provider.ProviderConfig + ``, + ExpectError: regexp.MustCompile(`(?s)deleting a wireless_network is not supported; wireless_networks can only\s*removed from state`), + }, + // Remove wireless_network from state + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_wireless_network.my_wireless_network + + lifecycle { + destroy = false + } + }`, + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/util/utils.go b/pkg/config-api-provider/test/util/utils.go new file mode 100644 index 00000000..6bf8a822 --- /dev/null +++ b/pkg/config-api-provider/test/util/utils.go @@ -0,0 +1,243 @@ +package util + +import ( + "encoding/json" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/h2non/gock" +) + +var mockPaginationResponse = map[string]interface{}{ + "limit": 10, + "first": nil, + "next": nil, + "previous": nil, + "last": nil, +} + +func GenerateSensorResponseModel(uid string, postfix string) resources.SensorResponseModel { + return resources.SensorResponseModel{ + UID: uid, + Serial: "serial" + postfix, + Name: "name" + postfix, + ModelNumber: "model_number" + postfix, + WifiMacAddress: "wifi_mac_address" + postfix, + EthernetMacAddress: "ethernet_mac_address" + postfix, + AddressNote: "address_note" + postfix, + Longitude: "longitude" + postfix, + Latitude: "latitude" + postfix, + Notes: "notes" + postfix, + PCapMode: "light" + postfix, + } +} + +func GenerateAgentResponseModel(uid string, postfix string) resources.AgentResponseModel { + return resources.AgentResponseModel{ + UID: uid, + Serial: "serial" + postfix, + Name: "name" + postfix, + ModelNumber: "model_number" + postfix, + WifiMacAddress: "wifi_mac_address" + postfix, + EthernetMacAddress: "ethernet_mac_address" + postfix, + Notes: "notes" + postfix, + PCapMode: "light" + postfix, + } +} + +func GenerateGroupResponseModel(uid string, nonReplacementFieldPostfix string, replacementFieldPostfix string) resources.GroupResponseModel { + parent_uid := "parent_uid" + replacementFieldPostfix + return resources.GroupResponseModel{ + UID: uid, + Name: "name" + nonReplacementFieldPostfix, + ParentUid: &parent_uid, + Path: parent_uid + "." + uid, + } +} + +func GenerateGroupPaginatedResponse(groups []map[string]interface{}) map[string]interface{} { + return map[string]interface{}{ + "groups": groups, + "pagination": mockPaginationResponse, + } +} + +func GenerateRootGroupResponseModel(uid string) resources.GroupResponseModel { + return resources.GroupResponseModel{ + UID: uid, + Name: "root", + ParentUid: nil, + Path: uid, + } +} + +func GenerateServiceTestResponseModel(uid string, postfix string) resources.ServiceTestResponseModel { + return resources.ServiceTestResponseModel{ + Uid: uid, + Category: "external" + postfix, + Title: "title" + postfix, + Target: "target" + postfix, + Template: "template" + postfix, + IsEnabled: true, + } +} + +func GenerateWiredNetworkResponse(uid string, postfix string) map[string]interface{} { + return map[string]interface{}{ + "uid": uid, + "alias": "alias" + postfix, + "datetime_created": "2024-09-11T12:00:00.000Z", + "datetime_updated": "2024-09-11T12:00:00.000Z", + "ip_version": "ip_version" + postfix, + "security": "security" + postfix, + "dns_lookup_domain": "dns_lookup_domain" + postfix, + "disable_edns": false, + "use_dns64": false, + "external_connectivity": false, + "vlan_id": 123, + } +} + +func GenerateWiredNetworkPaginatedResponse(wiredNetworks []map[string]interface{}) map[string]interface{} { + return map[string]interface{}{ + "wired_networks": wiredNetworks, + "pagination": mockPaginationResponse, + } +} + +func GenerateWirelessNetworkResponse(uid string, postfix string) map[string]interface{} { + return map[string]interface{}{ + "uid": uid, + "ssid": "ssid" + postfix, + "datetime_created": "2024-09-11T12:00:00.000Z", + "datetime_updated": "2024-09-11T12:00:00.000Z", + "alias": "alias" + postfix, + "ip_version": "ip_version" + postfix, + "security": "security" + postfix, + "hidden": false, + "band_locking": "band_locking" + postfix, + "dns_lookup_domain": "dns_lookup_domain" + postfix, + "disable_edns": false, + "use_dns64": false, + "external_connectivity": false, + } +} + +func GenerateWirelessNetworkPaginatedResponse(wirelessNetworks []map[string]interface{}) map[string]interface{} { + return map[string]interface{}{ + "wireless_networks": wirelessNetworks, + "pagination": mockPaginationResponse, + } +} + +func GenerateSensorGroupAssignmentResponse(uid string, postfix string) resources.SensorGroupAssignmentResponseModel { + return resources.SensorGroupAssignmentResponseModel{ + UID: uid, + GroupUID: "group_uid" + postfix, + SensorUID: "sensor_uid" + postfix, + } +} + +func GenerateSensorGroupAssignmentPaginatedResponse(sensorGroupAssignments []map[string]interface{}) map[string]interface{} { + return map[string]interface{}{ + "sensor_group_assignments": sensorGroupAssignments, + "pagination": mockPaginationResponse, + } +} + +func GenerateAgentGroupAssignmentResponse(uid string, postfix string) resources.AgentGroupAssignmentResponseModel { + return resources.AgentGroupAssignmentResponseModel{ + UID: uid, + GroupUID: "group_uid" + postfix, + AgentUID: "agent_uid" + postfix, + } +} + +func GenerateNetworkGroupAssignmentResponse(uid string, postfix string) resources.NetworkGroupAssignmentResponseModel { + return resources.NetworkGroupAssignmentResponseModel{ + UID: uid, + GroupUID: "group_uid" + postfix, + NetworkUID: "network_uid" + postfix, + } +} + +func GenerateServiceTestGroupAssignmentResponse(uid string, postfix string) resources.ServiceTestGroupAssignmentResponseModel { + return resources.ServiceTestGroupAssignmentResponseModel{ + UID: uid, + GroupUID: "group_uid" + postfix, + ServiceTestUID: "service_test_uid" + postfix, + } +} + +// Converts a struct to a map while maintaining the json alias as keys +func StructToMap(obj interface{}) map[string]interface{} { + data, _ := json.Marshal(obj) // Convert to a json string + + newMap := map[string]interface{}{} + + _ = json.Unmarshal(data, &newMap) // Convert to a map + return newMap +} + +func MockOAuth() *gock.Response { + return gock.New("https://test.sso.common.cloud.hpe.com"). + Post("/as/token.oauth2"). + MatchHeader("Content-Type", "application/x-www-form-urlencoded"). + Persist(). + Reply(200). + JSON(map[string]interface{}{ + "access_token": "mock_token", + "token_type": "bearer", + "expires_in": 3600, + }) + +} + +func MockPostGroup(response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Post("/configuration/app/v1/groups"). + MatchHeader("Content-Type", "application/json"). + MatchHeader("Authorization", "mock_token"). + Times(times). + Reply(200). + JSON(response) +} + +func MockGetGroup(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/groups"). + MatchHeader("Authorization", "mock_token"). + MatchParam("uid", uid). + Times(times). + Reply(200). + JSON(response) +} + +func MockGetWiredNetwork(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/wired-networks"). + MatchHeader("Authorization", "mock_token"). + MatchParam("uid", uid). + Times(times). + Reply(200). + JSON(response) +} + +func MockGetWirelessNetwork(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/wireless-networks"). + MatchHeader("Authorization", "mock_token"). + MatchParam("uid", uid). + Times(times). + Reply(200). + JSON(response) +} + +func MockGetSensorGroupAssignment(uid string, response map[string]interface{}, times int) { + gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/sensor-group-assignments"). + MatchHeader("Authorization", "mock_token"). + MatchParam("uid", uid). + Times(times). + Reply(200). + JSON(response) +}