Skip to content

Commit

Permalink
Merge pull request #111 from yunkon-kim/241115-02
Browse files Browse the repository at this point in the history
Staging v0.0.16
  • Loading branch information
yunkon-kim authored Nov 14, 2024
2 parents f82a9de + 2951c72 commit 60ed69e
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ secrets/**
!secrets/template-*
!secrets/.keep
!secrets/README.md
!secrets/load-ncp-cred-env.sh

# Ignore container volumes
container-volume/
4 changes: 2 additions & 2 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,8 @@ const docTemplate = `{
}
],
"responses": {
"201": {
"description": "Created",
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.Response"
}
Expand Down
4 changes: 2 additions & 2 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,8 @@
}
],
"responses": {
"201": {
"description": "Created",
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.Response"
}
Expand Down
4 changes: 2 additions & 2 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,8 @@ paths:
produces:
- application/json
responses:
"201":
description: Created
"200":
description: OK
schema:
$ref: '#/definitions/model.Response'
"400":
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ networks:

services:
mc-terrarium:
image: cloudbaristaorg/mc-terrarium:0.0.10
image: cloudbaristaorg/mc-terrarium:0.0.16
container_name: mc-terrarium
build:
context: .
Expand All @@ -18,6 +18,7 @@ services:
env_file:
- ./secrets/credentials # AWS credentials
- ./secrets/credential-azure.env # Azure credentials
- ./secrets/credential-ncp.env # NCP credentials
volumes:
- ./secrets:/app/secrets:ro # GCP credentials
- ./container-volume/mc-terrarium-container/.terrarium:/app/.terrarium
Expand Down
40 changes: 32 additions & 8 deletions pkg/api/rest/handler/sql-db.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func InitEnvForSqlDb(c echo.Context) error {
}

if !validProvidersForSqlDb[provider] {
err := fmt.Errorf("invalid request, provider must be one of [aws, azure, gcp, ncp]")
err := fmt.Errorf("invalid request, provider must be one of [aws, azure, gcp, ncpvpc]")
log.Warn().Msg(err.Error())
res := model.Response{
Success: false,
Expand Down Expand Up @@ -396,7 +396,7 @@ func CheckInfracodeForSqlDb(c echo.Context) error {
// subcommand: plan
ret, err := tofu.ExecuteTofuCommand(trId, reqId, "-chdir="+workingDir, "plan")
if err != nil {
err2 := fmt.Errorf("encountered an issue during the infracode checking process")
err2 := fmt.Errorf("returned: %s", ret)
log.Error().Err(err).Msg(err2.Error()) // error
res := model.Response{
Success: false,
Expand Down Expand Up @@ -424,7 +424,7 @@ func CheckInfracodeForSqlDb(c echo.Context) error {
// @Produce json
// @Param trId path string true "Terrarium ID" default(tr01)
// @Param x-request-id header string false "Custom request ID"
// @Success 201 {object} model.Response "Created"
// @Success 200 {object} model.Response "OK"
// @Failure 400 {object} model.Response "Bad Request"
// @Failure 500 {object} model.Response "Internal Server Error"
// @Failure 503 {object} model.Response "Service Unavailable"
Expand Down Expand Up @@ -469,7 +469,7 @@ func CreateSqlDb(c echo.Context) error {

// global option to set working dir: -chdir=/home/ubuntu/dev/cloud-barista/mc-terrarium/.terrarium/{trId}/vpn/gcp-aws
// subcommand: apply
ret, err := tofu.ExecuteTofuCommandAsync(trId, reqId, "-chdir="+workingDir, "apply", "-auto-approve")
_, err = tofu.ExecuteTofuCommand(trId, reqId, "-chdir="+workingDir, "apply", "-auto-approve")
if err != nil {
err2 := fmt.Errorf("failed, previous request in progress")
log.Error().Err(err).Msg(err2.Error()) // error
Expand All @@ -479,15 +479,39 @@ func CreateSqlDb(c echo.Context) error {
}
return c.JSON(http.StatusInternalServerError, res)
}

// global option to set working dir: -chdir=/home/ubuntu/dev/cloud-barista/mc-terrarium/.terrarium/{trId}/sql-db
// show: subcommand
ret, err := tofu.ExecuteTofuCommand(trId, reqId, "-chdir="+workingDir, "output", "-json", "sql_db_info")
if err != nil {
err2 := fmt.Errorf("failed to read resource info (detail: %s) specified as 'output' in the state file", "refined")
log.Error().Err(err).Msg(err2.Error())
res := model.Response{
Success: false,
Message: err2.Error(),
}
return c.JSON(http.StatusInternalServerError, res)
}

var resourceInfo map[string]interface{}
err = json.Unmarshal([]byte(ret), &resourceInfo)
if err != nil {
log.Error().Err(err).Msg("") // error
res := model.Response{
Success: false,
Message: "failed to unmarshal resource info",
}
return c.JSON(http.StatusInternalServerError, res)
}

res := model.Response{
Success: true,
Message: "the request (id: " + reqId + ") is successfully accepted and still deploying resource",
Detail: ret,
Message: "refined read resource info (map)",
Object: resourceInfo,
}

log.Debug().Msgf("%+v", res) // debug

return c.JSON(http.StatusCreated, res)
return c.JSON(http.StatusOK, res)
}

// GetResourceInfoOfSqlDb godoc
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/rest/model/sql-db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type TfVarsSqlDb struct {
DBEnginePort int `json:"db_engine_port,omitempty" example:"3306"`
IngressCIDRBlock string `json:"ingress_cidr_block,omitempty" example:"0.0.0.0/0"`
EgressCIDRBlock string `json:"egress_cidr_block,omitempty" example:"0.0.0.0/0"`
DBEngineVersion string `json:"db_engine_version" example:"8.0.39"`
DBInstanceSpec string `json:"db_instance_spec" example:"db.t3.micro"`
DBEngineVersion string `json:"db_engine_version,omitempty" example:"8.0.39"`
DBInstanceSpec string `json:"db_instance_spec,omitempty" example:"db.t3.micro"`
DBAdminUsername string `json:"db_admin_username" example:"mydbadmin"`
DBAdminPassword string `json:"db_admin_password" example:"Password1234!"`
// DBInstanceID string `json:"db_instance_identifier" example:"mydbinstance"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/tofu/tofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func ExecuteTofuCommand(trId, reqId string, args ...string) (string, error) {
if err != nil {
log.Error().Msgf("Command execution failed: %v", err)
setRunningStatus(trId, "Failed")
return "", err
return output, err
}
// log.Debug().Msgf("Command output: %s", output)
setRunningStatus(trId, "Success")
Expand Down
17 changes: 17 additions & 0 deletions secrets/load-ncp-cred-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Find the script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$SCRIPT_DIR/credential-ncp.env"

# Check if .env file exists
if [[ -f "$ENV_FILE" ]]; then
while IFS= read -r line; do
if [[ -n "$line" && ! "$line" =~ ^# ]]; then
eval export "$line"
fi
done < "$ENV_FILE"
echo "successfully loaded the NCP credential environment variables"
else
echo "error: dose not exist file, $ENV_FILE"
fi
2 changes: 0 additions & 2 deletions secrets/template-credential-ncp

This file was deleted.

2 changes: 2 additions & 0 deletions secrets/template-credential-ncp.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NCLOUD_ACCESS_KEY="YOUR_ACCESS_KEY"
NCLOUD_SECRET_KEY="YOUR_SECRET_KEY"
2 changes: 1 addition & 1 deletion templates/sql-db/gcp/sql-db.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resource "google_sql_database_instance" "instance" {
tier = var.db_instance_spec # Set the instance type, such as db-f1-micro
}

# deletion_protection = false # Disable deletion protection
deletion_protection = false # Disable deletion protection
}

# Create database
Expand Down
4 changes: 2 additions & 2 deletions templates/sql-db/ncp/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ terraform {
provider "ncloud" {
access_key = var.ncloud_access_key
secret_key = var.ncloud_secret_key
region = var.csp_region # Set the desired region (e.g., "KR", "JP", etc.)
support_vpc = true # Enable VPC support
region = upper(var.csp_region) # Set the desired region (e.g., "KR", "JP", etc.)
support_vpc = true # Enable VPC support
}

# Declare variables
Expand Down
12 changes: 6 additions & 6 deletions templates/sql-db/ncp/sql-db.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Create MySQL RDS Instance
resource "ncloud_mysql" "mysql" {
subnet_no = var.csp_subnet1_id
service_name = "${var.terrarium_id}-db-instance" # Service name: Only English alphabets, numbers, dash ( - ) and Korean letters can be entered. Min: 3, Max: 30
server_name_prefix = "svr-name-prefix" # Server name prefix: In order to prevent overlapping host names, random text is added. Min: 3, Max: 20
user_name = var.db_admin_username # Master username
user_password = var.db_admin_password # Master password
host_ip = "%" # Host IP: "%" For overall access (use cautiously), specific IPs permitted: 1.1.1.1, IP band connection permitted: 1.1.1.%
database_name = "${var.terrarium_id}-db" # Initial database name
service_name = "${var.terrarium_id}-svc" # Service name: Only English alphabets, numbers, dash ( - ) and Korean letters can be entered. Min: 3, Max: 30
server_name_prefix = "svr-name-prefix" # Server name prefix: In order to prevent overlapping host names, random text is added. Min: 3, Max: 20
user_name = var.db_admin_username # Master username
user_password = var.db_admin_password # Master password
host_ip = "%" # Host IP: "%" For overall access (use cautiously), specific IPs permitted: 1.1.1.1, IP band connection permitted: 1.1.1.%
database_name = "${var.terrarium_id}-db" # Initial database name
}
5 changes: 5 additions & 0 deletions templates/sql-db/ncp/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ variable "csp_region" {
variable "csp_subnet1_id" {
type = string
description = "The subnet ID in NCP."

validation {
condition = can(regex("^[0-9]+$", var.csp_subnet1_id))
error_message = "The csp_subnet1_id must only contain digits (e.g, 123456)."
}
}

# variable "csp_subnet2_id" {
Expand Down

0 comments on commit 60ed69e

Please sign in to comment.