-
Notifications
You must be signed in to change notification settings - Fork 31
/
gettransfermapping.py
executable file
·187 lines (137 loc) · 5.65 KB
/
gettransfermapping.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
# November 2022
#
# pass in a mapping set name and download to a mapping file for use with the Viya CLI
#
# Change History
#
# 06NOV2022 Initial Development
#
#
# Copyright © 2022, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# 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
#
# http://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 Python modules
from __future__ import print_function
import argparse, json, os, sys
from sharedfunctions import callrestapi, printresult, getidsanduris
parser = argparse.ArgumentParser(description="Create a JSON Mapping File from a Viya Mapping Set.")
parser.add_argument("-n","--name", help="Name of the Mapping Set (also the name of the output file).",default='@systemMap')
parser.add_argument("-d","--directory", help="Directory where mapping files are written.",required='True')
parser.add_argument("--debug", action='store_true', help="Debug")
parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_true')
# get python version
version=int(str(sys.version_info[0]))
args = parser.parse_args()
mapname=args.name
debug=args.debug
basedir=args.directory
quietmode=args.quiet
# prompt if directory exists because existing json files are deleted
if os.path.exists(basedir):
# if the quiet mode flag is not passed then prompt to continue
if not quietmode:
if version > 2:
areyousure=input("The folder exists any existing json files in it will be deleted. Continue? (Y)")
else:
areyousure=raw_input("he folder exists any existing json files in it will be deleted. Continue? (Y)")
else:
areyousure="Y"
else: areyousure="Y"
# prompt is Y if user selected Y, its a new directory, or user selected quiet mode
if areyousure.upper() =='Y':
# get all transfer mapping sets
reqval="/transfer/mappings"
if debug : print(reqval)
originaljson=callrestapi(reqval,'get')
#if debug: printresult(originaljson,output_style)
allmaps={}
#get id of selected mapping set
#because filter by name does not work on previous REST call
if 'items' in originaljson:
maps=originaljson["items"]
found=0
for map in maps:
if map["name"]==mapname:
idformap=map["id"]
found=1
break
if found: print("NOTE: Mapping set "+mapname+" with id "+idformap+" selected." )
else:
print("ERROR: Mapping set with name "+mapname+" not found.")
print("NOTE: list mapping sets with './callrestapi.py -m get -e /transfer/mappings -o csv' ")
sys.exit()
reqvalbase="/transfer/mappings/"+idformap
# get options
# currently mapping sets do not contain options :(
reqval=reqvalbase+"/options"
options=callrestapi(reqval,'get')
# get substitutions
reqval=reqvalbase+"/substitutions"
substitutions=callrestapi(reqval,'get')
# reformat substitutions into list of dictionaries for mapping file
new_substitutions=[]
if 'items' in substitutions:
allitems=substitutions["items"]
for asub in allitems:
thisdict={}
thisdict["resourceId"]=asub["contentSourceLocation"]
thisdict["resourceName"]=asub["name"]
props=asub["mapSubstitutionProperties"]
#remove id from properties list
for aproperty in props:
discard=aproperty.pop("id","notfound")
thisdict["properties"]=props
new_substitutions.append(thisdict)
# get mappings or connectors
reqval=reqvalbase+"/items"
connectors=callrestapi(reqval,'get')
#if debug: print(json.dumps(connectors,3))
# reformat connector (Data Resources in the UI) into a list of connectors for each TYPE
new_connectors={}
tables=[]
users=[]
usergroups=[]
if 'items' in connectors:
allconnectors=connectors["items"]
for connection in allconnectors:
newconnection={}
newconnection["resourceName"]=connection["name"]
newconnection["target"]=connection["mapTargetProperties"][0]["value"]
newconnection["source"]=connection["mapSourceProperties"][0]["value"]
# upcase comparison as it seems to be inconsistent
type=connection["type"].upper()
if type.upper()=="TABLE":
tables.append(newconnection)
elif type=="UserGroup":
usergroups.append(newconnection)
elif type=="User":
users.append(newconnection)
new_connectors["Table"]=tables
new_connectors["User"]=users
new_connectors["UserGroup"]=usergroups
# Create mapping file and write to the file-system
m_json={'version':1}
m_json["connectors"]=new_connectors
m_json["substitutions"]=new_substitutions
if not os.path.exists(basedir): os.makedirs(basedir)
filecontent=json.dumps(m_json,indent=2)
file=mapname+'.json'
filename=os.path.join(basedir,file)
with open(filename, "w") as outfile:
outfile.write(filecontent)
print("NOTE: Mapping set "+mapname+" output to file "+filename )