github.com/jenkins-x/jx/v2@v2.1.155/pkg/cloud/amazon/provider.go (about)

     1  package amazon
     2  
     3  import (
     4  	"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/awscli"
     5  	"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/ec2"
     6  	"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/eks"
     7  	"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/eksctl"
     8  	"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/session"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // Provider provides an interface to access AWS services with supported actions
    13  type Provider interface {
    14  	EKS() eks.EKSer
    15  	EC2() ec2.EC2er
    16  	EKSCtl() eksctl.EKSCtl
    17  	AWSCli() awscli.AWS
    18  }
    19  
    20  type clusterProvider struct {
    21  	providerServices
    22  	Region  string
    23  	Profile string
    24  }
    25  
    26  type providerServices struct {
    27  	cli    awscli.AWS
    28  	eks    eks.EKSer
    29  	ec2    ec2.EC2er
    30  	eksctl eksctl.EKSCtl
    31  }
    32  
    33  // EKS returns an initialized instance of eks.EKSer
    34  func (p providerServices) EKS() eks.EKSer {
    35  	return p.eks
    36  }
    37  
    38  // EC2 returns an initialized instance of ec2.EC2er
    39  func (p providerServices) EC2() ec2.EC2er {
    40  	return p.ec2
    41  }
    42  
    43  // EKSCtl returns an abstraction of the eksctl CLI
    44  func (p providerServices) EKSCtl() eksctl.EKSCtl {
    45  	return p.eksctl
    46  }
    47  
    48  // AWSCli returns an abstraction of the AWS CLI
    49  func (p providerServices) AWSCli() awscli.AWS {
    50  	return p.cli
    51  }
    52  
    53  // NewProvider returns a Provider implementation configured with a session and implementations for AWS services
    54  func NewProvider(region string, profile string) (*clusterProvider, error) {
    55  	session, err := session.NewAwsSession(profile, region)
    56  	if err != nil {
    57  		return nil, errors.Wrap(err, "error obtaining a valid AWS session")
    58  	}
    59  
    60  	services := providerServices{}
    61  
    62  	ec2Options, err := ec2.NewEC2APIHandler(session)
    63  	if err != nil {
    64  		return nil, errors.Wrap(err, "error initializing the EC2 API")
    65  	}
    66  	services.ec2 = ec2Options
    67  
    68  	eksOptions, err := eks.NewEKSAPIHandler(session)
    69  	if err != nil {
    70  		return nil, errors.Wrap(err, "error initializing the EKS API")
    71  	}
    72  	services.eks = eksOptions
    73  
    74  	services.eksctl = eksctl.NewEksctlClient()
    75  
    76  	services.cli = awscli.NewAWSCli()
    77  
    78  	provider := &clusterProvider{
    79  		providerServices: services,
    80  		Region:           region,
    81  		Profile:          profile,
    82  	}
    83  	return provider, nil
    84  }