2

I am trying to use the task FileTransform to modify the values of grafana json templates. And it's working for modifying the values of somes keys the following way:

- task: FileTransform@2
  displayName: "Transform Jsons"
  inputs:
    folderPath: 'metrics/dashboards/**/'
    xmlTransformationRules: ''
    jsonTargetFiles: '**/*.json'

And having declared the variables with the keys to substitute:

  templating.list.0.query: $(azureClusterName)
  templating.list.0.current.text: $(azureClusterName)
  templating.list.0.current.value: $(azureClusterName)
  templating.list.0.options.0.text: $(azureClusterName)
  templating.list.0.options.0.value: $(azureClusterName)

If in jsonTargetFiles I only declare one file it works perfectly, but I want to know how can I assign different values for files that have the same keys.

I've tried using "replaceTokens", and having different variable names inside of the jsons files:

- task: replacetokens@3
  displayName: 'Replace tokens'
  inputs:
    rootDirectory: 'metrics/dashboards'
    targetFiles: '**/*.json'
    encoding: 'auto'
    verbosity: 'detailed'
    actionOnMissing: 'fail'
    tokenPrefix: '#{'
    tokenSuffix: '}#'

But with replace tokens the template in grafana doesn't work even it says that the values have been replaced correctly.

Best

Javi Hernandez
  • 314
  • 8
  • 17

1 Answers1

0

How can I assign different values for files that have the same keys.

You can use a extension called Magic Chunks

Here is an example:

In transformations, Find the variable you want to assign to using {Node A}/{Node B}/... and specify the value of the variable

- task: MagicChunks@2
  inputs:
    sourcePath: '{target json file path}'
    fileType: 'Auto'
    targetPathType: 'source'
    transformationType: 'json'
    transformations: |
      {
        "ConnectionStrings/DefaultConnection": "Data Source=10.0.0.5;Initial Catalog=Db1"
      }

Target JSON file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=webapp"
  }
}

Output JSON file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=10.0.0.5;Initial Catalog=Db1"
  }
}

But with replace tokens the template in grafana doesn't work even it says that the values have been replaced correctly.

The logic of using the Replace Token task is different from using the File Transform task. In the Replace Token task, you need to place the string you want to replace in specific tokens (defined in tokenPrefix and tokenSuffix). In addition, in variables, you need to place the strings need to be replaced on the left and the strings used to replace on the right. Here is an example:

variables:
  enabled: disabled

- task: replacetokens@3
  inputs:
    targetFiles: 'A.json'
    encoding: 'auto'
    writeBOM: true
    verbosity: 'detailed'
    actionOnMissing: 'fail'
    keepToken: false
    tokenPrefix: '#{'
    tokenSuffix: '}'
    useLegacyPattern: false
    enableTelemetry: true

Target JSON file:

{
  "B": "#{enabled}"
}

Output JSON file:

{
  "B": "disabled"
}
Jane Ma-MSFT
  • 4,461
  • 1
  • 6
  • 12
  • Hello, thank you for your answer. In the first question that you ask, after the replacement the keys in the file A.json and B.json will have the same value. Is there any way of using File Transform to assign to A.json and B.json different values to keys that are the same?. I tried with replace tokens the way that you mentioned, but the template in grafana doesn't work, even it has the correct values. Only works for me with File Transform – Javi Hernandez Dec 23 '20 at 10:23
  • @Javi Hernandez Hello, I have updated my answer about the first question. Please check whether it can help you. I have tried Replace Token task in my side and it works well. Are there `0 tokens replaced out of 0` in the running log of this task? Or does it correctly show the number of strings that should be replaced, but not actually be replaced in the result? – Jane Ma-MSFT Dec 24 '20 at 08:47