From fc7aad333d89dab475c9e1c034da80cc30f59094 Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Tue, 10 Dec 2024 08:16:13 +1100 Subject: [PATCH] fix(aws): improve the error if an undefined service is declared in APIs --- cloud/aws/deploy/api.go | 12 +++++++++--- cloud/aws/deploytf/api.go | 22 ++++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cloud/aws/deploy/api.go b/cloud/aws/deploy/api.go index f3143cbff..f8e3c1ead 100644 --- a/cloud/aws/deploy/api.go +++ b/cloud/aws/deploy/api.go @@ -72,8 +72,6 @@ type nameArnPair struct { func (a *NitricAwsPulumiProvider) Api(ctx *pulumi.Context, parent pulumi.Resource, name string, config *deploymentspb.Api) error { opts := []pulumi.ResourceOption{pulumi.Parent(parent)} - nameArnPairs := make([]interface{}, 0, len(a.Lambdas)) - additionalApiConfig := a.AwsConfig.Apis[name] if config.GetOpenapi() == "" { @@ -116,12 +114,20 @@ func (a *NitricAwsPulumiProvider) Api(ctx *pulumi.Context, parent pulumi.Resourc if v, ok := op.Extensions["x-nitric-target"]; ok { if targetMap, isMap := v.(map[string]any); isMap { serviceName := targetMap["name"].(string) - nitricServiceTargets[serviceName] = a.Lambdas[serviceName] + + lambda, ok := a.Lambdas[serviceName] + if !ok { + return fmt.Errorf("service %s is registered for path %s on API %s, but that service does not exist in the project", serviceName, op.OperationID, name) + } + + nitricServiceTargets[serviceName] = lambda } } } } + nameArnPairs := make([]interface{}, 0, len(a.Lambdas)) + // collect name arn pairs for output iteration for k, v := range nitricServiceTargets { nameArnPairs = append(nameArnPairs, pulumi.All(k, v.InvokeArn).ApplyT(func(args []interface{}) nameArnPair { diff --git a/cloud/aws/deploytf/api.go b/cloud/aws/deploytf/api.go index cefd5f0e5..24012372f 100644 --- a/cloud/aws/deploytf/api.go +++ b/cloud/aws/deploytf/api.go @@ -61,9 +61,6 @@ func awsOperation(op *openapi3.Operation, funcs map[string]*string) *openapi3.Op } func (n *NitricAwsTerraformProvider) Api(stack cdktf.TerraformStack, name string, config *deploymentspb.Api) error { - nameArnPairs := map[string]*string{} - targetNames := map[string]*string{} - if config.GetOpenapi() == "" { return fmt.Errorf("aws provider can only deploy OpenAPI specs") } @@ -99,17 +96,26 @@ func (n *NitricAwsTerraformProvider) Api(stack cdktf.TerraformStack, name string } nitricServiceTargets := map[string]service.Service{} - for _, p := range openapiDoc.Paths { - for _, op := range p.Operations() { - if v, ok := op.Extensions["x-nitric-target"]; ok { - if targetMap, isMap := v.(map[string]any); isMap { + for _, apiPath := range openapiDoc.Paths { + for _, pathOperation := range apiPath.Operations() { + if apiNitricTarget, ok := pathOperation.Extensions["x-nitric-target"]; ok { + if targetMap, isMap := apiNitricTarget.(map[string]any); isMap { serviceName := targetMap["name"].(string) - nitricServiceTargets[serviceName] = n.Services[serviceName] + + nitricService, ok := n.Services[serviceName] + if !ok { + return fmt.Errorf("service %s is registered for path %s on API %s, but that service does not exist in the project", serviceName, pathOperation.OperationID, name) + } + + nitricServiceTargets[serviceName] = nitricService } } } } + nameArnPairs := map[string]*string{} + targetNames := map[string]*string{} + // collect name arn pairs for output iteration for k, v := range nitricServiceTargets { nameArnPairs[k] = v.InvokeArnOutput()