Skip to content

Commit

Permalink
jsonprovider: Expose ephemeral resource schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 2, 2024
1 parent 4c78666 commit c303f28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
18 changes: 10 additions & 8 deletions internal/command/jsonprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type Providers struct {
}

type Provider struct {
Provider *Schema `json:"provider,omitempty"`
ResourceSchemas map[string]*Schema `json:"resource_schemas,omitempty"`
DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"`
Functions map[string]*jsonfunction.FunctionSignature `json:"functions,omitempty"`
Provider *Schema `json:"provider,omitempty"`
ResourceSchemas map[string]*Schema `json:"resource_schemas,omitempty"`
DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"`
EphemeralResourceSchemas map[string]*Schema `json:"ephemeral_resource_schemas,omitempty"`
Functions map[string]*jsonfunction.FunctionSignature `json:"functions,omitempty"`
}

func newProviders() *Providers {
Expand Down Expand Up @@ -58,9 +59,10 @@ func Marshal(s *terraform.Schemas) ([]byte, error) {

func marshalProvider(tps providers.ProviderSchema) *Provider {
return &Provider{
Provider: marshalSchema(tps.Provider),
ResourceSchemas: marshalSchemas(tps.ResourceTypes),
DataSourceSchemas: marshalSchemas(tps.DataSources),
Functions: jsonfunction.MarshalProviderFunctions(tps.Functions),
Provider: marshalSchema(tps.Provider),
ResourceSchemas: marshalSchemas(tps.ResourceTypes),
DataSourceSchemas: marshalSchemas(tps.DataSources),
EphemeralResourceSchemas: marshalSchemas(tps.EphemeralResourceTypes),
Functions: jsonfunction.MarshalProviderFunctions(tps.Functions),
}
}
12 changes: 7 additions & 5 deletions internal/command/jsonprovider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func TestMarshalProvider(t *testing.T) {
{
providers.ProviderSchema{},
&Provider{
Provider: &Schema{},
ResourceSchemas: map[string]*Schema{},
DataSourceSchemas: map[string]*Schema{},
Provider: &Schema{},
ResourceSchemas: map[string]*Schema{},
DataSourceSchemas: map[string]*Schema{},
EphemeralResourceSchemas: map[string]*Schema{},
},
},
{
Expand Down Expand Up @@ -147,15 +148,16 @@ func TestMarshalProvider(t *testing.T) {
},
},
},
EphemeralResourceSchemas: map[string]*Schema{},
},
},
}

for i, test := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
got := marshalProvider(test.Input)
if !cmp.Equal(got, test.Want, cmpOpts) {
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want, cmpOpts))
if diff := cmp.Diff(test.Want, got, cmpOpts); diff != "" {
t.Fatalf("wrong result:\n %s\n", diff)
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions internal/schemarepo/loadschemas/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ func (cp *Plugins) ProviderSchema(addr addrs.Provider) (providers.ProviderSchema
}
}

for t, r := range resp.EphemeralResourceTypes {
if err := r.Block.InternalValidate(); err != nil {
return resp, fmt.Errorf("provider %s has invalid schema for ephemeral resource type %q, which is a bug in the provider: %q", addr, t, err)
}
}

for n, f := range resp.Functions {
if !hclsyntax.ValidIdentifier(n) {
return resp, fmt.Errorf("provider %s declares function with invalid name %q", addr, n)
Expand Down

0 comments on commit c303f28

Please sign in to comment.