From 12cdf4812c29cc6a8d01a5728c16609628aaf909 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 23 Jun 2023 15:35:22 -0400 Subject: [PATCH] Add HandlerSchema. --- handler.go | 5 +- handler_schema.go | 12 +++ handler_schema_test.go | 75 +++++++++++++++++++ resource.go | 2 + ...orkManager_TransitGatewayRegistration.json | 62 +++++++++++++++ 5 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 handler_schema.go create mode 100644 handler_schema_test.go create mode 100644 testdata/AWS_NetworkManager_TransitGatewayRegistration.json diff --git a/handler.go b/handler.go index aa8b796..bba792b 100644 --- a/handler.go +++ b/handler.go @@ -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"` } diff --git a/handler_schema.go b/handler_schema.go new file mode 100644 index 0000000..3b14431 --- /dev/null +++ b/handler_schema.go @@ -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"` +} diff --git a/handler_schema_test.go b/handler_schema_test.go new file mode 100644 index 0000000..5f3252b --- /dev/null +++ b/handler_schema_test.go @@ -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) + } + }) + } +} diff --git a/resource.go b/resource.go index 9859967..1b8bb70 100644 --- a/resource.go +++ b/resource.go @@ -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"` diff --git a/testdata/AWS_NetworkManager_TransitGatewayRegistration.json b/testdata/AWS_NetworkManager_TransitGatewayRegistration.json new file mode 100644 index 0000000..1e4bab7 --- /dev/null +++ b/testdata/AWS_NetworkManager_TransitGatewayRegistration.json @@ -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 + } + } +} + \ No newline at end of file