From 758987e28cc5e6d7a5a732dbaf0dba292c9beb8e Mon Sep 17 00:00:00 2001 From: Abhishekism9450 <32683845+Abhishekism9450@users.noreply.github.com> Date: Thu, 27 Apr 2023 13:09:24 +0530 Subject: [PATCH] Bug/ndb schema validation (#585) --- nutanix/common_schema_validation.go | 26 ++- nutanix/resource_nutanix_ndb_database.go | 2 +- nutanix/resource_nutanix_ndb_database_test.go | 196 ++++++++++++++++++ 3 files changed, 217 insertions(+), 7 deletions(-) diff --git a/nutanix/common_schema_validation.go b/nutanix/common_schema_validation.go index 752762509..043bcbb1d 100644 --- a/nutanix/common_schema_validation.go +++ b/nutanix/common_schema_validation.go @@ -6,19 +6,33 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var requiredResourceFields map[string][]string = map[string][]string{ - "era_provision_database": {"databasetype", "dbparameterprofileid", "timemachineinfo", "nodes"}, +var requiredResourceFields map[string]map[string][]string = map[string]map[string][]string{ + "ndb_provision_database": { + "createdbserver": {"databasetype", "softwareprofileid", "softwareprofileversionid", "computeprofileid", + "networkprofileid", "dbparameterprofileid", "nxclusterid", "sshpublickey", "timemachineinfo", "nodes"}, + "registerdbserver": {"databasetype", "dbparameterprofileid", "timemachineinfo", "nodes"}}, } func schemaValidation(resourceName string, d *schema.ResourceData) error { var diagMap []string if vals, ok := requiredResourceFields[resourceName]; ok { - for _, attr := range vals { - if _, ok := d.GetOk(attr); !ok { - diagMap = append(diagMap, attr) + if dbVal, ok := d.GetOkExists("createdbserver"); ok { + if dbVal.(bool) { + createVals := vals["createdbserver"] + for _, attr := range createVals { + if _, ok := d.GetOk(attr); !ok { + diagMap = append(diagMap, attr) + } + } + } else { + registerVals := vals["registerdbserver"] + for _, attr := range registerVals { + if _, ok := d.GetOk(attr); !ok { + diagMap = append(diagMap, attr) + } + } } } - if diagMap != nil { return fmt.Errorf("missing required fields are %s for %s", diagMap, resourceName) } diff --git a/nutanix/resource_nutanix_ndb_database.go b/nutanix/resource_nutanix_ndb_database.go index 8fcc5e6c1..ec1c9b862 100644 --- a/nutanix/resource_nutanix_ndb_database.go +++ b/nutanix/resource_nutanix_ndb_database.go @@ -467,7 +467,7 @@ func createDatabaseInstance(ctx context.Context, d *schema.ResourceData, meta in conn := meta.(*Client).Era // check for resource schema validation - er := schemaValidation("era_provision_database", d) + er := schemaValidation("ndb_provision_database", d) if er != nil { return diag.FromErr(er) } diff --git a/nutanix/resource_nutanix_ndb_database_test.go b/nutanix/resource_nutanix_ndb_database_test.go index d8fb584b2..a35979183 100644 --- a/nutanix/resource_nutanix_ndb_database_test.go +++ b/nutanix/resource_nutanix_ndb_database_test.go @@ -2,6 +2,7 @@ package nutanix import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -15,6 +16,8 @@ func TestAccEra_basic(t *testing.T) { desc := "this is desc" vmName := fmt.Sprintf("testvm-%d", r) sshKey := testVars.SSHKey + updatedName := fmt.Sprintf("test-pg-inst-tf-updated-%d", r) + updatedesc := "this is updated desc" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccEraPreCheck(t) }, Providers: testAccProviders, @@ -30,6 +33,17 @@ func TestAccEra_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceNameDB, "time_machine.#"), ), }, + { + Config: testAccEraDatabaseConfig(updatedName, updatedesc, vmName, sshKey, r), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceNameDB, "name", updatedName), + resource.TestCheckResourceAttr(resourceNameDB, "description", updatedesc), + resource.TestCheckResourceAttr(resourceNameDB, "databasetype", "postgres_database"), + resource.TestCheckResourceAttr(resourceNameDB, "database_nodes.#", "1"), + resource.TestCheckResourceAttrSet(resourceNameDB, "time_machine_id"), + resource.TestCheckResourceAttrSet(resourceNameDB, "time_machine.#"), + ), + }, }, }) } @@ -58,6 +72,42 @@ func TestAccEraDatabaseProvisionHA(t *testing.T) { }) } +func TestAccEra_SchemaValidationwithCreateDBserver(t *testing.T) { + r := randIntBetween(1, 10) + name := fmt.Sprintf("test-pg-inst-tf-%d", r) + desc := "this is desc" + vmName := fmt.Sprintf("testvm-%d", r) + sshKey := testVars.SSHKey + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccEraPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccEraDatabaseSchemaValidationConfig(name, desc, vmName, sshKey, r), + ExpectError: regexp.MustCompile(`missing required fields are \[softwareprofileid softwareprofileversionid computeprofileid networkprofileid dbparameterprofileid\] for ndb_provision_database`), + }, + }, + }) +} + +func TestAccEra_SchemaValidationwithCreateDBserverFalse(t *testing.T) { + r := randIntBetween(1, 10) + name := fmt.Sprintf("test-pg-inst-tf-%d", r) + desc := "this is desc" + vmName := fmt.Sprintf("testvm-%d", r) + sshKey := testVars.SSHKey + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccEraPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccEraDatabaseSchemaValidationConfigWithoutCreateDBserver(name, desc, vmName, sshKey, r), + ExpectError: regexp.MustCompile(`missing required fields are \[dbparameterprofileid timemachineinfo\] for ndb_provision_database`), + }, + }, + }) +} + func testAccEraDatabaseConfig(name, desc, vmName, sshKey string, r int) string { return fmt.Sprintf(` data "nutanix_ndb_profiles" "p"{ @@ -327,3 +377,149 @@ func testAccEraDatabaseHAConfig(name, desc, sshKey string, r int) string { } `, name, desc, sshKey, r) } + +func testAccEraDatabaseSchemaValidationConfig(name, desc, vmName, sshKey string, r int) string { + return fmt.Sprintf(` + data "nutanix_ndb_profiles" "p"{ + } + data "nutanix_ndb_slas" "slas"{} + data "nutanix_ndb_clusters" "clusters"{} + + locals { + profiles_by_type = { + for p in data.nutanix_ndb_profiles.p.profiles : p.type => p... + } + storage_profiles = { + for p in local.profiles_by_type.Storage: p.name => p + } + compute_profiles = { + for p in local.profiles_by_type.Compute: p.name => p + } + network_profiles = { + for p in local.profiles_by_type.Network: p.name => p + } + database_parameter_profiles = { + for p in local.profiles_by_type.Database_Parameter: p.name => p + } + software_profiles = { + for p in local.profiles_by_type.Software: p.name => p + } + slas = { + for p in data.nutanix_ndb_slas.slas.slas: p.name => p + } + clusters = { + for p in data.nutanix_ndb_clusters.clusters.clusters: p.name => p + } + } + + resource "nutanix_ndb_database" "acctest-managed" { + databasetype = "postgres_database" + name = "%[1]s" + description = "%[2]s" + + postgresql_info{ + listener_port = "5432" + database_size= "200" + db_password = "password" + database_names= "testdb1" + } + nxclusterid= local.clusters.EraCluster.id + sshpublickey= "%[4]s" + nodes{ + vmname= "%[3]s" + networkprofileid= local.network_profiles.DEFAULT_OOB_POSTGRESQL_NETWORK.id + } + timemachineinfo { + name= "test-pg-inst-%[5]d" + description="" + slaid=local.slas["DEFAULT_OOB_BRONZE_SLA"].id + schedule { + snapshottimeofday{ + hours= 16 + minutes= 0 + seconds= 0 + } + continuousschedule{ + enabled=true + logbackupinterval= 30 + snapshotsperday=1 + } + weeklyschedule{ + enabled=true + dayofweek= "WEDNESDAY" + } + monthlyschedule{ + enabled = true + dayofmonth= "27" + } + quartelyschedule{ + enabled=true + startmonth="JANUARY" + dayofmonth= 27 + } + yearlyschedule{ + enabled= false + dayofmonth= 31 + month="DECEMBER" + } + } + } + } + `, name, desc, vmName, sshKey, r) +} + +func testAccEraDatabaseSchemaValidationConfigWithoutCreateDBserver(name, desc, vmName, sshKey string, r int) string { + return fmt.Sprintf(` + data "nutanix_ndb_profiles" "p"{ + } + data "nutanix_ndb_slas" "slas"{} + data "nutanix_ndb_clusters" "clusters"{} + + locals { + profiles_by_type = { + for p in data.nutanix_ndb_profiles.p.profiles : p.type => p... + } + storage_profiles = { + for p in local.profiles_by_type.Storage: p.name => p + } + compute_profiles = { + for p in local.profiles_by_type.Compute: p.name => p + } + network_profiles = { + for p in local.profiles_by_type.Network: p.name => p + } + database_parameter_profiles = { + for p in local.profiles_by_type.Database_Parameter: p.name => p + } + software_profiles = { + for p in local.profiles_by_type.Software: p.name => p + } + slas = { + for p in data.nutanix_ndb_slas.slas.slas: p.name => p + } + clusters = { + for p in data.nutanix_ndb_clusters.clusters.clusters: p.name => p + } + } + + resource "nutanix_ndb_database" "acctest-managed" { + databasetype = "postgres_database" + name = "%[1]s" + description = "%[2]s" + + postgresql_info{ + listener_port = "5432" + database_size= "200" + db_password = "password" + database_names= "testdb1" + } + nxclusterid= local.clusters.EraCluster.id + sshpublickey= "%[4]s" + nodes{ + vmname= "%[3]s" + networkprofileid= local.network_profiles.DEFAULT_OOB_POSTGRESQL_NETWORK.id + } + createdbserver=false + } + `, name, desc, vmName, sshKey, r) +}