Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion objectrocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Import this object for ease of access.
from objectrocket.client import Client # noqa

VERSION = ('0', '3', '0')
VERSION = ('0', '3', '1')
__version__ = '.'.join(VERSION)
11 changes: 9 additions & 2 deletions objectrocket/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ def authenticate(self, username, password):

# Attempt to extract authentication data.
try:
json_data = resp.json()
token = json_data['data']['token']
if resp.status_code == 200:
json_data = resp.json()
token = json_data['data']['token']
elif resp.status_code == 401:
raise errors.AuthFailure(resp.json().get('message', 'Authentication Failure.'))
else:
raise errors.AuthFailure("Unknown exception while authenticating: '{}'".format(resp.text))
except errors.AuthFailure:
raise
except Exception as ex:
logging.exception(ex)
raise errors.AuthFailure('{}: {}'.format(ex.__class__.__name__, ex))
Expand Down
48 changes: 47 additions & 1 deletion objectrocket/bases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Base classes used throughout the library."""
import abc

import requests
import six

from objectrocket import errors
Expand Down Expand Up @@ -126,6 +126,52 @@ def __repr__(self):
)
return rep

def acl_sync(self, aws_sync=None, rackspace_sync=None):
"""Adjust Amazon Web Services and/or Rackspace Acl Sync feature for this instance.

:param bool aws_sync: True/False whether to enable AWS acl sync for this instance.
:param bool rackspace_sync: True/False whether to enable Rackspace acl sync for this instance.
"""
url = self._url + 'acl_sync'

data = {"aws_acl_sync_enabled": False, "rackspace_acl_sync_enabled": False}

# Let's get current status of acl sync for this intance to set proper defaults.
response = requests.get(url, **self._instances._default_request_kwargs)

if response.status_code == 200:
resp_json = response.json()
current_status = resp_json.get('data', {})
data.update({"aws_acl_sync_enabled": current_status.get("aws_acl_sync_enabled", False),
"rackspace_acl_sync_enabled": current_status.get("rackspace_acl_sync_enabled", False)})
if aws_sync is not None:
data.update({"aws_acl_sync_enabled": aws_sync})
if rackspace_sync is not None:
data.update({"rackspace_acl_sync_enabled": rackspace_sync})

response = requests.put(url, json=data, **self._instances._default_request_kwargs)
return response.json()
else:
raise errors.ObjectRocketException("Couldn't get current status of instance, failing. Error: {}".format(response.text))

def run_acl_sync(self, aws_sync=False, rackspace_sync=False):
"""Run Acl sync for this instance.

:param bool aws_sync: True/False whether to run AWS acl sync for this instance immediately.
:param bool rackspace_sync: True/False whether to run Rackspace acl sync for this instance immediately.
"""
url = self._url + 'acl_sync'

data = {"aws_acl_sync_enabled": False, "rackspace_acl_sync_enabled": False}

if aws_sync:
data.update({"aws_acl_sync_enabled": True})
if rackspace_sync:
data.update({"rackspace_acl_sync_enabled": True})

response = requests.post(url, json=data, **self._instances._default_request_kwargs)
return response.json()

@property
def connect_string(self):
"""This instance's connection string."""
Expand Down
1 change: 1 addition & 0 deletions objectrocket/instances/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from objectrocket import bases
from objectrocket import util
from objectrocket import errors

logger = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ sphinx_rtd_theme
tox>=2.1
wheel>=0.22.0
git+https://github.com/mverteuil/pytest-ipdb.git
responses>=0.4.0
13 changes: 4 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from objectrocket import instances
from objectrocket import constants


# def pytest_generate_tests(metafunc):
# """Generate tests for the different instance types."""
# if '_docs' in metafunc.fixturenames:
Expand Down Expand Up @@ -93,15 +92,15 @@ def mongodb_sharded_doc():


@pytest.fixture
def mongodb_replicaset_instance(client_token_auth, mongodb_replica_doc):
def mongodb_replicaset_instance(client, mongodb_replica_doc):
return instances.MongodbInstance(instance_document=mongodb_replica_doc,
client=client_token_auth)
instances=client.instances)


@pytest.fixture
def mongodb_sharded_instance(client_token_auth, mongodb_sharded_doc):
def mongodb_sharded_instance(client, mongodb_sharded_doc):
return instances.MongodbInstance(instance_document=mongodb_sharded_doc,
client=client_token_auth)
instances=client.instances)


######################
Expand All @@ -122,10 +121,6 @@ def patched_requests_map(request):
"""
patches = {}

mocked = mock.patch('objectrocket.auth.requests', autospec=True)
request.addfinalizer(mocked.stop)
patches['auth'] = mocked.start()

mocked = mock.patch('objectrocket.instances.requests', autospec=True)
request.addfinalizer(mocked.stop)
patches['instances'] = mocked.start()
Expand Down
127 changes: 127 additions & 0 deletions tests/test_acl_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import copy
import json
import mock
import pytest
import responses

from objectrocket.bases import BaseInstance

@pytest.yield_fixture(autouse=True)
def ensure_production_url(mongodb_sharded_instance, instance_acl_url):
"""Fixture that ensures that the proper production URLs are used in tests,
instead of the potentially overridden ones from environment variables.

See objectrocket.constants.OR_DEFAULT_API_URL
"""
inst = mongodb_sharded_instance
with mock.patch.object(BaseInstance, '_url', new_callable=mock.PropertyMock) as mock_url:
type(mock_url).return_value = instance_acl_url.replace('acl_sync', '')
yield

@pytest.fixture
def instance_acl_url(mongodb_sharded_instance):
return "https://sjc-api.objectrocket.com/v2/instances/{}/acl_sync".format(mongodb_sharded_instance.name)

class TestSetGetAclSync:

@responses.activate
def test_acl_sync_disables(self, client, mongodb_sharded_instance, instance_acl_url):
inst = mongodb_sharded_instance
responses.add(responses.PUT, instance_acl_url,
status=200,
body=json.dumps({'data': {'aws_acl_sync_enabled': False,
'rackspace_acl_sync_enabled': False}}),
content_type="application/json")
responses.add(responses.GET, instance_acl_url, status=200,
body=json.dumps({'data': {'aws_acl_sync_enabled': True,
'rackspace_acl_sync_enabled': True}}),
content_type="application/json")

response = inst.acl_sync(aws_sync=False, rackspace_sync=False)

assert isinstance(response, dict) is True
assert response.get('data', {}).get('aws_acl_sync_enabled', True) is False
assert response.get('data', {}).get('rackspace_acl_sync_enabled', True) is False

@responses.activate
def test_acl_sync_enables(self, client, mongodb_sharded_instance, instance_acl_url):
inst = mongodb_sharded_instance
responses.add(responses.PUT, instance_acl_url,
status=200,
body=json.dumps({'data': {'aws_acl_sync_enabled': True,
'rackspace_acl_sync_enabled': True}}),
content_type="application/json")
responses.add(responses.GET, instance_acl_url, status=200,
body=json.dumps({'data': {'aws_acl_sync_enabled': False,
'rackspace_acl_sync_enabled': False}}),
content_type="application/json")

response = inst.acl_sync(aws_sync=False, rackspace_sync=False)

assert isinstance(response, dict) is True
assert response.get('data', {}).get('aws_acl_sync_enabled', False) is True
assert response.get('data', {}).get('rackspace_acl_sync_enabled', False) is True

@responses.activate
def test_acl_sync_just_returns_status(self, client, mongodb_sharded_instance, instance_acl_url):
inst = mongodb_sharded_instance
responses.add(responses.GET, instance_acl_url, status=200,
body=json.dumps({'data': {'aws_acl_sync_enabled': True,
'rackspace_acl_sync_enabled': True}}),
content_type="application/json")
responses.add(responses.PUT, instance_acl_url,
status=200,
body=json.dumps({'data': {'aws_acl_sync_enabled': False,
'rackspace_acl_sync_enabled': False}}),
content_type="application/json")

response = inst.acl_sync()

assert isinstance(response, dict) is True
assert response.get('data', {}).get('aws_acl_sync_enabled', True) is False
assert response.get('data', {}).get('rackspace_acl_sync_enabled', True) is False

@responses.activate
def test_acl_sync_raises(self, client, mongodb_sharded_instance, instance_acl_url):
inst = mongodb_sharded_instance
exception = Exception('Test Exception')
responses.add(responses.GET, instance_acl_url, status=500,
body=exception)

with pytest.raises(Exception) as exinfo:
response = inst.acl_sync()

assert exinfo is not None
assert exinfo.value.args[0] == 'Test Exception'

class TestRunAclSync:

@responses.activate
def test_run_sync_without_args_is_unchanged(self, client, mongodb_sharded_instance, instance_acl_url):
inst = mongodb_sharded_instance
responses.add(responses.POST, instance_acl_url,
status=200,
body=json.dumps({'data': {'aws_acl_sync_state': 'unchanged',
'rackspace_acl_sync_state': 'unchanged'}}),
content_type="application/json")

response = inst.run_acl_sync()

assert isinstance(response, dict) is True
assert response.get('data', {}).get('aws_acl_sync_state', None) == 'unchanged'
assert response.get('data', {}).get('rackspace_acl_sync_state', None) == 'unchanged'

@responses.activate
def test_run_sync_starts(self, client, mongodb_sharded_instance, instance_acl_url):
inst = mongodb_sharded_instance
responses.add(responses.POST, instance_acl_url,
status=200,
body=json.dumps({'data': {'aws_acl_sync_state': 'started',
'rackspace_acl_sync_state': 'started'}}),
content_type="application/json")

response = inst.run_acl_sync(aws_sync=True, rackspace_sync=True)

assert isinstance(response, dict) is True
assert response.get('data', {}).get('aws_acl_sync_state', None) == 'started'
assert response.get('data', {}).get('rackspace_acl_sync_state', None) == 'started'
Loading