Skip to content

Commit

Permalink
Merge pull request #131 from shalb/remote-state
Browse files Browse the repository at this point in the history
Remote state
  • Loading branch information
romanprog authored May 20, 2021
2 parents 2c95851 + b0817bb commit 3622ab1
Show file tree
Hide file tree
Showing 42 changed files with 741 additions and 197 deletions.
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ go 1.16

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/apex/log v1.9.0
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.33.18
github.com/apparentlymart/go-cidr v1.1.0
github.com/aws/aws-sdk-go v1.38.39
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59
github.com/buger/goterm v0.0.0-20200322175922-2f3e71b85129 // indirect
github.com/buger/goterm v1.0.0 // indirect
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 // indirect
github.com/gookit/color v1.3.7 // indirect
github.com/gookit/color v1.3.7
github.com/hashicorp/go-getter v1.5.2 // indirect
github.com/hashicorp/hcl/v2 v2.8.2
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/kylelemons/godebug v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/paulrademacher/climenu v0.0.0-20151110221007-a1afbb4e378b // indirect
github.com/paulrademacher/climenu v0.0.0-20151110221007-a1afbb4e378b
github.com/pkg/term v1.1.0 // indirect
github.com/spf13/cobra v1.1.3
github.com/tj/go-spin v1.1.0
github.com/zclconf/go-cty v1.7.1
go.mozilla.org/sops/v3 v3.6.1
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
github.com/zclconf/go-cty v1.8.3
go.mozilla.org/sops/v3 v3.7.1
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
62 changes: 62 additions & 0 deletions go.sum

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions pkg/aws/s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package aws

import (
"bytes"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)

func S3Put(region, bucketName, keyName, data string) error {
svc := s3.New(session.New(
&aws.Config{
Region: aws.String(region),
},
))
input := &s3.PutObjectInput{
Body: strings.NewReader(data),
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
}
// log.Debugf("S3 uploading object. Bucket '%v', key: '%v', region: '%v'", bucketName, keyName, region)
// Create an uploader with the session and default options
_, err := svc.PutObject(input)
if err != nil {
return err
}
return err
}

func S3Get(region, bucketName, keyName string) (string, error) {
svc := s3.New(session.New(
&aws.Config{
Region: aws.String(region),
},
))
input := &s3.GetObjectInput{

Bucket: aws.String(bucketName),
Key: aws.String(keyName),
}
// log.Debugf("S3 downloading object. Bucket '%v', key: '%v', region: '%v'", bucketName, keyName, region)
// Create an uploader with the session and default options
result, err := svc.GetObject(input)
if err != nil {
// if aerr, ok := err.(awserr.Error); ok {
// switch aerr.Code() {
// case s3.ErrCodeNoSuchKey:
// log.Debugf("%v %v", s3.ErrCodeNoSuchKey, aerr.Error())
// case s3.ErrCodeInvalidObjectState:
// log.Debugf("%v %v", s3.ErrCodeInvalidObjectState, aerr.Error())
// default:
// log.Debug(aerr.Error())
// }
// }
return "", err
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(result.Body)
if err != nil {
return "", err
}
return buf.String(), nil
}

func S3Delete(region, bucketName, keyName string) error {
svc := s3.New(session.New(
&aws.Config{
Region: aws.String(region),
},
))
// log.Debugf("S3 deleting object. Bucket '%v', key: '%v', region: '%v'", bucketName, keyName, region)
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
})

if err != nil {
return err
}
return nil
}
7 changes: 5 additions & 2 deletions pkg/backend/azurerm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
type Factory struct{}

// New creates the new backend.
func (f *Factory) New(config []byte, name string) (project.Backend, error) {
bk := BackendAzureRm{name: name}
func (f *Factory) New(config []byte, name string, p *project.Project) (project.Backend, error) {
bk := Backend{
name: name,
ProjectPtr: p,
}
err := yaml.Unmarshal(config, &bk.state)
if err != nil {
return nil, err
Expand Down
37 changes: 27 additions & 10 deletions pkg/backend/azurerm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@ import (
"fmt"

"github.com/shalb/cluster.dev/pkg/hcltools"
"github.com/shalb/cluster.dev/pkg/project"
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/hcl/v2/hclwrite"
)

// BackendAzureRm - describe s3 backend for interface package.backend.
type BackendAzureRm struct {
name string
state map[string]interface{}
// Backend - describe s3 backend for interface package.backend.
type Backend struct {
name string
state map[string]interface{}
ProjectPtr *project.Project
}

func (b *BackendAzureRm) State() map[string]interface{} {
func (b *Backend) State() map[string]interface{} {
return b.state
}

// Name return name.
func (b *BackendAzureRm) Name() string {
func (b *Backend) Name() string {
return b.name
}

// Provider return name.
func (b *BackendAzureRm) Provider() string {
func (b *Backend) Provider() string {
return "azurerm"
}

// GetBackendBytes generate terraform backend config.
func (b *BackendAzureRm) GetBackendBytes(infraName, moduleName string) ([]byte, error) {
func (b *Backend) GetBackendBytes(infraName, moduleName string) ([]byte, error) {
f, err := b.GetBackendHCL(infraName, moduleName)
if err != nil {
return nil, err
Expand All @@ -39,7 +41,7 @@ func (b *BackendAzureRm) GetBackendBytes(infraName, moduleName string) ([]byte,
}

// GetBackendHCL generate terraform backend config.
func (b *BackendAzureRm) GetBackendHCL(infraName, moduleName string) (*hclwrite.File, error) {
func (b *Backend) GetBackendHCL(infraName, moduleName string) (*hclwrite.File, error) {
b.state["key"] = fmt.Sprintf("%s-%s.state", infraName, moduleName)

f := hclwrite.NewEmptyFile()
Expand All @@ -54,7 +56,7 @@ func (b *BackendAzureRm) GetBackendHCL(infraName, moduleName string) (*hclwrite.
}

// GetRemoteStateHCL generate terraform remote state for this backend.
func (b *BackendAzureRm) GetRemoteStateHCL(infraName, moduleName string) ([]byte, error) {
func (b *Backend) GetRemoteStateHCL(infraName, moduleName string) ([]byte, error) {
b.state["key"] = fmt.Sprintf("%s-%s.state", infraName, moduleName)

f := hclwrite.NewEmptyFile()
Expand All @@ -70,3 +72,18 @@ func (b *BackendAzureRm) GetRemoteStateHCL(infraName, moduleName string) ([]byte
dataBody.SetAttributeValue("config", config)
return f.Bytes(), nil
}
func (b *Backend) LockState() error {
return fmt.Errorf("cdev state azurerm not supported")
}

func (b *Backend) UnlockState() error {
return fmt.Errorf("cdev state azurerm not supported")
}

func (b *Backend) WriteState(stateData string) error {
return fmt.Errorf("cdev state azurerm not supported")
}

func (b *Backend) ReadState() (string, error) {
return "", fmt.Errorf("cdev state azurerm not supported")
}
46 changes: 32 additions & 14 deletions pkg/backend/do/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ import (

"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/shalb/cluster.dev/pkg/hcltools"
"github.com/shalb/cluster.dev/pkg/project"
)

// BackendDo - describe do spaces backend for interface package.backend.
type BackendDo struct {
name string
Bucket string `yaml:"bucket"`
Region string `yaml:"region"`
AccessKey string `yaml:"access_key,omitempty"`
SecretKey string `yaml:"secret_key,omitempty"`
state map[string]interface{}
// Backend - describe do spaces backend for interface package.backend.
type Backend struct {
name string
Bucket string `yaml:"bucket"`
Region string `yaml:"region"`
AccessKey string `yaml:"access_key,omitempty"`
SecretKey string `yaml:"secret_key,omitempty"`
state map[string]interface{}
ProjectPtr *project.Project
}

func (b *BackendDo) State() map[string]interface{} {
func (b *Backend) State() map[string]interface{} {
return b.state
}

// Name return name.
func (b *BackendDo) Name() string {
func (b *Backend) Name() string {
return b.name
}

// Provider return name.
func (b *BackendDo) Provider() string {
func (b *Backend) Provider() string {
return "do"
}

Expand All @@ -40,7 +42,7 @@ type backendConfigSpec struct {
}

// GetBackendBytes generate terraform backend config.
func (b *BackendDo) GetBackendBytes(infraName, moduleName string) ([]byte, error) {
func (b *Backend) GetBackendBytes(infraName, moduleName string) ([]byte, error) {
f, err := b.GetBackendHCL(infraName, moduleName)
if err != nil {
return nil, err
Expand All @@ -49,7 +51,7 @@ func (b *BackendDo) GetBackendBytes(infraName, moduleName string) ([]byte, error
}

// GetBackendHCL generate terraform backend config.
func (b *BackendDo) GetBackendHCL(infraName, moduleName string) (*hclwrite.File, error) {
func (b *Backend) GetBackendHCL(infraName, moduleName string) (*hclwrite.File, error) {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
terraformBlock := rootBody.AppendNewBlock("terraform", []string{})
Expand All @@ -71,7 +73,7 @@ func (b *BackendDo) GetBackendHCL(infraName, moduleName string) (*hclwrite.File,
}

// GetRemoteStateHCL generate terraform remote state for this backend.
func (b *BackendDo) GetRemoteStateHCL(infraName, moduleName string) ([]byte, error) {
func (b *Backend) GetRemoteStateHCL(infraName, moduleName string) ([]byte, error) {
f := hclwrite.NewEmptyFile()

rootBody := f.Body()
Expand All @@ -98,3 +100,19 @@ func (b *BackendDo) GetRemoteStateHCL(infraName, moduleName string) ([]byte, err
}
return f.Bytes(), nil
}

func (b *Backend) LockState() error {
return fmt.Errorf("cdev state do not supported")
}

func (b *Backend) UnlockState() error {
return fmt.Errorf("cdev state do not supported")
}

func (b *Backend) WriteState(stateData string) error {
return fmt.Errorf("cdev state do not supported")
}

func (b *Backend) ReadState() (string, error) {
return "", fmt.Errorf("cdev state do not supported")
}
7 changes: 5 additions & 2 deletions pkg/backend/do/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
type Factory struct{}

// New creates the new do backend.
func (f *Factory) New(config []byte, name string) (project.Backend, error) {
bk := BackendDo{name: name}
func (f *Factory) New(config []byte, name string, p *project.Project) (project.Backend, error) {
bk := Backend{
name: name,
ProjectPtr: p,
}
state := map[string]interface{}{}
err := yaml.Unmarshal(config, &bk)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/backend/gcs/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
type Factory struct{}

// New creates the new backend.
func (f *Factory) New(config []byte, name string) (project.Backend, error) {
bk := BackendGCS{name: name}
func (f *Factory) New(config []byte, name string, p *project.Project) (project.Backend, error) {
bk := Backend{
name: name,
ProjectPtr: p,
}
err := yaml.Unmarshal(config, &bk)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 3622ab1

Please sign in to comment.