From fadb5b18332506b769c4fe0011c0f241347c264d Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 11 Jan 2024 14:45:56 -0800 Subject: [PATCH 1/2] Initial support for provider-defined functions from providers schema -json Reference: https://github.com/hashicorp/terraform-json/issues/118 Reference: https://github.com/hashicorp/terraform/pull/34450 The Terraform `providers schema -json` output implementation currently uses the entire `metadata functions -json` implementation, so has the additional nesting layer containing `format_version` and `function_signatures`. --- metadata.go | 8 ++++++++ schemas.go | 3 +++ schemas_test.go | 17 +++++++++++++++++ testdata/functions/schemas.json | 1 + 4 files changed, 29 insertions(+) create mode 100644 testdata/functions/schemas.json diff --git a/metadata.go b/metadata.go index eb52577..8ac111a 100644 --- a/metadata.go +++ b/metadata.go @@ -77,6 +77,14 @@ type FunctionSignature struct { // of the function Description string `json:"description,omitempty"` + // Summary is an optional shortened description of the function + Summary string `json:"summary,omitempty"` + + // DeprecationMessage is an optional message that indicates that the + // function should be considered deprecated and what actions should be + // performed by the practitioner to handle the deprecation. + DeprecationMessage string `json:"deprecation_message,omitempty"` + // ReturnType is the ctyjson representation of the function's // return types based on supplying all parameters using // dynamic types. Functions can have dynamic return types. diff --git a/schemas.go b/schemas.go index 64f87d8..404ff71 100644 --- a/schemas.go +++ b/schemas.go @@ -86,6 +86,9 @@ type ProviderSchema struct { // The schemas for any data sources in this provider. DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"` + + // The definitions for any functions in this provider. + Functions *MetadataFunctions `json:"functions,omitempty"` } // Schema is the JSON representation of a particular schema diff --git a/schemas_test.go b/schemas_test.go index 5044559..53cbce2 100644 --- a/schemas_test.go +++ b/schemas_test.go @@ -26,6 +26,23 @@ func TestProviderSchemasValidate(t *testing.T) { } } +func TestProviderSchemasValidate_functions(t *testing.T) { + f, err := os.Open("testdata/functions/schemas.json") + if err != nil { + t.Fatal(err) + } + defer f.Close() + + var schemas *ProviderSchemas + if err := json.NewDecoder(f).Decode(&schemas); err != nil { + t.Fatal(err) + } + + if err := schemas.Validate(); err != nil { + t.Fatal(err) + } +} + func TestProviderSchemasValidate_nestedAttributes(t *testing.T) { f, err := os.Open("testdata/nested_attributes/schemas.json") if err != nil { diff --git a/testdata/functions/schemas.json b/testdata/functions/schemas.json new file mode 100644 index 0000000..5e073ef --- /dev/null +++ b/testdata/functions/schemas.json @@ -0,0 +1 @@ +{"format_version":"1.0","provider_schemas":{"example":{"provider":{"version":0,"block":{"attributes":{"example":{"type":"string","description_kind":"plain","optional":true}},"description_kind":"plain"}},"resource_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example resource","description_kind":"markdown"}}},"data_source_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example data source","description_kind":"markdown"}}},"functions":{"format_version":"1.0","function_signatures":{"example":{"description":"Echoes given argument as result","summary":"Example function","return_type":"string","parameters":[{"name":"input","description":"String to echo","type":"string"}]}}}}}} From 138b265988869d56de8c4618b0659bfb36a5e66a Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 18 Jan 2024 15:54:14 -0500 Subject: [PATCH 2/2] Remove extraneous function metadata object --- schemas.go | 2 +- testdata/functions/schemas.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/schemas.go b/schemas.go index 404ff71..a2918ef 100644 --- a/schemas.go +++ b/schemas.go @@ -88,7 +88,7 @@ type ProviderSchema struct { DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"` // The definitions for any functions in this provider. - Functions *MetadataFunctions `json:"functions,omitempty"` + Functions map[string]*FunctionSignature `json:"functions,omitempty"` } // Schema is the JSON representation of a particular schema diff --git a/testdata/functions/schemas.json b/testdata/functions/schemas.json index 5e073ef..1d616b7 100644 --- a/testdata/functions/schemas.json +++ b/testdata/functions/schemas.json @@ -1 +1 @@ -{"format_version":"1.0","provider_schemas":{"example":{"provider":{"version":0,"block":{"attributes":{"example":{"type":"string","description_kind":"plain","optional":true}},"description_kind":"plain"}},"resource_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example resource","description_kind":"markdown"}}},"data_source_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example data source","description_kind":"markdown"}}},"functions":{"format_version":"1.0","function_signatures":{"example":{"description":"Echoes given argument as result","summary":"Example function","return_type":"string","parameters":[{"name":"input","description":"String to echo","type":"string"}]}}}}}} +{"format_version":"1.0","provider_schemas":{"example":{"provider":{"version":0,"block":{"attributes":{"example":{"type":"string","description_kind":"plain","optional":true}},"description_kind":"plain"}},"resource_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example resource","description_kind":"markdown"}}},"data_source_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example data source","description_kind":"markdown"}}},"functions":{"example":{"description":"Echoes given argument as result","summary":"Example function","return_type":"string","parameters":[{"name":"input","description":"String to echo","type":"string"}]}}}}}