-
Notifications
You must be signed in to change notification settings - Fork 12
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
Allow override a Pulumi token by using SetToken of infer.Annotator #129
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f7a4fd
Allow override a Pulumi token by using SetToken of infer.Annotator
tmeckel 7071a97
centralize getToken logic
iwahbe 0d5ddaf
Add test
iwahbe 963589d
Change SetToken interface to take (module,name) instead of (token)
iwahbe 3241875
Validate SetToken inputs
iwahbe 4d9db7c
typo
iwahbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
// Copyright 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 tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/blang/semver" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens" | ||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
p "github.com/pulumi/pulumi-go-provider" | ||
"github.com/pulumi/pulumi-go-provider/infer" | ||
"github.com/pulumi/pulumi-go-provider/integration" | ||
) | ||
|
||
type CustomToken struct{} | ||
|
||
func (c *CustomToken) Annotate(a infer.Annotator) { a.SetToken("overwritten", "Tk") } | ||
|
||
func (*CustomToken) Create( | ||
ctx p.Context, name string, inputs TokenArgs, preview bool, | ||
) (string, TokenResult, error) { | ||
panic("unimplemented") | ||
} | ||
|
||
type TokenArgs struct { | ||
Array []ObjectToken `pulumi:"arr"` | ||
|
||
Single ObjectToken `pulumi:"single"` | ||
} | ||
type TokenResult struct { | ||
Map map[string]ObjectToken `pulumi:"m"` | ||
} | ||
|
||
type TokenComponent struct{ pulumi.ResourceState } | ||
|
||
type ComponentToken struct{} | ||
|
||
// Check that we allow other capitalization schemes | ||
func (c *ComponentToken) Annotate(a infer.Annotator) { a.SetToken("cmp", "tK") } | ||
|
||
func (*ComponentToken) Construct( | ||
ctx *pulumi.Context, name, typ string, inputs TokenArgs, opts pulumi.ResourceOption, | ||
) (*TokenComponent, error) { | ||
panic("unimplemented") | ||
} | ||
|
||
type FnToken struct{} | ||
|
||
func (c *FnToken) Annotate(a infer.Annotator) { a.SetToken("fn", "TK") } | ||
|
||
func (*FnToken) Call(ctx p.Context, input TokenArgs) (output TokenResult, err error) { | ||
panic("unimplemented") | ||
} | ||
|
||
type ObjectToken struct { | ||
Value string `pulumi:"value"` | ||
} | ||
|
||
func (c *ObjectToken) Annotate(a infer.Annotator) { a.SetToken("obj", "Customized") } | ||
|
||
func TestTokens(t *testing.T) { | ||
provider := infer.Provider(infer.Options{ | ||
Resources: []infer.InferredResource{ | ||
infer.Resource[*CustomToken, TokenArgs, TokenResult](), | ||
}, | ||
Components: []infer.InferredComponent{ | ||
infer.Component[*ComponentToken, TokenArgs, *TokenComponent](), | ||
}, | ||
Functions: []infer.InferredFunction{ | ||
infer.Function[*FnToken, TokenArgs, TokenResult](), | ||
}, | ||
ModuleMap: map[tokens.ModuleName]tokens.ModuleName{"overwritten": "index"}, | ||
}) | ||
server := integration.NewServer("test", semver.MustParse("1.0.0"), provider) | ||
|
||
schema, err := server.GetSchema(p.GetSchemaRequest{}) | ||
require.NoError(t, err) | ||
|
||
assert.JSONEq(t, `{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
"config": {}, | ||
"types": { | ||
"test:obj:Customized": { | ||
"properties": { | ||
"value": { | ||
"type": "string" | ||
} | ||
}, | ||
"type": "object", | ||
"required": [ | ||
"value" | ||
] | ||
} | ||
}, | ||
"provider": {}, | ||
"resources": { | ||
"test:cmp:tK": { | ||
"inputProperties": { | ||
"arr": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
}, | ||
"single": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
}, | ||
"requiredInputs": [ | ||
"arr", | ||
"single" | ||
], | ||
"isComponent": true | ||
}, | ||
"test:index:Tk": { | ||
"properties": { | ||
"m": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"m" | ||
], | ||
"inputProperties": { | ||
"arr": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
}, | ||
"single": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
}, | ||
"requiredInputs": [ | ||
"arr", | ||
"single" | ||
] | ||
} | ||
}, | ||
"functions": { | ||
"test:fn:TK": { | ||
"inputs": { | ||
"properties": { | ||
"arr": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
}, | ||
"single": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
}, | ||
"type": "object", | ||
"required": [ | ||
"arr", | ||
"single" | ||
] | ||
}, | ||
"outputs": { | ||
"properties": { | ||
"m": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/types/test:obj:Customized" | ||
} | ||
} | ||
}, | ||
"type": "object", | ||
"required": [ | ||
"m" | ||
] | ||
} | ||
} | ||
} | ||
}`, schema.Schema) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ package introspect | |
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens" | ||
) | ||
|
||
func NewAnnotator(resource any) Annotator { | ||
|
@@ -33,6 +35,7 @@ type Annotator struct { | |
Descriptions map[string]string | ||
Defaults map[string]any | ||
DefaultEnvs map[string][]string | ||
Token string | ||
|
||
matcher FieldMatcher | ||
} | ||
|
@@ -83,3 +86,13 @@ func (a *Annotator) SetDefault(i any, defaultValue any, env ...string) { | |
a.Defaults[field.Name] = defaultValue | ||
a.DefaultEnvs[field.Name] = append(a.DefaultEnvs[field.Name], env...) | ||
} | ||
|
||
func (a *Annotator) SetToken(module, token string) { | ||
if !tokens.IsQName(module) { | ||
panic(fmt.Sprintf("Module (%q) must comply with %s, but does not", module, tokens.QNameRegexp)) | ||
} | ||
if !tokens.IsName(token) { | ||
panic(fmt.Sprintf("Token (%q) must comply with %s, but does not", token, tokens.NameRegexp)) | ||
Comment on lines
+92
to
+95
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. If you get this wrong, when do you see the panic? At compile time for the provider? 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. At schema generation time. |
||
} | ||
a.Token = fmt.Sprintf("pkg:%s:%s", module, token) | ||
iwahbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It looks like this change touches the code that generates the default names; do we have a similar test that checks the names produced without these annotations?
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.
We have a bunch of integration tests that verify their schema.