Skip to content

Commit

Permalink
Merge pull request #102 from liketic/alert-api
Browse files Browse the repository at this point in the history
Migrate alert api to jobs api
  • Loading branch information
wjo1212 committed Dec 10, 2018
2 parents d329672 + f247842 commit 9d600ef
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
6 changes: 4 additions & 2 deletions aliyun/log/common_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def log_print(self):


class ListEntityResponse(LogResponse):
def __init__(self, header, resp, resource_name=None):
def __init__(self, header, resp, resource_name=None, entities_key=None):
LogResponse.__init__(self, header, resp)
self.count = resp['count']
self._total = resp['total']
Expand All @@ -47,7 +47,9 @@ def __init__(self, header, resp, resource_name=None):
resource_name = backup_resource_name

self.resource_name = resource_name
self.entities = resp.get(resource_name, [])
if entities_key is None:
entities_key = resource_name
self.entities = resp.get(entities_key, [])

if self.resource_name:
setattr(self, 'get_' + resource_name, self.get_entities)
Expand Down
2 changes: 1 addition & 1 deletion aliyun/log/logclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2752,5 +2752,5 @@ def arrange_shard(self, project, logstore, count):


make_lcrud_methods(LogClient, 'dashboard', name_field='dashboardName')
make_lcrud_methods(LogClient, 'alert', name_field='alertName')
make_lcrud_methods(LogClient, 'alert', name_field='name', root_resource='/jobs', entities_key='results')
make_lcrud_methods(LogClient, 'savedsearch', name_field='savedsearchName')
20 changes: 10 additions & 10 deletions aliyun/log/logclient_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def fn(self, project, detail):
"""

params = {}
resource_path = (root_resource and root_resource.lstrip('/')) or "/" + pluralize(entity_name)
resource_path = (root_resource and root_resource.rstrip('/')) or "/" + pluralize(entity_name)
headers = {"x-log-bodyrawsize": '0', "Content-Type": "application/json"}

if hasattr(detail, 'to_json'):
Expand Down Expand Up @@ -64,7 +64,7 @@ def fn(self, project, entity):
headers = dict()
params = dict()

resource_path = ((root_resource and root_resource.rstrip('/')) or ('/' + pluralize(entity_name) + '/')) + entity
resource_path = ((root_resource and root_resource.rstrip('/')) or ('/' + pluralize(entity_name))) + '/' + entity

(resp, header) = self._send("GET", project, None, resource_path, params, headers)
return GetEntityResponse(header, resp)
Expand Down Expand Up @@ -93,7 +93,7 @@ def fn(self, project, entity):

headers = {}
params = {}
resource_path = ((root_resource and root_resource.rstrip('/')) or ('/' + pluralize(entity_name) + '/')) + entity
resource_path = ((root_resource and root_resource.rstrip('/')) or ('/' + pluralize(entity_name))) + '/' + entity
(resp, header) = self._send("DELETE", project, None, resource_path, params, headers)
return DeleteEntityResponse(header, resp)

Expand All @@ -103,7 +103,7 @@ def fn(self, project, entity):
return fn


def list_entity(entity_name, root_resource=None, max_batch_size=DEFAULT_MAX_LIST_PAGING_SIZE):
def list_entity(entity_name, root_resource=None, max_batch_size=DEFAULT_MAX_LIST_PAGING_SIZE, entities_key=None):
def fn(self, project, offset=0, size=100):
""" list the {entity_title}, get first 100 items by default
Unsuccessful opertaion will cause an LogException.
Expand All @@ -126,14 +126,14 @@ def fn(self, project, offset=0, size=100):
if int(size) == -1 or int(size) > max_batch_size:
return list_more(fn, int(offset), int(size), max_batch_size, project)


headers = {}
params = {}
resource_path = (root_resource and root_resource.lstrip('/')) or "/" + pluralize(entity_name)
resource_name = pluralize(entity_name)
resource_path = (root_resource and root_resource.rstrip('/')) or "/" + resource_name
params['offset'] = str(offset)
params['size'] = str(size)
(resp, header) = self._send("GET", project, None, resource_path, params, headers)
return ListEntityResponse(header, resp, resource_name=resource_path.strip('/'))
return ListEntityResponse(header, resp, resource_name=resource_name, entities_key=entities_key)

fn.__name__ = 'list_' + entity_name
fn.__doc__ = fn.__doc__.format(entity_title=entity_name.title())
Expand Down Expand Up @@ -178,7 +178,7 @@ def fn(self, project, detail):

assert entity, LogException('InvalidParameter', 'unknown entity name "{0}" in "{1}"'.format(name_field, detail))

resource_path = ((root_resource and root_resource.rstrip('/')) or ('/' + pluralize(entity_name) + '/')) + entity
resource_path = ((root_resource and root_resource.rstrip('/')) or ('/' + pluralize(entity_name))) + '/' + entity

headers['Content-Type'] = 'application/json'
headers['x-log-bodyrawsize'] = str(len(body_str))
Expand All @@ -191,8 +191,8 @@ def fn(self, project, detail):
return fn


def make_lcrud_methods(obj, entity_name, name_field=None, root_resource=None):
setattr(obj, 'list_' + entity_name, list_entity(entity_name, root_resource=root_resource))
def make_lcrud_methods(obj, entity_name, name_field=None, root_resource=None, entities_key=None):
setattr(obj, 'list_' + entity_name, list_entity(entity_name, root_resource=root_resource, entities_key=entities_key))
setattr(obj, 'get_' + entity_name, get_entity(entity_name, root_resource=root_resource))
setattr(obj, 'delete_' + entity_name, delete_entity(entity_name, root_resource=root_resource))
setattr(obj, 'update_' + entity_name, update_entity(entity_name, root_resource=root_resource, name_field=name_field))
Expand Down
66 changes: 48 additions & 18 deletions tests/integration_test/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,53 @@
}

alert_detail = {
"actionDetail": {
"message": "xxxxxxx simple test"
},
"actionType": "notification",
"alertDetail": {
"alertKey": "f2",
"alertValue": "200",
"comparator": ">"
},
"checkInterval": 5,
"count": 10,
"from": "-300s",
"roleArn": "acs:ram::1654218965343050:role/aliyunlogreadrole",
"savedsearchName": "quck-query-all",
"to": "now",
"alertName": 'alert_' + str(time.time()).replace('.', '-')
"name": 'alert_' + str(time.time()).replace('.', '-'),
"displayName": "Alert for testing",
"description": "",
"type": "Alert",
"state": "Enabled",
"schedule": {
"type": "FixedRate",
"interval": "5m",
},
"configuration": {
"condition": "total >= 100",
"dashboard": "dashboardtest",
"queryList": [
{
"logStore": "test-logstore",
"start": "-120s",
"end": "now",
"timeSpanType": "Custom",
"chartTitle": "chart-test",
"query": "* | select count(1) as total",
}
],
"notificationList": [
{
"type": "DingTalk",
"serviceUri": "http://xxxx",
"content": "Message",
},
{
"type": "MessageCenter",
"content": "Message",
},
{
"type": "Email",
"emailList": ["abc@test.com"],
"content": "Email Message",
},
{
"type": "SMS",
"mobileList": ["132373830xx"],
"content": "Cellphone message"
}
],
"muteUntil": int(time.time()) + 300,
"notifyThreshold": 1,
"throttling": "5m",
}
}

savedsearch_detail = {
Expand Down Expand Up @@ -112,8 +143,7 @@ def main():
res = client.delete_dashboard(project, dashboard)
res.log_print()


alert = alert_detail.get('alertName')
alert = alert_detail.get('name')

res = client.create_alert(project, alert_detail)
res.log_print()
Expand Down

0 comments on commit 9d600ef

Please sign in to comment.