| 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 Flavor: |
| 20 |
|
| 21 |
def __init__(self, client, name, |
| 22 |
pool_list=None, auto_create=True, |
| 23 |
**kwargs): |
| 24 |
self.client = client |
| 25 |
|
| 26 |
self.name = name |
| 27 |
self.pool_list = pool_list |
| 28 |
self.capabilities = kwargs.get('capabilities', {}) |
| 29 |
|
| 30 |
if auto_create: |
| 31 |
self.ensure_exists() |
| 32 |
|
| 33 |
def ensure_exists(self): |
| 34 |
"""Ensures pool exists |
| 35 |
|
| 36 |
This method is not race safe, |
| 37 |
the pool could've been deleted |
| 38 |
right after it was called. |
| 39 |
""" |
| 40 |
req, trans = self.client._request_and_transport() |
| 41 |
# As of now on PUT, zaqar server updates flavor if it is already |
| 42 |
# exists else it will create a new one. The zaqar client should |
| 43 |
# maitain symmetry with zaqar server. |
| 44 |
# TBD(mdnadeem): Have to change this code when zaqar server |
| 45 |
# behaviour change for PUT operation. |
| 46 |
|
| 47 |
data = {'pool_list': self.pool_list} |
| 48 |
|
| 49 |
req, trans = self.client._request_and_transport() |
| 50 |
core.flavor_create(trans, req, self.name, data) |
| 51 |
|
| 52 |
def update(self, flavor_data): |
| 53 |
req, trans = self.client._request_and_transport() |
| 54 |
core.flavor_update(trans, req, self.name, flavor_data) |
| 55 |
|
| 56 |
for key, value in flavor_data.items(): |
| 57 |
setattr(self, key, value) |
| 58 |
|
| 59 |
def delete(self): |
| 60 |
req, trans = self.client._request_and_transport() |
| 61 |
core.flavor_delete(trans, req, self.name) |
| 62 |
|
| 63 |
def get(self): |
| 64 |
req, trans = self.client._request_and_transport() |
| 65 |
return core.flavor_get(trans, req, self.name, callback=None) |
| 66 |
|
| 67 |
|
| 68 |
def create_object(parent): |
| 69 |
return lambda kwargs: Flavor(parent, kwargs.pop('name'), |
| 70 |
auto_create=False, **kwargs) |