-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detailed TF schema dump #2125
Detailed TF schema dump #2125
Changes from all commits
7226a1f
75a17b3
76dbece
40d1c38
c6756bb
2f3b660
e48db5e
bb47ec0
48bd236
9cbb29a
eeccc25
7eb64d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ import ( | |
// | ||
// info.P must be constructed with ShimProvider or ShimProviderWithContext. | ||
func Main(ctx context.Context, pkg string, prov tfbridge.ProviderInfo, meta ProviderMetadata) { | ||
handleGetSchemaFlag(prov) | ||
handleFlags(ctx, prov.Version, | ||
func() (*tfbridge.MarshallableProviderInfo, error) { | ||
pp, err := newProviderWithContext(ctx, prov, meta) | ||
|
@@ -54,6 +55,22 @@ func Main(ctx context.Context, pkg string, prov tfbridge.ProviderInfo, meta Prov | |
} | ||
} | ||
|
||
func handleGetSchemaFlag(prov tfbridge.ProviderInfo) { | ||
flags := flag.NewFlagSet("get-schema-flags", flag.ContinueOnError) | ||
|
||
dumpSchema := flags.Bool("get-schema", false, "dump provider schema as JSON to stdout") | ||
|
||
flags.SetOutput(io.Discard) | ||
|
||
err := flags.Parse(os.Args[1:]) | ||
contract.IgnoreError(err) | ||
|
||
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does PF need this ceremony when pkg can just call dumpSchema := flags.Bool("get-schema", false, "dump provider schema as JSON to stdout") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handleFlags takes a MarshallableProviderInfo, which does not have the required fields for DetailedSchemaDump |
||
if *dumpSchema { | ||
fmt.Print(string(prov.P.DetailedSchemaDump())) | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
func handleFlags( | ||
ctx context.Context, version string, | ||
getProviderInfo func() (*tfbridge.MarshallableProviderInfo, error), | ||
|
@@ -107,6 +124,7 @@ func MainWithMuxer(ctx context.Context, pkg string, info tfbridge.ProviderInfo, | |
if len(info.MuxWith) > 0 { | ||
panic("mixin providers via tfbridge.ProviderInfo.MuxWith is currently not supported") | ||
} | ||
handleGetSchemaFlag(info) | ||
handleFlags(ctx, info.Version, func() (*tfbridge.MarshallableProviderInfo, error) { | ||
info := info | ||
return tfbridge.MarshalProviderInfo(&info), nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package sdkv2 | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
|
@@ -11,6 +12,7 @@ import ( | |
testing "github.com/mitchellh/go-testing-interface" | ||
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
) | ||
|
||
var _ = shim.Provider(v2Provider{}) | ||
|
@@ -222,3 +224,149 @@ func (p v2Provider) IsSet(_ context.Context, v interface{}) ([]interface{}, bool | |
} | ||
return nil, false | ||
} | ||
|
||
type tfSchemaMarshaller struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate files please, not provider.go |
||
Type schema.ValueType | ||
ConfigMode schema.SchemaConfigMode | ||
Required bool | ||
Optional bool | ||
Computed bool | ||
ForceNew bool | ||
DiffSuppressFuncDefined bool | ||
DiffSuppressOnRefresh bool | ||
Default interface{} | ||
DefaultFuncDefined bool | ||
Description string | ||
InputDefault string | ||
StateFuncDefined bool | ||
Elem interface{} | ||
MaxItems int | ||
MinItems int | ||
SetDefined bool | ||
ComputedWhen []string | ||
ConflictsWith []string | ||
ExactlyOneOf []string | ||
AtLeastOneOf []string | ||
RequiredWith []string | ||
Deprecated string | ||
ValidateFuncDefined bool | ||
ValidateDiagFuncDefined bool | ||
Sensitive bool | ||
} | ||
|
||
type tfResourceMarshaller struct { | ||
Schema map[string]*tfSchemaMarshaller | ||
SchemaVersion int | ||
MigrateStateDefined bool | ||
StateUpgradersLen int | ||
CustomizeDiffDefined bool | ||
DeprecationMessage string | ||
Description string | ||
UseJSONNumber bool | ||
EnableLegacyTypeSystemApplyErrors bool | ||
EnableLegacyTypeSystemPlanErrors bool | ||
} | ||
|
||
type tfProvMarshaller struct { | ||
Schema map[string]*tfSchemaMarshaller | ||
ResourcesMap map[string]*tfResourceMarshaller | ||
DataSourcesMap map[string]*tfResourceMarshaller | ||
ConfigureFuncDefined bool | ||
TerraformVersion string | ||
} | ||
|
||
func convertTFSchemaOrResource(s interface{}) interface{} { | ||
if s == nil { | ||
return nil | ||
} | ||
switch s := s.(type) { | ||
case *schema.Schema: | ||
return convertTFSchema(s) | ||
case *schema.Resource: | ||
return convertTfResource(s) | ||
} | ||
panic(fmt.Sprintf("unexpected type %T", s)) | ||
} | ||
|
||
func convertTFSchema(s *schema.Schema) *tfSchemaMarshaller { | ||
return &tfSchemaMarshaller{ | ||
Type: s.Type, | ||
ConfigMode: s.ConfigMode, | ||
Required: s.Required, | ||
Optional: s.Optional, | ||
Computed: s.Computed, | ||
ForceNew: s.ForceNew, | ||
DiffSuppressFuncDefined: s.DiffSuppressFunc != nil, | ||
DiffSuppressOnRefresh: s.DiffSuppressOnRefresh, | ||
Default: s.Default, | ||
DefaultFuncDefined: s.DefaultFunc != nil, | ||
Description: s.Description, | ||
InputDefault: s.InputDefault, | ||
StateFuncDefined: s.StateFunc != nil, | ||
Elem: convertTFSchemaOrResource(s.Elem), | ||
MaxItems: s.MaxItems, | ||
MinItems: s.MinItems, | ||
SetDefined: s.Set != nil, | ||
ComputedWhen: s.ComputedWhen, //nolint:staticcheck | ||
ConflictsWith: s.ConflictsWith, | ||
ExactlyOneOf: s.ExactlyOneOf, | ||
AtLeastOneOf: s.AtLeastOneOf, | ||
RequiredWith: s.RequiredWith, | ||
Deprecated: s.Deprecated, | ||
ValidateFuncDefined: s.ValidateFunc != nil, | ||
ValidateDiagFuncDefined: s.ValidateDiagFunc != nil, | ||
Sensitive: s.Sensitive, | ||
} | ||
} | ||
|
||
func convertTfResource(r *schema.Resource) *tfResourceMarshaller { | ||
tfResource := &tfResourceMarshaller{ | ||
Schema: make(map[string]*tfSchemaMarshaller), | ||
SchemaVersion: r.SchemaVersion, | ||
MigrateStateDefined: r.MigrateState != nil, //nolint:staticcheck | ||
StateUpgradersLen: len(r.StateUpgraders), | ||
CustomizeDiffDefined: r.CustomizeDiff != nil, | ||
DeprecationMessage: r.DeprecationMessage, | ||
Description: r.Description, | ||
UseJSONNumber: r.UseJSONNumber, | ||
EnableLegacyTypeSystemApplyErrors: r.EnableLegacyTypeSystemApplyErrors, | ||
EnableLegacyTypeSystemPlanErrors: r.EnableLegacyTypeSystemPlanErrors, | ||
} | ||
|
||
for k, v := range r.Schema { | ||
tfResource.Schema[k] = convertTFSchema(v) | ||
} | ||
|
||
return tfResource | ||
} | ||
|
||
func convertTfProv(p *schema.Provider) *tfProvMarshaller { | ||
tfProv := &tfProvMarshaller{ | ||
Schema: make(map[string]*tfSchemaMarshaller), | ||
ResourcesMap: make(map[string]*tfResourceMarshaller), | ||
DataSourcesMap: make(map[string]*tfResourceMarshaller), | ||
ConfigureFuncDefined: p.ConfigureFunc != nil, //nolint:staticcheck | ||
TerraformVersion: p.TerraformVersion, | ||
} | ||
|
||
for k, v := range p.Schema { | ||
tfProv.Schema[k] = convertTFSchema(v) | ||
} | ||
|
||
for k, v := range p.ResourcesMap { | ||
tfProv.ResourcesMap[k] = convertTfResource(v) | ||
} | ||
|
||
for k, v := range p.DataSourcesMap { | ||
tfProv.DataSourcesMap[k] = convertTfResource(v) | ||
} | ||
|
||
return tfProv | ||
} | ||
|
||
func (p v2Provider) DetailedSchemaDump() []byte { | ||
prov := convertTfProv(p.tf) | ||
sch, err := json.Marshal(prov) | ||
contract.AssertNoErrorf(err, "failed to marshal schema") | ||
return sch | ||
} | ||
VenelinMartinov marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of panicking, can we just return nil. That way we can leave actual panics uncaught.