Skip to content

Commit 5177c3d

Browse files
authored
better CM SaaS tag value support (#378)
1 parent 4eb6d1d commit 5177c3d

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

VenafiPS/Private/Get-VcData.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,24 @@ function Get-VcData {
249249
$allObject = $script:vcTag
250250

251251
if ( $InputObject ) {
252-
$thisObject = $allObject | Where-Object tagId -eq $InputObject
252+
# tags can be specified as name or name:value
253+
# ensure it exists either way
254+
if ( $InputObject.Contains(':') ) {
255+
$key, $value = $InputObject.Split(':', 2)
256+
$thisObject = $allObject | Where-Object { $_.tagId -eq $key -and $value -in $_.value }
257+
}
258+
else {
259+
$thisObject = $allObject | Where-Object tagId -eq $InputObject
260+
}
253261
if ( -not $thisObject -and -not $latest ) {
254262
$script:vcTag = Get-VcTag -All | Sort-Object -Property tagId
255-
$thisObject = $script:vcTag | Where-Object tagId -eq $InputObject
263+
if ( $InputObject.Contains(':') ) {
264+
$key, $value = $InputObject.Split(':', 2)
265+
$thisObject = $allObject | Where-Object { $_.tagId -eq $key -and $value -in $_.value }
266+
}
267+
else {
268+
$thisObject = $allObject | Where-Object tagId -eq $InputObject
269+
}
256270
}
257271
}
258272
break
@@ -316,7 +330,10 @@ function Get-VcData {
316330

317331
'Name' {
318332
switch ($Type) {
319-
'Tag' { $InputObject }
333+
'Tag' {
334+
$InputObject
335+
}
336+
320337
{ $_ -in 'Application', 'Team' } {
321338
$returnObject.name
322339
}

VenafiPS/Public/Get-VcTag.ps1

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
function Get-VcTag {
22
<#
33
.SYNOPSIS
4-
Get tags from TLSPC
4+
Get tag names and values
55
66
.DESCRIPTION
77
Get 1 or all tags.
88
Tag values will be provided.
99
1010
.PARAMETER Tag
11-
Tag name
11+
Tag name or name:value pair to get.
12+
If a value is provided, the tag must have that value to be returned.
1213
1314
.PARAMETER All
1415
Get all tags
@@ -26,6 +27,11 @@ function Get-VcTag {
2627
2728
Get a single tag
2829
30+
.EXAMPLE
31+
Get-VcTag -Tag 'MyTag:MyValue'
32+
33+
Get a single tag only if it has the specified value
34+
2935
.EXAMPLE
3036
Get-VcTag -All
3137
@@ -56,21 +62,52 @@ function Get-VcTag {
5662
process {
5763

5864
if ( $PSCmdlet.ParameterSetName -eq 'All' ) {
59-
$values = Invoke-VenafiRestMethod -UriLeaf 'tags/values' | Select-Object -ExpandProperty values
60-
$response = Invoke-VenafiRestMethod -UriLeaf 'tags' | Select-Object -ExpandProperty tags
61-
}
62-
else {
63-
$response = Invoke-VenafiRestMethod -UriLeaf "tags/$Tag"
64-
$values = Invoke-VenafiRestMethod -UriLeaf "tags/$Tag/values" | Select-Object -ExpandProperty values
65-
}
65+
$allTags = Invoke-VenafiRestMethod -UriLeaf 'tags' | Select-Object -ExpandProperty tags
66+
$allValues = Invoke-VenafiRestMethod -UriLeaf 'tags/values' | Select-Object -ExpandProperty values
6667

67-
if ( $response ) {
68-
$response | Select-Object @{'n' = 'tagId'; 'e' = { $_.key } },
68+
$allTags | Select-Object @{'n' = 'tagId'; 'e' = { $_.key } },
6969
@{
7070
'n' = 'value'
7171
'e' = {
7272
$thisId = $_.id
73-
, @(($values | Where-Object { $_.tagId -eq $thisId }).value)
73+
$thisTagValues = $allValues | Where-Object tagId -eq $thisId
74+
if ( $thisTagValues ) {
75+
@($thisTagValues.value)
76+
}
77+
else {
78+
$null
79+
}
80+
}
81+
}
82+
}
83+
else {
84+
if ($Tag.Contains(':')) {
85+
$requestName, $requestValue = $Tag.Split(':', 2)
86+
}
87+
else {
88+
$requestName = $Tag
89+
}
90+
91+
$thisTag = Invoke-VenafiRestMethod -UriLeaf "tags/$requestName"
92+
$thisTagValues = Invoke-VenafiRestMethod -UriLeaf "tags/$requestName/values" | Select-Object -ExpandProperty values
93+
94+
if ( $thisTag ) {
95+
96+
if ( $requestValue ) {
97+
if ( $thisTagValues ) {
98+
if ( -not ( $requestValue -in $thisTagValues.value ) ) {
99+
Write-Verbose "The tag '$requestName' exists but does not have a value of '$requestValue'"
100+
return
101+
}
102+
}
103+
else {
104+
Write-Verbose "The tag '$requestName' was found but does not have any values"
105+
return
106+
}
107+
}
108+
109+
return @{
110+
$thisTag.key = $thisTagValues.value
74111
}
75112
}
76113
}

0 commit comments

Comments
 (0)