-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Skripnick
committed
Dec 20, 2016
1 parent
84d13c3
commit f707005
Showing
14 changed files
with
263 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2016: Mirantis Inc. | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
class Client(object): | ||
"""REST client.""" | ||
|
||
def __init__(self, name, endpoint): | ||
self.name = name | ||
self.endpoint = endpoint | ||
|
||
def __repr__(self): | ||
return "<Client '%s'>" % self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2016: Mirantis Inc. | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import datetime | ||
|
||
from ceagle.api import base | ||
|
||
|
||
def _get_fake_issue(region, issue_type, days): | ||
now = datetime.datetime.now() | ||
discovered = now - datetime.timedelta(days=days) | ||
confirmed = now | ||
return { | ||
"type": issue_type, | ||
"description": "Sample issue " + issue_type, | ||
"discovered_at": discovered.isoformat(), | ||
"confirmed_at": confirmed.isoformat(), | ||
"object_id": "test-id-%s-%d" % (issue_type, days), | ||
"region": region, | ||
} | ||
|
||
FAKE_ISSUES = { | ||
"Region1": [ | ||
_get_fake_issue("Region1", "IssueType1", 2), | ||
_get_fake_issue("Region1", "IssueType2", 8), | ||
], | ||
"north-2.piedpiper.net": [ | ||
_get_fake_issue("north-2.piedpiper.net", "IssueType1", 0), | ||
_get_fake_issue("north-2.piedpiper.net", "IssueType2", 2), | ||
_get_fake_issue("north-2.piedpiper.net", "IssueType2", 8), | ||
], | ||
} | ||
|
||
PERIOD_MAP = { | ||
"day": 1, | ||
"week": 7, | ||
"month": 30, | ||
} | ||
|
||
|
||
class Client(base.Client): | ||
|
||
def get(self, uri="/", **kwargs): | ||
"""Make GET request and decode JSON data. | ||
:param uri: resource URI | ||
:param kwargs: query parameters | ||
:returns: dict response data | ||
""" | ||
region, security, issues, period = uri.split("/") | ||
if issues != "issues": | ||
return {"error": "Not found"}, 404 | ||
issues = [] | ||
all_issues = FAKE_ISSUES.get(region) | ||
if all_issues: | ||
now = datetime.datetime.now() | ||
try: | ||
discovered_before = (now - datetime.timedelta( | ||
days=PERIOD_MAP[period])).isoformat("T") | ||
except KeyError: | ||
return {"error": "Not found"}, 404 | ||
for issue in all_issues: | ||
if issue["discovered_at"] >= discovered_before: | ||
issues.append(issue) | ||
else: | ||
return {"error": "Region not found"}, 404 | ||
return {"issues": issues}, 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[ | ||
{ | ||
"issueType": "securityGroup", | ||
"regionId": "Region1", | ||
"description": "Security group too open", | ||
"discoveredAt": "2016-02-28T16:41:41.090Z", | ||
"confirmedAt": "2016-03-28T16:41:41.090Z", | ||
"subject": { | ||
"id": "d8b0be7c-2ad7-4083-8d5a-a7a9a56fdd14", | ||
"type": "securityGroup", | ||
"tenantId": "demo", | ||
"userId": "demo" | ||
} | ||
}, { | ||
"issueType": "securityGroup", | ||
"regionId": "Region1", | ||
"description": "Security group too open", | ||
"discoveredAt": "2016-02-28T16:21:41.090Z", | ||
"confirmedAt": "2016-03-28T16:31:41.090Z", | ||
"resolvedAt": "2016-04-28T16:31:41.090Z", | ||
"subject": { | ||
"id": "d8b0be7c-2ad7-4083-8d5a-a7a9a56fdd15", | ||
"type": "securityGroup", | ||
"tenantId": "demo", | ||
"userId": "demo" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
"Region1", | ||
"Region2" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"flask": { | ||
"PORT": 5000, | ||
"HOST": "0.0.0.0", | ||
"DEBUG": true | ||
}, | ||
"services": { | ||
"security": "http://example.org/api/security" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2016: Mirantis Inc. | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from tests.unit import test | ||
|
||
import mock | ||
|
||
|
||
fake_config = mock.Mock() | ||
fake_config.get_config.return_value = { | ||
"use_fake_api_data": True, | ||
"services": { | ||
"security": "ok", | ||
} | ||
} | ||
|
||
|
||
@mock.patch("ceagle.api.client.config", fake_config) | ||
class SecurityTestCase(test.TestCase): | ||
|
||
def test_get_issues_month(self): | ||
resp = self.get("/api/v1/region/Region1/security/issues/month") | ||
self.assertEqual(200, resp[0]) | ||
self.assertEqual(2, len(resp[1]["issues"])) | ||
|
||
def test_get_issues_week(self): | ||
resp = self.get( | ||
"/api/v1/region/north-2.piedpiper.net/security/issues/week") | ||
self.assertEqual(200, resp[0]) | ||
self.assertEqual(2, len(resp[1]["issues"])) | ||
|
||
def test_get_issues_day(self): | ||
resp = self.get( | ||
"/api/v1/region/north-2.piedpiper.net/security/issues/day") | ||
self.assertEqual(200, resp[0]) | ||
self.assertEqual(1, len(resp[1]["issues"])) | ||
|
||
def test_get_issues_empty(self): | ||
resp = self.get("/api/v1/region/Region1/security/issues/day") | ||
self.assertEqual((200, {"issues": []}), resp) | ||
|
||
def test_get_issues_unknown_region(self): | ||
resp = self.get("/api/v1/region/Region3/security/issues/day") | ||
self.assertEqual(404, resp[0]) | ||
|
||
def test_get_issues_404(self): | ||
resp = self.get("/api/v1/region/Region3/day") | ||
self.assertEqual(404, resp[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters