объединить списки словарей в terraform v0.12 - PullRequest
1 голос
/ 05 марта 2020

Я хотел бы сделать следующее, используя terraform:

У меня есть 2 JSON:

1. json:

[
    {
        "description": "description1",
        "url":         "url1",
        "data":        "data1"
    },
    {
        "description": "description2",
        "url":         "url2",
        "data":        "data2",
        "action":        "action2"
    },
    {
        "description": "description3",
        "url":         "url3",
        "data":        "data3"
    }
]

2. json :

[
    {  
        "description": "description1",
        "url":         "url1",
        "data":        "data1"
    },
    {
        "description": "description2_new",
        "url":         "url2",
        "data":        "data2_new"
    },
    {
        "description": "description4",
        "url":         "url4",
        "data":        "data4"
    }
]

и я хочу объединить их в один. Словари второго JSON должны переопределять словари первого, если ключ url совпадает. Т.е. в совокупности JSON должно выглядеть так:

[
    {
        "description": "description1",
        "url":         "url1",
        "data":        "data1"
    },
    {
        "description": "description2_new",
        "url":         "url2",
        "data":        "data2_new"
    },
    {
        "description": "description3",
        "url":         "url3",
        "data":        "data3"
    },
    {
        "description": "description4",
        "url":         "url4",
        "data":        "data4"
    }
]

Использование python Я легко могу это сделать:

import json
with open('1.json') as f:
  json1 = json.load(f)
with open('2.json') as f:
  json2 = json.load(f)


def list_to_dict(json_list):
    res_dict = {}
    for d in json_list:
        res_dict[d['url']] = d
    return res_dict

def merge_json(json1, json2):
    j1 = list_to_dict(json1)
    j2 = list_to_dict(json2)
    j1.update(j2)
    res_list = []
    for key in j1.keys():
        res_list.append(j1[key])
    return res_list


print(json.dumps(merge_json(json1, json2), indent=4))

Как я могу это сделать с помощью terraform?

1 Ответ

1 голос
/ 05 марта 2020

Использование terraform 0.12.x

$ cat main.tf

locals {
  # read from files and turn into json
  list1 = jsondecode(file("1.json"))
  list2 = jsondecode(file("2.json"))
  # iterate over lists and turn url into a unique key
  dict1 = { for item in local.list1 : item.url => item }
  dict2 = { for item in local.list2 : item.url => item }
  # combine both dictionaries so values converge
  # only take its values
  merged = values(merge(local.dict1, local.dict2))
}

output "this" {
  value = local.merged
}
$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

this = [
  {
    "data" = "data1"
    "description" = "description1"
    "url" = "url1"
  },
  {
    "data" = "data2_new"
    "description" = "description2_new"
    "url" = "url2"
  },
  {
    "data" = "data3"
    "description" = "description3"
    "url" = "url3"
  },
  {
    "data" = "data4"
    "description" = "description4"
    "url" = "url4"
  },
]
...