Skip to content

Commit

Permalink
fix(aws): improve the error if an undefined service is declared in APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Dec 9, 2024
1 parent 0ab9a90 commit fc7aad3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 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
22 changes: 14 additions & 8 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,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()
Expand Down

0 comments on commit fc7aad3

Please sign in to comment.