Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #314 from cloudant/313-bluemix-client-constructor
Browse files Browse the repository at this point in the history
Implement Cloudant.bluemix client class method
  • Loading branch information
smithsz authored Aug 8, 2017
2 parents 19104ec + 17d8c63 commit 74a2784
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2.6.0 (Unreleased)
==================
- [NEW] Added ``Cloudant.bluemix()`` class method to the Cloudant client allowing service credentials to be passed using the CloudFoundry VCAP_SERVICES environment variable.
- [FIXED] Fixed client construction in ``cloudant_bluemix`` context manager.
- [FIXED] Fixed validation for feed options to accept zero as a valid value.

Expand Down
31 changes: 30 additions & 1 deletion src/cloudant/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
USER_AGENT,
append_response_error_content,
InfiniteSession,
ClientSession)
ClientSession,
CloudFoundryService)


class CouchDB(dict):
Expand Down Expand Up @@ -764,3 +765,31 @@ def _write_cors_configuration(self, config):
resp.raise_for_status()

return resp.json()

@classmethod
def bluemix(cls, vcap_services, instance_name=None, **kwargs):
"""
Create a Cloudant session using a VCAP_SERVICES environment variable.
:param vcap_services: VCAP_SERVICES environment variable
:type vcap_services: dict or str
:param str instance_name: Optional Bluemix instance name. Only required
if multiple Cloudant instances are available.
Example usage:
.. code-block:: python
import os
from cloudant.client import Cloudant
client = Cloudant.bluemix(os.getenv('VCAP_SERVICES'),
'Cloudant NoSQL DB')
print client.all_dbs()
"""
service = CloudFoundryService(vcap_services, instance_name)
return Cloudant(service.username,
service.password,
url=service.url,
**kwargs)
108 changes: 108 additions & 0 deletions tests/unit/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,114 @@ def test_constructor_with_account(self):
'https://{0}.cloudant.com'.format(self.account)
)

def test_bluemix_constructor(self):
"""
Test instantiating a client object using a VCAP_SERVICES environment
variable.
"""
instance_name = 'Cloudant NoSQL DB-lv'
vcap_services = {'cloudantNoSQLDB': [{
'credentials': {
'username': self.user,
'password': self.pwd,
'host': '{0}.cloudant.com'.format(self.account),
'port': 443,
'url': self.url
},
'name': instance_name
}]}

# create Cloudant Bluemix client
c = Cloudant.bluemix(vcap_services)

try:
c.connect()
self.assertIsInstance(c, Cloudant)
self.assertIsInstance(c.r_session, requests.Session)
self.assertEquals(c.session()['userCtx']['name'], self.user)

except Exception as err:
self.fail('Exception {0} was raised.'.format(str(err)))

finally:
c.disconnect()

def test_bluemix_constructor_specify_instance_name(self):
"""
Test instantiating a client object using a VCAP_SERVICES environment
variable and specifying which instance name to use.
"""
instance_name = 'Cloudant NoSQL DB-lv'
vcap_services = {'cloudantNoSQLDB': [{
'credentials': {
'username': self.user,
'password': self.pwd,
'host': '{0}.cloudant.com'.format(self.account),
'port': 443,
'url': self.url
},
'name': instance_name
}]}

# create Cloudant Bluemix client
c = Cloudant.bluemix(vcap_services, instance_name=instance_name)

try:
c.connect()
self.assertIsInstance(c, Cloudant)
self.assertIsInstance(c.r_session, requests.Session)
self.assertEquals(c.session()['userCtx']['name'], self.user)

except Exception as err:
self.fail('Exception {0} was raised.'.format(str(err)))

finally:
c.disconnect()

def test_bluemix_constructor_with_multiple_services(self):
"""
Test instantiating a client object using a VCAP_SERVICES environment
variable that contains multiple services.
"""
instance_name = 'Cloudant NoSQL DB-lv'
vcap_services = {'cloudantNoSQLDB': [
{
'credentials': {
'username': self.user,
'password': self.pwd,
'host': '{0}.cloudant.com'.format(self.account),
'port': 443,
'url': self.url
},
'name': instance_name
},
{
'credentials': {
'username': 'foo',
'password': 'bar',
'host': 'baz.com',
'port': 1234,
'url': 'https://foo:[email protected]:1234'
},
'name': 'Cloudant NoSQL DB-yu'
}
]}

# create Cloudant Bluemix client
c = Cloudant.bluemix(vcap_services, instance_name=instance_name)

try:
c.connect()
self.assertIsInstance(c, Cloudant)
self.assertIsInstance(c.r_session, requests.Session)
self.assertEquals(c.session()['userCtx']['name'], self.user)

except Exception as err:
self.fail('Exception {0} was raised.'.format(str(err)))

finally:
c.disconnect()

def test_connect_headers(self):
"""
Test that the appropriate request headers are set
Expand Down

0 comments on commit 74a2784

Please sign in to comment.