Merge "Reuse BaseManager and Resource from oslo"

This commit is contained in:
Jenkins 2013-12-19 20:58:16 +00:00 committed by Gerrit Code Review
commit 6e38bc0f73
6 changed files with 66 additions and 31 deletions

View File

@ -17,6 +17,7 @@ import copy
import logging
import os
import posixpath
import requests
import socket
from heatclient.openstack.common import jsonutils
@ -222,6 +223,30 @@ class HTTPClient(object):
'application/octet-stream')
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):
"""httplib-compatibile connection using client-side SSL authentication

View File

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

View File

@ -12,8 +12,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# 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 import strutils
from heatclient.v1 import stacks
@ -34,6 +35,9 @@ class Event(base.Resource):
def data(self, **kwargs):
return self.manager.data(self, **kwargs)
def to_dict(self):
return copy.deepcopy(self._info)
class EventManager(stacks.StackChildManager):
resource_class = Event
@ -67,5 +71,5 @@ class EventManager(stacks.StackChildManager):
urlutils.quote(stack_id, ''),
urlutils.quote(strutils.safe_encode(resource_name), ''),
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'])

View File

@ -11,7 +11,7 @@
# License for the specific language governing permissions and limitations
# 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 import strutils
@ -27,7 +27,7 @@ class ResourceType(base.Resource):
self.resource_type = info
class ResourceTypeManager(base.Manager):
class ResourceTypeManager(base.BaseManager):
resource_class = ResourceType
def list(self):
@ -43,5 +43,5 @@ class ResourceTypeManager(base.Manager):
"""
url_str = '/resource_types/%s' % (
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

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# 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 import strutils
from heatclient.v1 import stacks
@ -56,7 +56,7 @@ class ResourceManager(stacks.StackChildManager):
url_str = '/stacks/%s/resources/%s' % (
urlutils.quote(stack_id, ''),
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'])
def metadata(self, stack_id, resource_name):
@ -70,12 +70,12 @@ class ResourceManager(stacks.StackChildManager):
url_str = '/stacks/%s/resources/%s/metadata' % (
urlutils.quote(stack_id, ''),
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']
def generate_template(self, resource_name):
# Use urlutils for python2/python3 compatibility
url_str = '/resource_types/%s/template' % (
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

View File

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