Skip to content

Commit fe4520c

Browse files
authored
Add flags for setting simple properties (string/bool/number) (#23323)
This adds flags for setting "simple" properties on resources/functions, rather than having to use the input files. Provider flags are always namespace'd with the provider name (e.g. `--aws:region`). Resource/function flags will use the plain name (e.g. `--bucket`) unless it conflicts with an existing builtin flag (like `input` or `dry-run`) in which case it gets namespace'd to `--input:dry-run`. We attempt to convert 'camelCase' to 'kebab-case' for the flag names. You can _always_ specify the flag with `--input:X` so that's always safe to use. Trying to set `--input:X` and `--X` is an error. If using pcl these simply get merged in as attributes to the input file. If using a converter we pass these as a separate attribute map to `ConvertSnippet` and the converter returns them back converted and they are then merged in.
1 parent 8b87e7b commit fe4520c

14 files changed

Lines changed: 732 additions & 62 deletions

File tree

‎pkg/cmd/pulumi/do/do_function.go‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func functionSchemaHelp(fn *schema.Function) string {
4444
b.WriteString(title)
4545
b.WriteString(":\n")
4646
for _, property := range properties {
47-
fmt.Fprintf(&b, " %s (%s", property.Name, schemaTypeString(property.Type))
47+
fmt.Fprintf(&b, " %s (%s", property.Name, unwrapType(property.Type))
4848
if includeRequired {
4949
if property.IsRequired() {
5050
b.WriteString(", required")
@@ -69,7 +69,7 @@ func functionSchemaHelp(fn *schema.Function) string {
6969
if b.Len() > 0 {
7070
b.WriteString("\n\n")
7171
}
72-
fmt.Fprintf(&b, "Outputs:\n result (%s)\n", schemaTypeString(fn.ReturnType))
72+
fmt.Fprintf(&b, "Outputs:\n result (%s)\n", unwrapType(fn.ReturnType))
7373
}
7474
return strings.TrimSuffix(b.String(), "\n")
7575
}
@@ -97,13 +97,18 @@ func (pc *packageCommand) newFunctionCommand(fn *schema.Function) *cobra.Command
9797
RunE: func(cmd *cobra.Command, args []string) error {
9898
ctx := cmd.Context()
9999

100-
if err := pc.configureProvider(ctx); err != nil {
100+
if err := pc.configureProvider(cmd, ctx); err != nil {
101101
return err
102102
}
103103

104+
var inputProperties []*schema.Property
105+
if fn.Inputs != nil {
106+
inputProperties = fn.Inputs.Properties
107+
}
104108
inputs, err := evaluateFunctionFile(
105109
ctx, inputFile, "input", pc.format, fn, pc.evalContext,
106-
pc.converter, pc.loaderTarget, pc.packageDescriptor)
110+
pc.converter, pc.loaderTarget, pc.packageDescriptor,
111+
collectInputFlags(cmd, "input", inputProperties))
107112
if err != nil {
108113
return fmt.Errorf("parse input file: %w", err)
109114
}
@@ -148,5 +153,10 @@ func (pc *packageCommand) newFunctionCommand(fn *schema.Function) *cobra.Command
148153
cmd.Flags().StringVar(&pc.format, "input", "pcl",
149154
"Format of the configuration files")
150155

156+
addInputFlags(cmd, pc.spec.Name, pc.spec.Provider.InputProperties)
157+
if fn.Inputs != nil {
158+
addInputFlags(cmd, "input", fn.Inputs.Properties)
159+
}
160+
151161
return cmd
152162
}

‎pkg/cmd/pulumi/do/do_function_test.go‎

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Flags:
134134
--input string Format of the configuration files (default "pcl")
135135
--input-file string Path to a file containing function inputs
136136
--package string The package to load, in the form 'name@version' or a path to a plugin binary or folder. If the package supports parameterization, additional space-separated parameters can be included after the package name, e.g. --package "name@version param1 \"multi word param\""
137+
--param1 string To set param1 things (alias for --input:param1)
137138
--provider-file string Path to a file containing provider configuration
138139
--show-secrets Show secret values in output
139140
`
@@ -2413,3 +2414,211 @@ func TestDoCmdFunctionInvokeWithConverterReturningInvalidPCL(t *testing.T) {
24132414
require.Error(t, err)
24142415
assert.ErrorContains(t, err, "not_an_input")
24152416
}
2417+
2418+
func TestDoCmdFunctionInvokeWithFlags(t *testing.T) {
2419+
t.Parallel()
2420+
2421+
configureCalled := false
2422+
mlm := &cmdBackend.MockLoginManager{}
2423+
mws := &pkgWorkspace.MockContext{}
2424+
loader := func(ctx context.Context, pctx *plugin.Context, wd, source string) (plugin.Provider, error) {
2425+
assert.Equal(t, "azure", source)
2426+
spec := schema.PackageSpec{
2427+
Name: "azure",
2428+
Provider: schema.ResourceSpec{
2429+
InputProperties: map[string]schema.PropertySpec{
2430+
"opt1": {TypeSpec: schema.TypeSpec{Type: "string"}},
2431+
"optTwo": {TypeSpec: schema.TypeSpec{Type: "string"}},
2432+
},
2433+
},
2434+
Functions: map[string]schema.FunctionSpec{
2435+
"azure:index:myFunction": {
2436+
Inputs: &schema.ObjectTypeSpec{
2437+
Properties: map[string]schema.PropertySpec{
2438+
"in1": {TypeSpec: schema.TypeSpec{Type: "string"}},
2439+
"inTwo": {TypeSpec: schema.TypeSpec{Type: "string"}},
2440+
"dryRun": {TypeSpec: schema.TypeSpec{Type: "boolean"}},
2441+
},
2442+
},
2443+
Outputs: &schema.ObjectTypeSpec{
2444+
Properties: map[string]schema.PropertySpec{
2445+
"output1": {TypeSpec: schema.TypeSpec{Type: "string"}},
2446+
},
2447+
},
2448+
},
2449+
},
2450+
}
2451+
return &testProvider{
2452+
spec: spec,
2453+
MockProvider: plugin.MockProvider{
2454+
ConfigureF: func(ctx context.Context, req plugin.ConfigureRequest) (plugin.ConfigureResponse, error) {
2455+
configureCalled = true
2456+
// The converted PCL ("opt1 = \"val1\"") + optTwo=val2 should be bound, evaluated, and reach Configure intact.
2457+
assert.Equal(t, "val1", req.Inputs["opt1"].StringValue())
2458+
assert.Equal(t, "val2", req.Inputs["optTwo"].StringValue())
2459+
return plugin.ConfigureResponse{}, nil
2460+
},
2461+
InvokeF: func(ctx context.Context, req plugin.InvokeRequest) (plugin.InvokeResponse, error) {
2462+
assert.Equal(t, "p1", req.Args["in1"].StringValue())
2463+
assert.Equal(t, "p2", req.Args["inTwo"].StringValue())
2464+
assert.Equal(t, true, req.Args["dryRun"].BoolValue())
2465+
return plugin.InvokeResponse{
2466+
Properties: resource.PropertyMap{"output1": resource.NewProperty("world")},
2467+
}, nil
2468+
},
2469+
},
2470+
}, nil
2471+
}
2472+
2473+
providerFile := writeHCLFile(t, "provider.pcl", "opt1 = \"val1\"\n")
2474+
2475+
var stdout bytes.Buffer
2476+
cmd := NewDoCmd(mlm, mws, loader, testHost, panicLoadConverterPlugin)
2477+
cmd.SetOut(&stdout)
2478+
cmd.SetErr(&stdout)
2479+
cmd.SetArgs([]string{
2480+
"azure:index:myFunction",
2481+
"--provider-file", providerFile,
2482+
"--azure:opt-two", "val2",
2483+
"--in1", "p1",
2484+
"--input:in-two", "p2",
2485+
"--input:dry-run",
2486+
})
2487+
err := cmd.Execute()
2488+
require.NoError(t, err)
2489+
assert.True(t, configureCalled, "Configure should be called with the converted provider config")
2490+
}
2491+
2492+
func TestDoCmdFunctionInvokeWithYAMLFlags(t *testing.T) {
2493+
t.Parallel()
2494+
2495+
configureCalled := false
2496+
mlm := &cmdBackend.MockLoginManager{}
2497+
mws := &pkgWorkspace.MockContext{}
2498+
yamlHost := func() (plugin.Host, error) {
2499+
return &plugin.MockHost{
2500+
LoaderAddrF: func() string { return "loader-address" },
2501+
}, nil
2502+
}
2503+
loadConverter := func(
2504+
_ *plugin.Context, name string, _ func(sev diag.Severity, msg string),
2505+
) (plugin.Converter, error) {
2506+
assert.Equal(t, "yaml", name)
2507+
return &plugin.MockConverter{
2508+
ConvertSnippetF: func(ctx context.Context, req *plugin.ConvertSnippetRequest) (
2509+
*plugin.ConvertSnippetResponse, error,
2510+
) {
2511+
assert.NotEmpty(t, req.TargetLoader)
2512+
switch filepath.Base(req.Filename) {
2513+
case "provider.yaml":
2514+
assert.Equal(t, "opt1: val1\n", string(req.Source))
2515+
assert.Equal(t, map[string]string{
2516+
"optTwo": "val2",
2517+
}, req.Attributes)
2518+
// The converter should be told this is a provider-config snippet via the provider's resource token,
2519+
// not the function token.
2520+
assert.Equal(t, "pulumi:providers:azure", req.Token)
2521+
require.NotNil(t, req.Package)
2522+
assert.Equal(t, "azure", req.Package.Package)
2523+
return &plugin.ConvertSnippetResponse{
2524+
Filename: "provider.pp",
2525+
Source: []byte(`opt1 = "val1"` + "\n"),
2526+
Attributes: map[string]string{
2527+
"optTwo": "\"val2\"",
2528+
},
2529+
}, nil
2530+
case "inputs.yaml":
2531+
assert.Equal(t, "in1: file\n", string(req.Source))
2532+
assert.Equal(t, map[string]string{
2533+
"dryRun": "true",
2534+
"in1": "p1",
2535+
"inTwo": "p2",
2536+
}, req.Attributes)
2537+
assert.Equal(t, "azure:index:myFunction", req.Token)
2538+
require.NotNil(t, req.Package)
2539+
assert.Equal(t, "azure", req.Package.Package)
2540+
return &plugin.ConvertSnippetResponse{
2541+
Filename: "inputs.pp",
2542+
Source: []byte(`in1 = "file"` + "\n"),
2543+
Attributes: map[string]string{
2544+
"in1": "\"p1\"",
2545+
"inTwo": "\"p2\"",
2546+
"dryRun": "true",
2547+
},
2548+
}, nil
2549+
default:
2550+
require.Failf(t, "unexpected converter input", "filename: %s", req.Filename)
2551+
return nil, nil
2552+
}
2553+
},
2554+
}, nil
2555+
}
2556+
loader := func(ctx context.Context, pctx *plugin.Context, wd, source string) (plugin.Provider, error) {
2557+
assert.Equal(t, "azure", source)
2558+
spec := schema.PackageSpec{
2559+
Name: "azure",
2560+
Provider: schema.ResourceSpec{
2561+
InputProperties: map[string]schema.PropertySpec{
2562+
"opt1": {TypeSpec: schema.TypeSpec{Type: "string"}},
2563+
"optTwo": {TypeSpec: schema.TypeSpec{Type: "string"}},
2564+
},
2565+
},
2566+
Functions: map[string]schema.FunctionSpec{
2567+
"azure:index:myFunction": {
2568+
Inputs: &schema.ObjectTypeSpec{
2569+
Properties: map[string]schema.PropertySpec{
2570+
"in1": {TypeSpec: schema.TypeSpec{Type: "string"}},
2571+
"inTwo": {TypeSpec: schema.TypeSpec{Type: "string"}},
2572+
"dryRun": {TypeSpec: schema.TypeSpec{Type: "boolean"}},
2573+
},
2574+
},
2575+
Outputs: &schema.ObjectTypeSpec{
2576+
Properties: map[string]schema.PropertySpec{
2577+
"output1": {TypeSpec: schema.TypeSpec{Type: "string"}},
2578+
},
2579+
},
2580+
},
2581+
},
2582+
}
2583+
return &testProvider{
2584+
spec: spec,
2585+
MockProvider: plugin.MockProvider{
2586+
ConfigureF: func(ctx context.Context, req plugin.ConfigureRequest) (plugin.ConfigureResponse, error) {
2587+
configureCalled = true
2588+
// The converted PCL ("opt1 = \"val1\"") + optTwo=val2 should be bound, evaluated, and reach Configure intact.
2589+
assert.Equal(t, "val1", req.Inputs["opt1"].StringValue())
2590+
assert.Equal(t, "val2", req.Inputs["optTwo"].StringValue())
2591+
return plugin.ConfigureResponse{}, nil
2592+
},
2593+
InvokeF: func(ctx context.Context, req plugin.InvokeRequest) (plugin.InvokeResponse, error) {
2594+
assert.Equal(t, "p1", req.Args["in1"].StringValue())
2595+
assert.Equal(t, "p2", req.Args["inTwo"].StringValue())
2596+
assert.Equal(t, true, req.Args["dryRun"].BoolValue())
2597+
return plugin.InvokeResponse{
2598+
Properties: resource.PropertyMap{"output1": resource.NewProperty("world")},
2599+
}, nil
2600+
},
2601+
},
2602+
}, nil
2603+
}
2604+
2605+
providerFile := writeHCLFile(t, "provider.yaml", "opt1: val1\n")
2606+
inputFile := writeHCLFile(t, "inputs.yaml", "in1: file\n")
2607+
2608+
var stdout bytes.Buffer
2609+
cmd := NewDoCmd(mlm, mws, loader, yamlHost, loadConverter)
2610+
cmd.SetOut(&stdout)
2611+
cmd.SetErr(&stdout)
2612+
cmd.SetArgs([]string{
2613+
"azure:index:myFunction",
2614+
"--provider-file", providerFile,
2615+
"--input-file", inputFile, "--input", "yaml",
2616+
"--azure:opt-two", "val2",
2617+
"--in1", "p1",
2618+
"--input:in-two", "p2",
2619+
"--input:dry-run",
2620+
})
2621+
err := cmd.Execute()
2622+
require.NoError(t, err)
2623+
assert.True(t, configureCalled, "Configure should be called with the converted provider config")
2624+
}

‎pkg/cmd/pulumi/do/do_resource.go‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func resourceSchemaHelp(res *schema.Resource) string {
5353
b.WriteString(title)
5454
b.WriteString(":\n")
5555
for _, property := range properties {
56-
fmt.Fprintf(&b, " %s (%s", property.Name, schemaTypeString(property.Type))
56+
fmt.Fprintf(&b, " %s (%s", property.Name, unwrapType(property.Type))
5757
if includeRequired {
5858
if property.IsRequired() {
5959
b.WriteString(", required")
@@ -104,6 +104,7 @@ func (pc *packageCommand) newResourceCommand(res *schema.Resource) *cobra.Comman
104104
"Path to a file containing provider configuration")
105105
cmd.PersistentFlags().StringVar(&pc.format, "input", "pcl",
106106
"Format of the provider configuration file")
107+
addPersistentInputFlags(cmd, pc.spec.Name, pc.spec.Provider.InputProperties)
107108
cmd.AddCommand(pc.newResourceCreateCommand(res))
108109
cmd.AddCommand(pc.newResourceReadCommand(res))
109110
cmd.AddCommand(pc.newResourcePatchCommand(res))
@@ -126,13 +127,14 @@ func (pc *packageCommand) newResourceCreateCommand(res *schema.Resource) *cobra.
126127
return err
127128
}
128129
ctx := cmd.Context()
129-
if err := pc.configureProvider(ctx); err != nil {
130+
if err := pc.configureProvider(cmd, ctx); err != nil {
130131
return err
131132
}
132133
urn := resourceURN(res)
133134
inputs, err := evaluateResourceFile(
134135
ctx, inputFile, "input", pc.format, res, pc.evalContext,
135-
pc.converter, pc.loaderTarget, pc.packageDescriptor)
136+
pc.converter, pc.loaderTarget, pc.packageDescriptor,
137+
collectInputFlags(cmd, "input", res.InputProperties))
136138
if err != nil {
137139
return fmt.Errorf("parse input file: %w", err)
138140
}
@@ -167,6 +169,7 @@ func (pc *packageCommand) newResourceCreateCommand(res *schema.Resource) *cobra.
167169
cmd.Flags().StringVar(&inputFile, "input-file", "", "Path to a file containing resource inputs")
168170
cmd.Flags().BoolVar(&yes, "yes", false,
169171
"Automatically approve and perform the operation without a confirmation prompt")
172+
addInputFlags(cmd, "input", res.InputProperties)
170173
return cmd
171174
}
172175

@@ -177,7 +180,7 @@ func (pc *packageCommand) newResourceReadCommand(res *schema.Resource) *cobra.Co
177180
Args: cobra.ExactArgs(1),
178181
RunE: func(cmd *cobra.Command, args []string) error {
179182
ctx := cmd.Context()
180-
if err := pc.configureProvider(ctx); err != nil {
183+
if err := pc.configureProvider(cmd, ctx); err != nil {
181184
return err
182185
}
183186
urn := resourceURN(res)
@@ -217,7 +220,7 @@ func (pc *packageCommand) newResourcePatchCommand(res *schema.Resource) *cobra.C
217220
return err
218221
}
219222
ctx := cmd.Context()
220-
if err := pc.configureProvider(ctx); err != nil {
223+
if err := pc.configureProvider(cmd, ctx); err != nil {
221224
return err
222225
}
223226
urn := resourceURN(res)
@@ -240,7 +243,8 @@ func (pc *packageCommand) newResourcePatchCommand(res *schema.Resource) *cobra.C
240243
// would otherwise reject any partial patch that omits a required input.
241244
patch, err := evaluateResourceFile(
242245
ctx, inputFile, "input", inputFormat, res, pc.evalContext,
243-
pc.converter, pc.loaderTarget, pc.packageDescriptor, pcl.AllowMissingProperties)
246+
pc.converter, pc.loaderTarget, pc.packageDescriptor,
247+
collectInputFlags(cmd, "input", res.InputProperties), pcl.AllowMissingProperties)
244248
if err != nil {
245249
return fmt.Errorf("parse input file: %w", err)
246250
}
@@ -294,6 +298,7 @@ func (pc *packageCommand) newResourcePatchCommand(res *schema.Resource) *cobra.C
294298
cmd.Flags().StringVar(&inputFile, "input-file", "", "Path to a file containing resource inputs")
295299
cmd.Flags().BoolVar(&yes, "yes", false,
296300
"Automatically approve and perform the operation without a confirmation prompt")
301+
addInputFlags(cmd, "input", res.InputProperties)
297302
return cmd
298303
}
299304

@@ -308,7 +313,7 @@ func (pc *packageCommand) newResourceDeleteCommand(res *schema.Resource) *cobra.
308313
return err
309314
}
310315
ctx := cmd.Context()
311-
if err := pc.configureProvider(ctx); err != nil {
316+
if err := pc.configureProvider(cmd, ctx); err != nil {
312317
return err
313318
}
314319
urn := resourceURN(res)
@@ -347,13 +352,14 @@ func (pc *packageCommand) newResourceListCommand(res *schema.Resource) *cobra.Co
347352
return errors.New("--all and --count are mutually exclusive")
348353
}
349354
ctx := cmd.Context()
350-
if err := pc.configureProvider(ctx); err != nil {
355+
if err := pc.configureProvider(cmd, ctx); err != nil {
351356
return err
352357
}
353358

354359
query, err := evaluateResourceListFile(
355360
ctx, inputFile, "input", inputFormat, res, pc.evalContext,
356-
pc.converter, pc.loaderTarget, pc.packageDescriptor)
361+
pc.converter, pc.loaderTarget, pc.packageDescriptor,
362+
collectInputFlags(cmd, "input", res.ListInputs.Properties))
357363
if err != nil {
358364
return fmt.Errorf("parse input file: %w", err)
359365
}
@@ -408,13 +414,15 @@ func (pc *packageCommand) newResourceListCommand(res *schema.Resource) *cobra.Co
408414
cmd.Flags().StringVar(&inputFile, "input-file", "", "Path to a file containing resource list inputs")
409415
cmd.Flags().BoolVar(&all, "all", false, "Enumerate all matching resources")
410416
cmd.Flags().Int64Var(&count, "count", 0, "Enumerate up to count matching resources")
417+
addInputFlags(cmd, "input", res.ListInputs.Properties)
411418
return cmd
412419
}
413420

414421
func evaluateResourceListFile(
415422
ctx context.Context, path, fileType, inputFormat string, res *schema.Resource, evalContext functionEvalContext,
416423
loadConverter func(string) (plugin.Converter, error), loaderTarget string,
417424
packageDescriptor *codegenrpc.GetSchemaRequest,
425+
inputFlags map[string]inputFlagValue,
418426
) (resource.PropertyMap, error) {
419427
contract.Assertf(res.ListInputs != nil, "should not call evaluateResourceListFile for resources without list inputs")
420428

@@ -424,6 +432,7 @@ func evaluateResourceListFile(
424432
}
425433
return evaluateFile(
426434
ctx, path, fileType, inputFormat, res.Token, bind, loadConverter, loaderTarget, packageDescriptor, evalContext,
435+
inputFlags,
427436
)
428437
}
429438

0 commit comments

Comments
 (0)