-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_snapshot_trigger.lua
More file actions
222 lines (203 loc) · 8.12 KB
/
create_snapshot_trigger.lua
File metadata and controls
222 lines (203 loc) · 8.12 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
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
-- (c) 2013 Flexiant Ltd
-- Released under the Apache 2.0 Licence - see LICENCE for details
function scheduled_jira_snapshot_trigger(p)
if(p == nil) then
return {
ref = "scheduled_create_snapshot",
name = "Scheduled Create Snapshot Tiggger",
description = "This will create a snapshot for user if he has right customer key enabled",
priority = 0,
triggerType = "SCHEDULED",
schedule={frequency={WEEK=1}},
api = "TRIGGER",
version = 1 ,
}
end
print("======== SCHEDULED TRIGGER CREATE SNAPSHOT =========")
local serverList = getServersWithKey('AUTO_SNAPSHOTS')
for serverUUID,server in pairs(serverList) do
local newSnapshotDate = p.timeStamp-server.snapshotTime*60*60*1000
--os.time()-server.snapshotTime*60*60*1000 removed os.time as it returned 0 to p.timeStamp
local checkSnapshot = checkSnapshotDate(serverUUID,newSnapshotDate)
--change from server.customerUUID to disk.customerUUID
if(checkSnapshot) then
local userToken = getUserToken(disk.customerUUID)
userAPI:setSessionUser(userToken)
createNewSnapshot(server.customerUUID, serverUUID, "SERVER")
if (server.maxSnapshots > 0) then
removeOldSnapshots(serverUUID, server.maxSnapshots);
end
end
end
print("========2 ========")
local diskList = getDisksWithKey('AUTO_SNAPSHOTS')
for diskUUID,disk in pairs(diskList) do
print("======== 2.5 =========")
print(p.timeStamp)
local newSnapshotDate = p.timeStamp-disk.snapshotTime*60*60*1000
--os.time()-disk.snapshotTime*60*60*1000 removed os.time as it returned 0 to p.timeStamp
local checkSnapshot = checkSnapshotDate(diskUUID,newSnapshotDate)
if(checkSnapshot) then
local userToken = getUserToken(disk.customerUUID)
--change from server.customerUUID to disk.customerUUID
userAPI:setSessionUser(userToken)
createNewSnapshot(disk.customerUUID, diskUUID, "DISK")
if(disk.maxSnapshots > 0) then
removeOldSnapshots(diskUUID, disk.maxSnapshots);
end
end
end
print("======== SCHEDULED TRIGGER CREATE SNAPSHOT COMPLETE=========")
return { exitState = "SUCCESS" }
end
function removeOldSnapshots(resourceUUID, maxSnapshots)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('parentuuid')
filterCondition1:setValue({resourceUUID})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
searchFilter:addCondition(filterCondition1)
local snapshots = adminAPI:listResources(searchFilter,nil,new("ResourceType","SNAPSHOT"))
if(snapshots:getList():size() > maxSnapshots) then
local snapshotList = {}
local dateHelper = new("FDLDateHelper")
for i = 0, snapshots:getList():size() - 1, 1 do
snapshotList[snapshots:getList():get(i):getResourceUUID()] = dateHelper:getTimestamp(snapshots:getList():get(i):getResourceCreateDate())
local oldSnapshots = filterOldSnapshots(snapshotList, maxSnapshots);
for oldSnapshotUUID, oldSnapshotCreateDate in pairs(oldSnapshots) do
userAPI:deleteResource(oldSnapshotUUID, true, nil)
end
end
end
end
function getUserToken(customerUUID)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('resourceuuid')
filterCondition1:setValue({customerUUID})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
searchFilter:addCondition(filterCondition1)
local customer = adminAPI:listResources(searchFilter,nil,new("ResourceType","CUSTOMER"))
local userEmail = customer:getList():get(0):getUsers():get(0):getEmail()
return userEmail .. "/" .. customer:getList():get(0):getResourceUUID()
end
function filterOldSnapshots(snapshotTable, maxSnapshots)
for name,value in pairs(snapshotTable) do
list[#list+1] = name
end
function byval(a,b)
return snapshotTable[a] < snapshotTable[b]
end
table.sort(list,byval)
local response = {}
for k=maxSnapshots,#list do
response[list[k]] = snapshotTable[list[k]]
end
return response
end
function createNewSnapshot(customerUUID,resourceUUID,type)
local snapshotSkeleton = new("Snapshot")
snapshotSkeleton:setCustomerUUID(customerUUID)
snapshotSkeleton:setParentUUID(resourceUUID)
snapshotSkeleton:setType(new("SnapshotType",type))
print('Creating new snapshot for customer :' .. customerUUID .. ' ResourceUUID : ' .. resourceUUID .. ' Type:' .. type)
userAPI:createSnapshot(snapshotSkeleton,nil)
end
function getServersWithKey(resourceKeyName)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('status')
filterCondition1:setValue({"RUNNING"})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
local filterCondition2 = new("FilterCondition")
filterCondition2:setField('resourcekey.name')
filterCondition2:setValue({resourceKeyName})
filterCondition2:setCondition(new("Condition","STARTS_WITH"))
searchFilter:addCondition(filterCondition1)
searchFilter:addCondition(filterCondition2)
local servers = adminAPI:listResources(searchFilter,nil,new("ResourceType","SERVER"))
local responseData = {}
for i = 0, servers:getList():size() - 1, 1 do
responseData[servers:getList():get(i):getResourceUUID()] = {snapshotTime = getResourceKeyValue(resourceKeyName,servers:getList():get(i):getResourceKey()) , customerUUID = servers:getList():get(i):getCustomerUUID(), maxSnapshots = getResourceKeyValue('MAX_SNAPSHOTS',servers:getList():get(i):getResourceKey())}
end
return responseData
end
function getDisksWithKey(resourceKeyName)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('status')
filterCondition1:setValue({"ATTACHED_TO_SERVER"})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
local filterCondition2 = new("FilterCondition")
filterCondition2:setField('resourcekey.name')
filterCondition2:setValue({resourceKeyName})
filterCondition2:setCondition(new("Condition","STARTS_WITH"))
searchFilter:addCondition(filterCondition1)
searchFilter:addCondition(filterCondition2)
local disks = adminAPI:listResources(searchFilter,nil,new("ResourceType","DISK"))
local responseData = {}
for i = 0, disks:getList():size() - 1, 1 do
responseData[disks:getList():get(i):getResourceUUID()] = {snapshotTime = getResourceKeyValue(resourceKeyName,disks:getList():get(i):getResourceKey()) , customerUUID = disks:getList():get(i):getCustomerUUID(), maxSnapshots = getResourceKeyValue('MAX_SNAPSHOTS',disks:getList():get(i):getResourceKey())}
end
return responseData
end
function getResourceKeyValue(resourceKeyName,resouceKeyList)
for j = 0, resouceKeyList:size() - 1, 1 do
if(resouceKeyList:get(j):getName() == resourceKeyName) then
return resouceKeyList:get(j):getValue()
end
end
return 0;
end
function checkSnapshotDate(serverUUID,newDate)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('parentuuid')
filterCondition1:setValue({serverUUID})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
searchFilter:addCondition(filterCondition1)
local queryLimit = new("QueryLimit")
local orderedField = new("OrderedField")
orderedField:setFieldName('resourcecreatedate')
orderedField:setSortOrder(new("ResultOrder","DESC"))
local list = new("List")
list:add(orderedField)
queryLimit:setOrderBy(list)
queryLimit:setMaxRecords(1)
queryLimit:setLoadChildren(false)
local snapshots = adminAPI:listResources(searchFilter,queryLimit,new("ResourceType","SNAPSHOT"))
if(snapshots:getList():size()==1) then
local dateHelper = new("FDLDateHelper")
local dateCreated = dateHelper:getTimestamp(snapshots:getList():get(0):getResourceCreateDate())
if(dateCreated-newDate<0) then
return true
else
return false
end
else
return true
end
end
function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
function register()
return {"scheduled_jira_snapshot_trigger"}
end