Skip to content
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

Add id validation to initialization process (#5) #6

Merged
merged 1 commit into from Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Functions/Invoke-PasmInitialize.ps1
Expand Up @@ -120,6 +120,20 @@ Resource: # required
- ap-northeast-2
'@

# Pattern match validation 'VpcId' and 'AssociationSubnetId'
if ($PSBoundParameters.ContainsKey('VpcId')) {
if ($vpcId -cnotmatch '^vpc-[0-9a-z]{17}$') {
throw [InvalidOperationException]::new($('''{0}'' does not match a valid id pattern.' -f $vpcId))
}
}
if ($PSBoundParameters.ContainsKey('SubnetId')) {
foreach ($id in $SubnetId) {
if ($id -cnotmatch '^subnet-[0-9a-z]{17}$') {
throw [InvalidOperationException]::new($('''{0}'' does not match a valid id pattern.' -f $id))
}
}
}

# If 'VpcId' or 'SubnetId' is passed from the parameter, it will overwrite the value in the sample template
$isPresent_VpcId = $PSBoundParameters.ContainsKey('VpcId')
$isPresent_SubnetId = $PSBoundParameters.ContainsKey('SubnetId')
Expand Down
37 changes: 23 additions & 14 deletions tests/Pasm.Tests.ps1
Expand Up @@ -168,21 +168,32 @@ InModuleScope 'Pasm' {
Describe 'UnitTest' {
BeforeAll {
$script:workingDirectory = $PSScriptRoot, '.work' -join $sepalator
$script:path = [path]::GetDirectoryName($workingDirectory)
$script:name = [path]::GetFileName($workingDirectory)
$script:obj = New-PasmTestVpc
}
Context 'InitializeWithTargetVpcParameter' {
BeforeAll {
$script:path = [path]::GetDirectoryName($workingDirectory)
$script:name = [path]::GetFileName($workingDirectory)
}
It 'Initialize: VpcId' {
Invoke-PasmInitialize -Path $path -Name $name -VpcId $obj.VpcId -Force -WarningAction SilentlyContinue | Should -BeTrue
}
It 'Initialize: AssociationSubnetId' {
Invoke-PasmInitialize -Path $path -Name $name -SubnetId $obj.SubnetId_A, $obj.SubnetId_C -Force -WarningAction SilentlyContinue | Should -BeTrue
Context 'ExpectedToPass' {
It 'Initialize: VpcId' {
Invoke-PasmInitialize -Path $path -Name $name -VpcId $obj.VpcId -Force -WarningAction SilentlyContinue | Should -BeTrue
}
It 'Initialize: AssociationSubnetId' {
Invoke-PasmInitialize -Path $path -Name $name -SubnetId $obj.SubnetId_A, $obj.SubnetId_C -Force -WarningAction SilentlyContinue | Should -BeTrue
}
It 'Initialize: Both' {
Invoke-PasmInitialize -Path $path -Name $name -VpcId $obj.VpcId -SubnetId $obj.SubnetId_A, $obj.SubnetId_C -Force -WarningAction SilentlyContinue | Should -BeTrue
}
}
It 'Initialize: Both' {
Invoke-PasmInitialize -Path $path -Name $name -VpcId $obj.VpcId -SubnetId $obj.SubnetId_A, $obj.SubnetId_C -Force -WarningAction SilentlyContinue | Should -BeTrue
Context 'ExpectedToThrow' {
It 'Initialize: VpcId' {
{ Invoke-PasmInitialize -Path $path -Name $name -VpcId 'Vpc-01234567891234567' -Force -WarningAction SilentlyContinue } | Should -Throw
}
It 'Initialize: AssociationSubnetId' {
{ Invoke-PasmInitialize -Path $path -Name $name -SubnetId 'subnet-01234567891234567', 'subnet-abcdefghijklmnopq!' -Force -WarningAction SilentlyContinue } | Should -Throw
}
It 'Initialize: Both' {
{ Invoke-PasmInitialize -Path $path -Name $name -VpcId 'vpc-01234567891234567' -SubnetId 'subnet-01234567891234567', 'Subnet-abcdefghijklmnopq' -Force -WarningAction SilentlyContinue } | Should -Throw
}
}
}
Context 'RunWithBasicTemplate1' {
Expand Down Expand Up @@ -376,9 +387,7 @@ InModuleScope 'Pasm' {
$script:blueprintFilePath = $PSScriptRoot, '.work', $blueprintFileName -join $sepalator
}
It 'Alias: psmi' {
$p = [path]::GetDirectoryName($workingDirectory)
$n = [path]::GetFileName($workingDirectory)
psmi -p $p -n $n -vpc $obj.VpcId -sbn $obj.SubnetId_A, $obj.SubnetId_C -Force -WarningAction SilentlyContinue | Should -BeTrue
psmi -p $path -n $name -vpc $obj.VpcId -sbn $obj.SubnetId_A, $obj.SubnetId_C -Force -WarningAction SilentlyContinue | Should -BeTrue
}
It 'Alias: psmv' {
psmv -file $outlineFilePath | Should -BeTrue
Expand Down