github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/aws_provider.go (about)

     1  package aws
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/sirupsen/logrus"
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/request"
     9  	"github.com/aws/aws-sdk-go/aws/session"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/aws/aws-sdk-go/service/s3"
    12  	"github.com/solo-io/unik/pkg/config"
    13  	"github.com/solo-io/unik/pkg/state"
    14  )
    15  
    16  func AwsStateFile() string {
    17  	return filepath.Join(config.Internal.UnikHome, "aws", "state.json")
    18  }
    19  
    20  type AwsProvider struct {
    21  	config config.Aws
    22  	state  state.State
    23  }
    24  
    25  func NewAwsProvier(config config.Aws) *AwsProvider {
    26  	logrus.Infof("state file: %s", AwsStateFile())
    27  	return &AwsProvider{
    28  		config: config,
    29  		state:  state.NewBasicState(AwsStateFile()),
    30  	}
    31  }
    32  
    33  func (p *AwsProvider) WithState(state state.State) *AwsProvider {
    34  	p.state = state
    35  	return p
    36  }
    37  
    38  func (p *AwsProvider) newEC2() *ec2.EC2 {
    39  	sess := session.New(&aws.Config{
    40  		Region: aws.String(p.config.Region),
    41  	})
    42  	sess.Handlers.Send.PushFront(func(r *request.Request) {
    43  		if r != nil {
    44  			logrus.WithFields(logrus.Fields{"params": r.Params}).Debugf("request sent to ec2")
    45  		}
    46  	})
    47  	return ec2.New(sess)
    48  }
    49  
    50  func (p *AwsProvider) newS3() *s3.S3 {
    51  	sess := session.New(&aws.Config{
    52  		Region: aws.String(p.config.Region),
    53  	})
    54  	sess.Handlers.Send.PushFront(func(r *request.Request) {
    55  		if r != nil {
    56  			logrus.WithFields(logrus.Fields{"params": r.Params}).Debugf("request sent to s3")
    57  		}
    58  	})
    59  	return s3.New(sess)
    60  }