@@ -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+ }
0 commit comments