ananace / fog-hyperv

Hyper-V provider for fog
MIT License
8 stars 1 forks source link

Use argument splatting #6

Open ananace opened 6 years ago

ananace commented 6 years ago

To replace stupidly long command lists with something more shell-ish. New-VM -BootDevice NetworkAdapter -Generation 1 -Name aimee-nappo.example.com -NewVHDPath C:\VHD\Disk.vhdx -NewVHDSizeBytes 68719467636 -SwitchName net_switch -MemoryStartupBytes 536870912 Would instead become;

$Args = @(
  'BootDevice' = "NetworkAdapter";
  'Generation' = 1;
  'Name' = "aimee-nappo.example.com";
  'NewVHDPath' = "C:\VHD\Disk.vhdx";
  'NewVHDSizeBytes' = 68719467636;
  'SwitchName' = "net_switch";
  'MemoryStartupBytes' = 536870912;
)
New-VM @Args

This would probably also allow transferring argument values in JSON format, which would drastically reduce the risk of encoding problems and code execution issues.

ananace commented 6 years ago

This might be a good thing to prioritize doing - if only to make object passing easier to support;

$Args = @(
  'Id' = '<guid>';
  'ComputerName' = '<computer>';
)
$VM = Get-VM @Args
$Args = @(
  'VM' = $VM;
  'ComputerName' = '<computer>';
  'Switch' = '<switch>';
)
Add-VMNetworkAdapter @Args

Or maybe even something alike;

$VMArgs = @(
  'Id' = '<guid>';
  'ComputerName' = '<computer>';
)
$Args = @(
  'VM' = Get-VM @VMArgs;
  'ComputerName' = '<computer>';
  'Switch' = '<switch>';
)
Add-VMNetworkAdapter @Args
ananace commented 6 years ago

Code is now available for argument splatting, and can be enabled with connection or method level flags.

c = Fog::Compute.new provider: :HyperV, hyperv_host: '...' # ...

c.bake_optmap = true # Splat arguments into pre-baked option maps
c.bake_json   = true # Additionally bake option maps by transferring JSON blobs

The code works in many places, but there are cases where the sending of false and nil leads to other responses from the cmdlets.