Skip to content

Commit a4ac7f4

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Refactor Key Manager for resource2"
2 parents 1059954 + 0a19263 commit a4ac7f4

File tree

14 files changed

+399
-67
lines changed

14 files changed

+399
-67
lines changed

doc/source/users/guides/key_manager.rst

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,51 @@ connection to your OpenStack cloud by following the :doc:`connect` user
66
guide. This will provide you with the ``conn`` variable used in the examples
77
below.
88

9-
.. TODO(thowe): Implement this guide
9+
.. contents:: Table of Contents
10+
:local:
11+
12+
.. note:: Some interactions with the Key Manager service differ from that
13+
of other services in that resources do not have a proper ``id`` parameter,
14+
which is necessary to make some calls. Instead, resources have a separately
15+
named id attribute, e.g., the Secret resource has ``secret_id``.
16+
17+
The examples below outline when to pass in those id values.
18+
19+
Create a Secret
20+
---------------
21+
22+
The Key Manager service allows you to create new secrets by passing the
23+
attributes of the :class:`~openstack.key_manager.v1.secret.Secret` to the
24+
:meth:`~openstack.key_manager.v1._proxy.Proxy.create_secret` method.
25+
26+
.. literalinclude:: ../examples/key_manager/create.py
27+
:pyobject: create_secret
28+
29+
List Secrets
30+
------------
31+
32+
Once you have stored some secrets, they are available for you to list
33+
via the :meth:`~openstack.key_manager.v1._proxy.Proxy.secrets` method.
34+
This method returns a generator, which yields each
35+
:class:`~openstack.key_manager.v1.secret.Secret`.
36+
37+
.. literalinclude:: ../examples/key_manager/list.py
38+
:pyobject: list_secrets
39+
40+
The :meth:`~openstack.key_manager.v1._proxy.Proxy.secrets` method can
41+
also make more advanced queries to limit the secrets that are returned.
42+
43+
.. literalinclude:: ../examples/key_manager/list.py
44+
:pyobject: list_secrets_query
45+
46+
Get Secret Payload
47+
------------------
48+
49+
Once you have received a :class:`~openstack.key_manager.v1.secret.Secret`,
50+
you can obtain the payload for it by passing the secret's id value to
51+
the :meth:`~openstack.key_manager.v1._proxy.Proxy.secrets` method.
52+
Use the :data:`~openstack.key_manager.v1.secret.Secret.secret_id` attribute
53+
when making this request.
54+
55+
.. literalinclude:: ../examples/key_manager/get.py
56+
:pyobject: get_secret_payload

examples/key_manager/__init__.py

Whitespace-only changes.

examples/key_manager/create.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
"""
14+
List resources from the Key Manager service.
15+
"""
16+
17+
18+
def create_secret(conn):
19+
print("Create a secret:")
20+
21+
conn.key_manager.create_secret(name="My public key",
22+
secret_type="public",
23+
expiration="2020-02-28T23:59:59",
24+
payload="ssh rsa...",
25+
payload_content_type="text/plain")

examples/key_manager/get.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
"""
14+
List resources from the Key Manager service.
15+
"""
16+
17+
s = None
18+
19+
20+
def get_secret_payload(conn):
21+
print("Get a secret's payload:")
22+
23+
# Assuming you have an object `s` which you perhaps received from
24+
# a conn.key_manager.secrets() call...
25+
secret = conn.key_manager.get_secret(s.secret_id)
26+
print(secret.payload)

examples/key_manager/list.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
"""
14+
List resources from the Key Manager service.
15+
"""
16+
17+
18+
def list_secrets(conn):
19+
print("List Secrets:")
20+
21+
for secret in conn.key_manager.secrets():
22+
print(secret)
23+
24+
25+
def list_secrets_query(conn):
26+
print("List Secrets:")
27+
28+
for secret in conn.key_manager.secrets(
29+
secret_type="symmetric",
30+
expiration="gte:2020-01-01T00:00:00"):
31+
print(secret)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
from openstack import format
14+
15+
from six.moves.urllib import parse
16+
17+
18+
class HREFToUUID(format.Formatter):
19+
20+
@classmethod
21+
def deserialize(cls, value):
22+
"""Convert a HREF to the UUID portion"""
23+
parts = parse.urlsplit(value)
24+
25+
# Only try to proceed if we have an actual URI.
26+
# Just check that we have a scheme, netloc, and path.
27+
if not all(parts[:3]):
28+
raise ValueError("Unable to convert %s to an ID" % value)
29+
30+
# The UUID will be the last portion of the URI.
31+
return parts.path.split("/")[-1]
32+
33+
@classmethod
34+
def serialize(cls, value):
35+
# NOTE(briancurtin): If we had access to the session to get
36+
# the endpoint we could do something smart here like take an ID
37+
# and give back an HREF, but this will just have to be something
38+
# that works different because Barbican does what it does...
39+
return value

openstack/key_manager/v1/_proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from openstack.key_manager.v1 import container as _container
1414
from openstack.key_manager.v1 import order as _order
1515
from openstack.key_manager.v1 import secret as _secret
16-
from openstack import proxy
16+
from openstack import proxy2
1717

1818

19-
class Proxy(proxy.BaseProxy):
19+
class Proxy(proxy2.BaseProxy):
2020

2121
def create_container(self, **attrs):
2222
"""Create a new container from attributes

openstack/key_manager/v1/container.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,39 @@
1111
# under the License.
1212

1313
from openstack.key_manager import key_manager_service
14-
from openstack import resource
14+
from openstack.key_manager.v1 import _format
15+
from openstack import resource2
1516

1617

17-
class Container(resource.Resource):
18-
id_attribute = 'container_ref'
18+
class Container(resource2.Resource):
1919
resources_key = 'containers'
2020
base_path = '/containers'
2121
service = key_manager_service.KeyManagerService()
2222

2323
# capabilities
2424
allow_create = True
25-
allow_retrieve = True
25+
allow_get = True
2626
allow_update = True
2727
allow_delete = True
2828
allow_list = True
2929

3030
# Properties
3131
#: A URI for this container
32-
container_ref = resource.prop('container_ref')
32+
container_ref = resource2.Body('container_ref')
33+
#: The ID for this container
34+
container_id = resource2.Body('container_ref', alternate_id=True,
35+
type=_format.HREFToUUID)
3336
#: The timestamp when this container was created.
34-
created_at = resource.prop('created')
37+
created_at = resource2.Body('created')
3538
#: The name of this container
36-
name = resource.prop('name')
39+
name = resource2.Body('name')
3740
#: A list of references to secrets in this container
38-
secret_refs = resource.prop('secret_refs')
41+
secret_refs = resource2.Body('secret_refs', type=list)
3942
#: The status of this container
40-
status = resource.prop('status')
43+
status = resource2.Body('status')
4144
#: The type of this container
42-
type = resource.prop('type')
45+
type = resource2.Body('type')
4346
#: The timestamp when this container was updated.
44-
updated_at = resource.prop('updated')
47+
updated_at = resource2.Body('updated')
48+
#: A party interested in this container.
49+
consumers = resource2.Body('consumers', type=list)

openstack/key_manager/v1/order.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,45 @@
1111
# under the License.
1212

1313
from openstack.key_manager import key_manager_service
14-
from openstack import resource
14+
from openstack.key_manager.v1 import _format
15+
from openstack import resource2
1516

1617

17-
class Order(resource.Resource):
18+
class Order(resource2.Resource):
1819
resources_key = 'orders'
1920
base_path = '/orders'
2021
service = key_manager_service.KeyManagerService()
2122

2223
# capabilities
2324
allow_create = True
24-
allow_retrieve = True
25+
allow_get = True
2526
allow_update = True
2627
allow_delete = True
2728
allow_list = True
2829

29-
# Properties
30-
# TODO(briancurtin): not documented
31-
error_reason = resource.prop('error_reason')
32-
# TODO(briancurtin): not documented
33-
error_status_code = resource.prop('error_status_code')
34-
#: a dictionary containing key-value parameters which specify the
30+
#: Timestamp in ISO8601 format of when the order was created
31+
created_at = resource2.Body('created')
32+
#: Keystone Id of the user who created the order
33+
creator_id = resource2.Body('creator_id')
34+
#: A dictionary containing key-value parameters which specify the
3535
#: details of an order request
36-
meta = resource.prop('meta')
36+
meta = resource2.Body('meta', type=dict)
3737
#: A URI for this order
38-
order_ref = resource.prop('order_ref')
39-
#: TODO(briancurtin): not documented
40-
secret_ref = resource.prop('secret_ref')
38+
order_ref = resource2.Body('order_ref')
39+
#: The ID of this order
40+
order_id = resource2.Body('order_ref', alternate_id=True,
41+
type=_format.HREFToUUID)
42+
#: Secret href associated with the order
43+
secret_ref = resource2.Body('secret_ref')
44+
#: Secret ID associated with the order
45+
secret_id = resource2.Body('secret_ref', type=_format.HREFToUUID)
4146
# The status of this order
42-
status = resource.prop('status')
47+
status = resource2.Body('status')
48+
#: Metadata associated with the order
49+
sub_status = resource2.Body('sub_status')
50+
#: Metadata associated with the order
51+
sub_status_message = resource2.Body('sub_status_message')
4352
# The type of order
44-
type = resource.prop('type')
53+
type = resource2.Body('type')
54+
#: Timestamp in ISO8601 format of the last time the order was updated.
55+
updated_at = resource2.Body('updated')

0 commit comments

Comments
 (0)