Skip to content

Commit

Permalink
fix(aws): improve the error if an undefined service is declared in AP…
Browse files Browse the repository at this point in the history
…Is (#706)

Co-authored-by: Rak Siva <[email protected]>
  • Loading branch information
jyecusch and raksiv authored Dec 10, 2024
1 parent 40fb680 commit 2dc61c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
12 changes: 9 additions & 3 deletions cloud/aws/deploy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() == "" {
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 18 additions & 9 deletions cloud/aws/deploytf/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -99,17 +96,29 @@ 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 {
serviceName := targetMap["name"].(string)
nitricServiceTargets[serviceName] = n.Services[serviceName]
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, ok := targetMap["name"].(string)
if !ok {
return fmt.Errorf("missing or invalid 'name' field in x-nitric-target for path %s on API %s", pathOperation.OperationID, name)
}

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()
Expand Down

0 comments on commit 2dc61c0

Please sign in to comment.