From 71d723f6f35522da7ebab250dc351380b9cef39b Mon Sep 17 00:00:00 2001 From: Gogen120 Date: Wed, 28 Feb 2024 20:16:21 +0300 Subject: [PATCH] Fix review change requests - Add base schemas for datastore and database resources - Add required fields for each datastore type in corresponding schema instead of deleting unused fields - Use Sensitive: true for password fields --- ...lectel_dbaas_configuration_parameter_v1.go | 6 +- selectel/dbaas_base_schemas.go | 154 ++++++++++++++ selectel/schema_selectel_dbaas_database_v1.go | 57 ------ .../schema_selectel_dbaas_datastore_v1.go | 190 ++++++------------ ...chema_selectel_dbaas_kafka_datastore_v1.go | 14 +- ...schema_selectel_dbaas_mysql_database_v1.go | 13 +- ...chema_selectel_dbaas_mysql_datastore_v1.go | 46 ++++- ...a_selectel_dbaas_postgresql_database_v1.go | 29 ++- ..._selectel_dbaas_postgresql_datastore_v1.go | 67 +++++- ...chema_selectel_dbaas_redis_datastore_v1.go | 52 ++++- selectel/schema_selectel_dbaas_user_v1.go | 5 +- 11 files changed, 387 insertions(+), 246 deletions(-) create mode 100644 selectel/dbaas_base_schemas.go delete mode 100644 selectel/schema_selectel_dbaas_database_v1.go diff --git a/selectel/data_source_selectel_dbaas_configuration_parameter_v1.go b/selectel/data_source_selectel_dbaas_configuration_parameter_v1.go index 96fc25c9..7af16775 100644 --- a/selectel/data_source_selectel_dbaas_configuration_parameter_v1.go +++ b/selectel/data_source_selectel_dbaas_configuration_parameter_v1.go @@ -195,9 +195,9 @@ func filterConfigurationParametersByName(configurationParameters []dbaas.Configu return filteredConfigurationParameters } -func convertListParametersTypes(parameter []interface{}) []string { - parameterList := make([]string, len(parameter)) - for i, value := range parameter { +func convertListParametersTypes(parameters []interface{}) []string { + parameterList := make([]string, len(parameters)) + for i, value := range parameters { parameterList[i] = convertFieldToStringByType(value) } diff --git a/selectel/dbaas_base_schemas.go b/selectel/dbaas_base_schemas.go new file mode 100644 index 00000000..dff3f57c --- /dev/null +++ b/selectel/dbaas_base_schemas.go @@ -0,0 +1,154 @@ +package selectel + +import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + +func resourceDBaaSDatastoreV1BaseSchema() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "project_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "region": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "subnet_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "type_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "flavor_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: false, + ConflictsWith: []string{"flavor"}, + }, + "node_count": { + Type: schema.TypeInt, + Required: true, + }, + "enabled": { + Type: schema.TypeBool, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "backup_retention_days": { + Type: schema.TypeInt, + Optional: true, + Description: "Number of days to retain backups.", + }, + "connections": { + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "flavor": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: false, + ConflictsWith: []string{"flavor_id"}, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "vcpus": { + Type: schema.TypeInt, + Required: true, + }, + "ram": { + Type: schema.TypeInt, + Required: true, + }, + "disk": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + }, + "firewall": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "ips": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "config": { + Type: schema.TypeMap, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "instances": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "role": { + Type: schema.TypeString, + Computed: true, + }, + "floating_ip": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + } +} + +func resourceDBaaSDatabaseV1BaseSchema() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "project_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "region": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "datastore_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + } +} diff --git a/selectel/schema_selectel_dbaas_database_v1.go b/selectel/schema_selectel_dbaas_database_v1.go deleted file mode 100644 index 244b52ea..00000000 --- a/selectel/schema_selectel_dbaas_database_v1.go +++ /dev/null @@ -1,57 +0,0 @@ -package selectel - -import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - -func resourceDBaaSDatabaseV1Schema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "project_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "region": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "datastore_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "status": { - Type: schema.TypeString, - Computed: true, - }, - "owner_id": { - Type: schema.TypeString, - Optional: true, - }, - "lc_collate": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - DiffSuppressFunc: dbaasDatabaseV1LocaleDiffSuppressFunc, - }, - "lc_ctype": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - DiffSuppressFunc: dbaasDatabaseV1LocaleDiffSuppressFunc, - }, - } -} - -func dbaasDatabaseV1LocaleDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool { - // The default locale value - C is the same as null value, so we need to suppress - if old == "C" && new == "" { - return true - } - - return false -} diff --git a/selectel/schema_selectel_dbaas_datastore_v1.go b/selectel/schema_selectel_dbaas_datastore_v1.go index f434937a..95e06ae7 100644 --- a/selectel/schema_selectel_dbaas_datastore_v1.go +++ b/selectel/schema_selectel_dbaas_datastore_v1.go @@ -6,149 +6,71 @@ import ( ) func resourceDBaaSDatastoreV1Schema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - }, - "project_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "region": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "subnet_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "type_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "flavor_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: false, - ConflictsWith: []string{"flavor"}, - }, - "node_count": { - Type: schema.TypeInt, - Required: true, - }, - "enabled": { - Type: schema.TypeBool, - Computed: true, - }, - "status": { - Type: schema.TypeString, - Computed: true, - }, - "backup_retention_days": { - Type: schema.TypeInt, - Optional: true, - Description: "Number of days to retain backups.", - }, - "connections": { - Type: schema.TypeMap, - Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - }, - "flavor": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - ForceNew: false, - ConflictsWith: []string{"flavor_id"}, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "vcpus": { - Type: schema.TypeInt, - Required: true, - }, - "ram": { - Type: schema.TypeInt, - Required: true, - }, - "disk": { - Type: schema.TypeInt, - Required: true, - }, + datastoreSchema := resourceDBaaSDatastoreV1BaseSchema() + datastoreSchema["backup_retention_days"] = &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Description: "Number of days to retain backups.", + } + datastoreSchema["pooler"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "mode": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + "session", + "transaction", + "statement", + }, false), }, - }, - }, - "pooler": { - Type: schema.TypeSet, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "mode": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringInSlice([]string{ - "session", - "transaction", - "statement", - }, false), - }, - "size": { - Type: schema.TypeInt, - Required: true, - }, + "size": { + Type: schema.TypeInt, + Required: true, }, }, }, - "firewall": { - Type: schema.TypeSet, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "ips": { - Type: schema.TypeList, - Required: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - }, + } + datastoreSchema["restore"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "datastore_id": { + Type: schema.TypeString, + Optional: true, }, - }, - }, - "restore": { - Type: schema.TypeSet, - Optional: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "datastore_id": { - Type: schema.TypeString, - Optional: true, - }, - "target_time": { - Type: schema.TypeString, - Optional: true, - }, + "target_time": { + Type: schema.TypeString, + Optional: true, }, }, }, - "config": { - Type: schema.TypeMap, - Optional: true, - Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, + } + datastoreSchema["redis_password"] = &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Sensitive: true, + } + datastoreSchema["floating_ips"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "master": { + Type: schema.TypeInt, + Required: true, + }, + "replica": { + Type: schema.TypeInt, + Required: true, + }, }, }, - "redis_password": { - Type: schema.TypeString, - Optional: true, - }, } + + return datastoreSchema } diff --git a/selectel/schema_selectel_dbaas_kafka_datastore_v1.go b/selectel/schema_selectel_dbaas_kafka_datastore_v1.go index 6915cc6d..5bac6306 100644 --- a/selectel/schema_selectel_dbaas_kafka_datastore_v1.go +++ b/selectel/schema_selectel_dbaas_kafka_datastore_v1.go @@ -2,18 +2,6 @@ package selectel import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -var invalidKafkaFields = []string{ - "backup_retention_days", - "pooler", - "restore", - "redis_password", -} - func resourceDBaaSKafkaDatastoreV1Schema() map[string]*schema.Schema { - datastoreSchema := resourceDBaaSDatastoreV1Schema() - for _, field := range invalidKafkaFields { - delete(datastoreSchema, field) - } - - return datastoreSchema + return resourceDBaaSDatastoreV1BaseSchema() } diff --git a/selectel/schema_selectel_dbaas_mysql_database_v1.go b/selectel/schema_selectel_dbaas_mysql_database_v1.go index fdbdc6e4..15c558d0 100644 --- a/selectel/schema_selectel_dbaas_mysql_database_v1.go +++ b/selectel/schema_selectel_dbaas_mysql_database_v1.go @@ -2,17 +2,6 @@ package selectel import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -var invalidMySQLDatabaseFields = []string{ - "owner_id", - "lc_ctype", - "lc_collate", -} - func resourceDBaaSMySQLDatabaseV1Schema() map[string]*schema.Schema { - databaseSchema := resourceDBaaSDatabaseV1Schema() - for _, field := range invalidMySQLDatabaseFields { - delete(databaseSchema, field) - } - - return databaseSchema + return resourceDBaaSDatabaseV1BaseSchema() } diff --git a/selectel/schema_selectel_dbaas_mysql_datastore_v1.go b/selectel/schema_selectel_dbaas_mysql_datastore_v1.go index 52e5fcb0..253ecb58 100644 --- a/selectel/schema_selectel_dbaas_mysql_datastore_v1.go +++ b/selectel/schema_selectel_dbaas_mysql_datastore_v1.go @@ -2,15 +2,45 @@ package selectel import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -var invalidMySQLFields = []string{ - "pooler", - "redis_password", -} - func resourceDBaaSMySQLDatastoreV1Schema() map[string]*schema.Schema { - datastoreSchema := resourceDBaaSDatastoreV1Schema() - for _, field := range invalidMySQLFields { - delete(datastoreSchema, field) + datastoreSchema := resourceDBaaSDatastoreV1BaseSchema() + datastoreSchema["backup_retention_days"] = &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Description: "Number of days to retain backups.", + } + datastoreSchema["restore"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "datastore_id": { + Type: schema.TypeString, + Optional: true, + }, + "target_time": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + } + datastoreSchema["floating_ips"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "master": { + Type: schema.TypeInt, + Required: true, + }, + "replica": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, } return datastoreSchema diff --git a/selectel/schema_selectel_dbaas_postgresql_database_v1.go b/selectel/schema_selectel_dbaas_postgresql_database_v1.go index 64360f34..b9effe6a 100644 --- a/selectel/schema_selectel_dbaas_postgresql_database_v1.go +++ b/selectel/schema_selectel_dbaas_postgresql_database_v1.go @@ -5,5 +5,32 @@ import ( ) func resourceDBaaSPostgreSQLDatabaseV1Schema() map[string]*schema.Schema { - return resourceDBaaSDatabaseV1Schema() + databaseSchema := resourceDBaaSDatabaseV1BaseSchema() + databaseSchema["owner"] = &schema.Schema{ + Type: schema.TypeString, + Optional: true, + } + databaseSchema["lc_collate"] = &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + DiffSuppressFunc: dbaasDatabaseV1LocaleDiffSuppressFunc, + } + databaseSchema["lc_ctype"] = &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + DiffSuppressFunc: dbaasDatabaseV1LocaleDiffSuppressFunc, + } + + return databaseSchema +} + +func dbaasDatabaseV1LocaleDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool { + // The default locale value - C is the same as null value, so we need to suppress + if old == "C" && new == "" { + return true + } + + return false } diff --git a/selectel/schema_selectel_dbaas_postgresql_datastore_v1.go b/selectel/schema_selectel_dbaas_postgresql_datastore_v1.go index 54cbd987..60bb67c9 100644 --- a/selectel/schema_selectel_dbaas_postgresql_datastore_v1.go +++ b/selectel/schema_selectel_dbaas_postgresql_datastore_v1.go @@ -2,16 +2,69 @@ package selectel import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -var invalidPostgreSQLFields = []string{ - "redis_password", -} - func resourceDBaaSPostgreSQLDatastoreV1Schema() map[string]*schema.Schema { - datastoreSchema := resourceDBaaSDatastoreV1Schema() - for _, field := range invalidPostgreSQLFields { - delete(datastoreSchema, field) + datastoreSchema := resourceDBaaSDatastoreV1BaseSchema() + datastoreSchema["backup_retention_days"] = &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Description: "Number of days to retain backups.", + } + datastoreSchema["pooler"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "mode": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + "session", + "transaction", + "statement", + }, false), + }, + "size": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + } + datastoreSchema["restore"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "datastore_id": { + Type: schema.TypeString, + Optional: true, + }, + "target_time": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + } + datastoreSchema["floating_ips"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "master": { + Type: schema.TypeInt, + Required: true, + }, + "replica": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, } return datastoreSchema diff --git a/selectel/schema_selectel_dbaas_redis_datastore_v1.go b/selectel/schema_selectel_dbaas_redis_datastore_v1.go index 3d362b3c..c21b1412 100644 --- a/selectel/schema_selectel_dbaas_redis_datastore_v1.go +++ b/selectel/schema_selectel_dbaas_redis_datastore_v1.go @@ -2,16 +2,29 @@ package selectel import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -var invalidRedisFields = []string{ - "flavor", - "flavor_id", - "pooler", -} - func resourceDBaaSRedisDatastoreV1Schema() map[string]*schema.Schema { - datastoreSchema := resourceDBaaSDatastoreV1Schema() - for _, field := range invalidRedisFields { - delete(datastoreSchema, field) + datastoreSchema := resourceDBaaSDatastoreV1BaseSchema() + datastoreSchema["backup_retention_days"] = &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Description: "Number of days to retain backups.", + } + datastoreSchema["restore"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "datastore_id": { + Type: schema.TypeString, + Optional: true, + }, + "target_time": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, } datastoreSchema["flavor"] = &schema.Schema{ Type: schema.TypeSet, @@ -37,6 +50,27 @@ func resourceDBaaSRedisDatastoreV1Schema() map[string]*schema.Schema { Type: schema.TypeString, Required: true, } + datastoreSchema["redis_password"] = &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Sensitive: true, + } + datastoreSchema["floating_ips"] = &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "master": { + Type: schema.TypeInt, + Required: true, + }, + "replica": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + } return datastoreSchema } diff --git a/selectel/schema_selectel_dbaas_user_v1.go b/selectel/schema_selectel_dbaas_user_v1.go index 025d548f..20796427 100644 --- a/selectel/schema_selectel_dbaas_user_v1.go +++ b/selectel/schema_selectel_dbaas_user_v1.go @@ -20,8 +20,9 @@ func resourceDBaaSUserV1Schema() map[string]*schema.Schema { ForceNew: true, }, "password": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + Sensitive: true, }, "status": { Type: schema.TypeString,