-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from janritter/feat/multi-backend
Feat/multi backend
- Loading branch information
Showing
13 changed files
with
330 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package iface | ||
|
||
type BackendAPI interface { | ||
GenerateStatefileForBackend(in interface{}) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package backend | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
|
||
"github.com/janritter/terrastate/backend/iface" | ||
"github.com/janritter/terrastate/backend/s3" | ||
) | ||
|
||
func GetBackendInterface(backendType string) (iface.BackendAPI, error) { | ||
switch backendType { | ||
case "s3": | ||
return s3.NewS3Backend(), nil | ||
} | ||
|
||
err := errors.New("Backend " + backendType + " is currently not supported") | ||
log.Println(err) | ||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package s3 | ||
|
||
type S3BackendAPI interface { | ||
GenerateStatefileForBackend(in interface{}) error | ||
} | ||
|
||
type S3Backend struct{} | ||
|
||
func NewS3Backend() *S3Backend { | ||
return &S3Backend{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package s3 | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
func createStateFile(in stateConfig) error { | ||
fileContent := | ||
`terraform { | ||
backend "s3" { | ||
encrypt = true | ||
bucket = "` + in.Bucket + `" | ||
region = "` + in.Region + `" | ||
key = "` + in.Key + `" | ||
dynamodb_table = "` + in.DynamoDBTable + `" | ||
} | ||
} | ||
` | ||
|
||
data := []byte(fileContent) | ||
err := ioutil.WriteFile("terrastate.tf", data, 0644) | ||
if err != nil { | ||
log.Println(err) | ||
return err | ||
} | ||
|
||
color.Green("Successfully created terrastate.tf") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package s3 | ||
|
||
import ( | ||
"github.com/janritter/terrastate/helper" | ||
) | ||
|
||
func (backend *S3Backend) GenerateStatefileForBackend(in interface{}) error { | ||
stateParams := stateConfig{} | ||
err := parseBackendParameter(in, &stateParams) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
helper.PrintStateValues(stateParams) | ||
|
||
err = createStateFile(stateParams) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package s3 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func parseBackendParameter(in interface{}, out *stateConfig) error { | ||
switch in.(type) { | ||
case map[string]interface{}: | ||
mapped := in.(map[string]interface{}) | ||
|
||
if mapped["state_bucket"] == nil { | ||
err := errors.New("state_bucket must be defined") | ||
log.Println(err) | ||
return err | ||
} | ||
if reflect.TypeOf(mapped["state_bucket"]).String() != "string" { | ||
err := errors.New("state_bucket must be of type string, was " + reflect.TypeOf(mapped["state_bucket"]).String()) | ||
log.Println(err) | ||
return err | ||
} | ||
out.Bucket = mapped["state_bucket"].(string) | ||
|
||
if mapped["state_dynamodb_table"] == nil { | ||
err := errors.New("state_dynamodb_table must be defined") | ||
log.Println(err) | ||
return err | ||
} | ||
if reflect.TypeOf(mapped["state_dynamodb_table"]).String() != "string" { | ||
err := errors.New("state_dynamodb_table must be of type string, was " + reflect.TypeOf(mapped["state_dynamodb_table"]).String()) | ||
log.Println(err) | ||
return err | ||
} | ||
out.DynamoDBTable = mapped["state_dynamodb_table"].(string) | ||
|
||
if mapped["state_key"] == nil { | ||
err := errors.New("state_key must be defined") | ||
log.Println(err) | ||
return err | ||
} | ||
if reflect.TypeOf(mapped["state_key"]).String() != "string" { | ||
err := errors.New("state_key must be of type string, was " + reflect.TypeOf(mapped["state_key"]).String()) | ||
log.Println(err) | ||
return err | ||
} | ||
|
||
// Remove all spaces | ||
key := strings.ReplaceAll(mapped["state_key"].(string), " ", "") | ||
|
||
// Check if the key string contains current.dir | ||
if !strings.Contains(key, "{{current.dir}}") { | ||
err := errors.New("{{current.dir}} is missing the state_key string") | ||
log.Println(err) | ||
return err | ||
} | ||
|
||
// Replace placeholder with current dir | ||
path, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
dir := filepath.Base(path) | ||
fmt.Println("Current Directory = " + dir) | ||
key = strings.ReplaceAll(key, "{{current.dir}}", dir) | ||
|
||
out.Key = key | ||
|
||
if mapped["region"] == nil { | ||
err := errors.New("region must be defined") | ||
log.Println(err) | ||
return err | ||
} | ||
if reflect.TypeOf(mapped["region"]).String() != "string" { | ||
err := errors.New("region must be of type string, was " + reflect.TypeOf(mapped["region"]).String()) | ||
log.Println(err) | ||
return err | ||
} | ||
out.Region = mapped["region"].(string) | ||
|
||
default: | ||
err := errors.New("Unknown var-file format") | ||
log.Println(err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package s3 | ||
|
||
type stateConfig struct { | ||
Bucket string | ||
DynamoDBTable string | ||
Key string | ||
Region string | ||
} |
Oops, something went wrong.