diff --git a/equinix/equinix_sweeper_test.go b/equinix/equinix_sweeper_test.go index b37a0ed4b..10905e5b2 100644 --- a/equinix/equinix_sweeper_test.go +++ b/equinix/equinix_sweeper_test.go @@ -1,50 +1,11 @@ package equinix import ( - "fmt" - "strconv" - "strings" "testing" - "time" - - "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) -const tstResourcePrefix = "tfacc" - func TestMain(m *testing.M) { resource.TestMain(m) } - -func sharedConfigForRegion(region string) (*config.Config, error) { - endpoint := getFromEnvDefault(config.EndpointEnvVar, config.DefaultBaseURL) - clientToken := getFromEnvDefault(config.ClientTokenEnvVar, "") - clientID := getFromEnvDefault(config.ClientIDEnvVar, "") - clientSecret := getFromEnvDefault(config.ClientSecretEnvVar, "") - clientTimeout := getFromEnvDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout)) - clientTimeoutInt, err := strconv.Atoi(clientTimeout) - if err != nil { - return nil, fmt.Errorf("cannot convert value of '%s' env variable to int", config.ClientTimeoutEnvVar) - } - metalAuthToken := getFromEnvDefault(config.MetalAuthTokenEnvVar, "") - - if clientToken == "" && (clientID == "" || clientSecret == "") && metalAuthToken == "" { - return nil, fmt.Errorf("To run acceptance tests sweeper, one of '%s' or pair '%s' - '%s' must be set for Equinix Fabric and Network Edge, and '%s' for Equinix Metal", - config.ClientTokenEnvVar, config.ClientIDEnvVar, config.ClientSecretEnvVar, config.MetalAuthTokenEnvVar) - } - - return &config.Config{ - AuthToken: metalAuthToken, - BaseURL: endpoint, - Token: clientToken, - ClientID: clientID, - ClientSecret: clientSecret, - RequestTimeout: time.Duration(clientTimeoutInt) * time.Second, - }, nil -} - -func isSweepableTestResource(namePrefix string) bool { - return strings.HasPrefix(namePrefix, tstResourcePrefix) -} diff --git a/equinix/provider_test.go b/equinix/provider_test.go index 0d38371ce..d8ce96d61 100644 --- a/equinix/provider_test.go +++ b/equinix/provider_test.go @@ -44,11 +44,6 @@ var ( } ) -type testAccConfig struct { - ctx map[string]interface{} - config string -} - func init() { testAccProvider = Provider() testAccProviders = map[string]*schema.Provider{ @@ -112,17 +107,6 @@ func testAccPreCheck(t *testing.T) { } } -func newTestAccConfig(ctx map[string]interface{}) *testAccConfig { - return &testAccConfig{ - ctx: ctx, - config: "", - } -} - -func (t *testAccConfig) build() string { - return t.config -} - func getFromEnv(varName string) (string, error) { if v := os.Getenv(varName); v != "" { return v, nil @@ -130,21 +114,6 @@ func getFromEnv(varName string) (string, error) { return "", fmt.Errorf("environmental variable '%s' is not set", varName) } -func getFromEnvDefault(varName string, defaultValue string) string { - if v := os.Getenv(varName); v != "" { - return v - } - return defaultValue -} - -func copyMap(source map[string]interface{}) map[string]interface{} { - target := make(map[string]interface{}) - for k, v := range source { - target[k] = v - } - return target -} - func setSchemaValueIfNotEmpty(key string, value interface{}, d *schema.ResourceData) error { if !comparisons.IsEmpty(value) { return d.Set(key, value) diff --git a/internal/acceptance/acceptance.go b/internal/acceptance/acceptance.go index 0e994f7aa..f7e65a23d 100644 --- a/internal/acceptance/acceptance.go +++ b/internal/acceptance/acceptance.go @@ -5,11 +5,9 @@ import ( "sync" "testing" - "github.com/equinix/terraform-provider-equinix/equinix" "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/env" "github.com/equinix/terraform-provider-equinix/internal/provider" - "github.com/equinix/terraform-provider-equinix/version" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" terraformsdk "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -33,19 +31,6 @@ var ( testAccProviderConfigure sync.Once ) -func init() { - TestAccProvider = equinix.Provider() - TestAccProviders = map[string]*schema.Provider{ - "equinix": TestAccProvider, - } - TestExternalProviders = map[string]resource.ExternalProvider{ - "random": { - Source: "hashicorp/random", - }, - } - TestAccFrameworkProvider = provider.CreateFrameworkProvider(version.ProviderVersion).(*provider.FrameworkProvider) -} - func TestAccPreCheck(t *testing.T) { var err error diff --git a/internal/acceptance/acceptance_acc_test.go b/internal/acceptance/acceptance_acc_test.go new file mode 100644 index 000000000..4e703a951 --- /dev/null +++ b/internal/acceptance/acceptance_acc_test.go @@ -0,0 +1,22 @@ +package acceptance + +import ( + "github.com/equinix/terraform-provider-equinix/equinix" + "github.com/equinix/terraform-provider-equinix/internal/provider" + "github.com/equinix/terraform-provider-equinix/version" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func init() { + TestAccProvider = equinix.Provider() + TestAccProviders = map[string]*schema.Provider{ + "equinix": TestAccProvider, + } + TestExternalProviders = map[string]resource.ExternalProvider{ + "random": { + Source: "hashicorp/random", + }, + } + TestAccFrameworkProvider = provider.CreateFrameworkProvider(version.ProviderVersion).(*provider.FrameworkProvider) +} diff --git a/internal/resources/networkedge/account/data_source_acc_test.go b/internal/resources/networkedge/account/data_source_acc_test.go index 2c774e9ca..c5ec33255 100644 --- a/internal/resources/networkedge/account/data_source_acc_test.go +++ b/internal/resources/networkedge/account/data_source_acc_test.go @@ -4,11 +4,16 @@ import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/nprintf" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) +const ( + networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO" +) + func TestAccDataSourceNetworkAccount_basic(t *testing.T) { metro, _ := schema.EnvDefaultFunc(networkDeviceMetroEnvVar, "SV")() context := map[string]interface{}{ @@ -19,8 +24,8 @@ func TestAccDataSourceNetworkAccount_basic(t *testing.T) { } resourceName := fmt.Sprintf("data.equinix_network_account.%s", context["resourceName"].(string)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccDataSourceNetworkAccountConfig_basic(context), diff --git a/internal/resources/networkedge/acl_template/resource_acc_test.go b/internal/resources/networkedge/acl_template/resource_acc_test.go index 1a69043e3..fa36822c9 100644 --- a/internal/resources/networkedge/acl_template/resource_acc_test.go +++ b/internal/resources/networkedge/acl_template/resource_acc_test.go @@ -6,8 +6,10 @@ import ( "log" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/nprintf" + "github.com/equinix/terraform-provider-equinix/internal/sweep" "github.com/equinix/ne-go" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -15,6 +17,10 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) +const ( + tstResourcePrefix = "tfacc" +) + func init() { resource.AddTestSweepers("equinix_network_acl_template", &resource.Sweeper{ Name: "equinix_network_acl_template", @@ -22,8 +28,16 @@ func init() { }) } +func copyMap(source map[string]interface{}) map[string]interface{} { + target := make(map[string]interface{}) + for k, v := range source { + target[k] = v + } + return target +} + func testSweepNetworkACLTemplate(region string) error { - config, err := sharedConfigForRegion(region) + config, err := sweep.SharedConfigForRegion(region) if err != nil { return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping Network ACL Templates: %s", err) } @@ -38,7 +52,7 @@ func testSweepNetworkACLTemplate(region string) error { } nonSweepableCount := 0 for _, template := range templates { - if !isSweepableTestResource(ne.StringValue(template.Name)) { + if !sweep.IsSweepableTestResource(ne.StringValue(template.Name)) { nonSweepableCount++ continue } @@ -82,8 +96,8 @@ func TestAccNetworkACLTemplate(t *testing.T) { resourceName := "equinix_network_acl_template." + context["resourceName"].(string) var template ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccNetworkACLTemplate(context), @@ -147,7 +161,7 @@ func testAccNetworkACLTemplateExists(resourceName string, template *ne.ACLTempla if !ok { return fmt.Errorf("resource not found: %s", resourceName) } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } diff --git a/internal/resources/networkedge/bgp/resource_acc_test.go b/internal/resources/networkedge/bgp/resource_acc_test.go index 6185504b1..bb6670d7a 100644 --- a/internal/resources/networkedge/bgp/resource_acc_test.go +++ b/internal/resources/networkedge/bgp/resource_acc_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/nprintf" @@ -68,8 +69,8 @@ func TestAccNetworkBGP_CSR1000V_Single_AWS(t *testing.T) { resourceName := fmt.Sprintf("equinix_network_bgp.%s", context["bgp-resourceName"].(string)) var bgpConfig ne.BGPConfiguration resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withSSHUser().withVDConnection().withBGP().build(), @@ -142,7 +143,7 @@ func testAccNeBGPExists(resourceName string, bgpConfig *ne.BGPConfiguration) res if !ok { return fmt.Errorf("resource not found: %s", resourceName) } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } diff --git a/internal/resources/networkedge/bgp/templates_acc_test.go b/internal/resources/networkedge/bgp/templates_acc_test.go new file mode 100644 index 000000000..a1e3e050d --- /dev/null +++ b/internal/resources/networkedge/bgp/templates_acc_test.go @@ -0,0 +1,461 @@ +package bgp + +import ( + "github.com/equinix/terraform-provider-equinix/internal/comparisons" + "github.com/equinix/terraform-provider-equinix/internal/nprintf" +) + +const ( + tstResourcePrefix = "tfacc" + + networkDeviceProjectId = "TF_ACC_NETWORK_DEVICE_PROJECT_ID" + networkDeviceAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_BILLING_ACCOUNT_NAME" + networkDeviceSecondaryAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_BILLING_ACCOUNT_NAME" + networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO" + networkDeviceSecondaryMetroEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_METRO" + networkDeviceCSRSDWANLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_CSRSDWAN_LICENSE_FILE" + networkDeviceVSRXLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_VSRX_LICENSE_FILE" + networkDeviceVersaController1EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER1" + networkDeviceVersaController2EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER2" + networkDeviceVersaLocalIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_LOCALID" + networkDeviceVersaRemoteIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_REMOTEID" + networkDeviceVersaSerialNumberEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_SERIAL" + networkDeviceCGENIXLicenseKeyEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_KEY" + networkDeviceCGENIXLicenseSecretEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_SECRET" + networkDevicePANWLicenseTokenEnvVar = "TF_ACC_NETWORK_DEVICE_PANW_LICENSE_TOKEN" +) + +type testAccConfig struct { + ctx map[string]interface{} + config string +} + +func newTestAccConfig(ctx map[string]interface{}) *testAccConfig { + return &testAccConfig{ + ctx: ctx, + config: "", + } +} + +func (t *testAccConfig) build() string { + return t.config +} + +func (t *testAccConfig) withDevice() *testAccConfig { + t.config += testAccNetworkDevice(t.ctx) + return t +} + +func copyMap(source map[string]interface{}) map[string]interface{} { + target := make(map[string]interface{}) + for k, v := range source { + target[k] = v + } + return target +} + +func testAccNetworkDeviceUser(ctx map[string]interface{}) string { + config := nprintf.Nprintf(` +resource "equinix_network_ssh_user" "%{user-resourceName}" { + username = "%{user-username}" + password = "%{user-password}" + device_ids = [ + equinix_network_device.%{device-resourceName}.id`, ctx) + if _, ok := ctx["device-secondary_name"]; ok { + config += nprintf.Nprintf(`, + equinix_network_device.%{device-resourceName}.redundant_id`, ctx) + } + config += ` + ] +}` + return config +} + +func testAccNetworkDevice(ctx map[string]interface{}) string { + var config string + config += nprintf.Nprintf(` +data "equinix_network_account" "test" { + metro_code = "%{device-metro_code}" + status = "Active" + project_id = "%{device-project_id}"`, ctx) + if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) { + config += nprintf.Nprintf(` + name = "%{device-account_name}"`, ctx) + } + config += nprintf.Nprintf(` +}`, ctx) + if _, ok := ctx["device-secondary_metro_code"]; ok { + config += nprintf.Nprintf(` +data "equinix_network_account" "test-secondary" { + metro_code = "%{device-secondary_metro_code}" + status = "Active"`, ctx) + if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) { + config += nprintf.Nprintf(` + name = "%{device-secondary_account_name}"`, ctx) + } + config += nprintf.Nprintf(` +}`, ctx) + } + config += nprintf.Nprintf(` +resource "equinix_network_device" "%{device-resourceName}" { + self_managed = %{device-self_managed} + byol = %{device-byol} + name = "%{device-name}" + metro_code = "%{device-metro_code}" + type_code = "%{device-type_code}" + project_id = "%{device-project_id}" + package_code = "%{device-package_code}" + notifications = %{device-notifications} + term_length = %{device-term_length} + account_number = data.equinix_network_account.test.number + version = "%{device-version}" + core_count = %{device-core_count}`, ctx) + if _, ok := ctx["device-purchase_order_number"]; ok { + config += nprintf.Nprintf(` + purchase_order_number = "%{device-purchase_order_number}"`, ctx) + } + if _, ok := ctx["device-purchase_order_number"]; ok { + config += nprintf.Nprintf(` + order_reference = "%{device-order_reference}"`, ctx) + } + if _, ok := ctx["device-additional_bandwidth"]; ok { + config += nprintf.Nprintf(` + additional_bandwidth = %{device-additional_bandwidth}`, ctx) + } + if _, ok := ctx["device-throughput"]; ok { + config += nprintf.Nprintf(` + throughput = %{device-throughput} + throughput_unit = "%{device-throughput_unit}"`, ctx) + } + if _, ok := ctx["device-hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-hostname}"`, ctx) + } + if _, ok := ctx["device-interface_count"]; ok { + config += nprintf.Nprintf(` + interface_count = %{device-interface_count}`, ctx) + } + if _, ok := ctx["acl-resourceName"]; ok { + config += nprintf.Nprintf(` + acl_template_id = equinix_network_acl_template.%{acl-resourceName}.id`, ctx) + } + if _, ok := ctx["mgmtAcl-resourceName"]; ok { + config += nprintf.Nprintf(` + mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-resourceName}.id`, ctx) + } + if _, ok := ctx["sshkey-resourceName"]; ok { + config += nprintf.Nprintf(` + ssh_key { + username = "test" + key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name + }`, ctx) + } + if _, ok := ctx["device-license_file"]; ok { + config += nprintf.Nprintf(` + license_file = "%{device-license_file}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration = {`, ctx) + if _, ok := ctx["device-vendorConfig_siteId"]; ok { + config += nprintf.Nprintf(` + siteId = "%{device-vendorConfig_siteId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_systemIpAddress"]; ok { + config += nprintf.Nprintf(` + systemIpAddress = "%{device-vendorConfig_systemIpAddress}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_licenseKey"]; ok { + config += nprintf.Nprintf(` + licenseKey = "%{device-vendorConfig_licenseKey}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_licenseSecret"]; ok { + config += nprintf.Nprintf(` + licenseSecret = "%{device-vendorConfig_licenseSecret}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_controller2"]; ok { + config += nprintf.Nprintf(` + controller2 = "%{device-vendorConfig_controller2}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_localId"]; ok { + config += nprintf.Nprintf(` + localId = "%{device-vendorConfig_localId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_remoteId"]; ok { + config += nprintf.Nprintf(` + remoteId = "%{device-vendorConfig_remoteId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_serialNumber"]; ok { + config += nprintf.Nprintf(` + serialNumber = "%{device-vendorConfig_serialNumber}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + if _, ok := ctx["device-secondary_name"]; ok { + config += nprintf.Nprintf(` + secondary_device { + name = "%{device-secondary_name}"`, ctx) + if _, ok := ctx["device-secondary_metro_code"]; ok { + config += nprintf.Nprintf(` + metro_code = "%{device-secondary_metro_code}" + account_number = data.equinix_network_account.test-secondary.number`, ctx) + } else { + config += nprintf.Nprintf(` + metro_code = "%{device-metro_code}" + account_number = data.equinix_network_account.test.number`, ctx) + } + config += nprintf.Nprintf(` + notifications = %{device-secondary_notifications}`, ctx) + if _, ok := ctx["device-secondary_additional_bandwidth"]; ok { + config += nprintf.Nprintf(` + additional_bandwidth = %{device-secondary_additional_bandwidth}`, ctx) + } + if _, ok := ctx["device-secondary_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-secondary_hostname}"`, ctx) + } + if _, ok := ctx["acl-secondary_resourceName"]; ok { + config += nprintf.Nprintf(` + acl_template_id = equinix_network_acl_template.%{acl-secondary_resourceName}.id`, ctx) + } + if _, ok := ctx["mgmtAcl-secondary_resourceName"]; ok { + config += nprintf.Nprintf(` + mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-secondary_resourceName}.id`, ctx) + } + if _, ok := ctx["sshkey-resourceName"]; ok { + config += nprintf.Nprintf(` + ssh_key { + username = "test" + key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name + }`, ctx) + } + if _, ok := ctx["device-secondary_license_file"]; ok { + config += nprintf.Nprintf(` + license_file = "%{device-secondary_license_file}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration = {`, ctx) + if _, ok := ctx["device-secondary_vendorConfig_siteId"]; ok { + config += nprintf.Nprintf(` + siteId = "%{device-secondary_vendorConfig_siteId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_systemIpAddress"]; ok { + config += nprintf.Nprintf(` + systemIpAddress = "%{device-secondary_vendorConfig_systemIpAddress}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_licenseKey"]; ok { + config += nprintf.Nprintf(` + licenseKey = "%{device-secondary_vendorConfig_licenseKey}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_licenseSecret"]; ok { + config += nprintf.Nprintf(` + licenseSecret = "%{device-secondary_vendorConfig_licenseSecret}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-secondary_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_controller2"]; ok { + config += nprintf.Nprintf(` + controller2 = "%{device-secondary_vendorConfig_controller2}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_localId"]; ok { + config += nprintf.Nprintf(` + localId = "%{device-secondary_vendorConfig_localId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_remoteId"]; ok { + config += nprintf.Nprintf(` + remoteId = "%{device-secondary_vendorConfig_remoteId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_serialNumber"]; ok { + config += nprintf.Nprintf(` + serialNumber = "%{device-secondary_vendorConfig_serialNumber}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + } + if _, ok := ctx["device-cluster_name"]; ok { + config += nprintf.Nprintf(` + cluster_details { + cluster_name = "%{device-cluster_name}"`, ctx) + config += ` + node0 {` + if _, ok := ctx["device-node0_license_file_id"]; ok { + config += nprintf.Nprintf(` + license_file_id = "%{device-node0_license_file_id}"`, ctx) + } + if _, ok := ctx["device-node0_license_token"]; ok { + config += nprintf.Nprintf(` + license_token = "%{device-node0_license_token}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration {`, ctx) + if _, ok := ctx["device-node0_vendorConfig_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-node0_vendorConfig_hostname}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_adminPassword"]; ok { + config += nprintf.Nprintf(` + admin_password = "%{device-node0_vendorConfig_adminPassword}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-node0_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_activationKey"]; ok { + config += nprintf.Nprintf(` + activation_key = "%{device-node0_vendorConfig_activationKey}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_controllerFqdn"]; ok { + config += nprintf.Nprintf(` + controller_fqdn = "%{device-node0_vendorConfig_controllerFqdn}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_rootPassword"]; ok { + config += nprintf.Nprintf(` + root_password = "%{device-node0_vendorConfig_rootPassword}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + config += ` + node1 {` + if _, ok := ctx["device-node1_license_file_id"]; ok { + config += nprintf.Nprintf(` + license_file_id = "%{device-node1_license_file_id}"`, ctx) + } + if _, ok := ctx["device-node1_license_token"]; ok { + config += nprintf.Nprintf(` + license_token = "%{device-node1_license_token}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration {`, ctx) + if _, ok := ctx["device-node1_vendorConfig_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-node1_vendorConfig_hostname}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_adminPassword"]; ok { + config += nprintf.Nprintf(` + admin_password = "%{device-node1_vendorConfig_adminPassword}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-node1_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_activationKey"]; ok { + config += nprintf.Nprintf(` + activation_key = "%{device-node1_vendorConfig_activationKey}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_controllerFqdn"]; ok { + config += nprintf.Nprintf(` + controller_fqdn = "%{device-node1_vendorConfig_controllerFqdn}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_rootPassword"]; ok { + config += nprintf.Nprintf(` + root_password = "%{device-node1_vendorConfig_rootPassword}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + config += ` + }` + } + config += ` +}` + return config +} + +func testAccNetworkDeviceACL(ctx map[string]interface{}) string { + var config string + if _, ok := ctx["acl-name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{acl-resourceName}" { + name = "%{acl-name}" + description = "%{acl-description}" + inbound_rule { + subnet = "10.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["mgmtAcl-name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{mgmtAcl-resourceName}" { + name = "%{mgmtAcl-name}" + description = "%{mgmtAcl-description}" + inbound_rule { + subnet = "11.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["acl-secondary_name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{acl-secondary_resourceName}" { + name = "%{acl-secondary_name}" + description = "%{acl-secondary_description}" + inbound_rule { + subnet = "192.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["mgmtAcl-secondary_name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{mgmtAcl-secondary_resourceName}" { + name = "%{mgmtAcl-secondary_name}" + description = "%{mgmtAcl-secondary_description}" + inbound_rule { + subnet = "193.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + return config +} + +func testAccNetworkDeviceSSHKey(ctx map[string]interface{}) string { + return nprintf.Nprintf(` +resource "equinix_network_ssh_key" "%{sshkey-resourceName}" { + name = "%{sshkey-name}" + public_key = "%{sshkey-public_key}" +} +`, ctx) +} + +func (t *testAccConfig) withACL() *testAccConfig { + t.config += testAccNetworkDeviceACL(t.ctx) + return t +} + +func (t *testAccConfig) withSSHKey() *testAccConfig { + t.config += testAccNetworkDeviceSSHKey(t.ctx) + return t +} + +func (t *testAccConfig) withSSHUser() *testAccConfig { + t.config += testAccNetworkDeviceUser(t.ctx) + return t +} diff --git a/internal/resources/networkedge/device/resource_acc_test.go b/internal/resources/networkedge/device/resource_acc_test.go index 87d9d86db..684794695 100644 --- a/internal/resources/networkedge/device/resource_acc_test.go +++ b/internal/resources/networkedge/device/resource_acc_test.go @@ -6,9 +6,10 @@ import ( "log" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/comparisons" "github.com/equinix/terraform-provider-equinix/internal/config" - "github.com/equinix/terraform-provider-equinix/internal/nprintf" + "github.com/equinix/terraform-provider-equinix/internal/sweep" "github.com/equinix/ne-go" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -17,26 +18,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) -const ( - tstResourcePrefix = "tfacc" - - networkDeviceProjectId = "TF_ACC_NETWORK_DEVICE_PROJECT_ID" - networkDeviceAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_BILLING_ACCOUNT_NAME" - networkDeviceSecondaryAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_BILLING_ACCOUNT_NAME" - networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO" - networkDeviceSecondaryMetroEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_METRO" - networkDeviceCSRSDWANLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_CSRSDWAN_LICENSE_FILE" - networkDeviceVSRXLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_VSRX_LICENSE_FILE" - networkDeviceVersaController1EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER1" - networkDeviceVersaController2EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER2" - networkDeviceVersaLocalIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_LOCALID" - networkDeviceVersaRemoteIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_REMOTEID" - networkDeviceVersaSerialNumberEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_SERIAL" - networkDeviceCGENIXLicenseKeyEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_KEY" - networkDeviceCGENIXLicenseSecretEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_SECRET" - networkDevicePANWLicenseTokenEnvVar = "TF_ACC_NETWORK_DEVICE_PANW_LICENSE_TOKEN" -) - func init() { resource.AddTestSweepers("equinix_network_device", &resource.Sweeper{ Name: "equinix_network_device", @@ -45,8 +26,97 @@ func init() { }) } +func testAccNeSSHUserExists(resourceName string, user *ne.SSHUser) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("resource not found: %s", resourceName) + } + if rs.Primary.ID == "" { + return fmt.Errorf("resource has no ID attribute set") + } + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne + resp, err := client.GetSSHUser(rs.Primary.ID) + if err != nil { + return fmt.Errorf("error when fetching SSH user '%s': %s", rs.Primary.ID, err) + } + *user = *resp + return nil + } +} + +func testAccNeSSHUserAttributes(user *ne.SSHUser, devices []*ne.Device, ctx map[string]interface{}) resource.TestCheckFunc { + return func(s *terraform.State) error { + if v, ok := ctx["username"]; ok && ne.StringValue(user.Username) != v.(string) { + return fmt.Errorf("name does not match %v - %v", ne.StringValue(user.Username), v) + } + deviceIDs := make([]string, len(devices)) + for i := range devices { + deviceIDs[i] = ne.StringValue(devices[i].UUID) + } + if !comparisons.SlicesMatch(deviceIDs, user.DeviceUUIDs) { + return fmt.Errorf("device_ids does not match %v - %v", deviceIDs, user.DeviceUUIDs) + } + return nil + } +} + +func testAccNetworkACLTemplateExists(resourceName string, template *ne.ACLTemplate) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("resource not found: %s", resourceName) + } + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne + if rs.Primary.ID == "" { + return fmt.Errorf("resource has no ID attribute set") + } + resp, err := client.GetACLTemplate(rs.Primary.ID) + if err != nil { + return fmt.Errorf("error when fetching ACL template '%s': %s", rs.Primary.ID, err) + } + *template = *resp + return nil + } +} + +func testAccNetworkACLTemplateAttributes(template *ne.ACLTemplate, ctx map[string]interface{}) resource.TestCheckFunc { + return func(s *terraform.State) error { + if v, ok := ctx["name"]; ok && ne.StringValue(template.Name) != v.(string) { + return fmt.Errorf("name does not match %v - %v", ne.StringValue(template.Name), v) + } + if v, ok := ctx["description"]; ok && ne.StringValue(template.Description) != v.(string) { + return fmt.Errorf("description does not match %v - %v", ne.StringValue(template.Description), v) + } + if len(template.InboundRules) != 3 { + return fmt.Errorf("number of inbound rules does not match %v - %v", len(template.InboundRules), 3) + } + for i := 0; i < 3; i++ { + if ne.IntValue(template.InboundRules[i].SeqNo) != i+1 { + return fmt.Errorf("inbound_rule %d seqNo does not match %v - %v", i+1, ne.IntValue(template.InboundRules[i].SeqNo), i+1) + } + if v, ok := ctx[fmt.Sprintf("inbound_rule_%d_subnet", i+1)]; ok && ne.StringValue(template.InboundRules[i].Subnet) != v.(string) { + return fmt.Errorf("inbound_rule %d subnet does not match %v - %v", i+1, ne.StringValue(template.InboundRules[i].Subnet), v) + } + if v, ok := ctx[fmt.Sprintf("inbound_rule_%d_protocol", i+1)]; ok && ne.StringValue(template.InboundRules[i].Protocol) != v.(string) { + return fmt.Errorf("inbound_rule %d protocol does not match %v - %v", i+1, ne.StringValue(template.InboundRules[i].Protocol), v) + } + if v, ok := ctx[fmt.Sprintf("inbound_rule_%d_src_port", i+1)]; ok && ne.StringValue(template.InboundRules[i].SrcPort) != v.(string) { + return fmt.Errorf("inbound_rule %d src_port does not match %v - %v", i+1, ne.StringValue(template.InboundRules[i].SrcPort), v) + } + if v, ok := ctx[fmt.Sprintf("inbound_rule_%d_dst_port", i+1)]; ok && ne.StringValue(template.InboundRules[i].DstPort) != v.(string) { + return fmt.Errorf("inbound_rule %d dst_port does not match %v - %v", i+1, ne.StringValue(template.InboundRules[i].DstPort), v) + } + if v, ok := ctx[fmt.Sprintf("inbound_rule_%d_description", i+1)]; ok && ne.StringValue(template.InboundRules[i].Description) != v.(string) { + return fmt.Errorf("inbound_rule %d description does not match %v - %v", i+1, ne.StringValue(template.InboundRules[i].Description), v) + } + } + return nil + } +} + func testSweepNetworkDevice(region string) error { - config, err := sharedConfigForRegion(region) + config, err := sweep.SharedConfigForRegion(region) if err != nil { return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping Network devices: %s", err) } @@ -69,7 +139,7 @@ func testSweepNetworkDevice(region string) error { } nonSweepableCount := 0 for _, device := range devices { - if !isSweepableTestResource(ne.StringValue(device.Name)) { + if !sweep.IsSweepableTestResource(ne.StringValue(device.Name)) { nonSweepableCount++ continue } @@ -136,8 +206,8 @@ func TestAccNetworkDevice_CSR1000V_HA_Managed_Sub(t *testing.T) { var user ne.SSHUser var primaryACL, secondaryACL ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withSSHUser().build(), @@ -220,8 +290,8 @@ func TestAccNetworkDevice_CSR1000V_HA_Self_BYOL(t *testing.T) { var primary, secondary ne.Device var primaryACL, secondaryACL ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withSSHKey().build(), @@ -287,8 +357,8 @@ func TestAccNetworkDevice_vSRX_HA_Managed_Sub(t *testing.T) { var primary, secondary ne.Device var user ne.SSHUser resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().build(), @@ -371,8 +441,8 @@ func TestAccNetworkDevice_vSRX_HA_Managed_BYOL(t *testing.T) { var primary, secondary ne.Device var user ne.SSHUser resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withACL().build(), @@ -442,8 +512,8 @@ func TestAccNetworkDevice_vSRX_HA_Self_BYOL(t *testing.T) { deviceResourceName := fmt.Sprintf("equinix_network_device.%s", context["device-resourceName"].(string)) var primary, secondary ne.Device resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withACL().withSSHKey().build(), @@ -506,8 +576,8 @@ func TestAccNetworkDevice_PaloAlto_HA_Managed_Sub(t *testing.T) { secACLResourceName := fmt.Sprintf("equinix_network_acl_template.%s", context["acl-secondary_resourceName"].(string)) userResourceName := fmt.Sprintf("equinix_network_ssh_user.%s", contextWithChanges["user-resourceName"].(string)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withACL().build(), @@ -586,8 +656,8 @@ func TestAccNetworkDevice_PaloAlto_HA_Self_BYOL(t *testing.T) { var primary, secondary ne.Device var primaryACL, secondaryACL ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withSSHKey().build(), @@ -664,8 +734,8 @@ func TestAccNetworkDevice_CSRSDWAN_HA_Self_BYOL(t *testing.T) { var primary, secondary ne.Device var primaryACL, secondaryACL ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withACL().build(), @@ -743,8 +813,8 @@ func TestAccNetworkDevice_Versa_HA_Self_BYOL(t *testing.T) { var primary, secondary ne.Device var primaryACL, secondaryACL ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withACL().build(), @@ -811,8 +881,8 @@ func TestAccNetworkDevice_CGENIX_HA_Self_BYOL(t *testing.T) { var primary, secondary ne.Device var primaryACL, secondaryACL ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withACL().build(), @@ -879,8 +949,8 @@ func TestAccNetworkDevice_PaloAlto_Cluster_Self_BYOL(t *testing.T) { var primary ne.Device var wanAcl, mgmtAcl ne.ACLTemplate resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withSSHKey().withACL().build(), @@ -916,7 +986,7 @@ func testAccNeDeviceExists(resourceName string, device *ne.Device) resource.Test if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne resp, err := client.GetDevice(rs.Primary.ID) if err != nil { return fmt.Errorf("error when fetching network device '%s': %s", rs.Primary.ID, err) @@ -931,7 +1001,7 @@ func testAccNeDeviceSecondaryExists(primary, secondary *ne.Device) resource.Test if ne.StringValue(primary.RedundantUUID) == "" { return fmt.Errorf("secondary device UUID is not set") } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne resp, err := client.GetDevice(ne.StringValue(primary.RedundantUUID)) if err != nil { return fmt.Errorf("error when fetching network device '%s': %s", ne.StringValue(primary.RedundantUUID), err) @@ -950,7 +1020,7 @@ func testAccNeDevicePairExists(resourceName string, primary, secondary *ne.Devic if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne resp, err := client.GetDevice(rs.Primary.ID) if err != nil { return fmt.Errorf("error when fetching primary network device '%s': %s", rs.Primary.ID, err) @@ -1140,7 +1210,7 @@ func testAccNeDeviceACL(resourceName string, device *ne.Device) resource.TestChe } templateId := rs.Primary.ID deviceID := ne.StringValue(device.UUID) - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne if ne.StringValue(device.ACLTemplateUUID) != rs.Primary.ID { return fmt.Errorf("acl_template_id for device %s does not match %v - %v", deviceID, ne.StringValue(device.ACLTemplateUUID), templateId) } @@ -1227,392 +1297,3 @@ func testAccNeDeviceClusterNodeAttributes(device *ne.Device, ctx map[string]inte return nil } } - -func (t *testAccConfig) withDevice() *testAccConfig { - t.config += testAccNetworkDevice(t.ctx) - return t -} - -func (t *testAccConfig) withACL() *testAccConfig { - t.config += testAccNetworkDeviceACL(t.ctx) - return t -} - -func (t *testAccConfig) withSSHKey() *testAccConfig { - t.config += testAccNetworkDeviceSSHKey(t.ctx) - return t -} - -func testAccNetworkDevice(ctx map[string]interface{}) string { - var config string - config += nprintf.Nprintf(` -data "equinix_network_account" "test" { - metro_code = "%{device-metro_code}" - status = "Active" - project_id = "%{device-project_id}"`, ctx) - if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) { - config += nprintf.Nprintf(` - name = "%{device-account_name}"`, ctx) - } - config += nprintf.Nprintf(` -}`, ctx) - if _, ok := ctx["device-secondary_metro_code"]; ok { - config += nprintf.Nprintf(` -data "equinix_network_account" "test-secondary" { - metro_code = "%{device-secondary_metro_code}" - status = "Active"`, ctx) - if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) { - config += nprintf.Nprintf(` - name = "%{device-secondary_account_name}"`, ctx) - } - config += nprintf.Nprintf(` -}`, ctx) - } - config += nprintf.Nprintf(` -resource "equinix_network_device" "%{device-resourceName}" { - self_managed = %{device-self_managed} - byol = %{device-byol} - name = "%{device-name}" - metro_code = "%{device-metro_code}" - type_code = "%{device-type_code}" - project_id = "%{device-project_id}" - package_code = "%{device-package_code}" - notifications = %{device-notifications} - term_length = %{device-term_length} - account_number = data.equinix_network_account.test.number - version = "%{device-version}" - core_count = %{device-core_count}`, ctx) - if _, ok := ctx["device-purchase_order_number"]; ok { - config += nprintf.Nprintf(` - purchase_order_number = "%{device-purchase_order_number}"`, ctx) - } - if _, ok := ctx["device-purchase_order_number"]; ok { - config += nprintf.Nprintf(` - order_reference = "%{device-order_reference}"`, ctx) - } - if _, ok := ctx["device-additional_bandwidth"]; ok { - config += nprintf.Nprintf(` - additional_bandwidth = %{device-additional_bandwidth}`, ctx) - } - if _, ok := ctx["device-throughput"]; ok { - config += nprintf.Nprintf(` - throughput = %{device-throughput} - throughput_unit = "%{device-throughput_unit}"`, ctx) - } - if _, ok := ctx["device-hostname"]; ok { - config += nprintf.Nprintf(` - hostname = "%{device-hostname}"`, ctx) - } - if _, ok := ctx["device-interface_count"]; ok { - config += nprintf.Nprintf(` - interface_count = %{device-interface_count}`, ctx) - } - if _, ok := ctx["acl-resourceName"]; ok { - config += nprintf.Nprintf(` - acl_template_id = equinix_network_acl_template.%{acl-resourceName}.id`, ctx) - } - if _, ok := ctx["mgmtAcl-resourceName"]; ok { - config += nprintf.Nprintf(` - mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-resourceName}.id`, ctx) - } - if _, ok := ctx["sshkey-resourceName"]; ok { - config += nprintf.Nprintf(` - ssh_key { - username = "test" - key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name - }`, ctx) - } - if _, ok := ctx["device-license_file"]; ok { - config += nprintf.Nprintf(` - license_file = "%{device-license_file}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_enabled"]; ok { - config += nprintf.Nprintf(` - vendor_configuration = {`, ctx) - if _, ok := ctx["device-vendorConfig_siteId"]; ok { - config += nprintf.Nprintf(` - siteId = "%{device-vendorConfig_siteId}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_systemIpAddress"]; ok { - config += nprintf.Nprintf(` - systemIpAddress = "%{device-vendorConfig_systemIpAddress}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_licenseKey"]; ok { - config += nprintf.Nprintf(` - licenseKey = "%{device-vendorConfig_licenseKey}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_licenseSecret"]; ok { - config += nprintf.Nprintf(` - licenseSecret = "%{device-vendorConfig_licenseSecret}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_controller1"]; ok { - config += nprintf.Nprintf(` - controller1 = "%{device-vendorConfig_controller1}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_controller2"]; ok { - config += nprintf.Nprintf(` - controller2 = "%{device-vendorConfig_controller2}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_localId"]; ok { - config += nprintf.Nprintf(` - localId = "%{device-vendorConfig_localId}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_remoteId"]; ok { - config += nprintf.Nprintf(` - remoteId = "%{device-vendorConfig_remoteId}"`, ctx) - } - if _, ok := ctx["device-vendorConfig_serialNumber"]; ok { - config += nprintf.Nprintf(` - serialNumber = "%{device-vendorConfig_serialNumber}"`, ctx) - } - config += nprintf.Nprintf(` - }`, ctx) - } - if _, ok := ctx["device-secondary_name"]; ok { - config += nprintf.Nprintf(` - secondary_device { - name = "%{device-secondary_name}"`, ctx) - if _, ok := ctx["device-secondary_metro_code"]; ok { - config += nprintf.Nprintf(` - metro_code = "%{device-secondary_metro_code}" - account_number = data.equinix_network_account.test-secondary.number`, ctx) - } else { - config += nprintf.Nprintf(` - metro_code = "%{device-metro_code}" - account_number = data.equinix_network_account.test.number`, ctx) - } - config += nprintf.Nprintf(` - notifications = %{device-secondary_notifications}`, ctx) - if _, ok := ctx["device-secondary_additional_bandwidth"]; ok { - config += nprintf.Nprintf(` - additional_bandwidth = %{device-secondary_additional_bandwidth}`, ctx) - } - if _, ok := ctx["device-secondary_hostname"]; ok { - config += nprintf.Nprintf(` - hostname = "%{device-secondary_hostname}"`, ctx) - } - if _, ok := ctx["acl-secondary_resourceName"]; ok { - config += nprintf.Nprintf(` - acl_template_id = equinix_network_acl_template.%{acl-secondary_resourceName}.id`, ctx) - } - if _, ok := ctx["mgmtAcl-secondary_resourceName"]; ok { - config += nprintf.Nprintf(` - mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-secondary_resourceName}.id`, ctx) - } - if _, ok := ctx["sshkey-resourceName"]; ok { - config += nprintf.Nprintf(` - ssh_key { - username = "test" - key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name - }`, ctx) - } - if _, ok := ctx["device-secondary_license_file"]; ok { - config += nprintf.Nprintf(` - license_file = "%{device-secondary_license_file}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_enabled"]; ok { - config += nprintf.Nprintf(` - vendor_configuration = {`, ctx) - if _, ok := ctx["device-secondary_vendorConfig_siteId"]; ok { - config += nprintf.Nprintf(` - siteId = "%{device-secondary_vendorConfig_siteId}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_systemIpAddress"]; ok { - config += nprintf.Nprintf(` - systemIpAddress = "%{device-secondary_vendorConfig_systemIpAddress}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_licenseKey"]; ok { - config += nprintf.Nprintf(` - licenseKey = "%{device-secondary_vendorConfig_licenseKey}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_licenseSecret"]; ok { - config += nprintf.Nprintf(` - licenseSecret = "%{device-secondary_vendorConfig_licenseSecret}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_controller1"]; ok { - config += nprintf.Nprintf(` - controller1 = "%{device-secondary_vendorConfig_controller1}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_controller2"]; ok { - config += nprintf.Nprintf(` - controller2 = "%{device-secondary_vendorConfig_controller2}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_localId"]; ok { - config += nprintf.Nprintf(` - localId = "%{device-secondary_vendorConfig_localId}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_remoteId"]; ok { - config += nprintf.Nprintf(` - remoteId = "%{device-secondary_vendorConfig_remoteId}"`, ctx) - } - if _, ok := ctx["device-secondary_vendorConfig_serialNumber"]; ok { - config += nprintf.Nprintf(` - serialNumber = "%{device-secondary_vendorConfig_serialNumber}"`, ctx) - } - config += nprintf.Nprintf(` - }`, ctx) - } - config += ` - }` - } - if _, ok := ctx["device-cluster_name"]; ok { - config += nprintf.Nprintf(` - cluster_details { - cluster_name = "%{device-cluster_name}"`, ctx) - config += ` - node0 {` - if _, ok := ctx["device-node0_license_file_id"]; ok { - config += nprintf.Nprintf(` - license_file_id = "%{device-node0_license_file_id}"`, ctx) - } - if _, ok := ctx["device-node0_license_token"]; ok { - config += nprintf.Nprintf(` - license_token = "%{device-node0_license_token}"`, ctx) - } - if _, ok := ctx["device-node0_vendorConfig_enabled"]; ok { - config += nprintf.Nprintf(` - vendor_configuration {`, ctx) - if _, ok := ctx["device-node0_vendorConfig_hostname"]; ok { - config += nprintf.Nprintf(` - hostname = "%{device-node0_vendorConfig_hostname}"`, ctx) - } - if _, ok := ctx["device-node0_vendorConfig_adminPassword"]; ok { - config += nprintf.Nprintf(` - admin_password = "%{device-node0_vendorConfig_adminPassword}"`, ctx) - } - if _, ok := ctx["device-node0_vendorConfig_controller1"]; ok { - config += nprintf.Nprintf(` - controller1 = "%{device-node0_vendorConfig_controller1}"`, ctx) - } - if _, ok := ctx["device-node0_vendorConfig_activationKey"]; ok { - config += nprintf.Nprintf(` - activation_key = "%{device-node0_vendorConfig_activationKey}"`, ctx) - } - if _, ok := ctx["device-node0_vendorConfig_controllerFqdn"]; ok { - config += nprintf.Nprintf(` - controller_fqdn = "%{device-node0_vendorConfig_controllerFqdn}"`, ctx) - } - if _, ok := ctx["device-node0_vendorConfig_rootPassword"]; ok { - config += nprintf.Nprintf(` - root_password = "%{device-node0_vendorConfig_rootPassword}"`, ctx) - } - config += nprintf.Nprintf(` - }`, ctx) - } - config += ` - }` - config += ` - node1 {` - if _, ok := ctx["device-node1_license_file_id"]; ok { - config += nprintf.Nprintf(` - license_file_id = "%{device-node1_license_file_id}"`, ctx) - } - if _, ok := ctx["device-node1_license_token"]; ok { - config += nprintf.Nprintf(` - license_token = "%{device-node1_license_token}"`, ctx) - } - if _, ok := ctx["device-node1_vendorConfig_enabled"]; ok { - config += nprintf.Nprintf(` - vendor_configuration {`, ctx) - if _, ok := ctx["device-node1_vendorConfig_hostname"]; ok { - config += nprintf.Nprintf(` - hostname = "%{device-node1_vendorConfig_hostname}"`, ctx) - } - if _, ok := ctx["device-node1_vendorConfig_adminPassword"]; ok { - config += nprintf.Nprintf(` - admin_password = "%{device-node1_vendorConfig_adminPassword}"`, ctx) - } - if _, ok := ctx["device-node1_vendorConfig_controller1"]; ok { - config += nprintf.Nprintf(` - controller1 = "%{device-node1_vendorConfig_controller1}"`, ctx) - } - if _, ok := ctx["device-node1_vendorConfig_activationKey"]; ok { - config += nprintf.Nprintf(` - activation_key = "%{device-node1_vendorConfig_activationKey}"`, ctx) - } - if _, ok := ctx["device-node1_vendorConfig_controllerFqdn"]; ok { - config += nprintf.Nprintf(` - controller_fqdn = "%{device-node1_vendorConfig_controllerFqdn}"`, ctx) - } - if _, ok := ctx["device-node1_vendorConfig_rootPassword"]; ok { - config += nprintf.Nprintf(` - root_password = "%{device-node1_vendorConfig_rootPassword}"`, ctx) - } - config += nprintf.Nprintf(` - }`, ctx) - } - config += ` - }` - config += ` - }` - } - config += ` -}` - return config -} - -func testAccNetworkDeviceACL(ctx map[string]interface{}) string { - var config string - if _, ok := ctx["acl-name"]; ok { - config += nprintf.Nprintf(` -resource "equinix_network_acl_template" "%{acl-resourceName}" { - name = "%{acl-name}" - description = "%{acl-description}" - inbound_rule { - subnet = "10.0.0.0/24" - protocol = "IP" - src_port = "any" - dst_port = "any" - } -}`, ctx) - } - if _, ok := ctx["mgmtAcl-name"]; ok { - config += nprintf.Nprintf(` -resource "equinix_network_acl_template" "%{mgmtAcl-resourceName}" { - name = "%{mgmtAcl-name}" - description = "%{mgmtAcl-description}" - inbound_rule { - subnet = "11.0.0.0/24" - protocol = "IP" - src_port = "any" - dst_port = "any" - } -}`, ctx) - } - if _, ok := ctx["acl-secondary_name"]; ok { - config += nprintf.Nprintf(` -resource "equinix_network_acl_template" "%{acl-secondary_resourceName}" { - name = "%{acl-secondary_name}" - description = "%{acl-secondary_description}" - inbound_rule { - subnet = "192.0.0.0/24" - protocol = "IP" - src_port = "any" - dst_port = "any" - } -}`, ctx) - } - if _, ok := ctx["mgmtAcl-secondary_name"]; ok { - config += nprintf.Nprintf(` -resource "equinix_network_acl_template" "%{mgmtAcl-secondary_resourceName}" { - name = "%{mgmtAcl-secondary_name}" - description = "%{mgmtAcl-secondary_description}" - inbound_rule { - subnet = "193.0.0.0/24" - protocol = "IP" - src_port = "any" - dst_port = "any" - } -}`, ctx) - } - return config -} - -func testAccNetworkDeviceSSHKey(ctx map[string]interface{}) string { - return nprintf.Nprintf(` -resource "equinix_network_ssh_key" "%{sshkey-resourceName}" { - name = "%{sshkey-name}" - public_key = "%{sshkey-public_key}" -} -`, ctx) -} diff --git a/internal/resources/networkedge/device/templates_acc_test.go b/internal/resources/networkedge/device/templates_acc_test.go new file mode 100644 index 000000000..129037117 --- /dev/null +++ b/internal/resources/networkedge/device/templates_acc_test.go @@ -0,0 +1,461 @@ +package device + +import ( + "github.com/equinix/terraform-provider-equinix/internal/comparisons" + "github.com/equinix/terraform-provider-equinix/internal/nprintf" +) + +const ( + tstResourcePrefix = "tfacc" + + networkDeviceProjectId = "TF_ACC_NETWORK_DEVICE_PROJECT_ID" + networkDeviceAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_BILLING_ACCOUNT_NAME" + networkDeviceSecondaryAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_BILLING_ACCOUNT_NAME" + networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO" + networkDeviceSecondaryMetroEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_METRO" + networkDeviceCSRSDWANLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_CSRSDWAN_LICENSE_FILE" + networkDeviceVSRXLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_VSRX_LICENSE_FILE" + networkDeviceVersaController1EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER1" + networkDeviceVersaController2EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER2" + networkDeviceVersaLocalIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_LOCALID" + networkDeviceVersaRemoteIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_REMOTEID" + networkDeviceVersaSerialNumberEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_SERIAL" + networkDeviceCGENIXLicenseKeyEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_KEY" + networkDeviceCGENIXLicenseSecretEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_SECRET" + networkDevicePANWLicenseTokenEnvVar = "TF_ACC_NETWORK_DEVICE_PANW_LICENSE_TOKEN" +) + +type testAccConfig struct { + ctx map[string]interface{} + config string +} + +func newTestAccConfig(ctx map[string]interface{}) *testAccConfig { + return &testAccConfig{ + ctx: ctx, + config: "", + } +} + +func (t *testAccConfig) build() string { + return t.config +} + +func (t *testAccConfig) withDevice() *testAccConfig { + t.config += testAccNetworkDevice(t.ctx) + return t +} + +func copyMap(source map[string]interface{}) map[string]interface{} { + target := make(map[string]interface{}) + for k, v := range source { + target[k] = v + } + return target +} + +func testAccNetworkDeviceUser(ctx map[string]interface{}) string { + config := nprintf.Nprintf(` +resource "equinix_network_ssh_user" "%{user-resourceName}" { + username = "%{user-username}" + password = "%{user-password}" + device_ids = [ + equinix_network_device.%{device-resourceName}.id`, ctx) + if _, ok := ctx["device-secondary_name"]; ok { + config += nprintf.Nprintf(`, + equinix_network_device.%{device-resourceName}.redundant_id`, ctx) + } + config += ` + ] +}` + return config +} + +func testAccNetworkDevice(ctx map[string]interface{}) string { + var config string + config += nprintf.Nprintf(` +data "equinix_network_account" "test" { + metro_code = "%{device-metro_code}" + status = "Active" + project_id = "%{device-project_id}"`, ctx) + if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) { + config += nprintf.Nprintf(` + name = "%{device-account_name}"`, ctx) + } + config += nprintf.Nprintf(` +}`, ctx) + if _, ok := ctx["device-secondary_metro_code"]; ok { + config += nprintf.Nprintf(` +data "equinix_network_account" "test-secondary" { + metro_code = "%{device-secondary_metro_code}" + status = "Active"`, ctx) + if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) { + config += nprintf.Nprintf(` + name = "%{device-secondary_account_name}"`, ctx) + } + config += nprintf.Nprintf(` +}`, ctx) + } + config += nprintf.Nprintf(` +resource "equinix_network_device" "%{device-resourceName}" { + self_managed = %{device-self_managed} + byol = %{device-byol} + name = "%{device-name}" + metro_code = "%{device-metro_code}" + type_code = "%{device-type_code}" + project_id = "%{device-project_id}" + package_code = "%{device-package_code}" + notifications = %{device-notifications} + term_length = %{device-term_length} + account_number = data.equinix_network_account.test.number + version = "%{device-version}" + core_count = %{device-core_count}`, ctx) + if _, ok := ctx["device-purchase_order_number"]; ok { + config += nprintf.Nprintf(` + purchase_order_number = "%{device-purchase_order_number}"`, ctx) + } + if _, ok := ctx["device-purchase_order_number"]; ok { + config += nprintf.Nprintf(` + order_reference = "%{device-order_reference}"`, ctx) + } + if _, ok := ctx["device-additional_bandwidth"]; ok { + config += nprintf.Nprintf(` + additional_bandwidth = %{device-additional_bandwidth}`, ctx) + } + if _, ok := ctx["device-throughput"]; ok { + config += nprintf.Nprintf(` + throughput = %{device-throughput} + throughput_unit = "%{device-throughput_unit}"`, ctx) + } + if _, ok := ctx["device-hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-hostname}"`, ctx) + } + if _, ok := ctx["device-interface_count"]; ok { + config += nprintf.Nprintf(` + interface_count = %{device-interface_count}`, ctx) + } + if _, ok := ctx["acl-resourceName"]; ok { + config += nprintf.Nprintf(` + acl_template_id = equinix_network_acl_template.%{acl-resourceName}.id`, ctx) + } + if _, ok := ctx["mgmtAcl-resourceName"]; ok { + config += nprintf.Nprintf(` + mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-resourceName}.id`, ctx) + } + if _, ok := ctx["sshkey-resourceName"]; ok { + config += nprintf.Nprintf(` + ssh_key { + username = "test" + key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name + }`, ctx) + } + if _, ok := ctx["device-license_file"]; ok { + config += nprintf.Nprintf(` + license_file = "%{device-license_file}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration = {`, ctx) + if _, ok := ctx["device-vendorConfig_siteId"]; ok { + config += nprintf.Nprintf(` + siteId = "%{device-vendorConfig_siteId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_systemIpAddress"]; ok { + config += nprintf.Nprintf(` + systemIpAddress = "%{device-vendorConfig_systemIpAddress}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_licenseKey"]; ok { + config += nprintf.Nprintf(` + licenseKey = "%{device-vendorConfig_licenseKey}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_licenseSecret"]; ok { + config += nprintf.Nprintf(` + licenseSecret = "%{device-vendorConfig_licenseSecret}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_controller2"]; ok { + config += nprintf.Nprintf(` + controller2 = "%{device-vendorConfig_controller2}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_localId"]; ok { + config += nprintf.Nprintf(` + localId = "%{device-vendorConfig_localId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_remoteId"]; ok { + config += nprintf.Nprintf(` + remoteId = "%{device-vendorConfig_remoteId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_serialNumber"]; ok { + config += nprintf.Nprintf(` + serialNumber = "%{device-vendorConfig_serialNumber}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + if _, ok := ctx["device-secondary_name"]; ok { + config += nprintf.Nprintf(` + secondary_device { + name = "%{device-secondary_name}"`, ctx) + if _, ok := ctx["device-secondary_metro_code"]; ok { + config += nprintf.Nprintf(` + metro_code = "%{device-secondary_metro_code}" + account_number = data.equinix_network_account.test-secondary.number`, ctx) + } else { + config += nprintf.Nprintf(` + metro_code = "%{device-metro_code}" + account_number = data.equinix_network_account.test.number`, ctx) + } + config += nprintf.Nprintf(` + notifications = %{device-secondary_notifications}`, ctx) + if _, ok := ctx["device-secondary_additional_bandwidth"]; ok { + config += nprintf.Nprintf(` + additional_bandwidth = %{device-secondary_additional_bandwidth}`, ctx) + } + if _, ok := ctx["device-secondary_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-secondary_hostname}"`, ctx) + } + if _, ok := ctx["acl-secondary_resourceName"]; ok { + config += nprintf.Nprintf(` + acl_template_id = equinix_network_acl_template.%{acl-secondary_resourceName}.id`, ctx) + } + if _, ok := ctx["mgmtAcl-secondary_resourceName"]; ok { + config += nprintf.Nprintf(` + mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-secondary_resourceName}.id`, ctx) + } + if _, ok := ctx["sshkey-resourceName"]; ok { + config += nprintf.Nprintf(` + ssh_key { + username = "test" + key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name + }`, ctx) + } + if _, ok := ctx["device-secondary_license_file"]; ok { + config += nprintf.Nprintf(` + license_file = "%{device-secondary_license_file}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration = {`, ctx) + if _, ok := ctx["device-secondary_vendorConfig_siteId"]; ok { + config += nprintf.Nprintf(` + siteId = "%{device-secondary_vendorConfig_siteId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_systemIpAddress"]; ok { + config += nprintf.Nprintf(` + systemIpAddress = "%{device-secondary_vendorConfig_systemIpAddress}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_licenseKey"]; ok { + config += nprintf.Nprintf(` + licenseKey = "%{device-secondary_vendorConfig_licenseKey}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_licenseSecret"]; ok { + config += nprintf.Nprintf(` + licenseSecret = "%{device-secondary_vendorConfig_licenseSecret}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-secondary_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_controller2"]; ok { + config += nprintf.Nprintf(` + controller2 = "%{device-secondary_vendorConfig_controller2}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_localId"]; ok { + config += nprintf.Nprintf(` + localId = "%{device-secondary_vendorConfig_localId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_remoteId"]; ok { + config += nprintf.Nprintf(` + remoteId = "%{device-secondary_vendorConfig_remoteId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_serialNumber"]; ok { + config += nprintf.Nprintf(` + serialNumber = "%{device-secondary_vendorConfig_serialNumber}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + } + if _, ok := ctx["device-cluster_name"]; ok { + config += nprintf.Nprintf(` + cluster_details { + cluster_name = "%{device-cluster_name}"`, ctx) + config += ` + node0 {` + if _, ok := ctx["device-node0_license_file_id"]; ok { + config += nprintf.Nprintf(` + license_file_id = "%{device-node0_license_file_id}"`, ctx) + } + if _, ok := ctx["device-node0_license_token"]; ok { + config += nprintf.Nprintf(` + license_token = "%{device-node0_license_token}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration {`, ctx) + if _, ok := ctx["device-node0_vendorConfig_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-node0_vendorConfig_hostname}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_adminPassword"]; ok { + config += nprintf.Nprintf(` + admin_password = "%{device-node0_vendorConfig_adminPassword}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-node0_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_activationKey"]; ok { + config += nprintf.Nprintf(` + activation_key = "%{device-node0_vendorConfig_activationKey}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_controllerFqdn"]; ok { + config += nprintf.Nprintf(` + controller_fqdn = "%{device-node0_vendorConfig_controllerFqdn}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_rootPassword"]; ok { + config += nprintf.Nprintf(` + root_password = "%{device-node0_vendorConfig_rootPassword}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + config += ` + node1 {` + if _, ok := ctx["device-node1_license_file_id"]; ok { + config += nprintf.Nprintf(` + license_file_id = "%{device-node1_license_file_id}"`, ctx) + } + if _, ok := ctx["device-node1_license_token"]; ok { + config += nprintf.Nprintf(` + license_token = "%{device-node1_license_token}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration {`, ctx) + if _, ok := ctx["device-node1_vendorConfig_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-node1_vendorConfig_hostname}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_adminPassword"]; ok { + config += nprintf.Nprintf(` + admin_password = "%{device-node1_vendorConfig_adminPassword}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-node1_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_activationKey"]; ok { + config += nprintf.Nprintf(` + activation_key = "%{device-node1_vendorConfig_activationKey}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_controllerFqdn"]; ok { + config += nprintf.Nprintf(` + controller_fqdn = "%{device-node1_vendorConfig_controllerFqdn}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_rootPassword"]; ok { + config += nprintf.Nprintf(` + root_password = "%{device-node1_vendorConfig_rootPassword}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + config += ` + }` + } + config += ` +}` + return config +} + +func testAccNetworkDeviceACL(ctx map[string]interface{}) string { + var config string + if _, ok := ctx["acl-name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{acl-resourceName}" { + name = "%{acl-name}" + description = "%{acl-description}" + inbound_rule { + subnet = "10.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["mgmtAcl-name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{mgmtAcl-resourceName}" { + name = "%{mgmtAcl-name}" + description = "%{mgmtAcl-description}" + inbound_rule { + subnet = "11.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["acl-secondary_name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{acl-secondary_resourceName}" { + name = "%{acl-secondary_name}" + description = "%{acl-secondary_description}" + inbound_rule { + subnet = "192.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["mgmtAcl-secondary_name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{mgmtAcl-secondary_resourceName}" { + name = "%{mgmtAcl-secondary_name}" + description = "%{mgmtAcl-secondary_description}" + inbound_rule { + subnet = "193.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + return config +} + +func testAccNetworkDeviceSSHKey(ctx map[string]interface{}) string { + return nprintf.Nprintf(` +resource "equinix_network_ssh_key" "%{sshkey-resourceName}" { + name = "%{sshkey-name}" + public_key = "%{sshkey-public_key}" +} +`, ctx) +} + +func (t *testAccConfig) withACL() *testAccConfig { + t.config += testAccNetworkDeviceACL(t.ctx) + return t +} + +func (t *testAccConfig) withSSHKey() *testAccConfig { + t.config += testAccNetworkDeviceSSHKey(t.ctx) + return t +} + +func (t *testAccConfig) withSSHUser() *testAccConfig { + t.config += testAccNetworkDeviceUser(t.ctx) + return t +} diff --git a/internal/resources/networkedge/device_link/resource_acc_test.go b/internal/resources/networkedge/device_link/resource_acc_test.go index bcc032379..131a0d3b7 100644 --- a/internal/resources/networkedge/device_link/resource_acc_test.go +++ b/internal/resources/networkedge/device_link/resource_acc_test.go @@ -6,8 +6,10 @@ import ( "log" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/nprintf" + "github.com/equinix/terraform-provider-equinix/internal/sweep" "github.com/equinix/ne-go" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -23,8 +25,32 @@ func init() { }) } +func testAccNeDevicePairExists(resourceName string, primary, secondary *ne.Device) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("resource not found: %s", resourceName) + } + if rs.Primary.ID == "" { + return fmt.Errorf("resource has no ID attribute set") + } + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne + resp, err := client.GetDevice(rs.Primary.ID) + if err != nil { + return fmt.Errorf("error when fetching primary network device '%s': %s", rs.Primary.ID, err) + } + *primary = *resp + resp, err = client.GetDevice(ne.StringValue(resp.RedundantUUID)) + if err != nil { + return fmt.Errorf("error when fetching secondary network device '%s': %s", rs.Primary.ID, err) + } + *secondary = *resp + return nil + } +} + func testSweepNetworkDeviceLink(region string) error { - config, err := sharedConfigForRegion(region) + config, err := sweep.SharedConfigForRegion(region) if err != nil { return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping Network devices link: %s", err) } @@ -39,7 +65,7 @@ func testSweepNetworkDeviceLink(region string) error { } nonSweepableCount := 0 for _, link := range links { - if !isSweepableTestResource(ne.StringValue(link.Name)) { + if !sweep.IsSweepableTestResource(ne.StringValue(link.Name)) { nonSweepableCount++ continue } @@ -105,8 +131,8 @@ func TestAccNetworkDeviceLink(t *testing.T) { var deviceLink ne.DeviceLinkGroup var primaryDevice, secondaryDevice ne.Device resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: newTestAccConfig(context).withDevice().withDeviceLink().build(), @@ -182,7 +208,7 @@ func testAccNeDeviceLinkExists(resourceName string, deviceLink *ne.DeviceLinkGro if !ok { return fmt.Errorf("resource not found: %s", resourceName) } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } diff --git a/internal/resources/networkedge/device_link/templates_acc_test.go b/internal/resources/networkedge/device_link/templates_acc_test.go new file mode 100644 index 000000000..657860ce9 --- /dev/null +++ b/internal/resources/networkedge/device_link/templates_acc_test.go @@ -0,0 +1,461 @@ +package device_link + +import ( + "github.com/equinix/terraform-provider-equinix/internal/comparisons" + "github.com/equinix/terraform-provider-equinix/internal/nprintf" +) + +const ( + tstResourcePrefix = "tfacc" + + networkDeviceProjectId = "TF_ACC_NETWORK_DEVICE_PROJECT_ID" + networkDeviceAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_BILLING_ACCOUNT_NAME" + networkDeviceSecondaryAccountNameEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_BILLING_ACCOUNT_NAME" + networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO" + networkDeviceSecondaryMetroEnvVar = "TF_ACC_NETWORK_DEVICE_SECONDARY_METRO" + networkDeviceCSRSDWANLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_CSRSDWAN_LICENSE_FILE" + networkDeviceVSRXLicenseFileEnvVar = "TF_ACC_NETWORK_DEVICE_VSRX_LICENSE_FILE" + networkDeviceVersaController1EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER1" + networkDeviceVersaController2EnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_CONTROLLER2" + networkDeviceVersaLocalIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_LOCALID" + networkDeviceVersaRemoteIDEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_REMOTEID" + networkDeviceVersaSerialNumberEnvVar = "TF_ACC_NETWORK_DEVICE_VERSA_SERIAL" + networkDeviceCGENIXLicenseKeyEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_KEY" + networkDeviceCGENIXLicenseSecretEnvVar = "TF_ACC_NETWORK_DEVICE_CGENIX_LICENSE_SECRET" + networkDevicePANWLicenseTokenEnvVar = "TF_ACC_NETWORK_DEVICE_PANW_LICENSE_TOKEN" +) + +type testAccConfig struct { + ctx map[string]interface{} + config string +} + +func newTestAccConfig(ctx map[string]interface{}) *testAccConfig { + return &testAccConfig{ + ctx: ctx, + config: "", + } +} + +func (t *testAccConfig) build() string { + return t.config +} + +func (t *testAccConfig) withDevice() *testAccConfig { + t.config += testAccNetworkDevice(t.ctx) + return t +} + +func copyMap(source map[string]interface{}) map[string]interface{} { + target := make(map[string]interface{}) + for k, v := range source { + target[k] = v + } + return target +} + +func testAccNetworkDeviceUser(ctx map[string]interface{}) string { + config := nprintf.Nprintf(` +resource "equinix_network_ssh_user" "%{user-resourceName}" { + username = "%{user-username}" + password = "%{user-password}" + device_ids = [ + equinix_network_device.%{device-resourceName}.id`, ctx) + if _, ok := ctx["device-secondary_name"]; ok { + config += nprintf.Nprintf(`, + equinix_network_device.%{device-resourceName}.redundant_id`, ctx) + } + config += ` + ] +}` + return config +} + +func testAccNetworkDevice(ctx map[string]interface{}) string { + var config string + config += nprintf.Nprintf(` +data "equinix_network_account" "test" { + metro_code = "%{device-metro_code}" + status = "Active" + project_id = "%{device-project_id}"`, ctx) + if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) { + config += nprintf.Nprintf(` + name = "%{device-account_name}"`, ctx) + } + config += nprintf.Nprintf(` +}`, ctx) + if _, ok := ctx["device-secondary_metro_code"]; ok { + config += nprintf.Nprintf(` +data "equinix_network_account" "test-secondary" { + metro_code = "%{device-secondary_metro_code}" + status = "Active"`, ctx) + if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) { + config += nprintf.Nprintf(` + name = "%{device-secondary_account_name}"`, ctx) + } + config += nprintf.Nprintf(` +}`, ctx) + } + config += nprintf.Nprintf(` +resource "equinix_network_device" "%{device-resourceName}" { + self_managed = %{device-self_managed} + byol = %{device-byol} + name = "%{device-name}" + metro_code = "%{device-metro_code}" + type_code = "%{device-type_code}" + project_id = "%{device-project_id}" + package_code = "%{device-package_code}" + notifications = %{device-notifications} + term_length = %{device-term_length} + account_number = data.equinix_network_account.test.number + version = "%{device-version}" + core_count = %{device-core_count}`, ctx) + if _, ok := ctx["device-purchase_order_number"]; ok { + config += nprintf.Nprintf(` + purchase_order_number = "%{device-purchase_order_number}"`, ctx) + } + if _, ok := ctx["device-purchase_order_number"]; ok { + config += nprintf.Nprintf(` + order_reference = "%{device-order_reference}"`, ctx) + } + if _, ok := ctx["device-additional_bandwidth"]; ok { + config += nprintf.Nprintf(` + additional_bandwidth = %{device-additional_bandwidth}`, ctx) + } + if _, ok := ctx["device-throughput"]; ok { + config += nprintf.Nprintf(` + throughput = %{device-throughput} + throughput_unit = "%{device-throughput_unit}"`, ctx) + } + if _, ok := ctx["device-hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-hostname}"`, ctx) + } + if _, ok := ctx["device-interface_count"]; ok { + config += nprintf.Nprintf(` + interface_count = %{device-interface_count}`, ctx) + } + if _, ok := ctx["acl-resourceName"]; ok { + config += nprintf.Nprintf(` + acl_template_id = equinix_network_acl_template.%{acl-resourceName}.id`, ctx) + } + if _, ok := ctx["mgmtAcl-resourceName"]; ok { + config += nprintf.Nprintf(` + mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-resourceName}.id`, ctx) + } + if _, ok := ctx["sshkey-resourceName"]; ok { + config += nprintf.Nprintf(` + ssh_key { + username = "test" + key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name + }`, ctx) + } + if _, ok := ctx["device-license_file"]; ok { + config += nprintf.Nprintf(` + license_file = "%{device-license_file}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration = {`, ctx) + if _, ok := ctx["device-vendorConfig_siteId"]; ok { + config += nprintf.Nprintf(` + siteId = "%{device-vendorConfig_siteId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_systemIpAddress"]; ok { + config += nprintf.Nprintf(` + systemIpAddress = "%{device-vendorConfig_systemIpAddress}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_licenseKey"]; ok { + config += nprintf.Nprintf(` + licenseKey = "%{device-vendorConfig_licenseKey}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_licenseSecret"]; ok { + config += nprintf.Nprintf(` + licenseSecret = "%{device-vendorConfig_licenseSecret}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_controller2"]; ok { + config += nprintf.Nprintf(` + controller2 = "%{device-vendorConfig_controller2}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_localId"]; ok { + config += nprintf.Nprintf(` + localId = "%{device-vendorConfig_localId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_remoteId"]; ok { + config += nprintf.Nprintf(` + remoteId = "%{device-vendorConfig_remoteId}"`, ctx) + } + if _, ok := ctx["device-vendorConfig_serialNumber"]; ok { + config += nprintf.Nprintf(` + serialNumber = "%{device-vendorConfig_serialNumber}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + if _, ok := ctx["device-secondary_name"]; ok { + config += nprintf.Nprintf(` + secondary_device { + name = "%{device-secondary_name}"`, ctx) + if _, ok := ctx["device-secondary_metro_code"]; ok { + config += nprintf.Nprintf(` + metro_code = "%{device-secondary_metro_code}" + account_number = data.equinix_network_account.test-secondary.number`, ctx) + } else { + config += nprintf.Nprintf(` + metro_code = "%{device-metro_code}" + account_number = data.equinix_network_account.test.number`, ctx) + } + config += nprintf.Nprintf(` + notifications = %{device-secondary_notifications}`, ctx) + if _, ok := ctx["device-secondary_additional_bandwidth"]; ok { + config += nprintf.Nprintf(` + additional_bandwidth = %{device-secondary_additional_bandwidth}`, ctx) + } + if _, ok := ctx["device-secondary_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-secondary_hostname}"`, ctx) + } + if _, ok := ctx["acl-secondary_resourceName"]; ok { + config += nprintf.Nprintf(` + acl_template_id = equinix_network_acl_template.%{acl-secondary_resourceName}.id`, ctx) + } + if _, ok := ctx["mgmtAcl-secondary_resourceName"]; ok { + config += nprintf.Nprintf(` + mgmt_acl_template_uuid = equinix_network_acl_template.%{mgmtAcl-secondary_resourceName}.id`, ctx) + } + if _, ok := ctx["sshkey-resourceName"]; ok { + config += nprintf.Nprintf(` + ssh_key { + username = "test" + key_name = equinix_network_ssh_key.%{sshkey-resourceName}.name + }`, ctx) + } + if _, ok := ctx["device-secondary_license_file"]; ok { + config += nprintf.Nprintf(` + license_file = "%{device-secondary_license_file}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration = {`, ctx) + if _, ok := ctx["device-secondary_vendorConfig_siteId"]; ok { + config += nprintf.Nprintf(` + siteId = "%{device-secondary_vendorConfig_siteId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_systemIpAddress"]; ok { + config += nprintf.Nprintf(` + systemIpAddress = "%{device-secondary_vendorConfig_systemIpAddress}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_licenseKey"]; ok { + config += nprintf.Nprintf(` + licenseKey = "%{device-secondary_vendorConfig_licenseKey}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_licenseSecret"]; ok { + config += nprintf.Nprintf(` + licenseSecret = "%{device-secondary_vendorConfig_licenseSecret}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-secondary_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_controller2"]; ok { + config += nprintf.Nprintf(` + controller2 = "%{device-secondary_vendorConfig_controller2}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_localId"]; ok { + config += nprintf.Nprintf(` + localId = "%{device-secondary_vendorConfig_localId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_remoteId"]; ok { + config += nprintf.Nprintf(` + remoteId = "%{device-secondary_vendorConfig_remoteId}"`, ctx) + } + if _, ok := ctx["device-secondary_vendorConfig_serialNumber"]; ok { + config += nprintf.Nprintf(` + serialNumber = "%{device-secondary_vendorConfig_serialNumber}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + } + if _, ok := ctx["device-cluster_name"]; ok { + config += nprintf.Nprintf(` + cluster_details { + cluster_name = "%{device-cluster_name}"`, ctx) + config += ` + node0 {` + if _, ok := ctx["device-node0_license_file_id"]; ok { + config += nprintf.Nprintf(` + license_file_id = "%{device-node0_license_file_id}"`, ctx) + } + if _, ok := ctx["device-node0_license_token"]; ok { + config += nprintf.Nprintf(` + license_token = "%{device-node0_license_token}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration {`, ctx) + if _, ok := ctx["device-node0_vendorConfig_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-node0_vendorConfig_hostname}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_adminPassword"]; ok { + config += nprintf.Nprintf(` + admin_password = "%{device-node0_vendorConfig_adminPassword}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-node0_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_activationKey"]; ok { + config += nprintf.Nprintf(` + activation_key = "%{device-node0_vendorConfig_activationKey}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_controllerFqdn"]; ok { + config += nprintf.Nprintf(` + controller_fqdn = "%{device-node0_vendorConfig_controllerFqdn}"`, ctx) + } + if _, ok := ctx["device-node0_vendorConfig_rootPassword"]; ok { + config += nprintf.Nprintf(` + root_password = "%{device-node0_vendorConfig_rootPassword}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + config += ` + node1 {` + if _, ok := ctx["device-node1_license_file_id"]; ok { + config += nprintf.Nprintf(` + license_file_id = "%{device-node1_license_file_id}"`, ctx) + } + if _, ok := ctx["device-node1_license_token"]; ok { + config += nprintf.Nprintf(` + license_token = "%{device-node1_license_token}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_enabled"]; ok { + config += nprintf.Nprintf(` + vendor_configuration {`, ctx) + if _, ok := ctx["device-node1_vendorConfig_hostname"]; ok { + config += nprintf.Nprintf(` + hostname = "%{device-node1_vendorConfig_hostname}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_adminPassword"]; ok { + config += nprintf.Nprintf(` + admin_password = "%{device-node1_vendorConfig_adminPassword}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_controller1"]; ok { + config += nprintf.Nprintf(` + controller1 = "%{device-node1_vendorConfig_controller1}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_activationKey"]; ok { + config += nprintf.Nprintf(` + activation_key = "%{device-node1_vendorConfig_activationKey}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_controllerFqdn"]; ok { + config += nprintf.Nprintf(` + controller_fqdn = "%{device-node1_vendorConfig_controllerFqdn}"`, ctx) + } + if _, ok := ctx["device-node1_vendorConfig_rootPassword"]; ok { + config += nprintf.Nprintf(` + root_password = "%{device-node1_vendorConfig_rootPassword}"`, ctx) + } + config += nprintf.Nprintf(` + }`, ctx) + } + config += ` + }` + config += ` + }` + } + config += ` +}` + return config +} + +func testAccNetworkDeviceACL(ctx map[string]interface{}) string { + var config string + if _, ok := ctx["acl-name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{acl-resourceName}" { + name = "%{acl-name}" + description = "%{acl-description}" + inbound_rule { + subnet = "10.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["mgmtAcl-name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{mgmtAcl-resourceName}" { + name = "%{mgmtAcl-name}" + description = "%{mgmtAcl-description}" + inbound_rule { + subnet = "11.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["acl-secondary_name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{acl-secondary_resourceName}" { + name = "%{acl-secondary_name}" + description = "%{acl-secondary_description}" + inbound_rule { + subnet = "192.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + if _, ok := ctx["mgmtAcl-secondary_name"]; ok { + config += nprintf.Nprintf(` +resource "equinix_network_acl_template" "%{mgmtAcl-secondary_resourceName}" { + name = "%{mgmtAcl-secondary_name}" + description = "%{mgmtAcl-secondary_description}" + inbound_rule { + subnet = "193.0.0.0/24" + protocol = "IP" + src_port = "any" + dst_port = "any" + } +}`, ctx) + } + return config +} + +func testAccNetworkDeviceSSHKey(ctx map[string]interface{}) string { + return nprintf.Nprintf(` +resource "equinix_network_ssh_key" "%{sshkey-resourceName}" { + name = "%{sshkey-name}" + public_key = "%{sshkey-public_key}" +} +`, ctx) +} + +func (t *testAccConfig) withACL() *testAccConfig { + t.config += testAccNetworkDeviceACL(t.ctx) + return t +} + +func (t *testAccConfig) withSSHKey() *testAccConfig { + t.config += testAccNetworkDeviceSSHKey(t.ctx) + return t +} + +func (t *testAccConfig) withSSHUser() *testAccConfig { + t.config += testAccNetworkDeviceUser(t.ctx) + return t +} diff --git a/internal/resources/networkedge/device_software/data_source_acc_test.go b/internal/resources/networkedge/device_software/data_source_acc_test.go index 8920cf912..3f0ee4193 100644 --- a/internal/resources/networkedge/device_software/data_source_acc_test.go +++ b/internal/resources/networkedge/device_software/data_source_acc_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/nprintf" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) @@ -18,8 +19,8 @@ func TestAccDataSourceNetworkDeviceSoftware_versionRegex(t *testing.T) { } resourceName := fmt.Sprintf("data.equinix_network_device_software.%s", context["resourceName"].(string)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccDataSourceNetworkDeviceSoftwareConfig_versionRegex(context), diff --git a/internal/resources/networkedge/device_type/data_source_acc_test.go b/internal/resources/networkedge/device_type/data_source_acc_test.go index 45f1b0f1e..13fc01b0a 100644 --- a/internal/resources/networkedge/device_type/data_source_acc_test.go +++ b/internal/resources/networkedge/device_type/data_source_acc_test.go @@ -4,11 +4,14 @@ import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/nprintf" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) +const networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO" + func TestAccDataSourceNetworkDeviceType_basic(t *testing.T) { metro, _ := schema.EnvDefaultFunc(networkDeviceMetroEnvVar, "SV")() context := map[string]interface{}{ @@ -19,8 +22,8 @@ func TestAccDataSourceNetworkDeviceType_basic(t *testing.T) { } resourceName := fmt.Sprintf("data.equinix_network_device_type.%s", context["resourceName"].(string)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccDataSourceNetworkDeviceTypeConfig_basic(context), diff --git a/internal/resources/networkedge/file/resource_acc_test.go b/internal/resources/networkedge/file/resource_acc_test.go index 6e6e62883..b5208f49f 100644 --- a/internal/resources/networkedge/file/resource_acc_test.go +++ b/internal/resources/networkedge/file/resource_acc_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/nprintf" @@ -13,6 +14,8 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) +const tstResourcePrefix = "tfacc" + func TestAccNetworkFile_VSRX(t *testing.T) { context := map[string]interface{}{ "resourceName": "test", @@ -27,8 +30,8 @@ func TestAccNetworkFile_VSRX(t *testing.T) { resourceName := fmt.Sprintf("equinix_network_file.%s", context["resourceName"].(string)) var file ne.File resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccNetworkFile(context), @@ -69,7 +72,7 @@ func testAccNetworkFileExists(resourceName string, file *ne.File) resource.TestC if !ok { return fmt.Errorf("resource not found: %s", resourceName) } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } diff --git a/internal/resources/networkedge/platform/data_source_acc_test.go b/internal/resources/networkedge/platform/data_source_acc_test.go index 92f3fe957..ead75852a 100644 --- a/internal/resources/networkedge/platform/data_source_acc_test.go +++ b/internal/resources/networkedge/platform/data_source_acc_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/nprintf" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) @@ -17,8 +18,8 @@ func TestAccDataSourceNetworkDevicePlatform_basic(t *testing.T) { } resourceName := fmt.Sprintf("data.equinix_network_device_platform.%s", context["resourceName"].(string)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccDataSourceNetworkDevicePlatformConfig_basic(context), diff --git a/internal/resources/networkedge/ssh_key/resource_acc_test.go b/internal/resources/networkedge/ssh_key/resource_acc_test.go index 56a9485c4..8a36ca85a 100644 --- a/internal/resources/networkedge/ssh_key/resource_acc_test.go +++ b/internal/resources/networkedge/ssh_key/resource_acc_test.go @@ -6,8 +6,10 @@ import ( "log" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/nprintf" + "github.com/equinix/terraform-provider-equinix/internal/sweep" "github.com/equinix/ne-go" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -15,6 +17,8 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) +const tstResourcePrefix = "tfacc" + func init() { resource.AddTestSweepers("equinix_network_ssh_key", &resource.Sweeper{ Name: "equinix_network_ssh_key", @@ -23,7 +27,7 @@ func init() { } func testSweepNetworkSSHKey(region string) error { - config, err := sharedConfigForRegion(region) + config, err := sweep.SharedConfigForRegion(region) if err != nil { return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping Network SSH keys: %s", err) } @@ -38,7 +42,7 @@ func testSweepNetworkSSHKey(region string) error { } nonSweepableCount := 0 for _, key := range keys { - if !isSweepableTestResource(ne.StringValue(key.Name)) { + if !sweep.IsSweepableTestResource(ne.StringValue(key.Name)) { nonSweepableCount++ continue } @@ -64,8 +68,8 @@ func TestAccNetworkSSHKey(t *testing.T) { resourceName := fmt.Sprintf("equinix_network_ssh_key.%s", context["resourceName"].(string)) var key ne.SSHPublicKey resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccNetworkSSHKey(context), @@ -100,7 +104,7 @@ func testAccNetworkSSHKeyExists(resourceName string, key *ne.SSHPublicKey) resou if !ok { return fmt.Errorf("resource not found: %s", resourceName) } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } diff --git a/internal/resources/networkedge/ssh_user/resource_acc_test.go b/internal/resources/networkedge/ssh_user/resource_acc_test.go index dcfd83abb..1bfe7aaa7 100644 --- a/internal/resources/networkedge/ssh_user/resource_acc_test.go +++ b/internal/resources/networkedge/ssh_user/resource_acc_test.go @@ -5,9 +5,10 @@ import ( "fmt" "log" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/equinix/terraform-provider-equinix/internal/comparisons" "github.com/equinix/terraform-provider-equinix/internal/config" - "github.com/equinix/terraform-provider-equinix/internal/nprintf" + "github.com/equinix/terraform-provider-equinix/internal/sweep" "github.com/equinix/ne-go" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -24,8 +25,24 @@ func init() { }) } +type testAccConfig struct { + ctx map[string]interface{} + config string +} + +func newTestAccConfig(ctx map[string]interface{}) *testAccConfig { + return &testAccConfig{ + ctx: ctx, + config: "", + } +} + +func (t *testAccConfig) build() string { + return t.config +} + func testSweepNetworkSSHUser(region string) error { - config, err := sharedConfigForRegion(region) + config, err := sweep.SharedConfigForRegion(region) if err != nil { return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping Network SSH users: %s", err) } @@ -39,7 +56,7 @@ func testSweepNetworkSSHUser(region string) error { return err } for _, user := range users { - if !isSweepableTestResource(ne.StringValue(user.Username)) { + if !sweep.IsSweepableTestResource(ne.StringValue(user.Username)) { continue } if err := config.Ne.DeleteSSHUser(ne.StringValue(user.UUID)); err != nil { @@ -51,28 +68,6 @@ func testSweepNetworkSSHUser(region string) error { return nil } -func testAccNetworkDeviceUser(ctx map[string]interface{}) string { - config := nprintf.Nprintf(` -resource "equinix_network_ssh_user" "%{user-resourceName}" { - username = "%{user-username}" - password = "%{user-password}" - device_ids = [ - equinix_network_device.%{device-resourceName}.id`, ctx) - if _, ok := ctx["device-secondary_name"]; ok { - config += nprintf.Nprintf(`, - equinix_network_device.%{device-resourceName}.redundant_id`, ctx) - } - config += ` - ] -}` - return config -} - -func (t *testAccConfig) withSSHUser() *testAccConfig { - t.config += testAccNetworkDeviceUser(t.ctx) - return t -} - func testAccNeSSHUserExists(resourceName string, user *ne.SSHUser) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] @@ -82,7 +77,7 @@ func testAccNeSSHUserExists(resourceName string, user *ne.SSHUser) resource.Test if rs.Primary.ID == "" { return fmt.Errorf("resource has no ID attribute set") } - client := testAccProvider.Meta().(*config.Config).Ne + client := acceptance.TestAccProvider.Meta().(*config.Config).Ne resp, err := client.GetSSHUser(rs.Primary.ID) if err != nil { return fmt.Errorf("error when fetching SSH user '%s': %s", rs.Primary.ID, err) diff --git a/internal/sweep/sweep.go b/internal/sweep/sweep.go index 981295ab7..73352d7c3 100644 --- a/internal/sweep/sweep.go +++ b/internal/sweep/sweep.go @@ -39,3 +39,30 @@ func GetConfigForMetal() (*config.Config, error) { RequestTimeout: time.Duration(clientTimeoutInt) * time.Second, }, nil } + +func SharedConfigForRegion(region string) (*config.Config, error) { + endpoint := env.GetWithDefault(config.EndpointEnvVar, config.DefaultBaseURL) + clientToken := env.GetWithDefault(config.ClientTokenEnvVar, "") + clientID := env.GetWithDefault(config.ClientIDEnvVar, "") + clientSecret := env.GetWithDefault(config.ClientSecretEnvVar, "") + clientTimeout := env.GetWithDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout)) + clientTimeoutInt, err := strconv.Atoi(clientTimeout) + if err != nil { + return nil, fmt.Errorf("cannot convert value of '%s' env variable to int", config.ClientTimeoutEnvVar) + } + metalAuthToken := env.GetWithDefault(config.MetalAuthTokenEnvVar, "") + + if clientToken == "" && (clientID == "" || clientSecret == "") && metalAuthToken == "" { + return nil, fmt.Errorf("To run acceptance tests sweeper, one of '%s' or pair '%s' - '%s' must be set for Equinix Fabric and Network Edge, and '%s' for Equinix Metal", + config.ClientTokenEnvVar, config.ClientIDEnvVar, config.ClientSecretEnvVar, config.MetalAuthTokenEnvVar) + } + + return &config.Config{ + AuthToken: metalAuthToken, + BaseURL: endpoint, + Token: clientToken, + ClientID: clientID, + ClientSecret: clientSecret, + RequestTimeout: time.Duration(clientTimeoutInt) * time.Second, + }, nil +}