From 01467934b373f42fb822db7c269714854b8d104e Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Thu, 14 Nov 2024 20:58:37 +0900 Subject: [PATCH] Align the `sql_db_info` output of AWS, Azure, GCP, and NCP --- api/docs.go | 2 +- api/swagger.json | 2 +- api/swagger.yaml | 2 +- examples/aws/mysql-db-instance/main.tf | 4 +- examples/aws/mysql-db-instance/output.tf | 59 ++++++- examples/azure/mysql-db/output.tf | 70 +++++++- examples/azure/mysql-db/providers.tf | 2 +- examples/gcp/sql-db-instance/main.tf | 2 +- examples/gcp/sql-db-instance/output.tf | 64 ++++++- examples/ncp/mysql/output.tf | 73 +++++++- pkg/api/rest/handler/sql-db.go | 10 +- pkg/api/rest/model/sql-db.go | 208 +++++++++++++++++------ pkg/api/rest/server.go | 2 +- templates/sql-db/aws/output.tf | 56 ++++-- templates/sql-db/aws/sql-db.tf | 4 +- templates/sql-db/azure/output.tf | 58 ++++++- templates/sql-db/azure/variables.tf | 2 +- templates/sql-db/gcp/output.tf | 47 ++++- templates/sql-db/gcp/variables.tf | 2 +- templates/sql-db/ncp/output.tf | 66 ++++++- templates/sql-db/ncp/variables.tf | 2 +- 21 files changed, 593 insertions(+), 144 deletions(-) diff --git a/api/docs.go b/api/docs.go index 60244cd..83be514 100644 --- a/api/docs.go +++ b/api/docs.go @@ -2546,7 +2546,7 @@ const docTemplate = `{ }, "db_admin_password": { "type": "string", - "example": "P@ssword1234!" + "example": "Password1234!" }, "db_admin_username": { "type": "string", diff --git a/api/swagger.json b/api/swagger.json index 6db1319..6be9923 100644 --- a/api/swagger.json +++ b/api/swagger.json @@ -2540,7 +2540,7 @@ }, "db_admin_password": { "type": "string", - "example": "P@ssword1234!" + "example": "Password1234!" }, "db_admin_username": { "type": "string", diff --git a/api/swagger.yaml b/api/swagger.yaml index 1b85cae..64f1531 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -198,7 +198,7 @@ definitions: example: vpc-12345678 type: string db_admin_password: - example: P@ssword1234! + example: Password1234! type: string db_admin_username: example: mydbadmin diff --git a/examples/aws/mysql-db-instance/main.tf b/examples/aws/mysql-db-instance/main.tf index 25e6b42..3edf90a 100644 --- a/examples/aws/mysql-db-instance/main.tf +++ b/examples/aws/mysql-db-instance/main.tf @@ -85,7 +85,7 @@ resource "aws_route_table_association" "public_2" { # Create a DB subnet group resource "aws_db_subnet_group" "rds" { - name = "main" + name = "tofu-main" subnet_ids = [aws_subnet.public_1.id, aws_subnet.public_2.id] tags = { @@ -96,7 +96,7 @@ resource "aws_db_subnet_group" "rds" { # Create a security group for RDS Database Instance resource "aws_security_group" "rds_sg" { - name = "rds_sg" + name = "tofu-rds-sg" vpc_id = aws_vpc.main.id ingress { diff --git a/examples/aws/mysql-db-instance/output.tf b/examples/aws/mysql-db-instance/output.tf index a8a3cba..33dc9b5 100644 --- a/examples/aws/mysql-db-instance/output.tf +++ b/examples/aws/mysql-db-instance/output.tf @@ -1,7 +1,54 @@ -#outputs.tf -output "security_group_id" { - value = aws_security_group.rds_sg.id -} -output "db_instance_endpoint" { - value = aws_db_instance.myinstance.endpoint +# Outputs wrapped in sql_db_info object +output "sql_db_info" { + description = "Information for connecting to the MySQL RDS instance with dynamic variables." + value = { + sql_db_detail = { + # Basic Information + instance_name = aws_db_instance.myinstance.identifier # "myrdsinstance" + instance_resource_id = aws_db_instance.myinstance.id # "db-YMJCVDFDANINUTBJJU63AZTY5Q" + instance_spec = aws_db_instance.myinstance.instance_class # "db.t3.micro" + location = aws_db_instance.myinstance.availability_zone # "ap-northeast-2c" + tags = aws_db_instance.myinstance.tags # { "Name" = "myrdsinstance" } + + # Storage Configuration + storage_type = aws_db_instance.myinstance.storage_type # "gp2" + storage_size = aws_db_instance.myinstance.allocated_storage # 20 + + # Database Engine Information + engine_name = aws_db_instance.myinstance.engine # "mysql" + engine_version = aws_db_instance.myinstance.engine_version # "8.0.39" + + # Database Connection Details + connection_endpoint = aws_db_instance.myinstance.endpoint # "myrdsinstance.chrkjg2ktom1.ap-northeast-2.rds.amazonaws.com:3306" + connection_host = aws_db_instance.myinstance.address # "myrdsinstance.chrkjg2ktom1.ap-northeast-2.rds.amazonaws.com" + connection_port = aws_db_instance.myinstance.port # 3306 + public_access_enabled = aws_db_instance.myinstance.publicly_accessible # true + + # Authentication + admin_username = aws_db_instance.myinstance.username # "myrdsuser" + # amdin_password = aws_db_instance.myinstance.password # "myrdsuser" + + provider_specific_detail = { + provider = "aws" + resource_identifier = aws_db_instance.myinstance.arn # "arn:aws:rds:ap-northeast-2:635484366616:db:myrdsinstance" + is_multi_az = aws_db_instance.myinstance.multi_az # false + + status = aws_db_instance.myinstance.status # "available" + dns_zone_id = aws_db_instance.myinstance.hosted_zone_id # "ZLA2NUCOLGUUR" + security_group_ids = aws_db_instance.myinstance.vpc_security_group_ids # ["sg-0af75bda5c889cea6"] + subnet_group_name = aws_db_instance.myinstance.db_subnet_group_name # "tofu-main" + storage_encrypted = aws_db_instance.myinstance.storage_encrypted # false + storage_throughput = aws_db_instance.myinstance.storage_throughput # 0 + storage_iops = aws_db_instance.myinstance.iops # 0 + replicas = aws_db_instance.myinstance.replicas # [] + } + } + } } + +# +# output "db_instance_all" { +# description = "All information" +# value = aws_db_instance.myinstance +# sensitive = true +# } diff --git a/examples/azure/mysql-db/output.tf b/examples/azure/mysql-db/output.tf index 1700534..e23bf88 100644 --- a/examples/azure/mysql-db/output.tf +++ b/examples/azure/mysql-db/output.tf @@ -1,12 +1,64 @@ -output "MySqlDbInfo" { - description = "Information needed to connect and manage the Azure Database for MySQL instance." +# Outputs wrapped in sql_db_info object +output "sql_db_info" { + description = "Information for connecting to the MySQL Flexible Server instance in Azure." value = { - server_name = azurerm_mysql_flexible_server.example.name - fully_qualified_domain_name = azurerm_mysql_flexible_server.example.fqdn - administrator_login = azurerm_mysql_flexible_server.example.administrator_login - administrator_password = "YOUR_PASSWORD_HERE" # Note: Avoid exposing this directly; consider using a secret management tool - database_name = azurerm_mysql_flexible_database.example.name - port = 3306 - # ssl_enforcement = azurerm_mysql_flexible_server.example.ssl_enforcement_enabled + sql_db_detail = { + # Basic Information + instance_name = azurerm_mysql_flexible_server.example.name # "tofu-example-mysql-server" + instance_resource_id = azurerm_mysql_flexible_server.example.id # "/subscriptions/a20fed83-96bd-4480-92a9-140b8e3b7c3a/resourceGroups/tofu-example-rg/providers/Microsoft.DBforMySQL/flexibleServers/tofu-example-mysql-server" + instance_spec = azurerm_mysql_flexible_server.example.sku_name # "B_Standard_B1ms" + location = azurerm_mysql_flexible_server.example.location # "koreacentral" + tags = azurerm_mysql_flexible_server.example.tags # (if available) + + # Storage Configuration + storage_type = "Premium_LRS" # Azure MySQL Flexible Server uses Premium storage + storage_size = azurerm_mysql_flexible_server.example.storage[0].size_gb # 20 + + # Database Engine Information + engine_name = "mysql" # Always "mysql" + engine_version = azurerm_mysql_flexible_server.example.version # "5.7" + + # Database Connection Details + connection_endpoint = "${azurerm_mysql_flexible_server.example.fqdn}:3306" # "tofu-example-mysql-server.mysql.database.azure.com:3306" + connection_host = azurerm_mysql_flexible_server.example.fqdn # "tofu-example-mysql-server.mysql.database.azure.com" + connection_port = 3306 # Default MySQL port + public_access_enabled = azurerm_mysql_flexible_server.example.public_network_access_enabled # true + + # Authentication + admin_username = azurerm_mysql_flexible_server.example.administrator_login # "adminuser" + # admin_password = azurerm_mysql_flexible_server.example.administrator_login_password # "adminuser" + + provider_specific_detail = { + provider = "azure" + resource_identifier = azurerm_mysql_flexible_server.example.id + + resource_group_name = azurerm_mysql_flexible_server.example.resource_group_name # "tofu-example-rg" + zone = azurerm_mysql_flexible_server.example.zone # "2" + database_name = azurerm_mysql_flexible_database.example.name # "tofu-example-db" + charset = azurerm_mysql_flexible_database.example.charset # "utf8" + collation = azurerm_mysql_flexible_database.example.collation # "utf8_general_ci" + + storage_autogrow_enabled = azurerm_mysql_flexible_server.example.storage[0].auto_grow_enabled # true + io_scaling_enabled = azurerm_mysql_flexible_server.example.storage[0].io_scaling_enabled # false + + backup_retention_days = azurerm_mysql_flexible_server.example.backup_retention_days # 7 + geo_redundant_backup_enabled = azurerm_mysql_flexible_server.example.geo_redundant_backup_enabled # false + + replica_capacity = azurerm_mysql_flexible_server.example.replica_capacity # 10 + replication_role = azurerm_mysql_flexible_server.example.replication_role # "None" + } + } } } + +# output "azurerm_mysql_flexible_server_all" { +# description = "All attributes of the Azure Database for MySQL instance." +# value = azurerm_mysql_flexible_server.example +# sensitive = true +# } + +# output "azurerm_mysql_flexible_database_all" { +# description = "All attributes of the Azure Database for MySQL database." +# value = azurerm_mysql_flexible_database.example +# sensitive = true +# } diff --git a/examples/azure/mysql-db/providers.tf b/examples/azure/mysql-db/providers.tf index 617b8d3..2a340db 100644 --- a/examples/azure/mysql-db/providers.tf +++ b/examples/azure/mysql-db/providers.tf @@ -48,7 +48,7 @@ resource "azurerm_mysql_flexible_server" "example" { resource_group_name = azurerm_resource_group.example.name administrator_login = "adminuser" - administrator_password = "P@ssword1234!" + administrator_password = "Password1234!" sku_name = "B_Standard_B1ms" # e.g., General Purpose, Standard_D2s_v3 # storage_mb = 5120 # 5 GB diff --git a/examples/gcp/sql-db-instance/main.tf b/examples/gcp/sql-db-instance/main.tf index 805f423..37dded9 100644 --- a/examples/gcp/sql-db-instance/main.tf +++ b/examples/gcp/sql-db-instance/main.tf @@ -40,7 +40,7 @@ resource "google_sql_database_instance" "my_sql_instance" { tier = "db-f1-micro" # Set the instance type } - # deletion_protection = false # Disable deletion protection + deletion_protection = false # Disable deletion protection } # Create database diff --git a/examples/gcp/sql-db-instance/output.tf b/examples/gcp/sql-db-instance/output.tf index 1d727ef..080185a 100644 --- a/examples/gcp/sql-db-instance/output.tf +++ b/examples/gcp/sql-db-instance/output.tf @@ -1,11 +1,59 @@ -# Outputs wrapped in SqlDbInstanceInfo object, including public IP -output "SqlDbInstanceInfo" { +# Outputs wrapped in sql_db_info object +output "sql_db_info" { + description = "Information of the MySQL Cloud SQL instance in GCP." value = { - instance_name = google_sql_database_instance.my_sql_instance.name - database_name = google_sql_database.my_database.name - database_user = google_sql_user.my_user.name - connection_name = google_sql_database_instance.my_sql_instance.connection_name - public_ip = google_sql_database_instance.my_sql_instance.public_ip_address + sql_db_detail = { + # Basic Information + instance_name = google_sql_database_instance.my_sql_instance.name # "my-sql-instance" + instance_resource_id = google_sql_database_instance.my_sql_instance.id # "my-sql-instance" + instance_spec = google_sql_database_instance.my_sql_instance.settings[0].tier # "db-f1-micro" + location = google_sql_database_instance.my_sql_instance.settings[0].location_preference[0].zone # "asia-northeast3-a" + tags = google_sql_database_instance.my_sql_instance.settings[0].user_labels # {} + + # Storage Configuration + storage_type = google_sql_database_instance.my_sql_instance.settings[0].disk_type # "PD_SSD" + storage_size = google_sql_database_instance.my_sql_instance.settings[0].disk_size # 10 + + # Database Engine Information + engine_name = "mysql" # Not exposed by GCP + engine_version = google_sql_database_instance.my_sql_instance.database_version # "MYSQL_8_0" + + # Database Connection Details + connection_endpoint = "${google_sql_database_instance.my_sql_instance.first_ip_address}:3306" + connection_host = google_sql_database_instance.my_sql_instance.first_ip_address + connection_port = 3306 # Default MySQL port + public_access_enabled = google_sql_database_instance.my_sql_instance.settings[0].ip_configuration[0].ipv4_enabled + + # Authentication + admin_username = google_sql_user.my_user.name # "myuser" + # admin_password = google_sql_user.my_user.password # "mypassword" + + provider_specific_detail = { + provider = "gcp" + resource_identifier = google_sql_database_instance.my_sql_instance.self_link + availability_type = google_sql_database_instance.my_sql_instance.settings[0].availability_type + + project = google_sql_database_instance.my_sql_instance.project + region = google_sql_database_instance.my_sql_instance.region + } + } } - description = "Information for SQL Database instance, including instance name, database name, user, connection name, and public IP address" } + +# output "my_sql_instance_all" { +# description = "All information" +# value = google_sql_database_instance.my_sql_instance +# sensitive = true +# } + +# output "my_database_all" { +# description = "All information" +# value = google_sql_database.my_database +# sensitive = true +# } + +# output "my_user_all" { +# description = "All information" +# value = google_sql_user.my_user +# sensitive = true +# } diff --git a/examples/ncp/mysql/output.tf b/examples/ncp/mysql/output.tf index 902e941..513275e 100644 --- a/examples/ncp/mysql/output.tf +++ b/examples/ncp/mysql/output.tf @@ -1,12 +1,67 @@ -output "mysql_db_info" { - description = "Information needed to connect to the MySQL RDS instance." +# Outputs wrapped in sql_db_info object +output "sql_db_info" { + description = "Information for connecting to the MySQL instance in NCP." value = { - service_name = ncloud_mysql.mysql.service_name - server_name_prefix = ncloud_mysql.mysql.server_name_prefix - user_name = ncloud_mysql.mysql.user_name - host_ip = ncloud_mysql.mysql.host_ip - database_name = ncloud_mysql.mysql.database_name - # user_password = ncloud_mysql.mysql.user_password + sql_db_detail = { + # Basic Information + instance_name = ncloud_mysql.mysql.service_name # "tofu-example-mysql" + instance_resource_id = ncloud_mysql.mysql.id # "100457839" + instance_spec = ncloud_mysql.mysql.image_product_code # "SW.VMYSL.OS.LNX64.ROCKY.0810.MYSQL.B050" + location = ncloud_mysql.mysql.region_code # "KR" + tags = {} # (if available) + + # Storage Configuration + storage_type = ncloud_mysql.mysql.data_storage_type # "SSD" + storage_size = ncloud_mysql.mysql.mysql_server_list[0].data_storage_size / 1073741824 # 10 (Unit: GB) + + # Database Engine Information + engine_name = "mysql" # Always "mysql" + engine_version = ncloud_mysql.mysql.engine_version_code # "MYSQL8.0.36" + + # Database Connection Details + connection_endpoint = "${ncloud_mysql.mysql.mysql_server_list[0].private_domain}:${ncloud_mysql.mysql.port}" # "db-2vpnbg.vpc-cdb.ntruss.com:3306" + connection_host = ncloud_mysql.mysql.mysql_server_list[0].private_domain # "db-2vpnbg.vpc-cdb.ntruss.com" + connection_port = ncloud_mysql.mysql.port # 3306 + public_access_enabled = ncloud_mysql.mysql.mysql_server_list[0].is_public_subnet # true + + # Authentication + admin_username = ncloud_mysql.mysql.user_name # "username" + # admin_password = ncloud_mysql.mysql.user_password # "password" + + provider_specific_detail = { + provider = "ncp" + resource_identifier = ncloud_mysql.mysql.id # "100457839" + is_ha = ncloud_mysql.mysql.is_ha # true + + host_ip = ncloud_mysql.mysql.host_ip # "%" + server_name_prefix = ncloud_mysql.mysql.server_name_prefix # "tofu-example-prefix" + server_instances = [for server in ncloud_mysql.mysql.mysql_server_list : { + name = server.server_name # "tofu-example-prefix-001-61we" + role = server.server_role # "M" or "H" + cpu_count = server.cpu_count # 2 + memory_size = server.memory_size # 4294967296 + create_date = server.create_date # "2024-11-14T19:29:51+0900" + uptime = server.uptime # "2024-11-14T19:34:37+0900" + server_instance_no = server.server_instance_no # "100457840" + }] + + vpc_no = ncloud_mysql.mysql.vpc_no # "82836" + subnet_no = ncloud_mysql.mysql.subnet_no # "185880" + access_control_group_no_list = ncloud_mysql.mysql.access_control_group_no_list # ["218311"] + + backup_enabled = ncloud_mysql.mysql.is_backup # true + backup_time = ncloud_mysql.mysql.backup_time # "07:45" + backup_file_retention_period = ncloud_mysql.mysql.backup_file_retention_period # 1 + + is_multi_zone = ncloud_mysql.mysql.is_multi_zone # false + is_storage_encryption = ncloud_mysql.mysql.is_storage_encryption # false + } + } } - # sensitive = true // Mark as sensitive to hide sensitive details like passwords } + +# output "ncloud_mysql_all" { +# description = "All information about the MySQL RDS instance." +# value = ncloud_mysql.mysql +# sensitive = true // Mark as sensitive to hide sensitive details like passwords +# } diff --git a/pkg/api/rest/handler/sql-db.go b/pkg/api/rest/handler/sql-db.go index 75ccd5b..b430dbd 100644 --- a/pkg/api/rest/handler/sql-db.go +++ b/pkg/api/rest/handler/sql-db.go @@ -174,7 +174,7 @@ func InitEnvForSqlDb(c echo.Context) error { return c.JSON(http.StatusCreated, res) } -// ClearEnvForSqlDb godoc +// ClearEnvOfSqlDb godoc // @Summary Clear the entire directory and configuration files // @Description Clear the entire directory and configuration files // @Tags [SQL Database] Operations @@ -188,7 +188,7 @@ func InitEnvForSqlDb(c echo.Context) error { // @Failure 500 {object} model.Response "Internal Server Error" // @Failure 503 {object} model.Response "Service Unavailable" // @Router /tr/{trId}/sql-db/env [delete] -func ClearSqlDb(c echo.Context) error { +func ClearEnvOfSqlDb(c echo.Context) error { trId := c.Param("trId") if trId == "" { @@ -490,7 +490,7 @@ func CreateSqlDb(c echo.Context) error { return c.JSON(http.StatusCreated, res) } -// GetResourceInfoOfGcpAwsVpn godoc +// GetResourceInfoOfSqlDb godoc // @Summary Get resource info of SQL database // @Description Get resource info of SQL database // @Tags [SQL Database] Operations @@ -663,7 +663,7 @@ func GetResourceInfoOfSqlDb(c echo.Context) error { } } -// DestroyGcpAwsVpn godoc +// DestroySqlDb godoc // @Summary Destroy SQL database // @Description Destroy SQL database // @Tags [SQL Database] Operations @@ -739,7 +739,7 @@ func DestroySqlDb(c echo.Context) error { return c.JSON(http.StatusCreated, res) } -// GetRequestStatusOfGcpAwsVpn godoc +// GetRequestStatusOfSqlDb godoc // @Summary Check the status of a specific request by its ID // @Description Check the status of a specific request by its ID // @Tags [SQL Database] Operations diff --git a/pkg/api/rest/model/sql-db.go b/pkg/api/rest/model/sql-db.go index c9e02eb..cb4e648 100644 --- a/pkg/api/rest/model/sql-db.go +++ b/pkg/api/rest/model/sql-db.go @@ -14,68 +14,166 @@ type TfVarsSqlDb struct { DBEngineVersion string `json:"db_engine_version" example:"8.0.39"` DBInstanceSpec string `json:"db_instance_spec" example:"db.t3.micro"` DBAdminUsername string `json:"db_admin_username" example:"mydbadmin"` - DBAdminPassword string `json:"db_admin_password" example:"P@ssword1234!"` + DBAdminPassword string `json:"db_admin_password" example:"Password1234!"` // DBInstanceID string `json:"db_instance_identifier" example:"mydbinstance"` } -type OutputAWSSqlDbInfo struct { - Terrarium struct { - ID string `json:"id"` - } `json:"terrarium"` - AWS struct { - InstanceIdentifier string `json:"instance_identifier"` - ConnectionInfo string `json:"connection_info"` - Port int `json:"port"` - AdminUsername string `json:"admin_username"` - DatabaseEngine string `json:"database_engine"` - EngineVersion string `json:"engine_version"` - Region string `json:"region"` - VpcID string `json:"vpc_id"` - SubnetIDs []string `json:"subnet_ids"` - SecurityGroupName string `json:"security_group_name"` - } `json:"aws"` +// OutputSQLDBInfo represents the SQL Database information structure +type OutputSQLDBInfo struct { + Terrarium Terrarium `json:"terrarium"` + SQLDBDetail SQLDBDetail `json:"sql_db_detail"` + Description string `json:"description"` } -type OutputAzureSqlDbInfo struct { - Terrarium struct { - ID string `json:"id"` - } `json:"terrarium"` - Azure struct { - InstanceIdentifier string `json:"instance_identifier"` - ConnectionInfo string `json:"connection_info"` - Port int `json:"port"` - AdminUsername string `json:"admin_username"` - DatabaseName string `json:"database_name"` - Region string `json:"region"` - ResourceGroup string `json:"resource_group"` - } `json:"azure"` +type Terrarium struct { + ID string `json:"id"` } -type OutputGCPSqlDbInfo struct { - Terrarium struct { - ID string `json:"id"` - } `json:"terrarium"` - GCP struct { - InstanceIdentifier string `json:"instance_identifier"` - DatabaseName string `json:"database_name"` - AdminUsername string `json:"admin_username"` - ConnectionInfo string `json:"connection_info"` - IPAddress string `json:"ip_address"` - Port int `json:"port"` - Region string `json:"region"` - } `json:"gcp"` +type SQLDBDetail struct { + // Basic Information + InstanceName string `json:"instance_name"` + InstanceResourceID string `json:"instance_resource_id"` + InstanceSpec string `json:"instance_spec"` + Location string `json:"location"` + Tags map[string]string `json:"tags,omitempty"` + + // Storage Configuration + StorageType string `json:"storage_type"` + StorageSize int `json:"storage_size"` // GB + + // Database Engine Information + EngineName string `json:"engine_name"` + EngineVersion string `json:"engine_version"` + + // Database Connection Details + ConnectionEndpoint string `json:"connection_endpoint"` + ConnectionHost string `json:"connection_host"` + ConnectionPort int `json:"connection_port"` + PublicAccessEnabled bool `json:"public_access_enabled"` + + // Authentication + AdminUsername string `json:"admin_username"` + + // Provider Specific Details + ProviderSpecificDetail ProviderSpecificDetail `json:"provider_specific_detail"` } -type OutputNCPSqlDbInfo struct { - Terrarium struct { - ID string `json:"id"` - } `json:"terrarium"` - NCP struct { - InstanceIdentifier string `json:"instance_identifier"` - ConnectionInfo string `json:"connection_info"` - AdminUsername string `json:"admin_username"` - DatabaseName string `json:"database_name"` - Port int `json:"port"` - Region string `json:"region"` - } `json:"ncp"` +type ProviderSpecificDetail struct { + // Common Fields + Provider string `json:"provider"` // aws, azure, gcp, ncp + Region string `json:"region,omitempty"` + Zone string `json:"zone,omitempty"` + ResourceIdentifier string `json:"resource_identifier"` + + // AWS Specific + Status string `json:"status,omitempty"` + DNSZoneID string `json:"dns_zone_id,omitempty"` + SecurityGroupIDs []string `json:"security_group_ids,omitempty"` + SubnetGroupName string `json:"subnet_group_name,omitempty"` + StorageEncrypted bool `json:"storage_encrypted,omitempty"` + StorageThroughput int `json:"storage_throughput,omitempty"` + StorageIOPS int `json:"storage_iops,omitempty"` + Replicas []string `json:"replicas,omitempty"` + IsMultiAZ bool `json:"is_multi_az,omitempty"` + + // Azure Specific + ResourceGroupName string `json:"resource_group_name,omitempty"` + DatabaseName string `json:"database_name,omitempty"` + Charset string `json:"charset,omitempty"` + Collation string `json:"collation,omitempty"` + StorageAutogrowEnabled bool `json:"storage_autogrow_enabled,omitempty"` + IOScalingEnabled bool `json:"io_scaling_enabled,omitempty"` + BackupRetentionDays int `json:"backup_retention_days,omitempty"` + GeoRedundantBackupEnabled bool `json:"geo_redundant_backup_enabled,omitempty"` + ReplicaCapacity int `json:"replica_capacity,omitempty"` + ReplicationRole string `json:"replication_role,omitempty"` + + // GCP Specific + Project string `json:"project,omitempty"` + AvailabilityType string `json:"availability_type,omitempty"` + + // NCP Specific + HostIP string `json:"host_ip,omitempty"` + ServerNamePrefix string `json:"server_name_prefix,omitempty"` + ServerInstances []ServerInstance `json:"server_instances,omitempty"` + VpcNo string `json:"vpc_no,omitempty"` + SubnetNo string `json:"subnet_no,omitempty"` + AccessControlGroupNoList []string `json:"access_control_group_no_list,omitempty"` + BackupEnabled bool `json:"backup_enabled,omitempty"` + BackupTime string `json:"backup_time,omitempty"` + BackupFileRetentionPeriod int `json:"backup_file_retention_period,omitempty"` + IsMultiZone bool `json:"is_multi_zone,omitempty"` + IsStorageEncryption bool `json:"is_storage_encryption,omitempty"` +} + +type ServerInstance struct { + Name string `json:"name"` + Role string `json:"role"` + CPUCount int `json:"cpu_count"` + MemorySize int64 `json:"memory_size"` + CreateDate string `json:"create_date"` + Uptime string `json:"uptime"` + ServerInstanceNo string `json:"server_instance_no"` } + +// type OutputAWSSqlDbInfo struct { +// Terrarium struct { +// ID string `json:"id"` +// } `json:"terrarium"` +// AWS struct { +// InstanceIdentifier string `json:"instance_identifier"` +// ConnectionInfo string `json:"connection_info"` +// Port int `json:"port"` +// AdminUsername string `json:"admin_username"` +// DatabaseEngine string `json:"database_engine"` +// EngineVersion string `json:"engine_version"` +// Region string `json:"region"` +// VpcID string `json:"vpc_id"` +// SubnetIDs []string `json:"subnet_ids"` +// SecurityGroupName string `json:"security_group_name"` +// } `json:"aws"` +// } + +// type OutputAzureSqlDbInfo struct { +// Terrarium struct { +// ID string `json:"id"` +// } `json:"terrarium"` +// Azure struct { +// InstanceIdentifier string `json:"instance_identifier"` +// ConnectionInfo string `json:"connection_info"` +// Port int `json:"port"` +// AdminUsername string `json:"admin_username"` +// DatabaseName string `json:"database_name"` +// Region string `json:"region"` +// ResourceGroup string `json:"resource_group"` +// } `json:"azure"` +// } + +// type OutputGCPSqlDbInfo struct { +// Terrarium struct { +// ID string `json:"id"` +// } `json:"terrarium"` +// GCP struct { +// InstanceIdentifier string `json:"instance_identifier"` +// DatabaseName string `json:"database_name"` +// AdminUsername string `json:"admin_username"` +// ConnectionInfo string `json:"connection_info"` +// IPAddress string `json:"ip_address"` +// Port int `json:"port"` +// Region string `json:"region"` +// } `json:"gcp"` +// } + +// type OutputNCPSqlDbInfo struct { +// Terrarium struct { +// ID string `json:"id"` +// } `json:"terrarium"` +// NCP struct { +// InstanceIdentifier string `json:"instance_identifier"` +// ConnectionInfo string `json:"connection_info"` +// AdminUsername string `json:"admin_username"` +// DatabaseName string `json:"database_name"` +// Port int `json:"port"` +// Region string `json:"region"` +// } `json:"ncp"` +// } diff --git a/pkg/api/rest/server.go b/pkg/api/rest/server.go index b918c2c..34cc64d 100644 --- a/pkg/api/rest/server.go +++ b/pkg/api/rest/server.go @@ -208,7 +208,7 @@ func RunServer(port string) { // SQL database APIs groupTerrarium.POST("/tr/:trId/sql-db/env", handler.InitEnvForSqlDb) - groupTerrarium.DELETE("/tr/:trId/sql-db/env", handler.ClearSqlDb) + groupTerrarium.DELETE("/tr/:trId/sql-db/env", handler.ClearEnvOfSqlDb) groupTerrarium.POST("/tr/:trId/sql-db/infracode", handler.CreateInfracodeForSqlDb) groupTerrarium.POST("/tr/:trId/sql-db/plan", handler.CheckInfracodeForSqlDb) groupTerrarium.POST("/tr/:trId/sql-db", handler.CreateSqlDb) diff --git a/templates/sql-db/aws/output.tf b/templates/sql-db/aws/output.tf index 375a435..0760e8e 100644 --- a/templates/sql-db/aws/output.tf +++ b/templates/sql-db/aws/output.tf @@ -1,20 +1,52 @@ +# Outputs wrapped in sql_db_info object output "sql_db_info" { + description = "Information of the MySQL RDS instance in AWS." value = { terrarium = { id = var.terrarium_id } - aws = { - instance_identifier = aws_db_instance.db_instance.identifier - connection_info = aws_db_instance.db_instance.endpoint - port = var.db_engine_port - admin_username = aws_db_instance.db_instance.username - database_engine = aws_db_instance.db_instance.engine - engine_version = aws_db_instance.db_instance.engine_version - region = var.csp_region - vpc_id = var.csp_vnet_id - subnet_ids = [var.csp_subnet1_id, var.csp_subnet2_id] - security_group_name = "${var.terrarium_id}-rds-sg" + sql_db_detail = { + # Basic Information + instance_name = aws_db_instance.instance.identifier # "myrdsinstance" + instance_resource_id = aws_db_instance.instance.id # "db-YMJCVDFDANINUTBJJU63AZTY5Q" + instance_spec = aws_db_instance.instance.instance_class # "db.t3.micro" + location = aws_db_instance.instance.availability_zone # "ap-northeast-2c" + tags = aws_db_instance.instance.tags # { "Name" = "myrdsinstance" } + + # Storage Configuration + storage_type = aws_db_instance.instance.storage_type # "gp2" + storage_size = aws_db_instance.instance.allocated_storage # 20 + + # Database Engine Information + engine_name = aws_db_instance.instance.engine # "mysql" + engine_version = aws_db_instance.instance.engine_version # "8.0.39" + + # Database Connection Details + connection_endpoint = aws_db_instance.instance.endpoint # "myrdsinstance.chrkjg2ktom1.ap-northeast-2.rds.amazonaws.com:3306" + connection_host = aws_db_instance.instance.address # "myrdsinstance.chrkjg2ktom1.ap-northeast-2.rds.amazonaws.com" + connection_port = aws_db_instance.instance.port # 3306 + public_access_enabled = aws_db_instance.instance.publicly_accessible # true + + # Authentication + admin_username = aws_db_instance.instance.username # "myrdsuser" + # amdin_password = aws_db_instance.myinstance.password # "myrdsuser" + + provider_specific_detail = { + provider = "aws" + region = var.csp_region + zone = aws_db_instance.instance.availability_zone + resource_identifier = aws_db_instance.instance.arn # "arn:aws:rds:ap-northeast-2:635484366616:db:myrdsinstance" + is_multi_az = aws_db_instance.instance.multi_az # false + + status = aws_db_instance.instance.status # "available" + dns_zone_id = aws_db_instance.instance.hosted_zone_id # "ZLA2NUCOLGUUR" + security_group_ids = aws_db_instance.instance.vpc_security_group_ids # ["sg-0af75bda5c889cea6"] + subnet_group_name = aws_db_instance.instance.db_subnet_group_name # "tofu-main" + storage_encrypted = aws_db_instance.instance.storage_encrypted # false + storage_throughput = aws_db_instance.instance.storage_throughput # 0 + storage_iops = aws_db_instance.instance.iops # 0 + replicas = aws_db_instance.instance.replicas # [] + } } } - description = "Information for connecting to the MySQL RDS instance with dynamic variables." } diff --git a/templates/sql-db/aws/sql-db.tf b/templates/sql-db/aws/sql-db.tf index 33dcfca..8bed3c7 100644 --- a/templates/sql-db/aws/sql-db.tf +++ b/templates/sql-db/aws/sql-db.tf @@ -1,6 +1,6 @@ # Create a DB subnet group resource "aws_db_subnet_group" "rds" { - name = "main" + name = "${var.terrarium_id}-rds" subnet_ids = [var.csp_subnet1_id, var.csp_subnet2_id] tags = { @@ -36,7 +36,7 @@ resource "aws_security_group" "rds_sg" { } # Create an RDS Database Instance with updated instance class and engine version -resource "aws_db_instance" "db_instance" { +resource "aws_db_instance" "instance" { engine = "mysql" identifier = "${var.terrarium_id}-db-instance" allocated_storage = 20 diff --git a/templates/sql-db/azure/output.tf b/templates/sql-db/azure/output.tf index ecdb513..6fac111 100644 --- a/templates/sql-db/azure/output.tf +++ b/templates/sql-db/azure/output.tf @@ -1,17 +1,57 @@ +# Outputs wrapped in sql_db_info object output "sql_db_info" { - description = "Information needed to connect and manage the Azure Database for MySQL instance." + description = "Information of the MySQL Flexible Server instance in Azure." value = { terrarium = { id = var.terrarium_id } - azure = { - instance_identifier = azurerm_mysql_flexible_server.instance.name - connection_info = azurerm_mysql_flexible_server.instance.fqdn - port = 3306 # var.db_engine_port - admin_username = azurerm_mysql_flexible_server.instance.administrator_login - database_name = azurerm_mysql_flexible_database.engine.name - region = azurerm_mysql_flexible_server.instance.location - resource_group = azurerm_mysql_flexible_server.instance.resource_group_name + sql_db_detail = { + # Basic Information + instance_name = azurerm_mysql_flexible_server.instance.name # "tofu-example-mysql-server" + instance_resource_id = azurerm_mysql_flexible_server.instance.id # "/subscriptions/a20fed83-96bd-4480-92a9-140b8e3b7c3a/resourceGroups/tofu-example-rg/providers/Microsoft.DBforMySQL/flexibleServers/tofu-example-mysql-server" + instance_spec = azurerm_mysql_flexible_server.instance.sku_name # "B_Standard_B1ms" + location = azurerm_mysql_flexible_server.instance.location # "koreacentral" + tags = azurerm_mysql_flexible_server.instance.tags # (if available) + + # Storage Configuration + storage_type = "Premium_LRS" # Azure MySQL Flexible Server uses Premium storage + storage_size = azurerm_mysql_flexible_server.instance.storage[0].size_gb # 20 + + # Database Engine Information + engine_name = "mysql" # Always "mysql" + engine_version = azurerm_mysql_flexible_server.instance.version # "5.7" + + # Database Connection Details + connection_endpoint = "${azurerm_mysql_flexible_server.instance.fqdn}:3306" # "tofu-example-mysql-server.mysql.database.azure.com:3306" + connection_host = azurerm_mysql_flexible_server.instance.fqdn # "tofu-example-mysql-server.mysql.database.azure.com" + connection_port = 3306 # Default MySQL port + public_access_enabled = azurerm_mysql_flexible_server.instance.public_network_access_enabled # true + + # Authentication + admin_username = azurerm_mysql_flexible_server.instance.administrator_login # "adminuser" + # admin_password = azurerm_mysql_flexible_server.example.administrator_login_password # "adminuser" + + provider_specific_detail = { + provider = "azure" + region = azurerm_mysql_flexible_server.instance.location + zone = azurerm_mysql_flexible_server.instance.zone # "2" + resource_group_name = azurerm_mysql_flexible_server.instance.resource_group_name # "tofu-example-rg" + + resource_identifier = azurerm_mysql_flexible_server.instance.id + + database_name = azurerm_mysql_flexible_database.engine.name # "tofu-example-db" + charset = azurerm_mysql_flexible_database.engine.charset # "utf8" + collation = azurerm_mysql_flexible_database.engine.collation # "utf8_general_ci" + + storage_autogrow_enabled = azurerm_mysql_flexible_server.instance.storage[0].auto_grow_enabled # true + io_scaling_enabled = azurerm_mysql_flexible_server.instance.storage[0].io_scaling_enabled # false + + backup_retention_days = azurerm_mysql_flexible_server.instance.backup_retention_days # 7 + geo_redundant_backup_enabled = azurerm_mysql_flexible_server.instance.geo_redundant_backup_enabled # false + + replica_capacity = azurerm_mysql_flexible_server.instance.replica_capacity # 10 + replication_role = azurerm_mysql_flexible_server.instance.replication_role # "None" + } } } } diff --git a/templates/sql-db/azure/variables.tf b/templates/sql-db/azure/variables.tf index 4c0913c..69957be 100644 --- a/templates/sql-db/azure/variables.tf +++ b/templates/sql-db/azure/variables.tf @@ -97,6 +97,6 @@ variable "db_admin_username" { variable "db_admin_password" { type = string description = "The admin password for the database." - default = "P@ssword1234!" + default = "Password1234!" } diff --git a/templates/sql-db/gcp/output.tf b/templates/sql-db/gcp/output.tf index c9774fe..590e1a0 100644 --- a/templates/sql-db/gcp/output.tf +++ b/templates/sql-db/gcp/output.tf @@ -1,17 +1,46 @@ +# Outputs wrapped in sql_db_info object output "sql_db_info" { + description = "Information for connecting to the MySQL Cloud SQL instance with dynamic variables." value = { terrarium = { id = var.terrarium_id } - gcp = { - instance_identifier = google_sql_database_instance.instance.name - database_name = google_sql_database.engine.name - admin_username = google_sql_user.admin_user.name - connection_info = google_sql_database_instance.instance.connection_name - ip_address = google_sql_database_instance.instance.public_ip_address - port = 3306 # var.db_engine_port - region = google_sql_database_instance.instance.region + sql_db_detail = { + # Basic Information + instance_name = google_sql_database_instance.instance.name # "my-sql-instance" + instance_resource_id = google_sql_database_instance.instance.id # "my-sql-instance" + instance_spec = google_sql_database_instance.instance.settings[0].tier # "db-f1-micro" + location = google_sql_database_instance.instance.settings[0].location_preference[0].zone # "asia-northeast3-a" + tags = google_sql_database_instance.instance.settings[0].user_labels # {} + + # Storage Configuration + storage_type = google_sql_database_instance.instance.settings[0].disk_type # "PD_SSD" + storage_size = google_sql_database_instance.instance.settings[0].disk_size # 10 + + # Database Engine Information + engine_name = "mysql" # Not exposed by GCP + engine_version = google_sql_database_instance.instance.database_version # "MYSQL_8_0" + + # Database Connection Details + connection_endpoint = "${google_sql_database_instance.instance.first_ip_address}:3306" + connection_host = google_sql_database_instance.instance.first_ip_address + connection_port = 3306 # Default MySQL port + public_access_enabled = google_sql_database_instance.instance.settings[0].ip_configuration[0].ipv4_enabled + + # Authentication + admin_username = google_sql_user.admin_user.name # "myuser" + # admin_password = google_sql_user.my_user.password # "mypassword" + + provider_specific_detail = { + provider = "gcp" + region = google_sql_database_instance.instance.region + zone = google_sql_database_instance.instance.settings[0].location_preference[0].zone + project = google_sql_database_instance.instance.project + + availability_type = google_sql_database_instance.instance.settings[0].availability_type + + resource_identifier = google_sql_database_instance.instance.self_link + } } } - description = "Information for SQL Database instance, including instance name, database name, user, connection name, and public IP address" } diff --git a/templates/sql-db/gcp/variables.tf b/templates/sql-db/gcp/variables.tf index 744e0ce..831c388 100644 --- a/templates/sql-db/gcp/variables.tf +++ b/templates/sql-db/gcp/variables.tf @@ -96,6 +96,6 @@ variable "db_admin_username" { variable "db_admin_password" { type = string description = "The admin password for the database." - default = "P@ssword1234!" + default = "Password1234!" } diff --git a/templates/sql-db/ncp/output.tf b/templates/sql-db/ncp/output.tf index 3d6367f..0e75350 100644 --- a/templates/sql-db/ncp/output.tf +++ b/templates/sql-db/ncp/output.tf @@ -1,17 +1,65 @@ + +# Outputs wrapped in sql_db_info object output "sql_db_info" { - description = "Information needed to connect to the MySQL RDS instance." + description = "Information of the MySQL instance in NCP." value = { terrarium = { id = var.terrarium_id } - ncp = { - instance_identifier = ncloud_mysql.mysql.service_name - connection_info = ncloud_mysql.mysql.host_ip - admin_username = ncloud_mysql.mysql.user_name - database_name = ncloud_mysql.mysql.database_name - port = 3306 # var.db_engine_port - region = ncloud_mysql.mysql.region // Assume region is available + sql_db_detail = { + # Basic Information + instance_name = ncloud_mysql.mysql.service_name # "tofu-example-mysql" + instance_resource_id = ncloud_mysql.mysql.id # "100457839" + instance_spec = ncloud_mysql.mysql.image_product_code # "SW.VMYSL.OS.LNX64.ROCKY.0810.MYSQL.B050" + location = ncloud_mysql.mysql.region_code # "KR" + tags = {} # (if available) + + # Storage Configuration + storage_type = ncloud_mysql.mysql.data_storage_type # "SSD" + storage_size = ncloud_mysql.mysql.mysql_server_list[0].data_storage_size / 1073741824 # 10 (Unit: GB) + + # Database Engine Information + engine_name = "mysql" # Always "mysql" + engine_version = ncloud_mysql.mysql.engine_version_code # "MYSQL8.0.36" + + # Database Connection Details + connection_endpoint = "${ncloud_mysql.mysql.mysql_server_list[0].private_domain}:${ncloud_mysql.mysql.port}" # "db-2vpnbg.vpc-cdb.ntruss.com:3306" + connection_host = ncloud_mysql.mysql.mysql_server_list[0].private_domain # "db-2vpnbg.vpc-cdb.ntruss.com" + connection_port = ncloud_mysql.mysql.port # 3306 + public_access_enabled = ncloud_mysql.mysql.mysql_server_list[0].is_public_subnet # true + + # Authentication + admin_username = ncloud_mysql.mysql.user_name # "username" + # admin_password = ncloud_mysql.mysql.user_password # "password" + + provider_specific_detail = { + provider = "ncp" + resource_identifier = ncloud_mysql.mysql.id # "100457839" + is_ha = ncloud_mysql.mysql.is_ha # true + + host_ip = ncloud_mysql.mysql.host_ip # "%" + server_name_prefix = ncloud_mysql.mysql.server_name_prefix # "tofu-example-prefix" + server_instances = [for server in ncloud_mysql.mysql.mysql_server_list : { + name = server.server_name # "tofu-example-prefix-001-61we" + role = server.server_role # "M" or "H" + cpu_count = server.cpu_count # 2 + memory_size = server.memory_size # 4294967296 + create_date = server.create_date # "2024-11-14T19:29:51+0900" + uptime = server.uptime # "2024-11-14T19:34:37+0900" + server_instance_no = server.server_instance_no # "100457840" + }] + + vpc_no = ncloud_mysql.mysql.vpc_no # "82836" + subnet_no = ncloud_mysql.mysql.subnet_no # "185880" + access_control_group_no_list = ncloud_mysql.mysql.access_control_group_no_list # ["218311"] + + backup_enabled = ncloud_mysql.mysql.is_backup # true + backup_time = ncloud_mysql.mysql.backup_time # "07:45" + backup_file_retention_period = ncloud_mysql.mysql.backup_file_retention_period # 1 + + is_multi_zone = ncloud_mysql.mysql.is_multi_zone # false + is_storage_encryption = ncloud_mysql.mysql.is_storage_encryption # false + } } } - # sensitive = true // Mark as sensitive to hide sensitive details like passwords } diff --git a/templates/sql-db/ncp/variables.tf b/templates/sql-db/ncp/variables.tf index 3e93b3c..34ee00a 100644 --- a/templates/sql-db/ncp/variables.tf +++ b/templates/sql-db/ncp/variables.tf @@ -96,6 +96,6 @@ variable "db_admin_username" { variable "db_admin_password" { type = string description = "The admin password for the database." - default = "P@ssword1234!" + default = "Password1234!" }