-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalogServiceOperations.py
More file actions
139 lines (125 loc) · 5.1 KB
/
Copy pathCatalogServiceOperations.py
File metadata and controls
139 lines (125 loc) · 5.1 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
from utils import SentiloRestClient, SentiloLogs, SentiloUtils, SentiloResponse
path = {
'path' : '/catalog'
}
'''
Retrieve a sensors list for the provider, into a data estructure that
represents the entire provider information
@param inputMessage
A properties object that describes the request options
@returns JSON Object If the request goes OK, then return a JSON object
that contains a list of sensors for the provider. Returns an
error object in other case
'''
def getSensors(inputMessage):
SentiloLogs.debug('Retrieving authorized sensors from catalog!')
inputMessage.update(path)
inputMessage['path'] = inputMessage['path'] + '/' + inputMessage['provider']
try:
response = SentiloRestClient.get(inputMessage)
SentiloLogs.debug('Sensors retrieved!')
return response.json()['providers'][0]
except Exception as e:
SentiloLogs.error('Error while retrieving sensors!')
return SentiloResponse.error(type(e),e.__str__())
return None
'''
Registers a list of sensors into the catalog. The inputMessage must
contains, at least, a body field with a correct sensors list structure
like the especified in the Sentilo API Doc. Please, see this url for more
information:
http://www.sentilo.io/xwiki/bin/view/APIDocs.Service.Catalog/CreateSensors
@param inputMessage
A properties object that describes the request options, with
the list of sensors
'''
def registerSensors(inputMessage):
SentiloLogs.debug('Registering sensors!')
inputMessage.update(path)
inputMessage['path'] = inputMessage['path'] + '/' + inputMessage['provider']
if inputMessage['body']['sensors']:
try:
response = SentiloRestClient.post(inputMessage)
SentiloLogs.debug('Sensors registered!')
if response.status_code == 200:
SentiloLogs.info('Successfully registered sensors!')
return SentiloResponse.success('200','Sensors registered!')
except Exception as e:
SentiloLogs.error('Error while registering sensors: ' + e.__str__())
return SentiloResponse.error(type(e), e.__str__())
else:
SentiloLogs.debug('There are no sensors to register!')
return None
'''
Update a list of sensors that already exists into the catalog. The
inputMessage must contains, at least, a body field with a correct sensors
list structure like the especified in the Sentilo API Doc. Please, see
this url for more information:
http://www.sentilo.io/xwiki/bin/view/APIDocs.Service.Catalog/CreateSensors
@param inputMessage
A properties object that describes the request options, with
the list of sensors
'''
def updateSensors(inputMessage):
SentiloLogs.debug('Updating sensors!')
inputMessage.update(path)
inputMessage['path'] = inputMessage['path'] + '/' + inputMessage['provider']
if inputMessage['body']['sensors']:
try:
response = SentiloRestClient.put(inputMessage)
SentiloLogs.debug('Sensors updated!')
if response.status_code == 200:
SentiloLogs.info('Successfully updated sensors!')
return SentiloResponse.success('200','Sensors updated!')
except Exception as e:
SentiloLogs.error('Error while updating sensors: ' + e.__str__())
return SentiloResponse.error(type(e), e.__str__())
else:
SentiloLogs.debug('There are no sensors to update!')
return None
'''
Registers a list of alerts into the catalog. The inputMessage must
contains, at least, a body field with a correct alerts list structure
like the especified in the Sentilo API Doc. Please, see this url for more
information:
http://www.sentilo.io/xwiki/bin/view/APIDocs.Services.Alert/CreateAlerts
@param inputMessage
A properties object that describes the request options, with
the list of alerts
'''
def registerAlerts(inputMessage):
SentiloLogs.debug('Registering alerts!')
inputMessage.update(path)
inputMessage['path'] = inputMessage['path'] + '/' + '/alert/' + inputMessage['provider']
if inputMessage['body']['alerts']:
try:
response = SentiloRestClient.post(inputMessage)
SentiloLogs.debug('Alerts registered!')
print(response)
if response.status_code == 200:
SentiloLogs.info('Successfully registered alerts!')
return SentiloResponse.success('200','Alerts registered!')
except Exception as e:
SentiloLogs.error('Error while registering alerts: ' + e.__str__())
return SentiloResponse.error(type(e), e.__str__())
else:
SentiloLogs.debug('There are no alerts to register!')
return None
'''
# Test the functions above
options = {
'host' : 'HOST_IP',
'port' : 'HOST_PORT',
'headers' : {
'identity_key' : 'PROVIDER_ID'
},
'provider' : 'TestProiver',
'sensor' : 'TestSensor',
'body' : {
'alerts' : [{
'id' : 'testAlert',
'type' : 'EXTERNAL'
}]
}
}
'''