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