Skip to content

Commit

Permalink
Add HandlerSchema.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jun 23, 2023
1 parent 89cb083 commit 12cdf48
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
5 changes: 3 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
)

type Handler struct {
Permissions []string `json:"permissions,omitempty"`
TimeoutInMinutes int `json:"timeoutInMinutes,omitempty"`
HandlerSchema *HandlerSchema `json:"handlerSchema,omitempty"`
Permissions []string `json:"permissions,omitempty"`
TimeoutInMinutes int `json:"timeoutInMinutes,omitempty"`
}
12 changes: 12 additions & 0 deletions handler_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package cfschema

type HandlerSchema struct {
AllOf []*PropertySubschema `json:"allOf,omitempty"`
AnyOf []*PropertySubschema `json:"anyOf,omitempty"`
OneOf []*PropertySubschema `json:"oneOf,omitempty"`
Properties map[string]*Property `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
}
75 changes: 75 additions & 0 deletions handler_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package cfschema_test

import (
"path/filepath"
"testing"

cfschema "github.com/hashicorp/aws-cloudformation-resource-schema-sdk-go"
)

func TestHandlerSchema(t *testing.T) {
testCases := []struct {
TestDescription string
MetaSchemaPath string
ResourceSchemaPath string
ExpectError bool
ExpectedHandlerSchema int
}{
{
TestDescription: "no handlerSchema",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_CloudWatch_MetricStream.json",
},
{
TestDescription: "list handlerSchema",
MetaSchemaPath: "provider.definition.schema.v1.json",
ResourceSchemaPath: "AWS_NetworkManager_TransitGatewayRegistration.json",
ExpectedHandlerSchema: 1,
},
}

for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.TestDescription, func(t *testing.T) {
metaSchema, err := cfschema.NewMetaJsonSchemaPath(filepath.Join("testdata", testCase.MetaSchemaPath))

if err != nil {
t.Fatalf("unexpected NewMetaJsonSchemaPath() error: %s", err)
}

resourceSchema, err := cfschema.NewResourceJsonSchemaPath(filepath.Join("testdata", testCase.ResourceSchemaPath))

if err != nil {
t.Fatalf("unexpected NewResourceJsonSchemaPath() error: %s", err)
}

err = metaSchema.ValidateResourceJsonSchema(resourceSchema)

if err != nil {
t.Fatalf("unexpected ValidateResourceJsonSchema() error: %s", err)
}

resource, err := resourceSchema.Resource()

if err != nil {
t.Fatalf("unexpected Resource() error: %s", err)
}

got := 0

for _, handler := range resource.Handlers {
if handler.HandlerSchema != nil {
got++
}
}

if actual, expected := got, testCase.ExpectedHandlerSchema; actual != expected {
t.Errorf("expected %d handlerSchema elements, got: %d", expected, actual)
}
})
}
}
2 changes: 2 additions & 0 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Resource struct {
DeprecatedProperties PropertyJsonPointers `json:"deprecatedProperties,omitempty"`
Description *string `json:"description,omitempty"`
Handlers map[string]*Handler `json:"handlers,omitempty"`
NonPublicDefinitions PropertyJsonPointers `json:"nonPublicDefinitions,omitempty"`
NonPublicProperties PropertyJsonPointers `json:"nonPublicProperties,omitempty"`
OneOf []*PropertySubschema `json:"oneOf,omitempty"`
PrimaryIdentifier PropertyJsonPointers `json:"primaryIdentifier,omitempty"`
Properties map[string]*Property `json:"properties,omitempty"`
Expand Down
62 changes: 62 additions & 0 deletions testdata/AWS_NetworkManager_TransitGatewayRegistration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"typeName": "AWS::NetworkManager::TransitGatewayRegistration",
"description": "The AWS::NetworkManager::TransitGatewayRegistration type registers a transit gateway in your global network. The transit gateway can be in any AWS Region, but it must be owned by the same AWS account that owns the global network. You cannot register a transit gateway in more than one global network.",
"sourceUrl": "https://github.com/aws-cloudformation/aws-cloudformation-resource-providers-networkmanager.git",
"properties": {
"GlobalNetworkId": {
"description": "The ID of the global network.",
"type": "string"
},
"TransitGatewayArn": {
"description": "The Amazon Resource Name (ARN) of the transit gateway.",
"type": "string"
}
},
"taggable": false,
"additionalProperties": false,
"required": [
"GlobalNetworkId",
"TransitGatewayArn"
],
"createOnlyProperties": [
"/properties/GlobalNetworkId",
"/properties/TransitGatewayArn"
],
"primaryIdentifier": [
"/properties/GlobalNetworkId",
"/properties/TransitGatewayArn"
],
"handlers": {
"create": {
"permissions": [
"networkmanager:RegisterTransitGateway"
],
"timeoutInMinutes": 30
},
"read": {
"permissions": [
"networkmanager:GetTransitGatewayRegistrations"
]
},
"list": {
"handlerSchema": {
"properties": {
"GlobalNetworkId": {
"$ref": "resource-schema.json#/properties/GlobalNetworkId"
}
},
"required": ["GlobalNetworkId"]
},
"permissions": [
"networkmanager:GetTransitGatewayRegistrations"
]
},
"delete": {
"permissions": [
"networkmanager:DeregisterTransitGateway"
],
"timeoutInMinutes": 30
}
}
}

0 comments on commit 12cdf48

Please sign in to comment.