deploy_mapping_info_records.py 1.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#  Copyright 2021 Google LLC
#  Copyright 2021 EPAM Systems, Inc
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import json
import os
from os import listdir
from os.path import isfile, join
from google.cloud import datastore

local_folder = os.path.join(os.path.dirname(__file__),"../../mapping_info_records/opendes")
wks_namespace = os.environ.get("WKS_NAMESPACE")
kind = os.environ.get("WKS_KIND")
tenant = os.environ.get("WKS_TENANT")
datastore_client = datastore.Client(namespace=wks_namespace)


def upload_records():
  files = [f for f in listdir(local_folder) if isfile(join(local_folder, f))]
  for file in files:
    local_file = join(local_folder, file)
    with open(local_file) as f:
      data = json.load(f)
      data['kind'] = data['kind'].replace('opendes', tenant)
      data['sourceSchemaAuthority'] = data['sourceSchemaAuthority'].replace('opendes', tenant)
      data['sourceSchemaKind'] = data['sourceSchemaKind'].replace('opendes', tenant)
      complete_key = datastore_client.key(kind, data['id'])
      mapping = datastore.Entity(key=complete_key)
      mapping.update(data)
      datastore_client.put(mapping)


if __name__ == "__main__":
  upload_records()