Skip to content
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

Ensure validate called during tfgen #1854

Merged
merged 14 commits into from
Apr 15, 2024
Merged
1 change: 1 addition & 0 deletions internal/testprovider_invalid_schema/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/cmd/*/Pulumi.yaml
5 changes: 5 additions & 0 deletions internal/testprovider_invalid_schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# testprovider_sdkv2

Defines a provider using `github.com/hashicorp/terraform-plugin-sdk/v2` that is dedicated to testing the features of
`pulumi-terraform-bridge`. Specifically the provider is crafted to support `ProgramTest` tests that exercise it together
with the latest Pulumi CLI. These tests verify that the bridged providers integrate correctly with the engine.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
_ "embed"

tpinvschema "github.com/pulumi/pulumi-terraform-bridge/v3/internal/testprovider_invalid_schema"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
)

//go:embed schema.json
var pulumiSchema []byte

func main() {
tfbridge.Main("tpinvschema", "1.0.0", tpinvschema.ProviderInfo(), pulumiSchema)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "tpinvschema",
"attribution": "This Pulumi package is based on the [`tpinvschema` Terraform Provider](https://github.com/terraform-providers/terraform-provider-tpinvschema).",
"meta": {
"moduleFormat": "(.*)(?:/[^/]*)"
},
"language": {
"nodejs": {
"readme": "\u003e This provider is a derived work of the [Terraform Provider](https://github.com/terraform-providers/terraform-provider-tpinvschema)\n\u003e distributed under [MPL 2.0](https://www.mozilla.org/en-US/MPL/2.0/). If you encounter a bug or missing feature,\n\u003e first check the [`pulumi-tpinvschema` repo](/issues); however, if that doesn't turn up anything,\n\u003e please consult the source [`terraform-provider-tpinvschema` repo](https://github.com/terraform-providers/terraform-provider-tpinvschema/issues).",
"compatibility": "tfbridge20",
"disableUnionOutputTypes": true
},
"python": {
"readme": "\u003e This provider is a derived work of the [Terraform Provider](https://github.com/terraform-providers/terraform-provider-tpinvschema)\n\u003e distributed under [MPL 2.0](https://www.mozilla.org/en-US/MPL/2.0/). If you encounter a bug or missing feature,\n\u003e first check the [`pulumi-tpinvschema` repo](/issues); however, if that doesn't turn up anything,\n\u003e please consult the source [`terraform-provider-tpinvschema` repo](https://github.com/terraform-providers/terraform-provider-tpinvschema/issues).",
"compatibility": "tfbridge20",
"pyproject": {}
}
},
"config": {},
"provider": {
"description": "The provider type for the tpinvschema package. By default, resources use package-wide configuration\nsettings, however an explicit `Provider` instance may be created and passed during resource\nconstruction to achieve fine-grained programmatic control over provider settings. See the\n[documentation](https://www.pulumi.com/docs/reference/programming-model/#providers) for more information.\n"
},
"resources": {
"tpinvschema:index/invalid_res:invalid_res": {
"properties": {
"blockStringProp": {
"type": "string"
},
"computedDefaultProp": {
"type": "string"
},
"maxItemsStringProp": {
"type": "string"
},
"optAndReqProp": {
"type": "string"
}
},
"required": [
"blockStringProp",
"computedDefaultProp",
"maxItemsStringProp"
],
"inputProperties": {
"optAndReqProp": {
"type": "string"
}
},
"stateInputs": {
"description": "Input properties used for looking up and filtering invalid_res resources.\n",
"properties": {
"blockStringProp": {
"type": "string"
},
"computedDefaultProp": {
"type": "string"
},
"maxItemsStringProp": {
"type": "string"
},
"optAndReqProp": {
"type": "string"
}
},
"type": "object"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
tpinvschema "github.com/pulumi/pulumi-terraform-bridge/v3/internal/testprovider_invalid_schema"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen"
)

func main() {
tfgen.Main("tpinvschema", "1.0.0", tpinvschema.ProviderInfo())
}
102 changes: 102 additions & 0 deletions internal/testprovider_invalid_schema/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tpinvschema

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// An assortment of invalid resources according to
// https://github.com/pulumi/terraform-plugin-sdk/blob/b374785cb6462f8d89eb31d4874a9e3228d74633/helper/schema/resource.go#L1132
//
//nolint:lll
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{},
ResourcesMap: map[string]*schema.Resource{
"invalid_res": {
Schema: map[string]*schema.Schema{
"opt_and_req_prop": {
Type: schema.TypeString,
Optional: true,
Required: true,
},
},
Create: func(*schema.ResourceData, interface{}) error { return nil },
Update: func(*schema.ResourceData, interface{}) error { return nil },
Read: func(*schema.ResourceData, interface{}) error { return nil },
Delete: func(*schema.ResourceData, interface{}) error { return nil },
},
"no_read_res": {
Schema: map[string]*schema.Schema{},
Create: func(*schema.ResourceData, interface{}) error { return nil },
Delete: func(*schema.ResourceData, interface{}) error { return nil },
},
"block_string_res": {
Schema: map[string]*schema.Schema{
"block_string_prop": {
Type: schema.TypeString,
Required: true,
ConfigMode: schema.SchemaConfigModeBlock,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
Create: func(*schema.ResourceData, interface{}) error { return nil },
Update: func(*schema.ResourceData, interface{}) error { return nil },
Read: func(*schema.ResourceData, interface{}) error { return nil },
Delete: func(*schema.ResourceData, interface{}) error { return nil },
},
"computed_default_res": {
Schema: map[string]*schema.Schema{
"computed_default_prop": {
Type: schema.TypeString,
Computed: true,
Default: "default",
},
},
Create: func(*schema.ResourceData, interface{}) error { return nil },
Read: func(*schema.ResourceData, interface{}) error { return nil },
Delete: func(*schema.ResourceData, interface{}) error { return nil },
},
"max_string_res": {
Schema: map[string]*schema.Schema{
"max_string_prop": {
Type: schema.TypeString,
MaxItems: 1,
Optional: true,
},
},
Create: func(*schema.ResourceData, interface{}) error { return nil },
Update: func(*schema.ResourceData, interface{}) error { return nil },
Read: func(*schema.ResourceData, interface{}) error { return nil },
Delete: func(*schema.ResourceData, interface{}) error { return nil },
},
"create_and_create_context_res": {
Schema: map[string]*schema.Schema{},
Create: func(*schema.ResourceData, interface{}) error { return nil },
Read: func(*schema.ResourceData, interface{}) error { return nil },
Delete: func(*schema.ResourceData, interface{}) error { return nil },
CreateContext: func(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics {
return nil
},
},
},
}
}
40 changes: 40 additions & 0 deletions internal/testprovider_invalid_schema/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tpinvschema

import (
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
sdkv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
)

func ProviderInfo() tfbridge.ProviderInfo {
prov := tfbridge.ProviderInfo{
P: sdkv2.NewProvider(Provider()),
Name: "tpinvschema",

PreConfigureCallback: func(vars resource.PropertyMap, config shim.ResourceConfig) error {
return nil
},
Resources: map[string]*tfbridge.ResourceInfo{
"invalid_res": {
Tok: tfbridge.MakeResource("tpinvschema", "index", "invalid_res"),
},
},
}

return prov
}
5 changes: 5 additions & 0 deletions pf/internal/schemashim/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package schemashim

import (
"context"

shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"

pfprovider "github.com/hashicorp/terraform-plugin-framework/provider"
Expand Down Expand Up @@ -59,6 +60,10 @@ func (p *SchemaOnlyProvider) DataSourcesMap() shim.ResourceMap {
return &schemaOnlyDataSourceMap{dataSources}
}

func (p *SchemaOnlyProvider) InternalValidate() error {
return nil
}

func (p *SchemaOnlyProvider) Validate(context.Context, shim.ResourceConfig) ([]string, []error) {
panic("schemaOnlyProvider does not implement runtime operation Validate")
}
Expand Down
13 changes: 11 additions & 2 deletions pf/tfgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package tfgen

import (
"fmt"
"os"

"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

Expand All @@ -39,7 +40,6 @@ func Main(provider string, info sdkBridge.ProviderInfo) {
version := info.Version

tfgen.MainWithCustomGenerate(provider, version, info, func(opts tfgen.GeneratorOptions) error {

if info.MetadataInfo == nil {
return fmt.Errorf("ProviderInfo.MetadataInfo is required and cannot be nil")
}
Expand Down Expand Up @@ -83,8 +83,17 @@ func MainWithMuxer(provider string, info sdkBridge.ProviderInfo) {
shim, ok := info.P.(*pfmuxer.ProviderShim)
contract.Assertf(ok, "MainWithMuxer must have a ProviderInfo.P created with AugmentShimWithPF")

tfgen.MainWithCustomGenerate(provider, info.Version, info, func(opts tfgen.GeneratorOptions) error {
// Validate any sdk providers that are being muxed in.
for _, prov := range shim.MuxedProviders {
err := prov.InternalValidate()
if err != nil {
_, fmterr := fmt.Fprintf(os.Stderr, "Internal validation of the provider failed: %v\n", err)
contract.IgnoreError(fmterr)
os.Exit(-1)
}
}

tfgen.MainWithCustomGenerate(provider, info.Version, info, func(opts tfgen.GeneratorOptions) error {
if info.MetadataInfo == nil {
return fmt.Errorf("ProviderInfo.MetadataInfo is required and cannot be nil")
}
Expand Down
Loading