Split API interface by required SDK func per command

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-04-27 10:04:07 +02:00 committed by Nicolas De loof
parent 3d8d982d4a
commit 4138dcfb5a
6 changed files with 36 additions and 28 deletions

View File

@ -98,7 +98,7 @@ func ConvertCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOpt
if err != nil { if err != nil {
return err return err
} }
template, err := client.Convert(project, opts.LoadBalancerArn()) template, err := client.Convert(project)
if err != nil { if err != nil {
return err return err
} }
@ -124,7 +124,7 @@ func UpCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions)
if err != nil { if err != nil {
return err return err
} }
return client.ComposeUp(project, opts.LoadBalancerArn()) return client.ComposeUp(project)
}), }),
} }
cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "") cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")

View File

@ -1,22 +1,7 @@
package amazon package amazon
import (
"github.com/awslabs/goformation/v4/cloudformation"
)
type API interface { type API interface {
ClusterExists(name string) (bool, error) downAPI
CreateCluster(name string) (string, error) upAPI
DeleteCluster(name string) error convertAPI
GetDefaultVPC() (string, error)
GetSubNets(vpcId string) ([]string, error)
ListRolesForPolicy(policy string) ([]string, error)
GetRoleArn(name string) (string, error)
StackExists(name string) (bool, error)
CreateStack(name string, template *cloudformation.Template) error
DescribeStackEvents(stack string) error
DeleteStack(name string) error
} }

View File

@ -2,9 +2,10 @@ package amazon
import ( import (
"fmt" "fmt"
"strings"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"strings"
ecsapi "github.com/aws/aws-sdk-go/service/ecs" ecsapi "github.com/aws/aws-sdk-go/service/ecs"
"github.com/awslabs/goformation/v4/cloudformation" "github.com/awslabs/goformation/v4/cloudformation"
@ -14,7 +15,7 @@ import (
"github.com/docker/ecs-plugin/pkg/convert" "github.com/docker/ecs-plugin/pkg/convert"
) )
func (c client) Convert(project *compose.Project, loadBalancerArn *string) (*cloudformation.Template, error) { func (c client) Convert(project *compose.Project) (*cloudformation.Template, error) {
template := cloudformation.NewTemplate() template := cloudformation.NewTemplate()
vpc, err := c.api.GetDefaultVPC() vpc, err := c.api.GetDefaultVPC()
if err != nil { if err != nil {
@ -113,3 +114,10 @@ func (c client) GetEcsTaskExecutionRole(spec types.ServiceConfig) (string, error
defaultTaskExecutionRole = arn defaultTaskExecutionRole = arn
return arn, nil return arn, nil
} }
type convertAPI interface {
GetDefaultVPC() (string, error)
GetSubNets(vpcId string) ([]string, error)
ListRolesForPolicy(policy string) ([]string, error)
GetRoleArn(name string) (string, error)
}

View File

@ -11,7 +11,6 @@ func (c *client) ComposeDown(projectName *string, keepLoadBalancer, deleteCluste
} }
fmt.Printf("Delete stack ") fmt.Printf("Delete stack ")
if !deleteCluster { if !deleteCluster {
return nil return nil
} }
@ -23,3 +22,8 @@ func (c *client) ComposeDown(projectName *string, keepLoadBalancer, deleteCluste
fmt.Printf("... done. \n") fmt.Printf("... done. \n")
return nil return nil
} }
type downAPI interface {
DeleteStack(name string) error
DeleteCluster(name string) error
}

View File

@ -1,10 +1,11 @@
package amazon package amazon
import ( import (
"testing"
"github.com/docker/ecs-plugin/pkg/amazon/mock" "github.com/docker/ecs-plugin/pkg/amazon/mock"
"github.com/docker/ecs-plugin/pkg/compose" "github.com/docker/ecs-plugin/pkg/compose"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"testing"
) )
func Test_down_dont_delete_cluster(t *testing.T) { func Test_down_dont_delete_cluster(t *testing.T) {
@ -22,7 +23,7 @@ func Test_down_dont_delete_cluster(t *testing.T) {
c.ComposeDown(&compose.Project{ c.ComposeDown(&compose.Project{
Name: "test_project", Name: "test_project",
}, false, false) }, false)
} }
func Test_down_delete_cluster(t *testing.T) { func Test_down_delete_cluster(t *testing.T) {
@ -41,5 +42,5 @@ func Test_down_delete_cluster(t *testing.T) {
c.ComposeDown(&compose.Project{ c.ComposeDown(&compose.Project{
Name: "test_project", Name: "test_project",
}, false, true) }, true)
} }

View File

@ -2,10 +2,12 @@ package amazon
import ( import (
"fmt" "fmt"
"github.com/awslabs/goformation/v4/cloudformation"
"github.com/docker/ecs-plugin/pkg/compose" "github.com/docker/ecs-plugin/pkg/compose"
) )
func (c *client) ComposeUp(project *compose.Project, loadBalancerArn *string) error { func (c *client) ComposeUp(project *compose.Project) error {
ok, err := c.api.ClusterExists(c.Cluster) ok, err := c.api.ClusterExists(c.Cluster)
if err != nil { if err != nil {
return err return err
@ -18,7 +20,7 @@ func (c *client) ComposeUp(project *compose.Project, loadBalancerArn *string) er
return fmt.Errorf("we do not (yet) support updating an existing CloudFormation stack") return fmt.Errorf("we do not (yet) support updating an existing CloudFormation stack")
} }
template, err := c.Convert(project, loadBalancerArn) template, err := c.Convert(project)
if err != nil { if err != nil {
return err return err
} }
@ -33,3 +35,11 @@ func (c *client) ComposeUp(project *compose.Project, loadBalancerArn *string) er
// TODO monitor progress // TODO monitor progress
return nil return nil
} }
type upAPI interface {
ClusterExists(name string) (bool, error)
CreateCluster(name string) (string, error)
StackExists(name string) (bool, error)
CreateStack(name string, template *cloudformation.Template) error
DescribeStackEvents(stack string) error
}