Skip to content

Commit

Permalink
refactor backend api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Sep 29, 2021
1 parent 2cef619 commit 2d11361
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 56 deletions.
67 changes: 49 additions & 18 deletions api/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,78 @@ import (
"fmt"

"github.com/mantil-io/mantil/aws"
"github.com/mantil-io/mantil/backend/api/destroy"
"github.com/mantil-io/mantil/config"
"github.com/mantil-io/mantil/terraform"
)

type Destroy struct{}

type DestroyRequest struct {
ProjectName string
StageName string
}

type DestroyResponse struct {
type DestroyResponse struct{}

type Destroy struct {
req *DestroyRequest
stage *config.Stage
bucketName string
region string
}

func (d *Destroy) Invoke(ctx context.Context, req *DestroyRequest) (*DestroyResponse, error) {
return d.Destroy(ctx, req)
func New() *Destroy {
return &Destroy{}
}

func (f *Destroy) Destroy(ctx context.Context, req *DestroyRequest) (*DestroyResponse, error) {
if req.ProjectName == "" || req.StageName == "" {
return nil, fmt.Errorf("bad request")
func (d *Destroy) Invoke(ctx context.Context, req *DestroyRequest) (*DestroyResponse, error) {
if err := d.init(req); err != nil {
return nil, err
}
return d.destroy()
}

func (d *Destroy) init(req *DestroyRequest) error {
stage, err := config.LoadDeploymentState(req.ProjectName, req.StageName)
if err != nil {
return nil, err
return err
}
awsClient, err := aws.New()
if err != nil {
return nil, err
return err
}
rc, err := config.LoadRuntimeConfig(awsClient)
bucketName, err := config.Bucket(awsClient)
if err != nil {
return nil, err
return err
}
err = destroy.Destroy(req.ProjectName, stage, awsClient, rc)
if err != nil {
return nil, err
d.req = req
d.stage = stage
d.bucketName = bucketName
d.region = awsClient.Region()
return nil
}

func (d *Destroy) destroy() (*DestroyResponse, error) {
if err := d.terraformDestroy(); err != nil {
return nil, fmt.Errorf("could not terraform destroy - %w", err)
}
if err := config.DeleteDeploymentState(d.req.ProjectName, d.req.StageName); err != nil {
return nil, fmt.Errorf("could not delete stage %s - %w", d.req.StageName, err)
}
return &DestroyResponse{}, nil
}

func New() *Destroy {
return &Destroy{}
func (d *Destroy) terraformDestroy() error {
tf, err := terraform.Project(d.terraformProjectTemplateData())
if err != nil {
return err
}
return tf.Destroy()
}

func (d *Destroy) terraformProjectTemplateData() terraform.ProjectTemplateData {
return terraform.ProjectTemplateData{
Name: d.req.ProjectName,
Bucket: d.bucketName,
BucketPrefix: config.DeploymentBucketPrefix(d.req.ProjectName, d.req.StageName),
Region: d.region,
}
}
27 changes: 27 additions & 0 deletions api/destroy/destroy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package destroy

import (
"testing"

"github.com/mantil-io/mantil/config"
"github.com/stretchr/testify/assert"
)

func TestTerraformProjectTemplateData(t *testing.T) {
d := &Destroy{
req: &DestroyRequest{
ProjectName: "test-project",
StageName: "test-stage",
},
stage: &config.Stage{
Name: "test-stage",
},
bucketName: "bucket",
region: "region,",
}
data := d.terraformProjectTemplateData()
assert.NotEmpty(t, data.Name)
assert.NotEmpty(t, data.Bucket)
assert.NotEmpty(t, data.BucketPrefix)
assert.NotEmpty(t, data.Region)
}
38 changes: 0 additions & 38 deletions backend/api/destroy/destroy.go

This file was deleted.

0 comments on commit 2d11361

Please sign in to comment.