Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add unique ids in postgresql database plugin and response event structure #451

Merged
merged 10 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/codeamp/circuit/cmd"
_ "github.com/codeamp/circuit/plugins/codeamp"
_ "github.com/codeamp/circuit/plugins/database"
_ "github.com/codeamp/circuit/plugins/dockerbuilder"
_ "github.com/codeamp/circuit/plugins/githubstatus"
_ "github.com/codeamp/circuit/plugins/gitsync"
Expand Down
29 changes: 16 additions & 13 deletions plugins/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"errors"
"fmt"

"github.com/codeamp/circuit/plugins"
log "github.com/codeamp/logger"
Expand Down Expand Up @@ -57,6 +58,13 @@ func (x *Database) sendFailedStatusEvent(err error) {
x.events <- ev
}

func (x *Database) sendSuccessResponse(e transistor.Event, state transistor.State, artifacts []transistor.Artifact) {
event := e.NewEvent(transistor.GetAction("status"), transistor.GetState("complete"), fmt.Sprintf("%s has completed successfully", e.Event()))
event.Artifacts = artifacts

x.events <- event
}

// Process
func (x *Database) Process(e transistor.Event) error {
log.Debug("Processing database event")
Expand Down Expand Up @@ -118,17 +126,14 @@ func (x *Database) Process(e transistor.Event) error {
return nil
}

// store db metadata into instance
respEvent := transistor.NewEvent(e.Name, transistor.GetAction("status"), nil)
respEvent.State = transistor.GetState("complete")

respEvent.AddArtifact("DB_USER", dbMetadata.Credentials.Username, false)
respEvent.AddArtifact("DB_PASSWORD", dbMetadata.Credentials.Password, false)
respEvent.AddArtifact("DB_NAME", dbMetadata.Name, false)
respEvent.AddArtifact("DB_ENDPOINT", (*dbInstance).GetInstanceMetadata().Endpoint, false)
respEvent.AddArtifact("DB_PORT", (*dbInstance).GetInstanceMetadata().Port, false)
artifacts := make([]transistor.Artifact, 5)
drshrey marked this conversation as resolved.
Show resolved Hide resolved
artifacts[0] = transistor.Artifact{Key: "DB_USER", Value: dbMetadata.Credentials.Username, Secret: false}
artifacts[1] = transistor.Artifact{Key: "DB_PASSWORD", Value: dbMetadata.Credentials.Password, Secret: false}
artifacts[2] = transistor.Artifact{Key: "DB_NAME", Value: dbMetadata.Name, Secret: false}
artifacts[3] = transistor.Artifact{Key: "DB_ENDPOINT", Value: (*dbInstance).GetInstanceMetadata().Endpoint, Secret: false}
artifacts[4] = transistor.Artifact{Key: "DB_PORT", Value: (*dbInstance).GetInstanceMetadata().Port, Secret: false}

x.events <- respEvent
x.sendSuccessResponse(e, transistor.GetState("complete"), artifacts)
case transistor.GetAction("delete"):
dbName, err := e.GetArtifact("DB_NAME")
if err != nil {
Expand All @@ -148,10 +153,8 @@ func (x *Database) Process(e transistor.Event) error {
}

// store db metadata into instance
respEvent := transistor.NewEvent(e.Name, transistor.GetAction("status"), nil)
respEvent.State = transistor.GetState("complete")

x.events <- respEvent
x.sendSuccessResponse(e, transistor.GetState("complete"), nil)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion plugins/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/codeamp/circuit/plugins"
"github.com/codeamp/circuit/plugins/database"
"github.com/codeamp/circuit/test"
"github.com/codeamp/transistor"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (suite *DatabaseTestSuite) TestDatabase_Success() {
dbAdminUsername := "postgres"
dbAdminPassword := ""
dbInstancePort := "5432"
dbType := "postgresql"
dbType := database.POSTGRESQL

payload := plugins.ProjectExtension{
Project: plugins.Project{
Expand Down
30 changes: 28 additions & 2 deletions plugins/database/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,47 @@ package database
import (
"fmt"
"math/rand"
"strings"
"time"

"github.com/codeamp/circuit/plugins"
uuid "github.com/satori/go.uuid"
)

// genDBName creates a database name for the specified
// project extension with the format <project.slug>-<environment>
func genDBName(pe plugins.ProjectExtension) string {
return fmt.Sprintf("%s_%s", pe.Project.Slug, pe.Environment)
projectSlugWithUnderscores := strings.Replace(pe.Project.Slug, "-", "_", -1)
envWithUnderscores := strings.Replace(pe.Environment, "-", "_", -1)
if len(projectSlugWithUnderscores) > 20 {
projectSlugWithUnderscores = projectSlugWithUnderscores[:20]
}

if len(envWithUnderscores) > 20 {
envWithUnderscores = envWithUnderscores[:20]
}

uniqueID := uuid.NewV4()

return fmt.Sprintf("%s_%s_%s", projectSlugWithUnderscores, envWithUnderscores, strings.Replace(uniqueID.String()[:15], "-", "_", -1))
}

// genDBUsername creates a database username for the specified
// project extension with the format <project.slug>-<environment>
func genDBUser(pe plugins.ProjectExtension) string {
return fmt.Sprintf("%s_%s_user", pe.Project.Slug, pe.Environment)
projectSlugWithUnderscores := strings.Replace(pe.Project.Slug, "-", "_", -1)
envWithUnderscores := strings.Replace(pe.Environment, "-", "_", -1)
if len(projectSlugWithUnderscores) > 20 {
projectSlugWithUnderscores = projectSlugWithUnderscores[:20]
}

if len(pe.Environment) > 20 {
envWithUnderscores = envWithUnderscores[:20]
}

uniqueID := uuid.NewV4()

return fmt.Sprintf("%s_%s_%s_user", projectSlugWithUnderscores, envWithUnderscores, strings.Replace(uniqueID.String()[:11], "-", "_", -1))
}

func genDBPassword() string {
Expand Down
7 changes: 6 additions & 1 deletion plugins/database/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"fmt"

log "github.com/codeamp/logger"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
Expand All @@ -16,7 +17,11 @@ type Postgres struct {
// initPostgresInstance opens a postgresql connection to the host
// and returns a DatabaseInstance object, holding the connection object
func initPostgresInstance(host string, username string, password string, port string) DatabaseInstance {
db, _ := gorm.Open("postgres", fmt.Sprintf("user=%s host=%s sslmode=%s password=%s port=%s", username, host, "disable", password, port))
db, err := gorm.Open("postgres", fmt.Sprintf("user=%s host=%s sslmode=%s password=%s port=%s", username, host, "disable", password, port))
if err != nil {
log.Info(err.Error())
drshrey marked this conversation as resolved.
Show resolved Hide resolved
}

return &Postgres{
BaseDatabaseInstance: BaseDatabaseInstance{
instanceMetadata: InstanceMetadata{
Expand Down