Merge "replace windows line endings with unix format"
This commit is contained in:
commit
51816928ac
samples/python-zaqarclient
zaqar/storage/redis
@ -1,30 +1,30 @@
|
|||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
# use this file except in compliance with the License. You may obtain a copy
|
# use this file except in compliance with the License. You may obtain a copy
|
||||||
# of the License at
|
# of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
# 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 under
|
# License for the specific language governing permissions and limitations under
|
||||||
# the License.
|
# the License.
|
||||||
from zaqarclient.queues.v1 import client
|
from zaqarclient.queues.v1 import client
|
||||||
|
|
||||||
client = client.Client('http://localhost:8888', conf={
|
client = client.Client('http://localhost:8888', conf={
|
||||||
'auth_opts': {
|
'auth_opts': {
|
||||||
'options': {
|
'options': {
|
||||||
'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a',
|
'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a',
|
||||||
'os_auth_token': '8444886dd9b04a1b87ddb502b508261c',
|
'os_auth_token': '8444886dd9b04a1b87ddb502b508261c',
|
||||||
'os_auth_url': 'http://localhost:5000/v3.0/',
|
'os_auth_url': 'http://localhost:5000/v3.0/',
|
||||||
'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c'
|
'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, version=2)
|
}, version=2)
|
||||||
|
|
||||||
queue = client.queue('SampleQueue')
|
queue = client.queue('SampleQueue')
|
||||||
|
|
||||||
claim = queue.claim(ttl=600, grace=600) # refer to bug #1553387
|
claim = queue.claim(ttl=600, grace=600) # refer to bug #1553387
|
||||||
|
|
||||||
for msg in claim:
|
for msg in claim:
|
||||||
print(msg)
|
print(msg)
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
# use this file except in compliance with the License. You may obtain a copy
|
# use this file except in compliance with the License. You may obtain a copy
|
||||||
# of the License at
|
# of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
# 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 under
|
# License for the specific language governing permissions and limitations under
|
||||||
# the License.
|
# the License.
|
||||||
from zaqarclient.queues.v1 import client
|
from zaqarclient.queues.v1 import client
|
||||||
|
|
||||||
client = client.Client('http://localhost:8888', conf={
|
client = client.Client('http://localhost:8888', conf={
|
||||||
'auth_opts': {
|
'auth_opts': {
|
||||||
'options': {
|
'options': {
|
||||||
'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a',
|
'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a',
|
||||||
'os_auth_token': '8444886dd9b04a1b87ddb502b508261c',
|
'os_auth_token': '8444886dd9b04a1b87ddb502b508261c',
|
||||||
'os_auth_url': 'http://localhost:5000/v3.0/',
|
'os_auth_url': 'http://localhost:5000/v3.0/',
|
||||||
'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c'
|
'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, version=2)
|
}, version=2)
|
||||||
|
|
||||||
queue = client.queue('SampleQueue')
|
queue = client.queue('SampleQueue')
|
||||||
|
|
||||||
queue.post([{'body': 'Zaqar Sample'}])
|
queue.post([{'body': 'Zaqar Sample'}])
|
||||||
|
@ -1,181 +1,181 @@
|
|||||||
# Copyright (c) 2017 ZTE Corporation.
|
# Copyright (c) 2017 ZTE Corporation.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
# use this file except in compliance with the License. You may obtain a copy
|
# use this file except in compliance with the License. You may obtain a copy
|
||||||
# of the License at
|
# of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
# 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 under
|
# License for the specific language governing permissions and limitations under
|
||||||
# the License.
|
# the License.
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
import msgpack
|
import msgpack
|
||||||
import redis
|
import redis
|
||||||
|
|
||||||
from zaqar.storage import base
|
from zaqar.storage import base
|
||||||
from zaqar.storage import errors
|
from zaqar.storage import errors
|
||||||
from zaqar.storage.redis import utils
|
from zaqar.storage.redis import utils
|
||||||
|
|
||||||
|
|
||||||
class FlavorsController(base.FlavorsBase):
|
class FlavorsController(base.FlavorsBase):
|
||||||
"""Implements flavor resource operations using Redis.
|
"""Implements flavor resource operations using Redis.
|
||||||
|
|
||||||
Redis Data Structures:
|
Redis Data Structures:
|
||||||
1 All flavor_ids (Redis sorted set):
|
1 All flavor_ids (Redis sorted set):
|
||||||
|
|
||||||
Set of all flavor_ids, ordered by name. Used to
|
Set of all flavor_ids, ordered by name. Used to
|
||||||
delete the all records of table flavors
|
delete the all records of table flavors
|
||||||
|
|
||||||
Key: flavors
|
Key: flavors
|
||||||
|
|
||||||
+--------+-----------------------------+
|
+--------+-----------------------------+
|
||||||
| Id | Value |
|
| Id | Value |
|
||||||
+========+=============================+
|
+========+=============================+
|
||||||
| name | <flavor> |
|
| name | <flavor> |
|
||||||
+--------+-----------------------------+
|
+--------+-----------------------------+
|
||||||
|
|
||||||
2 Project Index (Redis sorted set):
|
2 Project Index (Redis sorted set):
|
||||||
|
|
||||||
Set of all flavors for the given project, ordered by name.
|
Set of all flavors for the given project, ordered by name.
|
||||||
|
|
||||||
Key: <project_id>.flavors
|
Key: <project_id>.flavors
|
||||||
|
|
||||||
+--------+-----------------------------+
|
+--------+-----------------------------+
|
||||||
| Id | Value |
|
| Id | Value |
|
||||||
+========+=============================+
|
+========+=============================+
|
||||||
| name | <flavor> |
|
| name | <flavor> |
|
||||||
+--------+-----------------------------+
|
+--------+-----------------------------+
|
||||||
|
|
||||||
3 Flavor Information (Redis hash):
|
3 Flavor Information (Redis hash):
|
||||||
|
|
||||||
Key: <flavor_id>.flavors
|
Key: <flavor_id>.flavors
|
||||||
|
|
||||||
+----------------------+---------+
|
+----------------------+---------+
|
||||||
| Name | Field |
|
| Name | Field |
|
||||||
+======================+=========+
|
+======================+=========+
|
||||||
| flavor | f |
|
| flavor | f |
|
||||||
+----------------------+---------+
|
+----------------------+---------+
|
||||||
| project | p |
|
| project | p |
|
||||||
+----------------------+---------+
|
+----------------------+---------+
|
||||||
| capabilities | c |
|
| capabilities | c |
|
||||||
+----------------------+---------+
|
+----------------------+---------+
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(FlavorsController, self).__init__(*args, **kwargs)
|
super(FlavorsController, self).__init__(*args, **kwargs)
|
||||||
self._client = self.driver.connection
|
self._client = self.driver.connection
|
||||||
self._packer = msgpack.Packer(encoding='utf-8',
|
self._packer = msgpack.Packer(encoding='utf-8',
|
||||||
use_bin_type=True).pack
|
use_bin_type=True).pack
|
||||||
self._unpacker = functools.partial(msgpack.unpackb, encoding='utf-8')
|
self._unpacker = functools.partial(msgpack.unpackb, encoding='utf-8')
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def list(self, project=None, marker=None, limit=10, detailed=False):
|
def list(self, project=None, marker=None, limit=10, detailed=False):
|
||||||
client = self._client
|
client = self._client
|
||||||
subset_key = utils.flavor_project_subset_key(project)
|
subset_key = utils.flavor_project_subset_key(project)
|
||||||
marker_key = utils.flavor_name_hash_key(marker)
|
marker_key = utils.flavor_name_hash_key(marker)
|
||||||
rank = client.zrank(subset_key, marker_key)
|
rank = client.zrank(subset_key, marker_key)
|
||||||
start = rank + 1 if rank is not None else 0
|
start = rank + 1 if rank is not None else 0
|
||||||
|
|
||||||
cursor = (f for f in client.zrange(subset_key, start,
|
cursor = (f for f in client.zrange(subset_key, start,
|
||||||
start + limit - 1))
|
start + limit - 1))
|
||||||
marker_next = {}
|
marker_next = {}
|
||||||
|
|
||||||
def normalizer(flavor):
|
def normalizer(flavor):
|
||||||
marker_next['next'] = flavor['f']
|
marker_next['next'] = flavor['f']
|
||||||
return self._normalize(flavor, detailed=detailed)
|
return self._normalize(flavor, detailed=detailed)
|
||||||
|
|
||||||
yield utils.FlavorListCursor(self._client, cursor, normalizer)
|
yield utils.FlavorListCursor(self._client, cursor, normalizer)
|
||||||
yield marker_next and marker_next['next']
|
yield marker_next and marker_next['next']
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def get(self, name, project=None, detailed=False):
|
def get(self, name, project=None, detailed=False):
|
||||||
hash_key = utils.flavor_name_hash_key(name)
|
hash_key = utils.flavor_name_hash_key(name)
|
||||||
flavors = self._client.hgetall(hash_key)
|
flavors = self._client.hgetall(hash_key)
|
||||||
|
|
||||||
if flavors is None or len(flavors) == 0:
|
if flavors is None or len(flavors) == 0:
|
||||||
raise errors.FlavorDoesNotExist(name)
|
raise errors.FlavorDoesNotExist(name)
|
||||||
|
|
||||||
return self._normalize(flavors, detailed)
|
return self._normalize(flavors, detailed)
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def create(self, name, project=None, capabilities=None):
|
def create(self, name, project=None, capabilities=None):
|
||||||
|
|
||||||
capabilities = {} if capabilities is None else capabilities
|
capabilities = {} if capabilities is None else capabilities
|
||||||
subset_key = utils.flavor_project_subset_key(project)
|
subset_key = utils.flavor_project_subset_key(project)
|
||||||
set_key = utils.flavor_set_key()
|
set_key = utils.flavor_set_key()
|
||||||
hash_key = utils.flavor_name_hash_key(name)
|
hash_key = utils.flavor_name_hash_key(name)
|
||||||
|
|
||||||
flavors = self._client.hgetall(hash_key)
|
flavors = self._client.hgetall(hash_key)
|
||||||
if len(flavors) == 0:
|
if len(flavors) == 0:
|
||||||
flavors = {
|
flavors = {
|
||||||
'f': name,
|
'f': name,
|
||||||
'p': project,
|
'p': project,
|
||||||
'c': self._packer(capabilities or {}),
|
'c': self._packer(capabilities or {}),
|
||||||
}
|
}
|
||||||
# Pipeline ensures atomic inserts.
|
# Pipeline ensures atomic inserts.
|
||||||
with self._client.pipeline() as pipe:
|
with self._client.pipeline() as pipe:
|
||||||
pipe.zadd(set_key, 1, hash_key)
|
pipe.zadd(set_key, 1, hash_key)
|
||||||
pipe.zadd(subset_key, 1, hash_key)
|
pipe.zadd(subset_key, 1, hash_key)
|
||||||
pipe.hmset(hash_key, flavors)
|
pipe.hmset(hash_key, flavors)
|
||||||
pipe.execute()
|
pipe.execute()
|
||||||
else:
|
else:
|
||||||
with self._client.pipeline() as pipe:
|
with self._client.pipeline() as pipe:
|
||||||
pipe.hset(hash_key, "c", self._packer(capabilities))
|
pipe.hset(hash_key, "c", self._packer(capabilities))
|
||||||
pipe.hset(hash_key, "p", project)
|
pipe.hset(hash_key, "p", project)
|
||||||
pipe.execute()
|
pipe.execute()
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def exists(self, name, project=None):
|
def exists(self, name, project=None):
|
||||||
set_key = utils.flavor_set_key()
|
set_key = utils.flavor_set_key()
|
||||||
hash_key = utils.flavor_name_hash_key(name)
|
hash_key = utils.flavor_name_hash_key(name)
|
||||||
return self._client.zrank(set_key, hash_key) is not None
|
return self._client.zrank(set_key, hash_key) is not None
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def update(self, name, project=None, capabilities=None):
|
def update(self, name, project=None, capabilities=None):
|
||||||
hash_key = utils.flavor_name_hash_key(name)
|
hash_key = utils.flavor_name_hash_key(name)
|
||||||
with self._client.pipeline() as pipe:
|
with self._client.pipeline() as pipe:
|
||||||
pipe.hset(hash_key, "c", self._packer(capabilities))
|
pipe.hset(hash_key, "c", self._packer(capabilities))
|
||||||
pipe.hset(hash_key, "p", project)
|
pipe.hset(hash_key, "p", project)
|
||||||
try:
|
try:
|
||||||
pipe.execute()
|
pipe.execute()
|
||||||
except redis.exceptions.ResponseError:
|
except redis.exceptions.ResponseError:
|
||||||
raise errors.FlavorDoesNotExist(name)
|
raise errors.FlavorDoesNotExist(name)
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def delete(self, name, project=None):
|
def delete(self, name, project=None):
|
||||||
subset_key = utils.flavor_project_subset_key(project)
|
subset_key = utils.flavor_project_subset_key(project)
|
||||||
set_key = utils.flavor_set_key()
|
set_key = utils.flavor_set_key()
|
||||||
hash_key = utils.flavor_name_hash_key(name)
|
hash_key = utils.flavor_name_hash_key(name)
|
||||||
if self._client.zrank(subset_key, hash_key) is not None:
|
if self._client.zrank(subset_key, hash_key) is not None:
|
||||||
with self._client.pipeline() as pipe:
|
with self._client.pipeline() as pipe:
|
||||||
pipe.zrem(set_key, hash_key)
|
pipe.zrem(set_key, hash_key)
|
||||||
pipe.zrem(subset_key, hash_key)
|
pipe.zrem(subset_key, hash_key)
|
||||||
pipe.delete(hash_key)
|
pipe.delete(hash_key)
|
||||||
pipe.execute()
|
pipe.execute()
|
||||||
|
|
||||||
@utils.raises_conn_error
|
@utils.raises_conn_error
|
||||||
def drop_all(self):
|
def drop_all(self):
|
||||||
allflavor_key = self._client.keys(pattern='*flavors')
|
allflavor_key = self._client.keys(pattern='*flavors')
|
||||||
if len(allflavor_key) == 0:
|
if len(allflavor_key) == 0:
|
||||||
return
|
return
|
||||||
with self._client.pipeline() as pipe:
|
with self._client.pipeline() as pipe:
|
||||||
for key in allflavor_key:
|
for key in allflavor_key:
|
||||||
pipe.delete(key)
|
pipe.delete(key)
|
||||||
try:
|
try:
|
||||||
pipe.execute()
|
pipe.execute()
|
||||||
except redis.exceptions.ResponseError:
|
except redis.exceptions.ResponseError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _normalize(self, flavor, detailed=False):
|
def _normalize(self, flavor, detailed=False):
|
||||||
ret = {
|
ret = {
|
||||||
'name': flavor['f'],
|
'name': flavor['f'],
|
||||||
}
|
}
|
||||||
|
|
||||||
if detailed:
|
if detailed:
|
||||||
ret['capabilities'] = self._unpacker(flavor['c'])
|
ret['capabilities'] = self._unpacker(flavor['c'])
|
||||||
return ret
|
return ret
|
||||||
|
Loading…
x
Reference in New Issue
Block a user