Reuse BaseManager and Resource from oslo

In the process of unification of the clients code we should
reuse common functionality from Oslo.

Related to blueprint common-client-library-2

Change-Id: I44a1344e3eb40220d8b795fd05e44623195116a2
This commit is contained in:
Andrey Kurilin
2013-12-16 14:44:26 +02:00
parent fdaf41cf8a
commit a07b8b0b3f
6 changed files with 66 additions and 31 deletions

View File

@@ -17,6 +17,7 @@ import copy
import logging import logging
import os import os
import posixpath import posixpath
import requests
import socket import socket
from heatclient.openstack.common import jsonutils from heatclient.openstack.common import jsonutils
@@ -222,6 +223,30 @@ class HTTPClient(object):
'application/octet-stream') 'application/octet-stream')
return self._http_request(url, method, **kwargs) return self._http_request(url, method, **kwargs)
def client_request(self, method, url, **kwargs):
resp, body = self.json_request(method, url, **kwargs)
r = requests.Response()
r._content = jsonutils.dumps(body)
return r
def head(self, url, **kwargs):
return self.client_request("HEAD", url, **kwargs)
def get(self, url, **kwargs):
return self.client_request("GET", url, **kwargs)
def post(self, url, **kwargs):
return self.client_request("POST", url, **kwargs)
def put(self, url, **kwargs):
return self.client_request("PUT", url, **kwargs)
def delete(self, url, **kwargs):
return self.raw_request("DELETE", url, **kwargs)
def patch(self, url, **kwargs):
return self.client_request("PATCH", url, **kwargs)
class VerifiedHTTPSConnection(httplib.HTTPSConnection): class VerifiedHTTPSConnection(httplib.HTTPSConnection):
"""httplib-compatibile connection using client-side SSL authentication """httplib-compatibile connection using client-side SSL authentication

View File

@@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from heatclient.common import base from heatclient.openstack.common.apiclient import base
from heatclient.v1 import stacks from heatclient.v1 import stacks
DEFAULT_PAGE_SIZE = 20 DEFAULT_PAGE_SIZE = 20
@@ -38,13 +38,13 @@ class ActionManager(stacks.StackChildManager):
def suspend(self, stack_id): def suspend(self, stack_id):
"""Suspend a stack.""" """Suspend a stack."""
body = {'suspend': None} body = {'suspend': None}
resp, body = self.api.json_request('POST', resp, body = self.client.json_request('POST',
'/stacks/%s/actions' % stack_id, '/stacks/%s/actions' % stack_id,
body=body) body=body)
def resume(self, stack_id): def resume(self, stack_id):
"""Resume a stack.""" """Resume a stack."""
body = {'resume': None} body = {'resume': None}
resp, body = self.api.json_request('POST', resp, body = self.client.json_request('POST',
'/stacks/%s/actions' % stack_id, '/stacks/%s/actions' % stack_id,
body=body) body=body)

View File

@@ -12,8 +12,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import copy
from heatclient.common import base from heatclient.openstack.common.apiclient import base
from heatclient.openstack.common.py3kcompat import urlutils from heatclient.openstack.common.py3kcompat import urlutils
from heatclient.openstack.common import strutils from heatclient.openstack.common import strutils
from heatclient.v1 import stacks from heatclient.v1 import stacks
@@ -34,6 +35,9 @@ class Event(base.Resource):
def data(self, **kwargs): def data(self, **kwargs):
return self.manager.data(self, **kwargs) return self.manager.data(self, **kwargs)
def to_dict(self):
return copy.deepcopy(self._info)
class EventManager(stacks.StackChildManager): class EventManager(stacks.StackChildManager):
resource_class = Event resource_class = Event
@@ -67,5 +71,5 @@ class EventManager(stacks.StackChildManager):
urlutils.quote(stack_id, ''), urlutils.quote(stack_id, ''),
urlutils.quote(strutils.safe_encode(resource_name), ''), urlutils.quote(strutils.safe_encode(resource_name), ''),
urlutils.quote(event_id, '')) urlutils.quote(event_id, ''))
resp, body = self.api.json_request('GET', url_str) resp, body = self.client.json_request('GET', url_str)
return Event(self, body['event']) return Event(self, body['event'])

View File

@@ -11,7 +11,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from heatclient.common import base from heatclient.openstack.common.apiclient import base
from heatclient.openstack.common.py3kcompat import urlutils from heatclient.openstack.common.py3kcompat import urlutils
from heatclient.openstack.common import strutils from heatclient.openstack.common import strutils
@@ -27,7 +27,7 @@ class ResourceType(base.Resource):
self.resource_type = info self.resource_type = info
class ResourceTypeManager(base.Manager): class ResourceTypeManager(base.BaseManager):
resource_class = ResourceType resource_class = ResourceType
def list(self): def list(self):
@@ -43,5 +43,5 @@ class ResourceTypeManager(base.Manager):
""" """
url_str = '/resource_types/%s' % ( url_str = '/resource_types/%s' % (
urlutils.quote(strutils.safe_encode(resource_type), '')) urlutils.quote(strutils.safe_encode(resource_type), ''))
resp, body = self.api.json_request('GET', url_str) resp, body = self.client.json_request('GET', url_str)
return body return body

View File

@@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from heatclient.common import base from heatclient.openstack.common.apiclient import base
from heatclient.openstack.common.py3kcompat import urlutils from heatclient.openstack.common.py3kcompat import urlutils
from heatclient.openstack.common import strutils from heatclient.openstack.common import strutils
from heatclient.v1 import stacks from heatclient.v1 import stacks
@@ -56,7 +56,7 @@ class ResourceManager(stacks.StackChildManager):
url_str = '/stacks/%s/resources/%s' % ( url_str = '/stacks/%s/resources/%s' % (
urlutils.quote(stack_id, ''), urlutils.quote(stack_id, ''),
urlutils.quote(strutils.safe_encode(resource_name), '')) urlutils.quote(strutils.safe_encode(resource_name), ''))
resp, body = self.api.json_request('GET', url_str) resp, body = self.client.json_request('GET', url_str)
return Resource(self, body['resource']) return Resource(self, body['resource'])
def metadata(self, stack_id, resource_name): def metadata(self, stack_id, resource_name):
@@ -70,12 +70,12 @@ class ResourceManager(stacks.StackChildManager):
url_str = '/stacks/%s/resources/%s/metadata' % ( url_str = '/stacks/%s/resources/%s/metadata' % (
urlutils.quote(stack_id, ''), urlutils.quote(stack_id, ''),
urlutils.quote(strutils.safe_encode(resource_name), '')) urlutils.quote(strutils.safe_encode(resource_name), ''))
resp, body = self.api.json_request('GET', url_str) resp, body = self.client.json_request('GET', url_str)
return body['metadata'] return body['metadata']
def generate_template(self, resource_name): def generate_template(self, resource_name):
# Use urlutils for python2/python3 compatibility # Use urlutils for python2/python3 compatibility
url_str = '/resource_types/%s/template' % ( url_str = '/resource_types/%s/template' % (
urlutils.quote(strutils.safe_encode(resource_name), '')) urlutils.quote(strutils.safe_encode(resource_name), ''))
resp, body = self.api.json_request('GET', url_str) resp, body = self.client.json_request('GET', url_str)
return body return body

View File

@@ -12,11 +12,11 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import copy
from heatclient.openstack.common.apiclient import base
from heatclient.openstack.common.py3kcompat import urlutils from heatclient.openstack.common.py3kcompat import urlutils
from heatclient.common import base
class Stack(base.Resource): class Stack(base.Resource):
def __repr__(self): def __repr__(self):
@@ -57,8 +57,11 @@ class Stack(base.Resource):
def identifier(self): def identifier(self):
return '%s/%s' % (self.stack_name, self.id) return '%s/%s' % (self.stack_name, self.id)
def to_dict(self):
return copy.deepcopy(self._info)
class StackManager(base.Manager):
class StackManager(base.BaseManager):
resource_class = Stack resource_class = Stack
def list(self, **kwargs): def list(self, **kwargs):
@@ -109,16 +112,16 @@ class StackManager(base.Manager):
def create(self, **kwargs): def create(self, **kwargs):
"""Create a stack.""" """Create a stack."""
headers = self.api.credentials_headers() headers = self.client.credentials_headers()
resp, body = self.api.json_request('POST', '/stacks', resp, body = self.client.json_request('POST', '/stacks',
body=kwargs, headers=headers) body=kwargs, headers=headers)
return body return body
def update(self, stack_id, **kwargs): def update(self, stack_id, **kwargs):
"""Update a stack.""" """Update a stack."""
headers = self.api.credentials_headers() headers = self.client.credentials_headers()
resp, body = self.api.json_request('PUT', '/stacks/%s' % stack_id, resp, body = self.client.json_request('PUT', '/stacks/%s' % stack_id,
body=kwargs, headers=headers) body=kwargs, headers=headers)
def delete(self, stack_id): def delete(self, stack_id):
"""Delete a stack.""" """Delete a stack."""
@@ -129,7 +132,7 @@ class StackManager(base.Manager):
:param stack_id: Stack ID to lookup :param stack_id: Stack ID to lookup
""" """
resp, body = self.api.json_request('GET', '/stacks/%s' % stack_id) resp, body = self.client.json_request('GET', '/stacks/%s' % stack_id)
return Stack(self, body['stack']) return Stack(self, body['stack'])
def template(self, stack_id): def template(self, stack_id):
@@ -138,24 +141,27 @@ class StackManager(base.Manager):
:param stack_id: Stack ID to get the template for :param stack_id: Stack ID to get the template for
""" """
resp, body = self.api.json_request( resp, body = self.client.json_request(
'GET', '/stacks/%s/template' % stack_id) 'GET', '/stacks/%s/template' % stack_id)
return body return body
def validate(self, **kwargs): def validate(self, **kwargs):
"""Validate a stack template.""" """Validate a stack template."""
resp, body = self.api.json_request('POST', '/validate', body=kwargs) resp, body = self.client.json_request('POST', '/validate', body=kwargs)
return body return body
class StackChildManager(base.Manager): class StackChildManager(base.BaseManager):
@property
def api(self):
return self.client
def _resolve_stack_id(self, stack_id): def _resolve_stack_id(self, stack_id):
# if the id already has a slash in it, # if the id already has a slash in it,
# then it is already {stack_name}/{stack_id} # then it is already {stack_name}/{stack_id}
if stack_id.find('/') > 0: if stack_id.find('/') > 0:
return stack_id return stack_id
resp, body = self.api.json_request('GET', resp, body = self.client.json_request('GET',
'/stacks/%s' % stack_id) '/stacks/%s' % stack_id)
stack = body['stack'] stack = body['stack']
return '%s/%s' % (stack['stack_name'], stack['id']) return '%s/%s' % (stack['stack_name'], stack['id'])