| 1 |
# Copyright (c) 2014 Red Hat, Inc. |
| 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 |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
from zaqarclient.queues.v2 import core |
| 17 |
|
| 18 |
|
| 19 |
class Pool: |
| 20 |
|
| 21 |
def __init__(self, client, name, |
| 22 |
weight=None, uri=None, |
| 23 |
flavor=None, auto_create=True, |
| 24 |
**kwargs): |
| 25 |
self.client = client |
| 26 |
self.uri = uri |
| 27 |
self.name = name |
| 28 |
self.weight = weight |
| 29 |
self.flavor = flavor |
| 30 |
self.options = kwargs.get("options", {}) |
| 31 |
|
| 32 |
if auto_create: |
| 33 |
self.ensure_exists() |
| 34 |
|
| 35 |
def ensure_exists(self): |
| 36 |
"""Ensures pool exists |
| 37 |
|
| 38 |
This method is not race safe, |
| 39 |
the pool could've been deleted |
| 40 |
right after it was called. |
| 41 |
""" |
| 42 |
req, trans = self.client._request_and_transport() |
| 43 |
# As of now on PUT, zaqar server updates pool if it is already |
| 44 |
# exists else it will create a new one. The zaqar client should |
| 45 |
# maitain symmetry with zaqar server. |
| 46 |
# TBD(mdnadeem): Have to change this code when zaqar server |
| 47 |
# behaviour change for PUT operation. |
| 48 |
|
| 49 |
data = {'uri': self.uri, |
| 50 |
'weight': self.weight, |
| 51 |
'options': self.options} |
| 52 |
|
| 53 |
if self.flavor: |
| 54 |
data['flavor'] = self.flavor |
| 55 |
|
| 56 |
req, trans = self.client._request_and_transport() |
| 57 |
core.pool_create(trans, req, self.name, data) |
| 58 |
|
| 59 |
def update(self, pool_data): |
| 60 |
req, trans = self.client._request_and_transport() |
| 61 |
core.pool_update(trans, req, self.name, pool_data) |
| 62 |
|
| 63 |
for key, value in pool_data.items(): |
| 64 |
setattr(self, key, value) |
| 65 |
|
| 66 |
def delete(self): |
| 67 |
req, trans = self.client._request_and_transport() |
| 68 |
core.pool_delete(trans, req, self.name) |
| 69 |
|
| 70 |
def get(self): |
| 71 |
req, trans = self.client._request_and_transport() |
| 72 |
return core.pool_get(trans, req, self.name, callback=None) |
| 73 |
|
| 74 |
|
| 75 |
def create_object(parent): |
| 76 |
return lambda kwargs: Pool(parent, kwargs.pop('name'), |
| 77 |
auto_create=False, **kwargs) |