From 67b36ca99868107914b6609e7757497814643864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Vicente=20Nu=C3=B1ez=20Morillo?= Date: Thu, 25 Aug 2022 11:57:52 +0200 Subject: [PATCH 1/2] destination id in uri instead of destination1 --- PowerShell/IoTC-Helper.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PowerShell/IoTC-Helper.ps1 b/PowerShell/IoTC-Helper.ps1 index 972c68f..f8a6b1c 100644 --- a/PowerShell/IoTC-Helper.ps1 +++ b/PowerShell/IoTC-Helper.ps1 @@ -408,7 +408,7 @@ Function Get-Roles { } #Get list of all API Tokens in the app -Function Get-Tokens{ +Function Get-Tokens { $Uri = $BaseUrl + "apiTokens?api-version=1.0" $Body = @{} $Parameters = @{ @@ -424,7 +424,7 @@ Function Get-Tokens{ } #Create a new API token for the specified role -Function Add-Token{ +Function Add-Token { Param( [Parameter(Mandatory = $true, Position = 0)] [String]$TokenId, [Parameter(Mandatory = $true, Position = 1)] [String]$Config @@ -443,7 +443,7 @@ Function Add-Token{ } #Get a list of all data export destinations in the app -Function Get-CDEDestinations{ +Function Get-CDEDestinations { $Uri = $BaseUrl + "dataExport/destinations?api-version=1.1-preview" $Body = @{} $Parameters = @{ @@ -459,11 +459,11 @@ Function Get-CDEDestinations{ } #Create a new data export destination -Function Add-Destination{ +Function Add-Destination { Param( [Parameter(Mandatory = $true, Position = 0)] [String]$Config ) - $Uri = $BaseUrl + "dataExport/destinations/destination1?api-version=1.1-preview" + $Uri = $BaseUrl + "dataExport/destinations/" + ($Config | ConvertFrom-Json).id + "?api-version=1.1-preview" $Parameters = @{ Method = "PUT" Uri = $Uri From f1802d8f995c1dfb9980c4618731c17517fa5f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Vicente=20Nu=C3=B1ez=20Morillo?= Date: Thu, 25 Aug 2022 12:24:54 +0200 Subject: [PATCH 2/2] destinations loops .value. $ConfigItem inside loop to avoid $ConfigObj overwrite --- PowerShell/IoTC-Task.ps1 | 144 ++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 70 deletions(-) diff --git a/PowerShell/IoTC-Task.ps1 b/PowerShell/IoTC-Task.ps1 index 447da4d..2529cab 100644 --- a/PowerShell/IoTC-Task.ps1 +++ b/PowerShell/IoTC-Task.ps1 @@ -107,11 +107,11 @@ else { #Compare Data Export Destinations to Config $ConfigDestinationsObj = $ConfigObj."destinations" -if($ConfigDestinationsObj.length -eq 0){ +if ($ConfigDestinationsObj.length -eq 0) { Write-Host " Data Export Destinations not in config file." -ForegroundColor DarkGray -NoNewLine Write-Host " Skipping" -ForegroundColor green } -else{ +else { $CloudDestinationsObj = Get-CDEDestinations -ErrorAction stop $CloudDestinationsObj = $CloudDestinationsObj | ConvertFrom-Json @@ -126,61 +126,63 @@ else{ $ConfigDestinations = $ConfigDestinationsObj | ConvertTo-Json -Depth 100 -Compress $ContentEqual = ($CloudDestinations -eq $ConfigDestinations) - if($ContentEqual){ + if ($ContentEqual) { Write-Host " Data export destinations match config " -ForegroundColor DarkGray -NoNewLine Write-Host @greenCheck } - else{ + else { #Iterate through destinations in config file to find the missing exports - $ConfigDestinationsObj | ForEach-Object { + $ConfigDestinationsObj.value | ForEach-Object { $Id = $_.id - $Name = $_.name - $ConfigObj = $_.value[0] - $ConfigObj.PSObject.Properties.Remove("status") + $Name = $_.displayName + $ConfigItem = $_ + $ConfigItem.PSObject.Properties.Remove("status") $SecretName = "Undefined" - switch($ConfigObj.type){ - "webhook@v1" { - if($ConfigObj.headerCustomizations."x-custom-region".secret -ne $false){ - $SecretName = $ConfigObj.headerCustomizations."x-custom-region".secret - $Secret = az keyvault secret show --vault-name $KeyVault --name $SecretName - $ConfigObj.headerCustomizations."x-custom-region".secret = $Secret - } - Break - } - "dataexplorer@v1" { - $SecretName = $ConfigObj.authorization.clientSecret - $Result = az keyvault secret show --vault-name $KeyVault --name $SecretName - $ResultObj = $Result | ConvertFrom-Json - $Secret = $ResultObj.value - $ConfigObj.authorization.clientSecret = $Secret - Break + switch ($ConfigItem.type) { + "webhook@v1" { + if ($ConfigItem.headerCustomizations."x-custom-region".secret -ne $false) { + $SecretName = $ConfigItem.headerCustomizations."x-custom-region".secret + $Secret = az keyvault secret show --vault-name $KeyVault --name $SecretName + $ConfigItem.headerCustomizations."x-custom-region".secret = $Secret } - Default { - $SecretName = $ConfigObj.authorization.connectionString + Break + } + "dataexplorer@v1" { + $SecretName = $ConfigItem.authorization.clientSecret + $Result = az keyvault secret show --vault-name $KeyVault --name $SecretName + $ResultObj = $Result | ConvertFrom-Json + $Secret = $ResultObj.value + $ConfigItem.authorization.clientSecret = $Secret + Break + } + Default { + if ($ConfigItem.authorization.type -eq "connectionString") { + $SecretName = $ConfigItem.authorization.connectionString $Secret = az keyvault secret show --vault-name $KeyVault --name $SecretName $Result = az keyvault secret show --vault-name $KeyVault --name $SecretName $ResultObj = $Result | ConvertFrom-Json $Secret = $ResultObj.value - $ConfigObj.authorization.connectionString = $Secret + $ConfigItem.authorization.connectionString = $Secret } } + } - $Config = $ConfigObj | ConvertTo-Json -Depth 100 -Compress + $Config = $ConfigItem | ConvertTo-Json -Depth 100 -Compress - if(($CloudDestinations.Length -eq 0) -or ($CloudDestinations -inotmatch $id)) #We need to add this data export - { + if (($CloudDestinations.Length -eq 0) -or ($CloudDestinations -inotmatch $id)) { + #We need to add this data export Write-Host " Adding missing data export destination $name " -ForegroundColor DarkGray -NoNewline $Result = Add-Destination -Config $Config Write-Host @greenCheck } - elseif(($CloudDestinations.Length -gt 0) -and (!$CloudDestinations.Contains($Config))) #We need to update this data export - { + elseif (($CloudDestinations.Length -gt 0) -and (!$CloudDestinations.Contains($Config))) { + #We need to update this data export Write-Host " Updating existing data export destination $name " -ForegroundColor DarkGray -NoNewline $Result = Add-Destination -Config $Config Write-Host @greenCheck } - else{ + else { Write-Host "We didn't do anything with data export destinations" Write-Host "Record: $Record" Write-Host "ID: $Id" @@ -292,54 +294,56 @@ else { Write-Host "##vso[task.LogIssue type=warning;] Roles do not match the config file. Roles will need to be manually updated in the target app." } - #Compare File Uploads to Config - $CloudUploads = Get-FileUploads -ErrorAction stop - $CloudUploadsObj = $CloudUploads | ConvertFrom-Json - $UploadsConfig = $ConfigObj."file uploads" +#Compare File Uploads to Config +$CloudUploads = Get-FileUploads -ErrorAction stop +$CloudUploadsObj = $CloudUploads | ConvertFrom-Json +$UploadsConfig = $ConfigObj."file uploads" - if($UploadsConfig.length -eq 0){ - Write-Host " File uploads not in config file. Skipping." +if ($UploadsConfig.length -eq 0) { + Write-Host " File uploads not in config file. Skipping." +} +else { + #Remove state and etag from the JSON so we can accurately compare the config to the app + $CloudUploadsObj | ForEach-Object { + $_.PSObject.Properties.Remove("state") + $_.PSObject.Properties.Remove("etag") } - else{ - #Remove state and etag from the JSON so we can accurately compare the config to the app - $CloudUploadsObj | ForEach-Object { - $_.PSObject.Properties.Remove("state") - $_.PSObject.Properties.Remove("etag") - } - $ContentEqual = ($CloudUploadsObj | ConvertTo-Json -Compress -Depth 100) -eq ($UploadsConfig | ConvertTo-Json -Compress -Depth 100) + $ContentEqual = ($CloudUploadsObj | ConvertTo-Json -Compress -Depth 100) -eq ($UploadsConfig | ConvertTo-Json -Compress -Depth 100) - if($ContentEqual){ - Write-Host " File uploads match config " + if ($ContentEqual) { + Write-Host " File uploads match config " + } + else { + $Secret = az keyvault secret show --vault-name $KeyVault --name $UploadsConfig.connectionString #Get the secret name from the connection string value in the config + $ConnectionString = ($Secret | ConvertFrom-Json).value + $UploadsConfig.connectionString = $ConnectionString + + if ($CloudUploads -eq "404") { + #There is no file upload configured currently + Write-Host " Adding file uploads config to IoT Central " -ForegroundColor DarkGray -NoNewLine + $UploadsConfig = $UploadsConfig | ConvertTo-Json -Compress -Depth 100 + $UploadsConfig = Add-FileUploads -Config $UploadsConfig + Write-Host @greenCheck } - else{ - $Secret = az keyvault secret show --vault-name $KeyVault --name $UploadsConfig.connectionString #Get the secret name from the connection string value in the config - $ConnectionString = ($Secret | ConvertFrom-Json).value - $UploadsConfig.connectionString = $ConnectionString - - if($CloudUploads -eq "404"){ #There is no file upload configured currently - Write-Host " Adding file uploads config to IoT Central " -ForegroundColor DarkGray -NoNewLine - $UploadsConfig = $UploadsConfig | ConvertTo-Json -Compress -Depth 100 - $UploadsConfig = Add-FileUploads -Config $UploadsConfig - Write-Host @greenCheck - } - else{ #We need to update the existing config - Write-Host " Updating file uploads config in IoT Central " - $UploadsConfig = $UploadsConfig | ConvertTo-Json -Compress -Depth 100 - $UploadsConfig = Add-FileUploads -Config $UploadsConfig - } + else { + #We need to update the existing config + Write-Host " Updating file uploads config in IoT Central " + $UploadsConfig = $UploadsConfig | ConvertTo-Json -Compress -Depth 100 + $UploadsConfig = Add-FileUploads -Config $UploadsConfig } } +} @@ -348,25 +352,25 @@ $CloudTokens = Get-Tokens -ErrorAction stop $CloudTokensObj = $CloudTokens | ConvertFrom-Json $TokensConfig = $ConfigObj."APITokens" -if($TokensConfig.length -eq 0){ +if ($TokensConfig.length -eq 0) { Write-Host " API Tokens not in config file." -ForegroundColor DarkGray -NoNewLine Write-Host " Skipping" -ForegroundColor green } -else{ +else { $ContentEqual = ($CloudTokensObj | ConvertTo-Json -Compress -Depth 100) -eq ($TokensConfig | ConvertTo-Json -Compress -Depth 100) - if($ContentEqual){ + if ($ContentEqual) { Write-Host " API tokens match config " -ForegroundColor DarkGray -NoNewLine Write-Host @greenCheck } - else{ + else { #Iterate through API tokens in config file to find the missing tokens $TokensConfig."value" | ForEach-Object { $id = $_.id $_.PSObject.Properties.remove("expiry") - if($CloudTokens -inotmatch $id) #We need to add this API token - { + if ($CloudTokens -inotmatch $id) { + #We need to add this API token Write-Host " Adding missing API token $id " -ForegroundColor DarkGray -NoNewline $Config = $_ | ConvertTo-Json -Depth 100 -Compress $Result = Add-Token -TokenId $id -Config $Config