forked from jnccneto/AwsResourceScheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourcesApi.py
executable file
·97 lines (80 loc) · 3.49 KB
/
ResourcesApi.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
import json
import os
import sys
import pprint
def lambda_handler(event, context):
print('############# EVENT #################')
print(event)
if 'resource' in event:
#if event['resource'] is not None:
print("API CALL ################################")
if event['resource'] == '/list':
print("List ####################################")
AsGroupsInfo = Scheduler_Functions.GetAutoScallingGroups("All")
RdsInfo = Scheduler_Functions.GetAllRdsInstances()
Ec2Info = Scheduler_Functions.GetAllEc2Instances()
DayTimeOnService = Scheduler_Functions.GetDayTime()
ResourcesInfo = AsGroupsInfo + Ec2Info + RdsInfo + [DayTimeOnService]
if OnAws == False:
pprint.pprint(ResourcesInfo)
return {
'statusCode': 200,
'headers': {
"Access-Control-Allow-Origin" : "*", ## Required for CORS support to work
## "Access-Control-Allow-Credentials" : True ## Required for cookies, authorization headers with HTTPS
},
'body': json.dumps(ResourcesInfo),
}
if event['resource'] == '/updateschedule':
print("updateschedule ####################################")
if ( event['queryStringParameters']['resourceId'] and event['queryStringParameters']['ResourceType'] and event['queryStringParameters']['ScheduleTimings'] and event['queryStringParameters']['ScheduleWeekDays'] and event['queryStringParameters']['ScheduleOverRide'] ) is not None:
response = Scheduler_Functions.UpdateResourceSchedule(event['queryStringParameters']['resourceId'],event['queryStringParameters']['ResourceType'],event['queryStringParameters']['ScheduleTimings'],event['queryStringParameters']['ScheduleWeekDays'],event['queryStringParameters']['ScheduleOverRide'])
if(response is True):
HtmlFeedBack = "ProcessOk"
else:
HtmlFeedBack = response
return {
'statusCode': 200,
'headers': {
"Access-Control-Allow-Origin" : "*", ## Required for CORS support to work
},
'body': json.dumps(HtmlFeedBack),
}
OnAws = True
if os.environ.get("AWS_EXECUTION_ENV") == None:
print("Not On AWS")
OnAws = False
import argparse
sys.path.append('python')
#from testcase import TestCase
import Scheduler_Functions
parser = argparse.ArgumentParser(description='A tutorial of argparse!')
parser.add_argument("--action")
parser.add_argument("--resourceId")
parser.add_argument("--ScheduleTimings")
parser.add_argument("--ScheduleWeekDays")
parser.add_argument("--ScheduleOverRide")
parser.add_argument("--ResourceType")
args = parser.parse_args()
action = args.action
resourceId = args.resourceId
ScheduleTimings = args.ScheduleTimings
ScheduleWeekDays = args.ScheduleWeekDays
ScheduleOverRide = args.ScheduleOverRide
ResourceType = args.ResourceType
if action == "update" and ( resourceId and ScheduleTimings and ScheduleWeekDays and ScheduleOverRide ) is not None:
print("ACTIONS: ", resourceId, ScheduleTimings, ScheduleWeekDays, ScheduleOverRide )
event = {}
event['queryStringParameters'] = {}
event['queryStringParameters']['resourceId'] = resourceId
event['queryStringParameters']['ScheduleTimings'] = ScheduleTimings
event['queryStringParameters']['ScheduleWeekDays'] = ScheduleWeekDays
event['queryStringParameters']['ScheduleOverRide'] = ScheduleOverRide
event['queryStringParameters']['ResourceType'] = ResourceType
event['resource'] = '/updateschedule'
lambda_handler(event, None)
elif action == "list":
lambda_handler({'resource':'/list'},None)
else:
print("On AWS")
import Scheduler_Functions