Skip to content

Commit

Permalink
feat: reworked api to make a little more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielChalk committed May 2, 2023
1 parent af18e68 commit 53e5c78
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 162 deletions.
10 changes: 5 additions & 5 deletions cmd/twrapper/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/adaptavist/terraform-wrapper/v1/pkg/aws"
terraformConfig "github.com/adaptavist/terraform-wrapper/v1/pkg/terraform/config"
"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform/config/model"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -61,8 +61,8 @@ func (c Config) getBackendKey() (key string, err error) {
return
}

func (c Config) Backend() (backend *terraformConfig.Backend) {
backend = terraformConfig.NewBackend(c.BackendType, c.BackendConfig)
func (c Config) Backend() (backend *model.Backend) {
backend = model.NewBackend(c.BackendType, c.BackendConfig)

if backend.IsEmpty() {
return backend
Expand All @@ -84,7 +84,7 @@ func (c Config) CloudIsset() bool {
return c.CloudConfig["organization"] != nil || c.CloudConfig["workspace"] != nil
}

func (c Config) Cloud() (cloud *terraformConfig.Cloud, err error) {
func (c Config) Cloud() (cloud *model.Cloud, err error) {
organizations, org_ok := c.CloudConfig["organization"]
workspace, space_ok := c.CloudConfig["workspace"]

Expand All @@ -98,7 +98,7 @@ func (c Config) Cloud() (cloud *terraformConfig.Cloud, err error) {
return
}

cloud = terraformConfig.NewCloud(organizations.(string), workspace.(string))
cloud = model.NewCloud(organizations.(string), workspace.(string))
return
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/twrapper/cmd/terraform_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"

"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform"
"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform/runner"
)

func runTerraform(args []string) {
Expand All @@ -17,14 +17,14 @@ func runTerraform(args []string) {
fatalIfNotNil(err, "failed to load config: %s")

// start configuration terraform workspace
opts := terraform.NewOpts()
opts.WithArguments(args)
terraform := runner.New()
terraform.WithArguments(args)

// If we have a backend configuration
backend := config.Backend()

if !backend.IsEmpty() {
opts.WithBackend(backend)
terraform.WithBackend(backend)
} else {
fmt.Println("no backend configuration found")
}
Expand All @@ -34,24 +34,24 @@ func runTerraform(args []string) {
fmt.Println("setting terraform cloud")
cloud, err := config.Cloud()
fatalIfNotNil(err, "failed to load cloud config")
opts.WithCloud(cloud)
terraform.WithCloud(cloud)
} else {
fmt.Println("no cloud configuration found")
}

// Get terraform configured for AWS
if !config.AWS.IsEmpty() {
fmt.Println("setting up for aws")
err = config.AWS.Configure(&opts)
err = config.AWS.Configure(&terraform)
fatalIfNotNil(err, "failed to configure Terraform for aws: %s")
}

err = terraform.Configure(opts)
err = terraform.Configure()
fatalIfNotNil(err, "%s")

fmt.Println("running terraform")
fmt.Println("-------------------------------------------------------------")
err = terraform.Execute(opts)
err = terraform.Go()

fatalIfNotNil(err, "%s")
}
37 changes: 0 additions & 37 deletions cmd/twrapper/cmd/terraform_run_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"reflect"

"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform"
"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform/runner"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
Expand All @@ -24,7 +24,7 @@ func (c ConfigAWS) IsEmpty() bool {
}

// Detect for an available role and set it as a variable ready for a Terraform provider.
func (c ConfigAWS) Configure(opts *terraform.Opts) (err error) {
func (c ConfigAWS) Configure(opts *runner.Runner) (err error) {
if c.RoleTFVar == "" {
return errors.New("we don't know what terraform variable we need to set for the role ARN as aws.tf_role_arn is empty")
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/aws/main_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package aws

import (
"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform"
"github.com/stretchr/testify/assert"
"testing"

"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform/runner"
"github.com/stretchr/testify/assert"
)

func TestFixedRoleIsConfigured(t *testing.T) {
Expand All @@ -12,8 +13,8 @@ func TestFixedRoleIsConfigured(t *testing.T) {
RoleARN: roleARN,
RoleTFVar: "role_arn",
}
opts := terraform.NewOpts()
err := awsConfig.Configure(&opts)
terraform := runner.New()
err := awsConfig.Configure(&terraform)
assert.Nil(t, err, "configured should return nil error")
assert.Equal(t, roleARN, opts.Variables["role_arn"], "RoleARN should have made it to terraform as a var")
assert.Equal(t, roleARN, terraform.Variables["role_arn"], "RoleARN should have made it to terraform as a var")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

type HCLEntity interface {
// Returns entity suitable for mashalling to HCL json files.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package config
package model

type Root struct {
Terraform Terraform `json:"terraform"`
}

func New() *Root {
return &Root{
Terraform: Terraform{},
}
}

func (r Root) ToHCL() (interface{}, error) {
terraform, err := r.Terraform.ToHCL()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package model

import "fmt"

Expand Down
55 changes: 0 additions & 55 deletions pkg/terraform/opts.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package terraform
package runner

type Arguments []string

Expand Down
88 changes: 88 additions & 0 deletions pkg/terraform/runner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package runner

import (
"fmt"

"github.com/adaptavist/terraform-wrapper/v1/pkg/terraform/config/model"
)

type Runner struct {
Config *model.Root // Terraform configuration E.G Backends
Arguments Arguments // Tarraform arguments E.G plan, apply
Variables Variables // Terraform variables to be places into terraform.tfvars.json
}

// New instance of runner
func New() Runner {
return Runner{
Config: model.New(),
Variables: Variables{},
Arguments: Arguments{},
}
}

// WithBackend adds backend configuration to the runner
func (o *Runner) WithBackend(b *model.Backend) *Runner {
o.Config.Terraform.Backend = b
return o
}

// WithCloud adds Terraform Cloud configuration to the runner
func (o *Runner) WithCloud(c *model.Cloud) *Runner {
o.Config.Terraform.Cloud = c
return o
}

// WithVariables adds terraform variables
func (o *Runner) WithVariables(v Variables) *Runner {
o.Variables = v
return o
}

// WithArguments adds terraform CLI arguments to the runner
func (o *Runner) WithArguments(a Arguments) *Runner {
o.Arguments = a
return o
}

// writeTerraformFile creates terraform configuration file
func (o *Runner) writeTerraformFile() error {
data, err := o.Config.ToHCL()

if err != nil {
return err
}

return writeJSONFile("terraform.tf.json", data)
}

// writeVarFile creates terraform variables file
func (o *Runner) writeVarFile() error {
if len(o.Variables) > 0 {
return writeJSONFile("terraform.tfvars.json", o.Variables)
}
return nil
}

// Configure writes terraform files
func (o *Runner) Configure() (err error) {
if !o.Config.Terraform.Backend.IsEmpty() {
fmt.Println("writing backend")
if err = o.writeTerraformFile(); err != nil {
return
}
}

err = o.writeVarFile()
return
}

// Init terraform
func (r *Runner) Init() error {
return run("terraform", "init")
}

// Go runs terraform with args
func (r *Runner) Go() error {
return run("terraform", r.Arguments...)
}
Loading

0 comments on commit 53e5c78

Please sign in to comment.