Skip to content

Commit c906f51

Browse files
kjnetherkjnether
authored andcommitted
many updates to support kirk restore
1 parent 7f09dfa commit c906f51

File tree

7 files changed

+223
-54
lines changed

7 files changed

+223
-54
lines changed

FMEUtil/FMEUtil/FMEConstants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
used to keep track of property names used by fme rest api.
77
'''
88
from enum import Enum
9-
from lxml.html.builder import ACRONYM
109

1110

1211
class Schedule(Enum):

FMEUtil/FMEUtil/PyFMEServerV2.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def fixUrlPath(self, url): # pylint: disable=no-self-use
7171
This is used when constructing urls to make sure the path can have
7272
another directory added to it using the urljoin method.
7373
'''
74-
if url[len(url) - 1] <> '/':
74+
if url[len(url) - 1] != '/':
7575
url = url + '/'
7676
return url
7777

@@ -433,7 +433,22 @@ def addSchedule(self, scheduleDescription):
433433
detail='low')
434434
# print 'response', response
435435
self.logger.debug("response is: %s", response)
436+
437+
def updateSchedule(self, category, scheduleName, scheduleDescription):
438+
self.logger.debug("root schedule url: {0}".format(self.schedules.url))
439+
url = self.schedules.baseObj.fixUrlPath(self.schedules.url)
436440

441+
# url = urlparse.urljoin(url, catEncode)
442+
url = urlparse.urljoin(url, category)
443+
url = self.schedules.baseObj.fixUrlPath(url)
444+
# url = urlparse.urljoin(url, scheduleNameEncode)
445+
url = urlparse.urljoin(url, scheduleName)
446+
url = self.schedules.baseObj.fixUrlPath(url)
447+
# print 'schedule url now:', url
448+
self.logger.debug("schedule url: {0}".format(url))
449+
header = {'Accept': 'application/json'}
450+
self.baseObj.putResponse(url, header=header, data=scheduleDescription)
451+
437452
def delete(self, category, scheduleName):
438453
'''
439454
:param category: The name of the category that the job exists within
@@ -457,7 +472,7 @@ def delete(self, category, scheduleName):
457472
# print 'schedule url now:', url
458473
self.logger.debug("schedule url: {0}".format(url))
459474
header = {'Accept': 'application/json'}
460-
response = self.baseObj.deleteResponse(url, header=header, acceptCodes=[204])
475+
response = self.baseObj.putResponse(url, header=header, acceptCodes=[204])
461476
# print 'response', response
462477
return response
463478

FMEUtil/FMEUtil/PyFMEServerV3.py

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import requests
2929

30-
3130
# pylint: disable=invalid-name
3231

3332

@@ -531,17 +530,77 @@ def delete(self, scheduleName, category):
531530
url = self.schedules.baseObj.fixUrlPath(url)
532531
self.logger.debug("schedule url now: %s", url)
533532
header = {'Accept': 'application/json'}
534-
response = self.baseObj.deleteResponse(url, header=header, acceptCodes=[204])
533+
response = self.baseObj.deleteResponse(url, header=header,
534+
acceptCodes=[204])
535535
self.logger.debug("response is: %s", response)
536536
return response
537537

538-
def __setEnabledFlag(self, scheduleName, category, enabledFlag):
539-
# first make sure the schedule exists, which at the same
540-
# time will load the schedule list which can then be
541-
# retrieved to determine if the schedule in question
542-
# is enable or not, then if its status has changed
543-
# we can resubmit that schedule as is but with the
544-
# changed enable parameter
538+
def updateParameters(self, scheduleName, category, newParams):
539+
'''
540+
used to update an existing parameter. Quick fix to support
541+
need to be able to automate update of kirk schedules.
542+
543+
:param scheduleName: Name of the schedule that is to be updated
544+
:type scheduleName: str
545+
:param category: Name of the category that is to be updated
546+
:type category: str
547+
:param newParams: a dictionary where the key is the parameter name
548+
and the value is the parameter value.
549+
:type newParams: dict
550+
'''
551+
# makeing the keys all lower case.
552+
paramsLowerCase = {}
553+
for k, v in newParams.iteritems():
554+
paramsLowerCase[k.lower()] = v
555+
556+
sched2Use = self.__verifyScheduleCategory(scheduleName, category)
557+
url = self.__getScheduleCategoryUrl(scheduleName, category)
558+
# update the params defined in sched2Use
559+
pubParams = sched2Use['request']['publishedParameters']
560+
paramCnt = 0
561+
for param in pubParams:
562+
paramValue = param['value']
563+
paramName = param['name']
564+
# now iterate through the list of new params
565+
if paramName.lower() in paramsLowerCase:
566+
pubParams[paramCnt]['value'] = \
567+
paramsLowerCase[paramName.lower()]
568+
msg = 'updated {0} from {1} to {2}'.format(
569+
paramName, paramValue, paramsLowerCase[paramName.lower()])
570+
self.logger.info(msg)
571+
paramCnt += 1
572+
sched2Use['request']['publishedParameters'] = pubParams
573+
body = sched2Use
574+
header = {'Content-Type': 'application/json',
575+
'Accept': 'application/json'}
576+
bodyStr = json.dumps(body)
577+
resp = self.baseObj.putResponse(url=url, data=bodyStr,
578+
header=header)
579+
return resp
580+
581+
def __getScheduleCategoryUrl(self, scheduleName, category):
582+
catEncode = urllib.quote(category)
583+
scheduleNameEncode = urllib.quote(scheduleName)
584+
url = self.schedules.baseObj.fixUrlPath(self.schedules.url)
585+
url = urlparse.urljoin(url, catEncode)
586+
url = self.schedules.baseObj.fixUrlPath(url)
587+
url = urlparse.urljoin(url, scheduleNameEncode)
588+
return url
589+
590+
def __verifyScheduleCategory(self, scheduleName, category):
591+
'''
592+
checks to make sure that a given schedule and category actually
593+
exist. Will also return the data object that is currently associated
594+
with a schedule / category.
595+
596+
:param scheduleName: name of the schedule to verify
597+
:type scheduleName: str
598+
:param category: name of the category to verify
599+
:type category: str
600+
601+
:return: the data associated with the schedule and category provided
602+
as arguments
603+
'''
545604
if not self.schedules.exists(scheduleName, category):
546605
msg = 'Cannot enable/disable the schedule: {0} in the category {1} ' + \
547606
'as there is no schedule with this name and category'
@@ -555,17 +614,17 @@ def __setEnabledFlag(self, scheduleName, category, enabledFlag):
555614
sched2Use = sched
556615
break
557616
if not sched2Use:
558-
msg = "Cannot enable/disable the schedule {0} in the category {1} as " + \
559-
"I am unable to find a schedule that matches this combination"
617+
msg = "Cannot enable/disable the schedule {0} in the category" + \
618+
" {1} as I am unable to find a schedule that matches " + \
619+
"this combination"
560620
msg = msg.format(scheduleName, category)
561-
raise ValueError, msg
621+
raise ValueError(msg)
622+
return sched2Use
623+
624+
def __setEnabledFlag(self, scheduleName, category, enabledFlag):
625+
sched2Use = self.__verifyScheduleCategory(scheduleName, category)
626+
url = self.__getScheduleCategoryUrl(scheduleName, category)
562627

563-
catEncode = urllib.quote(category)
564-
scheduleNameEncode = urllib.quote(scheduleName)
565-
url = self.schedules.baseObj.fixUrlPath(self.schedules.url)
566-
url = urlparse.urljoin(url, catEncode)
567-
url = self.schedules.baseObj.fixUrlPath(url)
568-
url = urlparse.urljoin(url, scheduleNameEncode)
569628
validStrings = ['true', 'false']
570629
if isinstance(enabledFlag, bool):
571630
if enabledFlag:
@@ -578,7 +637,7 @@ def __setEnabledFlag(self, scheduleName, category, enabledFlag):
578637
'this is not a valid value. Supply either a boolean value ' + \
579638
'or one of the following values: {1}'
580639
msg = msg.format(enabledFlag, validStrings)
581-
raise ValueError, msg
640+
raise ValueError(msg)
582641
else:
583642
enabledFlag = enabledFlag.lower()
584643
else:
@@ -587,7 +646,7 @@ def __setEnabledFlag(self, scheduleName, category, enabledFlag):
587646
'Supply either a boolean value or a string with one of the ' + \
588647
'following values: {2}'
589648
msg = msg.format(enabledFlag, type(enabledFlag), validStrings)
590-
raise ValueError, msg
649+
raise ValueError(msg)
591650

592651
if enabledFlag == 'true':
593652
enabledFlagBool = True
@@ -596,15 +655,16 @@ def __setEnabledFlag(self, scheduleName, category, enabledFlag):
596655

597656
# before proceed check to see if there is actually a change to the
598657
# enabled property of it is already set to whatever the target is.
599-
if sched2Use[self.const.enabled] <> enabledFlagBool:
658+
if sched2Use[self.const.enabled] != enabledFlagBool:
600659
sched2Use[self.const.enabled] = enabledFlag
601660
msg = "enabled flag: {0}".format(sched2Use[self.const.enabled])
602661
self.logger.debug(msg)
603662
body = sched2Use
604663
header = {'Content-Type': 'application/json',
605-
'Accept' : 'application/json'}
664+
'Accept': 'application/json'}
606665
bodyStr = json.dumps(body)
607-
resp = self.baseObj.putResponse(url=url, data=bodyStr, header=header)
666+
resp = self.baseObj.putResponse(url=url, data=bodyStr,
667+
header=header)
608668
return resp
609669

610670
def disable(self, scheduleName, category):

KirkUtil/KirkUtil/PyKirk.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,37 @@ def getTransformers(self, force=None):
437437
self.fldMaps = respJson
438438
return respJson
439439

440+
def postTransformer2(self, jobid, transformer_type, ts1_name, ts1_value,
441+
ts2_name, ts2_value, ts3_name, ts3_value,
442+
ts4_name, ts4_value, ts5_name, ts5_value,
443+
ts6_name, ts6_value):
444+
p = constants.TransformerProperties
445+
struct = {p.jobid.name: jobid,
446+
p.transformer_type.name: transformer_type,
447+
p.ts1_name.name: ts1_name,
448+
p.ts1_value.name: ts1_value,
449+
p.ts2_name.name: ts2_name,
450+
p.ts2_value.name: ts2_value,
451+
p.ts3_name.name: ts3_name,
452+
p.ts3_value.name: ts3_value,
453+
p.ts4_name.name: ts4_name,
454+
p.ts4_value.name: ts4_value,
455+
p.ts5_name.name: ts5_name,
456+
p.ts5_value.name: ts5_value,
457+
p.ts6_name.name: ts6_name,
458+
p.ts6_value.name: ts6_value}
459+
resp = requests.post(self.transformerUrl,
460+
data=struct,
461+
headers=self.baseObj.authHeader)
462+
self.logger.debug("response from transformer post: %s", resp.json())
463+
if resp.status_code < 200 or resp.status_code >= 300:
464+
msg = constants.POST_NON_200_ERROR_MSG.format(self.transformerUrl,
465+
resp.status_code,
466+
resp.text)
467+
raise APIError(msg)
468+
self.getTransformers(force=True)
469+
return resp.json()
470+
440471
def postTransformer(self, jobid, transformerType, parameters):
441472
'''
442473
:param jobid: the job id the transformer is related to

0 commit comments

Comments
 (0)