From 91427d0492a94af23f9733f014fefcc6d9296d92 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 18 Aug 2020 08:58:37 +0200 Subject: [PATCH] introduce `Convert` in compose API Signed-off-by: Nicolas De Loof --- aci/backend.go | 4 ++++ compose/api.go | 2 ++ ecs/cloudformation.go | 18 ++++++++++++++++-- ecs/convert.go | 2 +- ecs/up.go | 2 +- example/backend.go | 4 ++++ 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/aci/backend.go b/aci/backend.go index fc5fe758d..77879dfe6 100644 --- a/aci/backend.go +++ b/aci/backend.go @@ -451,6 +451,10 @@ func (cs *aciComposeService) Logs(ctx context.Context, opts *cli.ProjectOptions, return errdefs.ErrNotImplemented } +func Convert(ctx context.Context, opts *cli.ProjectOptions) ([]byte, error) { + return nil, errdefs.ErrNotImplemented +} + type aciCloudService struct { loginService login.AzureLoginServiceAPI } diff --git a/compose/api.go b/compose/api.go index d80050f0d..ec055e7e6 100644 --- a/compose/api.go +++ b/compose/api.go @@ -33,6 +33,8 @@ type Service interface { Logs(ctx context.Context, opts *cli.ProjectOptions, w io.Writer) error // Ps executes the equivalent to a `compose ps` Ps(ctx context.Context, opts *cli.ProjectOptions) ([]ServiceStatus, error) + // Convert translate compose model into backend's native format + Convert(ctx context.Context, opts *cli.ProjectOptions) ([]byte, error) } diff --git a/ecs/cloudformation.go b/ecs/cloudformation.go index dda69f00c..85f40a6a3 100644 --- a/ecs/cloudformation.go +++ b/ecs/cloudformation.go @@ -1,7 +1,9 @@ package ecs import ( + "context" "fmt" + "github.com/compose-spec/compose-go/cli" "github.com/docker/api/compose" "io/ioutil" "regexp" @@ -33,8 +35,20 @@ const ( ParameterLoadBalancerARN = "ParameterLoadBalancerARN" ) +func (b *ecsAPIService) Convert(ctx context.Context, opts *cli.ProjectOptions) ([]byte, error) { + project, err := cli.ProjectFromOptions(opts) + if err != nil { + return nil, err + } + template, err := b.convert(project) + if err != nil { + return nil, err + } + return Marshall(template) +} + // Convert a compose project into a CloudFormation template -func (b ecsAPIService) Convert(project *types.Project) (*cloudformation.Template, error) { +func (b *ecsAPIService) convert(project *types.Project) (*cloudformation.Template, error) { var checker compatibility.Checker = &FargateCompatibilityChecker{ compatibility.AllowList{ Supported: compatibleComposeAttributes, @@ -128,7 +142,7 @@ func (b ecsAPIService) Convert(project *types.Project) (*cloudformation.Template for _, service := range project.Services { - definition, err := Convert(project, service) + definition, err := convert(project, service) if err != nil { return nil, err } diff --git a/ecs/convert.go b/ecs/convert.go index 62eca65a4..17db60648 100644 --- a/ecs/convert.go +++ b/ecs/convert.go @@ -23,7 +23,7 @@ import ( const secretsInitContainerImage = "docker/ecs-secrets-sidecar" -func Convert(project *types.Project, service types.ServiceConfig) (*ecs.TaskDefinition, error) { +func convert(project *types.Project, service types.ServiceConfig) (*ecs.TaskDefinition, error) { cpu, mem, err := toLimits(service) if err != nil { return nil, err diff --git a/ecs/up.go b/ecs/up.go index dc36ecde7..44d16227b 100644 --- a/ecs/up.go +++ b/ecs/up.go @@ -27,7 +27,7 @@ func (b *ecsAPIService) Up(ctx context.Context, options *cli.ProjectOptions) err return err } - template, err := b.Convert(project) + template, err := b.convert(project) if err != nil { return err } diff --git a/example/backend.go b/example/backend.go index c9a10c624..3206926ea 100644 --- a/example/backend.go +++ b/example/backend.go @@ -142,3 +142,7 @@ func (cs *composeService) Ps(ctx context.Context, opts *cli.ProjectOptions) ([]c func (cs *composeService) Logs(ctx context.Context, opts *cli.ProjectOptions, w io.Writer) error { return errdefs.ErrNotImplemented } + +func Convert(ctx context.Context, opts *cli.ProjectOptions) ([]byte, error) { + return nil, errdefs.ErrNotImplemented +}