-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpdateGEEInputURLParam.py
266 lines (231 loc) · 11.1 KB
/
UpdateGEEInputURLParam.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'''
To use, you will need to install the ArcGIS_REST Python library on your machine:
1. Open a command prompt on your machine
2. Navigate (cd) to the scripts directory in your Python2.7 install (usually this is C:\Python27\ArcGISx6410.6\Scripts)
3. type ‘pip install arcrest_package’ and hit enter
'''
from datetime import timedelta
import datetime
import json
import ssl
import sys
from urllib import urlencode
from arcrest.manageags import AGSAdministration
from arcrest.security.security import AGSTokenSecurityHandler
from arcrest.web._base import RedirectHandler
from future.moves.urllib import request
############## Things to change every time ################
# Input parameters for GEE host server_fqdn, port number, username/password and start/stop
server_fqdn = "" # Input server_fqdn name (e.g. myserver.domain.com)
username = "" # Input AGS site username
password = "" # Input AGS user password
geeTargetInputList = [] # input a list of geoevent inputs to update the url pramater with
# Get list of all GeoEvent inputs from the REST api, copy 'Name' parameter for each output and paste into this list as a string
# If the list is empty, this script will operate on all inputs where the urlParamName is found (all others will be ignored)
urlParamName = "since" # input the URL parameter name that should be set to the current datetime
time_offset_sec = -60 # Input - the number of seconds to offset the time by (negative subtracts, positive adds). Use 0 or None to get the current time
############## Things to change every time ################
############## Defaults that rarely change ################
# Default ports used in ArcGIS
GES_PORT = "6143"
AGS_PORT = "6443"
# Default URLs used in ArcGIS
AGS_URL_TOKEN = 'https://{}:{}/arcgis/tokens/'.format(server_fqdn, AGS_PORT)
GES_URL_ADMIN = 'https://{}:{}/geoevent/admin'.format(server_fqdn, GES_PORT)
############## Defaults that rarely change ################
############## Things to change based on what you are doing ################
TIME_STRING_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
# Default API URLs
GES_URL_INPUTS = 'https://{}:{}/geoevent/admin/inputs'.format(server_fqdn, GES_PORT)
GES_URL_INPUT = 'https://{}:{}/geoevent/admin/input'.format(server_fqdn, GES_PORT)
# Default component property values
### DO NOT MODIFY!!!
NAME = "name"
VALUE = "value"
TRANSPORT = "transport"
PROPERTIES = "properties"
CLIENT_PARAMETERS = "clientParameters"
### DO NOT MODIFY!!!
############## Things to change based on what you are doing ################
def getTimeString(offset_sec=None):
'''
returns the current UTC time as a string based on the Python strftime() method
format is defined by the constant TIME_STRING_FORMAT
offset_sec is in seconds and will be ADDED to the time (so use a negative to go back in time)
'''
utc_datetime = datetime.datetime.utcnow()
if offset_sec is not None:
try:
utc_datetime = utc_datetime + +timedelta(seconds=offset_sec)
except:
print "failed to add {} seconds to current time".format(offset_sec)
return utc_datetime.strftime(TIME_STRING_FORMAT)
def updateInputClientParameter(inputComponent):
'''
Iterates through the input transport properties and updates the client parameters property
'''
print "\tLooking at {} on input named '{}'".format(CLIENT_PARAMETERS, inputComponent[NAME])
# This is a transport property, so look for client parameter there
for tprop in inputComponent[TRANSPORT][PROPERTIES]:
if str(tprop[NAME]).lower() == CLIENT_PARAMETERS.lower():
clientParametersValue = tprop[VALUE]
preParams, discard = str(clientParametersValue).split(urlParamName)
discard = discard.split("&", 1)
postParams = ""
if len(discard) > 1:
postParams = "&{}".format(discard[1])
newUrlParamValue = getTimeString(time_offset_sec)
print "\tUpdating {} '{}' parameter value {} to {}".format(CLIENT_PARAMETERS, urlParamName, discard[0], newUrlParamValue)
clientParametersValue = "{}{}={}{}".format(preParams, urlParamName, newUrlParamValue, postParams)
tprop[VALUE] = clientParametersValue
break # only updating one parameter
return inputComponent
def Main():
'''
Connects to the GeoEvent Server and iterates through the inputs
If an input is in the list of target inputs (or the list is empty) then it attemtps to modify that input
'''
# Get GeoEvent token to access admin API
sh = AGSTokenSecurityHandler(username=username,
password=password,
org_url=AGS_URL_TOKEN,
token_url=AGS_URL_TOKEN, # token URL for AGS
proxy_url=None,
proxy_port=None)
sh.referer_url = GES_URL_ADMIN # need to set the referrer to generate token correctly
if sh.token is not None:
# Instantiate the AGS admin object
ags = AGSAdministrationPut(url=GES_URL_ADMIN,
securityHandler=sh,
proxy_url=None,
proxy_port=None)
# use a get() method to get the requested component json
componentList = ags._get(url=GES_URL_INPUTS, securityHandler=sh)
for component in componentList:
if len(geeTargetInputList) > 0 and not any(component[NAME] in s for s in geeTargetInputList):
# we arn't interested in this item so
continue
else:
# geeTargetInputList is either empty or we care about this input
updatedInput = updateInputClientParameter(component)
ags._put(url=GES_URL_INPUT, securityHandler=sh, param_dict=updatedInput)
try:
print "Updated {} '{}' parameter on input named {}".format(CLIENT_PARAMETERS, urlParamName, component[NAME])
except:
print "Failed to update input"
del ags, sh, componentList
else:
print "Unable to get token, check username, password, referer url and token url"
del sh
sys.exit(1)
class AGSAdministrationPut(AGSAdministration):
'''
Adds the PUT method to the AGS administration class.
'''
def _put(self, url,
param_dict=None,
securityHandler=None,
additional_headers=None,
proxy_url=None,
proxy_port=80,
compress=True):
"""
Performs a put operation on a URL.
Inputs:
param_dict - key/preParams pair of values that will be turned into a json string
ex: {"foo": "bar"}
securityHandler - object that handles the token or other site
security. It must inherit from the base security class.
ex: arcrest.AGOLSecurityHandler("SomeUsername", "SOMEPASSWORD")
additional_headers - are additional key/preParams headers that a user
wants to pass during the operation.
ex: {"accept-encoding": "gzip"}
proxy_url - url of the proxy
proxy_port - default 80, port number of the proxy
compress - default true, determines if gzip should be used of not for
the web operation.
Output:
returns dictionary or string depending on web operation.
"""
# ensure that no spaces are in the url
url = url.replace(" ", "%20")
if param_dict is None:
param_dict = {}
if additional_headers is None:
additional_headers = {}
if self._verify == False and \
sys.version_info[0:3] >= (2, 7, 9) and \
hasattr(ssl, 'create_default_context'):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
headers = {
"User-Agent": self.useragent,
'Accept': '*/*',
'Content-type':'application/json'
}
if securityHandler and securityHandler.referer_url:
headers['referer'] = securityHandler.referer_url
# opener = None
# return_value = None
handlers = [RedirectHandler()]
param_dict, handler, cj = self._processHandler(securityHandler, param_dict)
if handler is not None:
handlers.append(handler)
if cj is not None:
handlers.append(request.HTTPCookieProcessor(cj))
if compress:
headers['Accept-Encoding'] = 'gzip'
else:
headers['Accept-Encoding'] = ''
for k, v in additional_headers.items():
headers[k] = v
del k, v
hasContext = 'context' in self._has_context(request.urlopen)
if self._verify == False and \
sys.version_info[0:3] >= (2, 7, 9) and \
hasattr(ssl, 'create_default_context'):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
opener = request.build_opener(*handlers)
opener.addheaders = [(k, v) for k, v in headers.items()]
request.install_opener(opener)
data = urlencode(param_dict)
if self.PY3:
data = data.encode('ascii')
data = json.dumps(param_dict)
opener.data = data
# request.get_method = lambda: 'PUT'
req = request.Request("{}?token={}".format(self._asString(url), self._securityHandler.token),
data=data,
headers=headers)
req.get_method = lambda: 'PUT'
for k, v in headers.items():
req.add_header(k, v)
if hasContext and self._verify == False:
resp = request.urlopen(req, context=ctx)
else:
resp = request.urlopen(req)
self._last_code = resp.getcode()
self._last_url = resp.geturl()
return_value = self._process_response(resp=resp)
if isinstance(return_value, dict):
if "error" in return_value and \
'message' in return_value['error']:
if return_value['error']['message'].lower() == 'request not made over ssl':
if url.startswith('http://'):
url = url.replace('http://', 'https://')
return self._put(url,
param_dict,
securityHandler,
additional_headers,
proxy_url,
proxy_port,
compress)
return return_value
else:
return return_value
return return_value
if __name__ == '__main__':
Main()