Skip to content

Commit

Permalink
feat: Implement CSharpName property on provider resource (#1551)
Browse files Browse the repository at this point in the history
This PR implements the missing part for PR
pulumi/pulumi#14308 from the Pulumi SDK to have
the ability to rename the generated C# (dotnet) class from it's provider
resource name.

This PR comes with an update of `github.com/pulumi/pulumi/pkg/v3` and
`github.com/pulumi/pulumi/sdk/v3` to at v3.95.0 because this is the
first version of Pulumi which implements the required code for the
`dotnet` codegen to emit a renamed class using the `CSharpName`
property.

---------

Co-authored-by: Thomas Meckel <[email protected]>
Co-authored-by: Anton Tayanovskyy <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent 9a9bbf1 commit bcf2ed0
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 9 deletions.
19 changes: 11 additions & 8 deletions pkg/tfbridge/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,9 @@ func (m *MarshallableDefaultInfo) Unmarshal() *DefaultInfo {

// MarshallableResourceInfo is the JSON-marshallable form of a Pulumi ResourceInfo value.
type MarshallableResourceInfo struct {
Tok tokens.Type `json:"tok"`
Fields map[string]*MarshallableSchemaInfo `json:"fields"`
Tok tokens.Type `json:"tok"`
CSharpName string `json:"csharpName,omitempty"`
Fields map[string]*MarshallableSchemaInfo `json:"fields"`

// Deprecated: IDFields is not currently used and will be deprecated in the next major version of
// pulumi-terraform-bridge.
Expand All @@ -1054,9 +1055,10 @@ func MarshalResourceInfo(r *ResourceInfo) *MarshallableResourceInfo {
fields[k] = MarshalSchemaInfo(v)
}
return &MarshallableResourceInfo{
Tok: r.Tok,
Fields: fields,
IDFields: r.IDFields,
Tok: r.Tok,
CSharpName: r.CSharpName,
Fields: fields,
IDFields: r.IDFields,
}
}

Expand All @@ -1067,9 +1069,10 @@ func (m *MarshallableResourceInfo) Unmarshal() *ResourceInfo {
fields[k] = v.Unmarshal()
}
return &ResourceInfo{
Tok: m.Tok,
Fields: fields,
IDFields: m.IDFields,
Tok: m.Tok,
Fields: fields,
IDFields: m.IDFields,
CSharpName: m.CSharpName,
}
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/tfgen/generate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ func (g *schemaGenerator) genProperty(prop *variable) pschema.PropertySpec {

language := map[string]pschema.RawMessage{}
if prop.info != nil && prop.info.CSharpName != "" {
language["csharp"] = rawMessage(map[string]string{"name": prop.info.CSharpName})
info := csgen.CSharpPropertyInfo{
Name: prop.info.CSharpName,
}
language["csharp"] = rawMessage(info)
}

var defaultValue interface{}
Expand Down Expand Up @@ -708,6 +711,14 @@ func (g *schemaGenerator) genResourceType(mod tokens.Module, res *resourceType)
})
}

spec.Language = map[string]pschema.RawMessage{}
if res.info != nil && res.info.CSharpName != "" {
info := csgen.CSharpResourceInfo{
Name: res.info.CSharpName,
}
spec.Language["csharp"] = rawMessage(info)
}

return spec
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/tfgen/generate_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func TestRegressMiniRandom(t *testing.T) {
bridgetesting.AssertEqualsJSONFile(t, "test_data/regress-minirandom-schema.json", schema)
}

func TestCSharpMiniRandom(t *testing.T) {
provider := testprovider.ProviderMiniRandomCSharp()
schema, err := GenerateSchema(provider, diag.DefaultSink(io.Discard, io.Discard, diag.FormatOptions{
Color: colors.Never,
}))
assert.NoError(t, err)
bridgetesting.AssertEqualsJSONFile(t, "test_data/minirandom-schema-csharp.json", schema)
}

func TestNestedMaxItemsOne(t *testing.T) {
provider := testprovider.ProviderMiniCloudflare()
meta, err := metadata.New(nil)
Expand Down
39 changes: 39 additions & 0 deletions pkg/tfgen/internal/testprovider/minirandom.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,42 @@ func ProviderMiniRandom() tfbridge.ProviderInfo {
},
}
}

func ProviderMiniRandomCSharp() tfbridge.ProviderInfo {
randomPkg := "random"
randomMod := "index"

randomMember := func(mod string, mem string) tokens.ModuleMember {
return tokens.ModuleMember(randomPkg + ":" + mod + ":" + mem)
}

randomType := func(mod string, typ string) tokens.Type {
return tokens.Type(randomMember(mod, typ))
}

randomResource := func(mod string, res string) tokens.Type {
fn := string(unicode.ToLower(rune(res[0]))) + res[1:]
return randomType(mod+"/"+fn, res)
}

return tfbridge.ProviderInfo{
P: shimv2.NewProvider(testproviderdata.ProviderMiniRandom()),
Name: "random",
Description: "A Pulumi package to safely use randomness in Pulumi programs.",
Keywords: []string{"pulumi", "random"},
License: "Apache-2.0",
Homepage: "https://pulumi.io",
Repository: "https://github.com/pulumi/pulumi-random",
Resources: map[string]*tfbridge.ResourceInfo{
"random_integer": {
Tok: randomResource(randomMod, "RandomInteger"),
CSharpName: "CSharpRandomInteger",
Fields: map[string]*tfbridge.SchemaInfo{
"seed": {
CSharpName: "CSharpSeed",
},
},
},
},
}
}
148 changes: 148 additions & 0 deletions pkg/tfgen/test_data/minirandom-schema-csharp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"name": "random",
"description": "A Pulumi package to safely use randomness in Pulumi programs.",
"keywords": [
"pulumi",
"random"
],
"homepage": "https://pulumi.io",
"license": "Apache-2.0",
"attribution": "This Pulumi package is based on the [`random` Terraform Provider](https://github.com/terraform-providers/terraform-provider-random).",
"repository": "https://github.com/pulumi/pulumi-random",
"meta": {
"moduleFormat": "(.*)(?:/[^/]*)"
},
"language": {
"nodejs": {
"packageDescription": "A Pulumi package to safely use randomness in Pulumi programs.",
"readme": "\u003e This provider is a derived work of the [Terraform Provider](https://github.com/terraform-providers/terraform-provider-random)\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-random` repo](https://github.com/pulumi/pulumi-random/issues); however, if that doesn't turn up anything,\n\u003e please consult the source [`terraform-provider-random` repo](https://github.com/terraform-providers/terraform-provider-random/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-random)\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-random` repo](https://github.com/pulumi/pulumi-random/issues); however, if that doesn't turn up anything,\n\u003e please consult the source [`terraform-provider-random` repo](https://github.com/terraform-providers/terraform-provider-random/issues).",
"compatibility": "tfbridge20",
"pyproject": {}
}
},
"config": {},
"provider": {
"description": "The provider type for the random 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": {
"random:index/randomInteger:RandomInteger": {
"properties": {
"keepers": {
"type": "object",
"additionalProperties": {
"$ref": "pulumi.json#/Any"
},
"description": "Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider\ndocumentation](../index.html) for more information.\n"
},
"max": {
"type": "integer",
"description": "The maximum inclusive value of the range.\n"
},
"min": {
"type": "integer",
"description": "The minimum inclusive value of the range.\n"
},
"result": {
"type": "integer",
"description": "The random integer result.\n"
},
"seed": {
"type": "string",
"description": "A custom seed to always produce the same value.\n",
"language": {
"csharp": {
"name": "CSharpSeed"
}
}
}
},
"required": [
"max",
"min",
"result"
],
"inputProperties": {
"keepers": {
"type": "object",
"additionalProperties": {
"$ref": "pulumi.json#/Any"
},
"description": "Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider\ndocumentation](../index.html) for more information.\n",
"willReplaceOnChanges": true
},
"max": {
"type": "integer",
"description": "The maximum inclusive value of the range.\n",
"willReplaceOnChanges": true
},
"min": {
"type": "integer",
"description": "The minimum inclusive value of the range.\n",
"willReplaceOnChanges": true
},
"seed": {
"type": "string",
"description": "A custom seed to always produce the same value.\n",
"willReplaceOnChanges": true,
"language": {
"csharp": {
"name": "CSharpSeed"
}
}
}
},
"requiredInputs": [
"max",
"min"
],
"stateInputs": {
"description": "Input properties used for looking up and filtering RandomInteger resources.\n",
"properties": {
"keepers": {
"type": "object",
"additionalProperties": {
"$ref": "pulumi.json#/Any"
},
"description": "Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider\ndocumentation](../index.html) for more information.\n",
"willReplaceOnChanges": true
},
"max": {
"type": "integer",
"description": "The maximum inclusive value of the range.\n",
"willReplaceOnChanges": true
},
"min": {
"type": "integer",
"description": "The minimum inclusive value of the range.\n",
"willReplaceOnChanges": true
},
"result": {
"type": "integer",
"description": "The random integer result.\n"
},
"seed": {
"type": "string",
"description": "A custom seed to always produce the same value.\n",
"willReplaceOnChanges": true,
"language": {
"csharp": {
"name": "CSharpSeed"
}
}
}
},
"type": "object"
},
"language": {
"csharp": {
"name": "CSharpRandomInteger"
}
}
}
}
}

0 comments on commit bcf2ed0

Please sign in to comment.