Skip to content

Commit

Permalink
refactor api setup
Browse files Browse the repository at this point in the history
Hopefully for better understanding and easier testing.
  • Loading branch information
ianic committed Sep 26, 2021
1 parent b729a59 commit 5caefd6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 42 deletions.
151 changes: 110 additions & 41 deletions api/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,145 @@ import (
"github.com/mantil-io/mantil/terraform"
)

type Setup struct{}
type Setup struct {
tf *terraform.Terraform
req *dto.SetupRequest
awsClient *aws.AWS
bucketName string
}

func New() *Setup {
return &Setup{}
}

func (f *Setup) Invoke(ctx context.Context, req *dto.SetupRequest) (*dto.SetupResponse, error) {
tf, err := terraform.New("mantil-setup")
if err != nil {
func (s *Setup) Invoke(ctx context.Context, req *dto.SetupRequest) (*dto.SetupResponse, error) {
if err := s.init(req); err != nil {
return nil, err
}
defer tf.Cleanup()
rc := &config.RuntimeConfig{
// TODO: sto ce mi ovaj version kada se nigdje ne koristi
Version: req.Version,
FunctionsBucket: req.FunctionsBucket,
FunctionsPath: req.FunctionsPath,
defer s.cleanup()

if req.Destroy {
return nil, s.destroy()
}
return setup(tf, rc, req.PublicKey, req.Destroy)

if err := s.create(); err != nil {
return nil, err
}
return s.readTerraformOutput()
}

func setup(tf *terraform.Terraform, rc *config.RuntimeConfig, publicKey string, destroy bool) (*dto.SetupResponse, error) {
assets.StartServer()
func (s *Setup) destroy() error {
if err := s.terraformApply(); err != nil {
return err
}
if err := s.deleteBucket(); err != nil {
return err
}
return nil
}

func (s *Setup) create() error {
if err := s.createBucket(); err != nil {
return err
}
if err := s.terraformApply(); err != nil {
return err
}
if err := s.saveConfig(); err != nil {
return err
}
return nil
}

func (s *Setup) init(req *dto.SetupRequest) error {
tf, err := terraform.New("mantil-setup")
if err != nil {
return err
}
awsClient, err := aws.New()
if err != nil {
return nil, fmt.Errorf("error initializing AWS client - %v", err)
return fmt.Errorf("error initializing AWS client - %w", err)
}
bucketName, err := config.Bucket(awsClient)
if err != nil {
return nil, fmt.Errorf("error creating bucket - %v", bucketName)
return err
}
bucketExists, err := awsClient.S3BucketExists(bucketName)
if err != nil {
return nil, fmt.Errorf("error checking if bucket exists - %v", err)
s.tf = tf
s.awsClient = awsClient
s.req = req
s.bucketName = bucketName
return nil
}

func (s *Setup) saveConfig() error {
return config.SaveRuntimeConfig(s.awsClient, &config.RuntimeConfig{
// TODO: sto ce mi ovaj version kada se nigdje ne koristi
Version: s.req.Version,
FunctionsBucket: s.req.FunctionsBucket,
FunctionsPath: s.req.FunctionsPath,
})
}

func (s *Setup) cleanup() {
if s.tf != nil {
s.tf.Cleanup()
}
if !bucketExists {
if err := awsClient.CreateS3Bucket(bucketName, awsClient.Region()); err != nil {
return nil, fmt.Errorf("error creating terraform bucket - %v", err)
}
}

func (s *Setup) terraformApply() error {
assets.StartServer()
data := terraform.SetupTemplateData{
Bucket: s.bucketName,
BucketPrefix: config.SetupBucketPrefix,
FunctionsBucket: s.req.FunctionsBucket,
FunctionsPath: s.req.FunctionsPath,
Region: s.awsClient.Region(),
PublicKey: s.req.PublicKey,
}
if err := tf.RenderSetupTemplate(bucketName, rc, publicKey, awsClient); err != nil {
return nil, err
if err := s.tf.RenderSetupTemplate(data); err != nil {
return err
}
if err := tf.Apply(destroy); err != nil {
return nil, err
if err := s.tf.Apply(s.req.Destroy); err != nil {
return err
}
if destroy {
if err := awsClient.EmptyS3Bucket(bucketName); err != nil {
return nil, fmt.Errorf("error emptying terraform bucket - %v", err)
}
if err := awsClient.DeleteS3Bucket(bucketName); err != nil {
return nil, fmt.Errorf("error deleting terraform bucket - %v", err)
}
} else {
if err := config.SaveRuntimeConfig(awsClient, rc); err != nil {
return nil, fmt.Errorf("error saving mantil version")
}
}
restURL, err := tf.Output("url", true)
return nil
}

func (s *Setup) readTerraformOutput() (*dto.SetupResponse, error) {
restURL, err := s.tf.Output("url", true)
if err != nil {
return nil, fmt.Errorf("error reading api gateway rest url - %v", err)
}
wsURL, err := tf.Output("ws_url", true)
wsURL, err := s.tf.Output("ws_url", true)
if err != nil {
return nil, fmt.Errorf("error reading api gateway ws url - %v", err)
}

return &dto.SetupResponse{
APIGatewayRestURL: restURL,
APIGatewayWsURL: wsURL,
}, nil
}

func (s *Setup) deleteBucket() error {
if err := s.awsClient.EmptyS3Bucket(s.bucketName); err != nil {
return fmt.Errorf("error emptying bucket %s - %w", s.bucketName, err)
}
if err := s.awsClient.DeleteS3Bucket(s.bucketName); err != nil {
return fmt.Errorf("error deleting bucket %s - %w", s.bucketName, err)
}
return nil
}

func (s *Setup) createBucket() error {
exists, err := s.awsClient.S3BucketExists(s.bucketName)
if err != nil {
return fmt.Errorf("error checking if bucket %s exists - %w", s.bucketName, err)
}
if exists {
return nil
}
if err := s.awsClient.CreateS3Bucket(s.bucketName, s.awsClient.Region()); err != nil {
return fmt.Errorf("error creating bucket %s - %w", s.bucketName, err)
}
return nil
}
2 changes: 1 addition & 1 deletion aws/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (a *AWS) AccountID() (string, error) {
gcio, err := a.stsClient.GetCallerIdentity(context.Background(), &sts.GetCallerIdentityInput{})
if err != nil {
return "", fmt.Errorf("could not get account ID - %v", err)
return "", fmt.Errorf("could not get account ID - %w", err)
}
return aws.ToString(gcio.Account), nil
}
Expand Down

0 comments on commit 5caefd6

Please sign in to comment.