Skip to content

Commit

Permalink
sdk: Rename aws session related functions for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Jan 24, 2021
1 parent 73cfa9a commit acd5b28
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/resource/cluster/aws_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func AWSSessionFromCluster(cluster *Cluster) *session.Session {
sess, _ := sdk.AWSCredsFromConfig(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig)
sess, _ := sdk.AWSCredsFromValues(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig)

return sess
}
2 changes: 1 addition & 1 deletion pkg/resource/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func mustNewContext(cluster *Cluster) *sdk.Context {
sess, creds := sdk.AWSCredsFromConfig(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig)
sess, creds := sdk.AWSCredsFromValues(cluster.Region, cluster.Profile, cluster.AssumeRoleConfig)

return &sdk.Context{Sess: sess, Creds: creds}
}
2 changes: 1 addition & 1 deletion pkg/resource/iamserviceaccount/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func mustContext(a *IAMServiceAccount) *sdk.Context {
sess, creds := sdk.AWSCredsFromConfig(a.Region, a.Profile, a.AssumeRoleConfig)
sess, creds := sdk.AWSCredsFromValues(a.Region, a.Profile, a.AssumeRoleConfig)

return &sdk.Context{Sess: sess, Creds: creds}
}
6 changes: 5 additions & 1 deletion pkg/sdk/assume_role_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"github.com/aws/aws-sdk-go/service/sts"
)

func AWSCredsFromConfig(region, profile string, assumeRole *AssumeRoleConfig) (*session.Session, *sts.Credentials) {
func AWSCredsFromConfig(conf *Config) (*session.Session, *sts.Credentials) {
return AWSCredsFromValues(conf.Region, conf.Profile, conf.AssumeRole)
}

func AWSCredsFromValues(region, profile string, assumeRole *AssumeRoleConfig) (*session.Session, *sts.Credentials) {
sess := NewSession(region, profile)

if assumeRole == nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/sdk/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sdk

type Config struct {
Region string
Profile string
AssumeRole *AssumeRoleConfig
}

17 changes: 11 additions & 6 deletions pkg/sdk/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

type Job struct {
ContextConfigFunc func() (string, string, *AssumeRoleConfig)
Conf *Config
}

func NewJob(f func() (string, string, *AssumeRoleConfig)) *Job {
return &Job{ContextConfigFunc: f}
func NewJob(conf *Config) *Job {
return &Job{Conf: conf}
}

func (s *Job) Task(name string, f func(*Context) error) (err error) {
Expand All @@ -35,9 +35,14 @@ func (s *Job) Task(name string, f func(*Context) error) (err error) {
}

func (s *Job) newContext() *Context {
region, profile, assumeRoleConfig := s.ContextConfigFunc()
return ContextConfig(s.Conf)
}

sess, creds := AWSCredsFromConfig(region, profile, assumeRoleConfig)
func ContextConfig(conf *Config) *Context {
sess, creds := AWSCredsFromConfig(conf)

return &Context{Sess: sess, Creds: creds}
return &Context{
Sess: sess,
Creds: creds,
}
}
4 changes: 2 additions & 2 deletions pkg/sdk/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func Run(cmd *exec.Cmd) (*CommandResult, error) {
// so that helmfile could return its own exit code accordingly
waitStatus := ee.Sys().(syscall.WaitStatus)
exitStatus = waitStatus.ExitStatus()
if exitStatus != 0 {
return nil, fmt.Errorf("running %q: %v\n%s", cmdToLog, runErr, out)
if exitStatus != 2 {
return nil, fmt.Errorf("%s: %v\n%s", cmd.Path, runErr, out)
}
default:
return nil, fmt.Errorf("running %q: %v\n%s", cmdToLog, runErr, out)
Expand Down
6 changes: 4 additions & 2 deletions pkg/sdk/tfsdk/assume_role_config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"log"
)

func GetAssumeRoleConfig(d api.Getter) (config *sdk.AssumeRoleConfig) {
if l, ok := d.Get(KeyAssumeRole).([]interface{}); ok && len(l) > 0 && l[0] != nil {
func GetAssumeRoleConfig(d api.Getter, opts ...SchemaOption) (config *sdk.AssumeRoleConfig) {
sc := CreateSchema(opts...)

if l, ok := d.Get(sc.KeyAWSAssumeRole).([]interface{}); ok && len(l) > 0 && l[0] != nil {
config = &sdk.AssumeRoleConfig{}

m := l[0].(map[string]interface{})
Expand Down
18 changes: 18 additions & 0 deletions pkg/sdk/tfsdk/aws_env_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tfsdk

import (
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
)

func ConfigFromResourceData(d api.Getter, opts ...SchemaOption) *sdk.Config {
region, profile := GetAWSRegionAndProfile(d, opts...)

assumeRoleConfig := GetAssumeRoleConfig(d, opts...)

return &sdk.Config{
Region: region,
Profile: profile,
AssumeRole: assumeRoleConfig,
}
}
8 changes: 5 additions & 3 deletions pkg/sdk/tfsdk/aws_region_profile_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package tfsdk

import "github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"

func GetAWSRegionAndProfile(d api.Getter) (string, string) {
func GetAWSRegionAndProfile(d api.Getter, opts ...SchemaOption) (string, string) {
schema := CreateSchema(opts...)

var region string

if v := d.Get(KeyRegion); v != nil {
if v := d.Get(schema.KeyAWSRegion); v != nil {
region = v.(string)
}

var profile string

if v := d.Get(KeyProfile); v != nil {
if v := d.Get(schema.KeyAWSProfile); v != nil {
profile = v.(string)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/tfsdk/aws_session_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/api"
)

func AWSSessionFromResourceData(d api.Getter) *session.Session {
region, profile := GetAWSRegionAndProfile(d)
func AWSSessionFromResourceData(d api.Getter, opts ...SchemaOption) *session.Session {
region, profile := GetAWSRegionAndProfile(d, opts...)

sess := sdk.NewSession(region, profile)

assumeRoleConfig := GetAssumeRoleConfig(d)
assumeRoleConfig := GetAssumeRoleConfig(d, opts...)
if assumeRoleConfig == nil {
return sess
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/sdk/tfsdk/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tfsdk

type Schema struct {
KeyAWSRegion string
KeyAWSProfile string
KeyAWSAssumeRole string
}

func defaultSchema() *Schema {
return &Schema{
KeyAWSRegion: KeyRegion,
KeyAWSProfile: KeyProfile,
KeyAWSAssumeRole: KeyAssumeRole,
}
}

type SchemaOption interface {
Apply(*Schema)
}

func (s *Schema) Apply(other *Schema) {
*other = *s
}

type schemaOptionFunc struct {
f func(*Schema)
}

func (f *schemaOptionFunc) Apply(schema *Schema) {
f.f(schema)
}

func SchemaOptionFunc(f func(*Schema)) SchemaOption {
return &schemaOptionFunc{
f: f,
}
}

func SchemaOptionAWSRegionKey(k string) SchemaOption {
return SchemaOptionFunc(func(schema *Schema) {
schema.KeyAWSRegion = k
})
}

func SchemaOptionAWSProfileKey(k string) SchemaOption {
return SchemaOptionFunc(func(schema *Schema) {
schema.KeyAWSProfile = k
})
}

func SchemaOptionAWSAssumeRole(k string) SchemaOption {
return SchemaOptionFunc(func(schema *Schema) {
schema.KeyAWSAssumeRole = k
})
}

func CreateSchema(opts ...SchemaOption) *Schema {
schema := defaultSchema()

for _, o := range opts {
o.Apply(schema)
}

return schema
}

0 comments on commit acd5b28

Please sign in to comment.