-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contributes to #517
- Loading branch information
Showing
5 changed files
with
272 additions
and
24 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
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 @@ | ||
''' | ||
Wrapper methods for ElastiGroup's API | ||
''' | ||
import requests | ||
import json | ||
|
||
|
||
SPOTINST_API_URL = 'https://api.spotinst.io' | ||
|
||
|
||
def update_capacity(minimum, maximum, target, elastigroup_id, spotinst_account_id, spotinst_token): | ||
''' | ||
Updates the capacity (number of instances) for an ElastiGroup by calling the SpotInst API. | ||
Returns the updated description of the ElastiGroup as a dict. | ||
Exceptions will be thrown for HTTP errors. | ||
For more details see https://api.spotinst.com/elastigroup/amazon-web-services/update/ | ||
''' | ||
headers = { | ||
"Authorization": "Bearer {}".format(spotinst_token), | ||
"Content-Type": "application/json" | ||
} | ||
|
||
new_capacity = { | ||
'target': target, | ||
'minimum': minimum, | ||
'maximum': maximum | ||
} | ||
|
||
body = {'group': {'capacity': new_capacity}} | ||
response = requests.put( | ||
'{}/aws/ec2/group/{}?accountId={}'.format(SPOTINST_API_URL, elastigroup_id, spotinst_account_id), | ||
headers=headers, timeout=10, data=json.dumps(body)) | ||
response.raise_for_status() | ||
data = response.json() | ||
groups = data.get("response", {}).get("items", []) | ||
|
||
return groups[0] | ||
|
||
|
||
def get_elastigroup(elastigroup_id, spotinst_account_id, spotinst_token): | ||
''' | ||
Returns the description of an ElastiGroup as a dict. | ||
Exceptions will be thrown for HTTP errors. | ||
For more details see https://api.spotinst.com/elastigroup/amazon-web-services/list-group/ | ||
''' | ||
headers = { | ||
"Authorization": "Bearer {}".format(spotinst_token), | ||
"Content-Type": "application/json" | ||
} | ||
|
||
response = requests.get( | ||
'https://api.spotinst.io/aws/ec2/group/{}?accountId={}'.format(elastigroup_id, spotinst_account_id), | ||
headers=headers, timeout=5) | ||
response.raise_for_status() | ||
data = response.json() | ||
groups = data.get("response", {}).get("items", []) | ||
|
||
return groups[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
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,59 @@ | ||
import responses | ||
from spotinst.components.elastigroup_api import update_capacity, get_elastigroup, SPOTINST_API_URL | ||
|
||
|
||
def test_update_capacity(monkeypatch): | ||
update = { | ||
'response': { | ||
'items': [{ | ||
'id': 'sig-xfy', | ||
'name': 'my-app-1', | ||
'capacity': { | ||
'minimum': 1, | ||
'maximum': 3, | ||
'target': 3, | ||
'unit': 'instance' | ||
} | ||
}] | ||
} | ||
} | ||
|
||
elastigroup_id = 'sig-xfy' | ||
spotinst_account_id = 'act-zwk' | ||
spotinst_token = 'fake-token' | ||
with responses.RequestsMock() as rsps: | ||
rsps.add(rsps.PUT, | ||
'{}/aws/ec2/group/{}?accountId={}'.format(SPOTINST_API_URL, elastigroup_id, spotinst_account_id), | ||
status=200, | ||
json=update) | ||
|
||
update_response = update_capacity(1, 3, 3, elastigroup_id, spotinst_account_id, spotinst_token) | ||
assert update_response['id'] == elastigroup_id | ||
assert update_response['name'] == 'my-app-1' | ||
assert update_response['capacity']['minimum'] == 1 | ||
assert update_response['capacity']['maximum'] == 3 | ||
assert update_response['capacity']['target'] == 3 | ||
|
||
|
||
def test_get_elastigroup(monkeypatch): | ||
group = { | ||
'response': { | ||
'items': [{ | ||
'id': 'sig-xfy', | ||
'name': 'my-app-1', | ||
'region': 'eu-central-1', | ||
}] | ||
} | ||
} | ||
|
||
elastigroup_id = 'sig-xfy' | ||
spotinst_account_id = 'act-zwk' | ||
spotinst_token = 'fake-token' | ||
with responses.RequestsMock() as rsps: | ||
rsps.add(rsps.GET, 'https://api.spotinst.io/aws/ec2/group/{}?accountId={}'.format(elastigroup_id, spotinst_account_id), | ||
status=200, | ||
json=group) | ||
|
||
group = get_elastigroup(elastigroup_id, spotinst_account_id, spotinst_token) | ||
assert group['id'] == elastigroup_id | ||
assert group['name'] == 'my-app-1' |