From 6a2467adc3ff8442bb980c20cd13b226e8f8b1c6 Mon Sep 17 00:00:00 2001 From: Brenno Costa Date: Wed, 13 Sep 2023 11:09:12 +0200 Subject: [PATCH 1/2] feat(aws_schemas_schema): add JSON Schema support This commit relies on types exposed by `aws-sdk-go-v2` to compose the validation of the `aws_schemas_schema` types as it includes `JSONSchemaDraft4`. This is a long supported featured by AWS and a years long request from Terraform users. Ref. https://github.com/hashicorp/terraform-provider-aws/issues/23753 --- .changelog/35971.txt | 3 + go.mod | 1 + go.sum | 2 + internal/service/schemas/schema.go | 10 ++- internal/service/schemas/schema_test.go | 80 ++++++++++++++++++++- website/docs/r/schemas_schema.html.markdown | 2 +- 6 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 .changelog/35971.txt diff --git a/.changelog/35971.txt b/.changelog/35971.txt new file mode 100644 index 00000000000..abca4bb2645 --- /dev/null +++ b/.changelog/35971.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_schemas_schema: Add `JSONSchemaDraft4` schema type support +``` diff --git a/go.mod b/go.mod index dc2c1385351..902da591cf0 100644 --- a/go.mod +++ b/go.mod @@ -57,6 +57,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5 github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0 github.com/aws/aws-sdk-go-v2/service/scheduler v1.2.5 + github.com/aws/aws-sdk-go-v2/service/schemas v1.16.6 github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0 github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.0 github.com/aws/aws-sdk-go-v2/service/signer v1.16.5 diff --git a/go.sum b/go.sum index aa10863654d..2fb163d2ad1 100644 --- a/go.sum +++ b/go.sum @@ -150,6 +150,8 @@ github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0 h1:f4qHghGTcns4L4F7u8AHH6 github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0/go.mod h1:YSdqo9knBVm5H3JVmWDhx9Wts9828nColUJzL3OKXDk= github.com/aws/aws-sdk-go-v2/service/scheduler v1.2.5 h1:AGRPn7Hef59Eb9zfXjf6MGn0xRPpO73dIV8u8pfo5Z8= github.com/aws/aws-sdk-go-v2/service/scheduler v1.2.5/go.mod h1:cdpHC7Nd4Yvtf/rhRqyqqI0fzoCb0fpo2oOFVZ0HTeQ= +github.com/aws/aws-sdk-go-v2/service/schemas v1.16.6 h1:Sqx33Tr3OBbMpuvj1zmltG1iXbHIZkZmuGkKNSMTi+Q= +github.com/aws/aws-sdk-go-v2/service/schemas v1.16.6/go.mod h1:IJI1++/Kjsjg6R3ZjOkR+MtW6TP1j1pU7CffPQDRNVQ= github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0 h1:Ou2rjk3siybv09bzpi5fs+9zOYZACxT1LT0KkcDAtIs= github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0/go.mod h1:/MCawoN8Xib5q04k2HsIQ+K2cNtC3CHamrfLZXd6KmA= github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.0 h1:BVjuGDN2ek2gjSB46aIODXIYq3Aw/o0F/ZwBPP883GU= diff --git a/internal/service/schemas/schema.go b/internal/service/schemas/schema.go index fd99232fbff..5f274484746 100644 --- a/internal/service/schemas/schema.go +++ b/internal/service/schemas/schema.go @@ -9,6 +9,7 @@ import ( "time" "github.com/YakDriver/regexache" + "github.com/aws/aws-sdk-go-v2/service/schemas/types" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/schemas" "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" @@ -77,7 +78,7 @@ func ResourceSchema() *schema.Resource { "type": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(schemas.Type_Values(), true), + ValidateFunc: validation.StringInSlice(schemaTypes(), true), }, "version": { @@ -234,3 +235,10 @@ func resourceSchemaDelete(ctx context.Context, d *schema.ResourceData, meta inte return diags } + +func schemaTypes() []string { + return []string{ + string(types.TypeOpenApi3), + string(types.TypeJSONSchemaDraft4), + } +} diff --git a/internal/service/schemas/schema_test.go b/internal/service/schemas/schema_test.go index 3dad536d424..2a384319a3b 100644 --- a/internal/service/schemas/schema_test.go +++ b/internal/service/schemas/schema_test.go @@ -63,14 +63,36 @@ const ( "format": "date-time" } } - } - } + } + } } } +` + + testAccJSONSchemaContent = ` +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://example.com/product.schema.json", + "title": "Event", + "description": "An generic example", + "type": "object", + "properties": { + "name": { + "description": "The unique identifier for a product", + "type": "string" + }, + "created_at": { + "description": "Date-time format", + "type": "string", + "format": "date-time" + } + }, + "required": [ "name" ] +} ` ) -func TestAccSchemasSchema_basic(t *testing.T) { +func TestAccSchemasSchema_openAPI3(t *testing.T) { ctx := acctest.Context(t) var v schemas.DescribeSchemaOutput rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -107,6 +129,43 @@ func TestAccSchemasSchema_basic(t *testing.T) { }) } +func TestAccSchemasSchema_jsonSchemaDraftv4(t *testing.T) { + ctx := acctest.Context(t) + var v schemas.DescribeSchemaOutput + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_schemas_schema.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, schemas.EndpointsID) }, + ErrorCheck: acctest.ErrorCheck(t, schemas.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckSchemaDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccSchemaConfig_jsonSchema(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckSchemaExists(ctx, resourceName, &v), + acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "schemas", fmt.Sprintf("schema/%s/%s", rName, rName)), + resource.TestCheckResourceAttr(resourceName, "description", ""), + resource.TestCheckResourceAttr(resourceName, "content", testAccJSONSchemaContent), + resource.TestCheckResourceAttrSet(resourceName, "last_modified"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "registry_name", rName), + resource.TestCheckResourceAttr(resourceName, "type", "JSONSchemaDraft4"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, "version", "1"), + resource.TestCheckResourceAttrSet(resourceName, "version_created_date"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccSchemasSchema_disappears(t *testing.T) { ctx := acctest.Context(t) var v schemas.DescribeSchemaOutput @@ -302,6 +361,21 @@ resource "aws_schemas_schema" "test" { `, rName, testAccSchemaContent) } +func testAccSchemaConfig_jsonSchema(rName string) string { + return fmt.Sprintf(` +resource "aws_schemas_registry" "test" { + name = %[1]q +} + +resource "aws_schemas_schema" "test" { + name = %[1]q + registry_name = aws_schemas_registry.test.name + type = "JSONSchemaDraft4" + content = %[2]q +} +`, rName, testAccJSONSchemaContent) +} + func testAccSchemaConfig_contentDescription(rName, content, description string) string { return fmt.Sprintf(` resource "aws_schemas_registry" "test" { diff --git a/website/docs/r/schemas_schema.html.markdown b/website/docs/r/schemas_schema.html.markdown index f1921004120..338aece0393 100644 --- a/website/docs/r/schemas_schema.html.markdown +++ b/website/docs/r/schemas_schema.html.markdown @@ -55,7 +55,7 @@ This resource supports the following arguments: * `name` - (Required) The name of the schema. Maximum of 385 characters consisting of lower case letters, upper case letters, ., -, _, @. * `content` - (Required) The schema specification. Must be a valid Open API 3.0 spec. * `registry_name` - (Required) The name of the registry in which this schema belongs. -* `type` - (Required) The type of the schema. Valid values: `OpenApi3`. +* `type` - (Required) The type of the schema. Valid values: `OpenApi3` or `JSONSchemaDraft4`. * `description` - (Optional) The description of the schema. Maximum of 256 characters. * `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. From 2f6e21eb27441fba2fbb4deba2be5213948f0303 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 14 Sep 2023 10:06:32 -0400 Subject: [PATCH 2/2] Don't import 'github.com/aws/aws-sdk-go-v2/service/schemas' just for a single constant. --- go.mod | 1 - go.sum | 2 -- internal/service/schemas/schema.go | 12 +++++------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index b22c15aed06..e4494c022c1 100644 --- a/go.mod +++ b/go.mod @@ -57,7 +57,6 @@ require ( github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5 github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0 github.com/aws/aws-sdk-go-v2/service/scheduler v1.2.5 - github.com/aws/aws-sdk-go-v2/service/schemas v1.16.6 github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0 github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.0 github.com/aws/aws-sdk-go-v2/service/signer v1.16.5 diff --git a/go.sum b/go.sum index 67e39612e2b..fd8a5b9380f 100644 --- a/go.sum +++ b/go.sum @@ -150,8 +150,6 @@ github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0 h1:f4qHghGTcns4L4F7u8AHH6 github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0/go.mod h1:YSdqo9knBVm5H3JVmWDhx9Wts9828nColUJzL3OKXDk= github.com/aws/aws-sdk-go-v2/service/scheduler v1.2.5 h1:AGRPn7Hef59Eb9zfXjf6MGn0xRPpO73dIV8u8pfo5Z8= github.com/aws/aws-sdk-go-v2/service/scheduler v1.2.5/go.mod h1:cdpHC7Nd4Yvtf/rhRqyqqI0fzoCb0fpo2oOFVZ0HTeQ= -github.com/aws/aws-sdk-go-v2/service/schemas v1.16.6 h1:Sqx33Tr3OBbMpuvj1zmltG1iXbHIZkZmuGkKNSMTi+Q= -github.com/aws/aws-sdk-go-v2/service/schemas v1.16.6/go.mod h1:IJI1++/Kjsjg6R3ZjOkR+MtW6TP1j1pU7CffPQDRNVQ= github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0 h1:Ou2rjk3siybv09bzpi5fs+9zOYZACxT1LT0KkcDAtIs= github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0/go.mod h1:/MCawoN8Xib5q04k2HsIQ+K2cNtC3CHamrfLZXd6KmA= github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.0 h1:BVjuGDN2ek2gjSB46aIODXIYq3Aw/o0F/ZwBPP883GU= diff --git a/internal/service/schemas/schema.go b/internal/service/schemas/schema.go index 5f274484746..9370e4ec02e 100644 --- a/internal/service/schemas/schema.go +++ b/internal/service/schemas/schema.go @@ -9,7 +9,6 @@ import ( "time" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go-v2/service/schemas/types" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/schemas" "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" @@ -18,6 +17,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" @@ -78,7 +78,7 @@ func ResourceSchema() *schema.Resource { "type": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(schemaTypes(), true), + ValidateFunc: validation.StringInSlice(type_Values(), true), }, "version": { @@ -236,9 +236,7 @@ func resourceSchemaDelete(ctx context.Context, d *schema.ResourceData, meta inte return diags } -func schemaTypes() []string { - return []string{ - string(types.TypeOpenApi3), - string(types.TypeJSONSchemaDraft4), - } +func type_Values() []string { + // For some reason AWS SDK for Go v1 does not define a TypeJSONSchemaDraft4 constant. + return tfslices.AppendUnique(schemas.Type_Values(), "JSONSchemaDraft4") }