Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ec3d11

Browse files
committed
Merge pull request #1138 from tseaver/dns-client
Add DNS client / connection
2 parents 072990d + 23eadfc commit 5ec3d11

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

gcloud/dns/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Google Cloud DNS API wrapper.
16+
17+
The main concepts with this API are:
18+
19+
- :class:`gcloud.DNS.zone.ManagedZone` represents an collection of tables.
20+
"""
21+
22+
from gcloud.dns.client import Client
23+
from gcloud.dns.connection import Connection
24+
25+
26+
SCOPE = Connection.SCOPE

gcloud/dns/client.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""gcloud dns client for interacting with API."""
16+
17+
18+
from gcloud.client import JSONClient
19+
from gcloud.dns.connection import Connection
20+
21+
22+
class Client(JSONClient):
23+
"""Client to bundle configuration needed for API requests.
24+
25+
:type project: string
26+
:param project: the project which the client acts on behalf of. Will be
27+
passed when creating a dataset / job. If not passed,
28+
falls back to the default inferred from the environment.
29+
30+
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
31+
:class:`NoneType`
32+
:param credentials: The OAuth2 Credentials to use for the connection
33+
owned by this client. If not passed (and if no ``http``
34+
object is passed), falls back to the default inferred
35+
from the environment.
36+
37+
:type http: :class:`httplib2.Http` or class that defines ``request()``.
38+
:param http: An optional HTTP object to make requests. If not passed, an
39+
``http`` object is created that is bound to the
40+
``credentials`` for the current object.
41+
"""
42+
43+
_connection_class = Connection
44+
45+
def quotas(self):
46+
"""Return DNS quots for the project associated with this client.
47+
48+
See:
49+
https://cloud.google.com/dns/api/v1/projects/get
50+
51+
:rtype: mapping
52+
:returns: keys for the mapping correspond to those of the ``quota``
53+
sub-mapping of the project resource.
54+
"""
55+
path = '/projects/%s' % (self.project,)
56+
resp = self.connection.api_request(method='GET', path=path)
57+
return dict([(key, int(value))
58+
for key, value in resp['quota'].items()])

gcloud/dns/connection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Create / interact with gcloud dns connections."""
16+
17+
from gcloud import connection as base_connection
18+
19+
20+
class Connection(base_connection.JSONConnection):
21+
"""A connection to Google Cloud DNS via the JSON REST API."""
22+
23+
API_BASE_URL = 'https://www.googleapis.com'
24+
"""The base of the API call URL."""
25+
26+
API_VERSION = 'v1'
27+
"""The version of the API, used in building the API call's URL."""
28+
29+
API_URL_TEMPLATE = '{api_base_url}/dns/{api_version}{path}'
30+
"""A template for the URL of a particular API call."""
31+
32+
SCOPE = ('https://www.googleapis.com/auth/ndev.clouddns.readwrite',)
33+
"""The scopes required for authenticating as a Cloud DNS consumer."""

gcloud/dns/test_client.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
18+
class TestClient(unittest2.TestCase):
19+
20+
def _getTargetClass(self):
21+
from gcloud.dns.client import Client
22+
return Client
23+
24+
def _makeOne(self, *args, **kw):
25+
return self._getTargetClass()(*args, **kw)
26+
27+
def test_ctor(self):
28+
from gcloud.dns.connection import Connection
29+
PROJECT = 'PROJECT'
30+
creds = _Credentials()
31+
http = object()
32+
client = self._makeOne(project=PROJECT, credentials=creds, http=http)
33+
self.assertTrue(isinstance(client.connection, Connection))
34+
self.assertTrue(client.connection.credentials is creds)
35+
self.assertTrue(client.connection.http is http)
36+
37+
def test_quotas_defaults(self):
38+
PROJECT = 'PROJECT'
39+
PATH = 'projects/%s' % PROJECT
40+
MANAGED_ZONES = 1234
41+
RRS_PER_RRSET = 23
42+
RRSETS_PER_ZONE = 345
43+
RRSET_ADDITIONS = 456
44+
RRSET_DELETIONS = 567
45+
TOTAL_SIZE = 67890
46+
DATA = {
47+
'quota': {
48+
'managedZones': str(MANAGED_ZONES),
49+
'resourceRecordsPerRrset': str(RRS_PER_RRSET),
50+
'rrsetsPerManagedZone': str(RRSETS_PER_ZONE),
51+
'rrsetAdditionsPerChange': str(RRSET_ADDITIONS),
52+
'rrsetDeletionsPerChange': str(RRSET_DELETIONS),
53+
'totalRrdataSizePerChange': str(TOTAL_SIZE),
54+
}
55+
}
56+
CONVERTED = dict([(key, int(value))
57+
for key, value in DATA['quota'].items()])
58+
creds = _Credentials()
59+
client = self._makeOne(PROJECT, creds)
60+
conn = client.connection = _Connection(DATA)
61+
62+
quotas = client.quotas()
63+
64+
self.assertEqual(quotas, CONVERTED)
65+
66+
self.assertEqual(len(conn._requested), 1)
67+
req = conn._requested[0]
68+
self.assertEqual(req['method'], 'GET')
69+
self.assertEqual(req['path'], '/%s' % PATH)
70+
71+
72+
class _Credentials(object):
73+
74+
_scopes = None
75+
76+
@staticmethod
77+
def create_scoped_required():
78+
return True
79+
80+
def create_scoped(self, scope):
81+
self._scopes = scope
82+
return self
83+
84+
85+
class _Connection(object):
86+
87+
def __init__(self, *responses):
88+
self._responses = responses
89+
self._requested = []
90+
91+
def api_request(self, **kw):
92+
self._requested.append(kw)
93+
response, self._responses = self._responses[0], self._responses[1:]
94+
return response

gcloud/dns/test_connection.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
18+
class TestConnection(unittest2.TestCase):
19+
20+
def _getTargetClass(self):
21+
from gcloud.dns.connection import Connection
22+
return Connection
23+
24+
def _makeOne(self, *args, **kw):
25+
return self._getTargetClass()(*args, **kw)
26+
27+
def test_build_api_url_no_extra_query_params(self):
28+
conn = self._makeOne()
29+
URI = '/'.join([
30+
conn.API_BASE_URL,
31+
'dns',
32+
conn.API_VERSION,
33+
'foo',
34+
])
35+
self.assertEqual(conn.build_api_url('/foo'), URI)
36+
37+
def test_build_api_url_w_extra_query_params(self):
38+
from six.moves.urllib.parse import parse_qsl
39+
from six.moves.urllib.parse import urlsplit
40+
conn = self._makeOne()
41+
uri = conn.build_api_url('/foo', {'bar': 'baz'})
42+
scheme, netloc, path, qs, _ = urlsplit(uri)
43+
self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL)
44+
self.assertEqual(path,
45+
'/'.join(['', 'dns', conn.API_VERSION, 'foo']))
46+
parms = dict(parse_qsl(qs))
47+
self.assertEqual(parms['bar'], 'baz')

0 commit comments

Comments
 (0)