Need to duplicate a tag response from 1 API by getting rules data and then posting the rules data with a new tag name Initial Get response from API
``` {
"metadata": {
"configurationVersions": [
7
],
"clusterVersion": "1.200.99.20200828-133130"
},
"id": "a257c448-7cbf-48cd-9cdf-a58d60ade6ef",
"name": "Billing",
"rules": [
{
"type": "SERVICE",
"enabled": true,
"valueFormat": null,
"propagationTypes": [
"SERVICE_TO_PROCESS_GROUP_LIKE",
"SERVICE_TO_HOST_LIKE"
],
"conditions": [
{
"key": {
"attribute": "PROCESS_GROUP_PREDEFINED_METADATA",
"dynamicKey": "KUBERNETES_NAMESPACE",
"type": "PROCESS_PREDEFINED_METADATA_KEY"
},
"comparisonInfo": {
"type": "STRING",
"operator": "CONTAINS",
"value": "billing-test",
"negate": false,
"caseSensitive": false
}
}
]
},
{
"type": "SERVICE",
"enabled": true,
"valueFormat": null,
"propagationTypes": [
"SERVICE_TO_PROCESS_GROUP_LIKE",
"SERVICE_TO_HOST_LIKE"
],
"conditions": [
{
"key": {
"attribute": "PROCESS_GROUP_PREDEFINED_METADATA",
"dynamicKey": "KUBERNETES_NAMESPACE",
"type": "PROCESS_PREDEFINED_METADATA_KEY"
},
"comparisonInfo": {
"type": "STRING",
"operator": "CONTAINS",
"value": "billingservices-test",
"negate": false,
"caseSensitive": false
}
}
]
}
]}```
I'm only retrieving the rules from above api and removing the other properties. When I use post api, the rules are not similar to the initial get response above. I'm sure i'm not parsing the json response properly. Clueless as to how to get the json like the former one for conditions and propagation types. tried creating lists and hashtables but running into errors.
"name": "Billingnew",
"rules": [
{
"type": "SERVICE",
"enabled": true,
"valueFormat": null,
"propagationTypes": "SERVICE_TO_PROCESS_GROUP_LIKE SERVICE_TO_HOST_LIKE",
"conditions": ""
},
{
"type": "SERVICE",
"enabled": true,
"valueFormat": null,
"propagationTypes": "SERVICE_TO_PROCESS_GROUP_LIKE SERVICE_TO_HOST_LIKE",
"conditions": ""
}
]
}
powershell script below.
#param([string]$Arguments)
#$oldtagName,$newTagName=$Arguments.split(",")
#Write-Host $tagName,$userid
$oldtagName = "Billing"
$newTagName = "Bill5"
$input = "C:\Users\$userid\rules_$oldtagName.csv"
$uri="{{nonprod}}/api/config/v1/autoTags"
$url = "{{nonprod}}/api/config/v1/autoTags?"
$Tags = (Invoke-RestMethod -Method Get -Uri $url)
#Write-Host $Tags.values
$jsonBase = @{}
foreach ( $tag in $Tags.values)
{
$tagName=$tag.name
$tagId=$tag.id
$rules = @{}
if ( $oldtagName -eq $tagName){
Write-Host $tagName,$tagId
$aurl = "{{nonprod}}/api/config/v1/autoTags/$tagId/?"
$taginfo = (Invoke-RestMethod -Method Get -Uri $aurl)
$tagName = $taginfo.name
$taginfo.PSObject.Properties.Remove('metadata')
$taginfo.PSObject.Properties.Remove('id')
$taginfo.PSObject.Properties.Remove('name')
$rule = $taginfo | Select-Object -Property rules
$taginfo
$jsonBase.add("name",$newTagName)
$jsonBase.add("rules", $taginfo.rules)
$jsonBase
$data= $jsonBase | ConvertTo-Json
Write-Host "after" $data
}#if tag end
}#for each end
$headers = @{ Accept="application/json";Authorization="Api-Token <removed>"}
$newTagInfo = (Invoke-RestMethod $uri -Headers $headers -Method Post -Body $data -ContentType "application/json")
$newTagId = $newTagInfo.id
$newTagName = $newTagInfo.name
Write-Host $newTagInfo.id, $newTagInfo.name