Skip to content

Commit 60d2548

Browse files
authored
Add support for using cloud_id when instantiating conections (#957)
* add cloud_id * adding doc strings * update changelog * adding some tests to check for cloud_id * update the read me
1 parent e1968ed commit 60d2548

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

Changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
=========
55
7.1.0 (dev)
66
-----------
7+
* Add connection parameter for Elastic Cloud cloud_id.
78

89
7.0.1 (2019-05019)
910
-----------

README

+11-4
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,22 @@ Simple use-case::
8080

8181
# create an index in elasticsearch, ignore status code 400 (index already exists)
8282
>>> es.indices.create(index='my-index', ignore=400)
83-
{u'acknowledged': True}
83+
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'my-index'}
8484

8585
# datetimes will be serialized
8686
>>> es.index(index="my-index", id=42, body={"any": "data", "timestamp": datetime.now()})
87-
{u'_id': u'42', u'_index': u'my-index', u'_type': u'test-type', u'_version': 1, u'ok': True}
87+
{'_index': 'my-index',
88+
'_type': '_doc',
89+
'_id': '42',
90+
'_version': 1,
91+
'result': 'created',
92+
'_shards': {'total': 2, 'successful': 1, 'failed': 0},
93+
'_seq_no': 0,
94+
'_primary_term': 1}
8895

8996
# but not deserialized
9097
>>> es.get(index="my-index", id=42)['_source']
91-
{u'any': u'data', u'timestamp': u'2013-05-12T19:45:31.804229'}
98+
{'any': 'data', 'timestamp': '2019-05-17T17:28:10.329598'}
9299

93100
`Full documentation`_.
94101

@@ -97,7 +104,7 @@ Simple use-case::
97104
Elastic Cloud (and SSL) use-case::
98105

99106
>>> from elasticsearch import Elasticsearch
100-
>>> es = Elasticsearch("https://elasticsearch.url:port", http_auth=('elastic','yourpassword'))
107+
>>> es = Elasticsearch(cloud_id="<some_long_cloud_id>", http_auth=('elastic','yourpassword'))
101108
>>> es.info()
102109

103110
Using SSL Context with a self-signed cert use-case::

elasticsearch/connection/http_requests.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
import warnings
3+
from base64 import decodestring
34

45
try:
56
import requests
@@ -34,6 +35,8 @@ class RequestsHttpConnection(Connection):
3435
:arg client_key: path to the file containing the private key if using
3536
separate cert and key files (client_cert will contain only the cert)
3637
:arg headers: any custom http headers to be add to requests
38+
:arg cloud_id: The Cloud ID from ElasticCloud. Convient way to connect to cloud instances.
39+
Other host connection params will be ignored.
3740
"""
3841

3942
def __init__(
@@ -48,12 +51,21 @@ def __init__(
4851
client_cert=None,
4952
client_key=None,
5053
headers=None,
54+
cloud_id=None,
5155
**kwargs
5256
):
5357
if not REQUESTS_AVAILABLE:
5458
raise ImproperlyConfigured(
5559
"Please install requests to use RequestsHttpConnection."
5660
)
61+
if cloud_id:
62+
cluster_name, cloud_id = cloud_id.split(":")
63+
url, es_uuid, kibana_uuid = (
64+
decodestring(cloud_id.encode("utf-8")).decode("utf-8").split("$")
65+
)
66+
host = "%s.%s" % (es_uuid, url)
67+
port = 9243
68+
use_ssl = True
5769

5870
super(RequestsHttpConnection, self).__init__(
5971
host=host, port=port, use_ssl=use_ssl, **kwargs

elasticsearch/connection/http_urllib3.py

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from urllib3.util.retry import Retry
66
import warnings
77
import gzip
8+
from base64 import decodestring
89

910
# sentinal value for `verify_certs`.
1011
# This is used to detect if a user is passing in a value for `verify_certs`
@@ -72,6 +73,8 @@ class Urllib3HttpConnection(Connection):
7273
information.
7374
:arg headers: any custom http headers to be add to requests
7475
:arg http_compress: Use gzip compression
76+
:arg cloud_id: The Cloud ID from ElasticCloud. Convient way to connect to cloud instances.
77+
Other host connection params will be ignored.
7578
"""
7679

7780
def __init__(
@@ -92,9 +95,18 @@ def __init__(
9295
headers=None,
9396
ssl_context=None,
9497
http_compress=False,
98+
cloud_id=None,
9599
**kwargs
96100
):
97101

102+
if cloud_id:
103+
cluster_name, cloud_id = cloud_id.split(":")
104+
url, es_uuid, kibana_uuid = (
105+
decodestring(cloud_id.encode("utf-8")).decode("utf-8").split("$")
106+
)
107+
host = "%s.%s" % (es_uuid, url)
108+
port = "9243"
109+
use_ssl = True
98110
super(Urllib3HttpConnection, self).__init__(
99111
host=host, port=port, use_ssl=use_ssl, **kwargs
100112
)

test_elasticsearch/test_connection.py

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def test_ssl_context(self):
3434
self.assertIsInstance(con.pool.conn_kw["ssl_context"], ssl.SSLContext)
3535
self.assertTrue(con.use_ssl)
3636

37+
def test_http_cloud_id(self):
38+
con = Urllib3HttpConnection(
39+
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n"
40+
)
41+
self.assertTrue(con.use_ssl)
42+
self.assertEquals(
43+
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
44+
)
45+
3746
def test_http_compression(self):
3847
con = Urllib3HttpConnection(http_compress=True)
3948
self.assertTrue(con.http_compress)
@@ -151,6 +160,15 @@ def test_timeout_set(self):
151160
con = RequestsHttpConnection(timeout=42)
152161
self.assertEquals(42, con.timeout)
153162

163+
def test_http_cloud_id(self):
164+
con = RequestsHttpConnection(
165+
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n"
166+
)
167+
self.assertTrue(con.use_ssl)
168+
self.assertEquals(
169+
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
170+
)
171+
154172
def test_uses_https_if_verify_certs_is_off(self):
155173
with warnings.catch_warnings(record=True) as w:
156174
con = self._get_mock_connection(

0 commit comments

Comments
 (0)