Check context created by `context` command

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-07-10 09:22:38 +02:00
parent 1783716a6a
commit 794ea3cc24
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
7 changed files with 60 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package commands
import (
"context"
"fmt"
"os"
"path/filepath"
@ -10,6 +11,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
contextStore "github.com/docker/ecs-plugin/pkg/docker"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
@ -53,6 +55,14 @@ func SetupCommand() *cobra.Command {
return err
}
}
backend, err := amazon.NewBackend(opts.context.Profile, opts.context.Cluster, opts.context.Region)
if err != nil {
return err
}
_, _, err = backend.CreateContextData(context.Background(), nil)
if err != nil {
return err
}
return contextStore.NewContext(opts.name, &opts.context)
},
}
@ -206,6 +216,7 @@ func setCluster(opts *setupOptions, err error) error {
if err != nil {
return err
}
opts.context.Cluster = result
return nil
}

View File

@ -1,32 +0,0 @@
package commands
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/docker/cli/cli/config"
"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
"gotest.tools/v3/golden"
)
func TestDefaultAwsContextName(t *testing.T) {
dir := fs.NewDir(t, "setup")
defer dir.Remove()
cmd := NewRootCmd(nil)
dockerConfig := config.Dir()
config.SetDir(dir.Path())
defer config.SetDir(dockerConfig)
cmd.SetArgs([]string{"setup", "--cluster", "clusterName", "--profile", "profileName", "--region", "regionName"})
err := cmd.Execute()
assert.NilError(t, err)
files, err := filepath.Glob(dir.Join("contexts", "meta", "*", "meta.json"))
assert.NilError(t, err)
assert.Equal(t, len(files), 1)
b, err := ioutil.ReadFile(files[0])
assert.NilError(t, err)
golden.Assert(t, string(b), "context.golden")
}

View File

@ -1 +0,0 @@
{"Name":"aws","Metadata":{"Type":"aws"},"Endpoints":{"aws":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"},"docker":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"}}}

View File

@ -0,0 +1,24 @@
package backend
import (
"context"
"fmt"
)
func (b *Backend) CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error) {
err = b.api.CheckRequirements(ctx)
if err != nil {
return "", "", err
}
if b.Cluster != "" {
exists, err := b.api.ClusterExists(ctx, b.Cluster)
if err != nil {
return "", "", err
}
if !exists {
return "", "", fmt.Errorf("cluster %s does not exists", b.Cluster)
}
}
return "", "", nil
}

View File

@ -9,6 +9,8 @@ import (
)
type API interface {
CheckRequirements(ctx context.Context) error
GetDefaultVPC(ctx context.Context) (string, error)
VpcExists(ctx context.Context, vpcID string) (bool, error)
GetSubNets(ctx context.Context, vpcID string) ([]string, error)

View File

@ -2,15 +2,13 @@ package sdk
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/docker/ecs-plugin/internal"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
@ -27,6 +25,7 @@ import (
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface"
cf "github.com/awslabs/goformation/v4/cloudformation"
"github.com/docker/ecs-plugin/internal"
"github.com/docker/ecs-plugin/pkg/compose"
"github.com/sirupsen/logrus"
)
@ -57,8 +56,23 @@ func NewAPI(sess *session.Session) API {
}
}
func (s sdk) CheckRequirements(ctx context.Context) error {
settings, err := s.ECS.ListAccountSettingsWithContext(ctx, &ecs.ListAccountSettingsInput{
EffectiveSettings: aws.Bool(true),
Name: aws.String("serviceLongArnFormat"),
})
if err != nil {
return err
}
serviceLongArnFormat := settings.Settings[0].Value
if *serviceLongArnFormat != "enabled" {
return errors.New("this tool requires the \"new ARN resource ID format\"")
}
return nil
}
func (s sdk) ClusterExists(ctx context.Context, name string) (bool, error) {
logrus.Debug("Check if cluster was already created: ", name)
logrus.Debug("CheckRequirements if cluster was already created: ", name)
clusters, err := s.ECS.DescribeClustersWithContext(ctx, &ecs.DescribeClustersInput{
Clusters: []*string{aws.String(name)},
})
@ -78,7 +92,7 @@ func (s sdk) CreateCluster(ctx context.Context, name string) (string, error) {
}
func (s sdk) VpcExists(ctx context.Context, vpcID string) (bool, error) {
logrus.Debug("Check if VPC exists: ", vpcID)
logrus.Debug("CheckRequirements if VPC exists: ", vpcID)
_, err := s.EC2.DescribeVpcsWithContext(ctx, &ec2.DescribeVpcsInput{VpcIds: []*string{&vpcID}})
return err == nil, err
}
@ -421,7 +435,7 @@ func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) (map[string
}
func (s sdk) LoadBalancerExists(ctx context.Context, arn string) (bool, error) {
logrus.Debug("Check if LoadBalancer exists: ", arn)
logrus.Debug("CheckRequirements if LoadBalancer exists: ", arn)
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: []*string{aws.String(arn)},
})

View File

@ -12,6 +12,8 @@ type API interface {
Up(ctx context.Context, options cli.ProjectOptions) error
Down(ctx context.Context, options cli.ProjectOptions) error
CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error)
Convert(project *types.Project) (*cloudformation.Template, error)
Logs(ctx context.Context, projectName cli.ProjectOptions) error
Ps(background context.Context, options cli.ProjectOptions) ([]ServiceStatus, error)