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

fix: fixes are not applied to property deprecation messages #1765

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions internal/testprovider/schema_mini_aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 testprovider

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

func ProviderMiniAws() *schema.Provider {
resourceBucket := func() *schema.Resource {
return &schema.Resource{
Schema: resourceMiniAwsBucket(),
Description: "Provides a S3 bucket resource.",
}
}

return &schema.Provider{
Schema: map[string]*schema.Schema{},
ResourcesMap: map[string]*schema.Resource{
"aws_s3_bucket": resourceBucket(),
"aws_s3_bucket_acl": {
Description: "Provides a S3 bucket ACL resource.",
Schema: map[string]*schema.Schema{},
},
},
}
}

func resourceMiniAwsBucket() map[string]*schema.Schema {
return map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
},
"acl": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"grant"},
Deprecated: "Use the aws_s3_bucket_acl resource instead",
},
}
}
21 changes: 14 additions & 7 deletions pkg/tfgen/generate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ const (
)

type schemaGenerator struct {
pkg tokens.Package
version string
info tfbridge.ProviderInfo
pkg tokens.Package
version string
info tfbridge.ProviderInfo
language Language
}

type schemaNestedType struct {
Expand Down Expand Up @@ -214,9 +215,10 @@ func genPulumiSchema(
) (pschema.PackageSpec, error) {

g := &schemaGenerator{
pkg: name,
version: version,
info: info,
pkg: name,
version: version,
info: info,
language: pack.language,
}
pulumiPackageSpec, err := g.genPackageSpec(pack)
if err != nil {
Expand Down Expand Up @@ -648,14 +650,19 @@ func (g *schemaGenerator) genProperty(prop *variable) pschema.PropertySpec {
if prop.info != nil && prop.info.Secret != nil {
secret = *prop.info.Secret
}
ic := infoContext{
language: g.language,
pkg: g.pkg,
info: g.info,
}

propPath := paths.NewProperyPath(prop.parentPath, prop.propertyName)
return pschema.PropertySpec{
TypeSpec: g.schemaType(propPath, prop.typ, prop.out),
Description: description,
Default: defaultValue,
DefaultInfo: defaultInfo,
DeprecationMessage: prop.deprecationMessage(),
DeprecationMessage: ic.fixupPropertyReference(prop.deprecationMessage()),
Language: language,
Secret: secret,
WillReplaceOnChanges: prop.forceNew(),
Expand Down
33 changes: 33 additions & 0 deletions pkg/tfgen/generate_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@ func TestCSharpMiniRandom(t *testing.T) {
bridgetesting.AssertEqualsJSONFile(t, "test_data/minirandom-schema-csharp.json", schema)
}

// TestPropertyDocumentationEdits tests that documentation edits are applied to
// individual properties. This includes both the property description and
// deprecation message. This tests the following workflow
// 1. The generator finds markdown documentation for the `aws_s3_bucket`
// resource
// 2. The generator applies `DocsEdit` rules to the markdown documentation
// 3. The generator parses the markdown documentation and pulls out the `acl`
// argument description and merges that into the schema
// 3. The generator cleans up the `acl` description and deprecation message,
// replacing terraform references with pulumi references e.g.
// `aws_s3_bucket_acl` -> `aws.s3.BucketAclV2`
func TestPropertyDocumentationEdits(t *testing.T) {
provider := testprovider.ProviderMiniAws()
provider.MetadataInfo = tfbridge.NewProviderMetadata(nil)
schema, err := GenerateSchema(provider, diag.DefaultSink(io.Discard, io.Discard, diag.FormatOptions{
Color: colors.Never,
}))
assert.NoError(t, err)

// asserts that `aws_s3_bucket_acl` has been changed to `aws.s3.BucketAclV2`
assert.Equal(t,
"The [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply."+
" Valid values are `private`, `public-read`, `public-read-write`, `aws-exec-read`, `authenticated-read`,"+
" and `log-delivery-write`. Defaults to `private`. Conflicts with `grant`. The provider will only perform drift "+
"detection if a configuration value is provided. Use the resource `aws.s3.BucketAclV2` instead.\n",
schema.Resources["aws:s3/bucketV2:BucketV2"].InputProperties["acl"].Description,
)
assert.Equal(t,
"Use the aws.s3.BucketAclV2 resource instead",
schema.Resources["aws:s3/bucketV2:BucketV2"].InputProperties["acl"].DeprecationMessage,
)
}

func TestNestedMaxItemsOne(t *testing.T) {
provider := testprovider.ProviderMiniCloudflare()
meta, err := metadata.New(nil)
Expand Down
75 changes: 75 additions & 0 deletions pkg/tfgen/internal/testprovider/miniaws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 testprovider

import (
"bytes"

testproviderdata "github.com/pulumi/pulumi-terraform-bridge/v3/internal/testprovider"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
)

func ProviderMiniAws() tfbridge.ProviderInfo {

return tfbridge.ProviderInfo{
P: shimv2.NewProvider(testproviderdata.ProviderMiniAws()),
Name: "aws",
Description: "A Pulumi package to safely use aws in Pulumi programs.",
Keywords: []string{"pulumi", "aws"},
License: "Apache-2.0",
Homepage: "https://pulumi.io",
Repository: "https://github.com/pulumi/pulumi-aws",
DocRules: &tfbridge.DocRuleInfo{EditRules: func(defaults []tfbridge.DocsEdit) []tfbridge.DocsEdit {
return []tfbridge.DocsEdit{
{
Path: "*",
Edit: func(_ string, content []byte) ([]byte, error) {
content = bytes.ReplaceAll(
content,
// This replacement is done in the aws provider
// here. This replacement is necessary because the
// bridge will drop any docs that contain the word
// 'Terraform'
// https://github.com/pulumi/pulumi-aws/blob/df5d52299c72b936df9c9289d83d10225dc1dce7/provider/replacements.json#L1688
//nolint:lll
[]byte(" Terraform will only perform drift detection if a configuration value is provided."),
[]byte(" The provider will only perform drift detection if a configuration value is provided."),
)
return content, nil

},
},
}
}},
UpstreamRepoPath: "./test_data",
Resources: map[string]*tfbridge.ResourceInfo{
"aws_s3_bucket_acl": {
Tok: tokens.Type(tokens.ModuleMember("aws:s3/bucketAclV2:BucketAclV2")),
},
"aws_s3_bucket": {
Tok: tokens.Type(tokens.ModuleMember("aws:s3/bucketV2:BucketV2")),
Aliases: []tfbridge.AliasInfo{
{
Type: ref("aws:s3/bucket:Bucket"),
},
},
},
},
}
}

func ref[T any](value T) *T { return &value }
21 changes: 21 additions & 0 deletions pkg/tfgen/test_data/website/docs/r/s3_bucket.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
subcategory: "S3 (Simple Storage)"
layout: "aws"
page_title: "AWS: aws_s3_bucket"
description: |-
Provides a S3 bucket resource.
---

# Resource: aws_s3_bucket

Provides a S3 bucket resource.

## Argument Reference

This resource supports the following arguments:

* `bucket` - (Optional, Forces new resource) Name of the bucket. If omitted, the provider will assign a random, unique name. Must be lowercase and less than or equal to 63 characters in length. A full list of bucket naming rules [may be found here](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html). The name must not be in the format `[bucket_name]--[azid]--x-s3`. Use the `aws_s3_directory_bucket` resource to manage S3 Express buckets.

The following arguments are deprecated, and will be removed in a future major version:

* `acl` - (Optional, **Deprecated**) The [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Valid values are `private`, `public-read`, `public-read-write`, `aws-exec-read`, `authenticated-read`, and `log-delivery-write`. Defaults to `private`. Conflicts with `grant`. Terraform will only perform drift detection if a configuration value is provided. Use the resource `aws_s3_bucket_acl` instead.