-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathexportcustomgroups.py
More file actions
executable file
·150 lines (113 loc) · 4.63 KB
/
Copy pathexportcustomgroups.py
File metadata and controls
executable file
·150 lines (113 loc) · 4.63 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# exportcustomgroups.py
# Feb 2021
#
# Pass in a customgroups and this tool will export the customgroups to a apcakge
#
#
# Change History
#
#
# Copyright © 2019, 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
import argparse, sys, subprocess, uuid, time, os, glob, json, tempfile
from sharedfunctions import getfolderid, callrestapi, getapplicationproperties, printresult, getclicommand
# get python version
version=int(str(sys.version_info[0]))
tempdir=tempfile.gettempdir()
# get input parameters
parser = argparse.ArgumentParser(description="Export Custom Groups to a Package")
parser.add_argument("-f","--filename", help="Full path to file. (No extension)",default="/tmp/customgroups")
parser.add_argument("--id", help="Subset based on group id containing a string",default=None )
parser.add_argument("-n","--name", help="Subset based on name containing a string",default=None )
parser.add_argument("-t","--transferremove", help="Remove transfer file after download?", action='store_true')
parser.add_argument("-l","--limit", type=int,help="Specify the number of records to pull. Default is 1000.",default=1000)
parser.add_argument("-d","--debug", action='store_true', help="Debug")
# get cli location from properties, check that cli is there if not ERROR and stop
clicommand=getclicommand()
args= parser.parse_args()
filename=args.filename
debug=args.debug
idval=args.id
nameval=args.name
autotransferremove=args.transferremove
limit=args.limit
# create filter
filtercond=[]
filtercond.append('eq(providerId,"local")')
if idval!=None: filtercond.append('contains(id,"'+idval+'")')
if nameval!=None: filtercond.append('contains(name,"'+nameval+'")')
delimiter = ','
completefilter = 'and('+delimiter.join(filtercond)+')'
# create the requests file of the custom groups
# get all groups that are custom
reqtype='get'
reqval='/identities/groups/?limit='+str(limit)+'&filter='+completefilter
groupslist_result_json=callrestapi(reqval,reqtype)
groups = groupslist_result_json['items']
#if debug: print(json.dumps(groups,indent=2))
""" This is the json format {
"version": 1,
"name": "Modelers",
"description": "Modelers",
"items": [ "/identities/groups/SalesModelers",
"/identities/groups/HRModelers"
]
} """
package_name=str(uuid.uuid1())
requests_dict={"version":1}
requests_dict["name"]=package_name
requests_dict["description"]="Custom Groups from pyviyatools "+package_name
grouplist=[]
# create a dictionary that will generate the requests file
for group in groups:
group_uri="/identities/groups/"+group['id']
grouplist.append(group_uri)
requests_dict["items"]=grouplist
if debug: print(json.dumps(requests_dict,indent=2))
# build the requests file from the list
request_file=os.path.join(tempdir,filename+"_requests.json")
with open(request_file, "w") as outfile:
json.dump(requests_dict, outfile)
# export the groups to a package file
command=clicommand+' transfer export -r @'+request_file+' --name "'+package_name+'"'
try:
print(command)
except UnicodeEncodeError:
print(command.encode('ascii','replace'))
rc=subprocess.call(command, shell=True)
# if the export command fails then skip to the next item
if rc != 0:
sys.exit("ERROR: There was a problem exporting Custom Groups, command returned code "+str(rc))
reqtype='get'
reqval='/transfer/packages?filter=eq(name,"'+package_name+'")'
package_info=callrestapi(reqval,reqtype)
package_id=package_info['items'][0]['id']
completefile=os.path.join(filename+'.json')
command=clicommand+' transfer download --file '+completefile+' --id '+package_id
try:
print(command)
except UnicodeEncodeError:
print(command.encode('ascii','replace'))
subprocess.call(command, shell=True)
# if -t set then remove transfer package from infrastructure data server after download
if autotransferremove:
print(clicommand+' transfer delete --id '+package_id+"\n")
remTransferObject = subprocess.Popen(clicommand+' transfer delete --id '+package_id, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
remTransferObjectOutput = remTransferObject.communicate(b'Y\n')
remTransferObject.wait()