Skip to content

Commit

Permalink
Initial support for provider-defined functions from providers schema …
Browse files Browse the repository at this point in the history
…-json (#119)

* Initial support for provider-defined functions from providers schema -json

Reference: #118
Reference: hashicorp/terraform#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`.

* Remove extraneous function metadata object
  • Loading branch information
bflad committed Jan 19, 2024
1 parent 884568c commit f2686e9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions metadata.go
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions schemas.go
Expand Up @@ -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 map[string]*FunctionSignature `json:"functions,omitempty"`
}

// Schema is the JSON representation of a particular schema
Expand Down
17 changes: 17 additions & 0 deletions schemas_test.go
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions 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":{"example":{"description":"Echoes given argument as result","summary":"Example function","return_type":"string","parameters":[{"name":"input","description":"String to echo","type":"string"}]}}}}}

0 comments on commit f2686e9

Please sign in to comment.