support get and list server's metadata

Adds support for Nova Server get and list metadata API.

Change-Id: I4e53c6af932537004564371374bb54396f0cc05c
This commit is contained in:
Kien Nguyen 2024-04-16 16:20:04 +07:00
parent 2bd135c137
commit 8f8f5ab49d
4 changed files with 54 additions and 0 deletions

View File

@ -205,6 +205,19 @@ class Base(base.Fixture):
self.requests_mock.delete(self.url(u, 'metadata', 'key1'),
json=self.diagnostic,
headers=self.json_headers)
metadata3 = {'meta': {
'Server Label': 'Web Head 1'
}}
self.requests_mock.get(self.url(1234, 'metadata', 'Server Label'),
json=metadata3,
headers=self.json_headers)
metadata4 = {'metadata': {
'Server Label': 'Web Head 1',
'Image Version': '2.1'
}}
self.requests_mock.get(self.url(1234, 'metadata'),
json=metadata4,
headers=self.json_headers)
get_security_groups = {
"security_groups": [{

View File

@ -463,6 +463,23 @@ class ServersTest(utils.FixturedTestCase):
self.assert_called('PUT', '/servers/1234/metadata/test_key',
{'meta': {'test_key': 'test_value'}})
def test_get_server_meta(self):
m = self.cs.servers.get_meta(1234, 'Server Label')
self.assert_request_id(m, fakes.FAKE_REQUEST_ID_LIST)
self.assert_called('GET', '/servers/1234/metadata/Server%20Label')
self.assertEqual(m, {'meta': {
'Server Label': 'Web Head 1'
}})
def test_list_server_meta(self):
m = self.cs.servers.list_meta(1234)
self.assert_request_id(m, fakes.FAKE_REQUEST_ID_LIST)
self.assert_called('GET', '/servers/1234/metadata')
self.assertEqual(m, {'metadata': {
'Server Label': 'Web Head 1',
'Image Version': '2.1'
}})
def test_find(self):
server = self.cs.servers.find(name='sample-server')
self.assert_request_id(server, fakes.FAKE_REQUEST_ID_LIST)

View File

@ -2039,6 +2039,26 @@ class ServerManager(base.BootingManagerWithFind):
return self._update("/servers/%s/metadata/%s" %
(base.getid(server), key), body)
def list_meta(self, server):
"""
Lists all metadata for a server.
:param server: The :class:`Server` (or its ID).
:returns: A dict of metadata.
"""
resp, body = self.api.client.get("/servers/%s/metadata" %
base.getid(server))
return base.DictWithMeta(body, resp)
def get_meta(self, server, key):
"""
Shows details for a metadata item, by key, for a server.
:param server: The :class:`Server` (or its ID).
:param key: metadata key to get
"""
resp, body = self.api.client.get("/servers/%s/metadata/%s" %
(base.getid(server), key))
return base.DictWithMeta(body, resp)
def get_console_output(self, server, length=None):
"""
Get text console log output from Server.

View File

@ -0,0 +1,4 @@
---
features:
- |
Adds supports for Nova Server get and list metadata API.