0

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
Anu
  • 1
  • I would suggest to try step-by-step to create the JSON object. `@{}` is a dictionary, `@()` is a list. Finally, keep in mind to set the correct `-Depth` when converting it to JSON. – Alex_P Sep 23 '20 at 07:16
  • I've reproduced the issue here, and as per @Alex_P, if you add the ```-Depth``` parameter to your ```ConvertTo-Json``` you'll get the result that I think you're expecting - e.g. ```$data = $jsonBase | ConvertTo-Json -Depth 99```. – mclayton Sep 23 '20 at 10:16
  • Thank you so much! That helped me! I did add the depth earlier. But i missed out on that when i scraped everything trying to find out what the issue was. – Anu Sep 23 '20 at 15:43

0 Answers0