Make the instance 'host' attribute optional

Make the ``host`` resource attribute on the ``instance`` resource type
optional in Gnocchi.

This allows the hypervisor a compute instance is running on to be
withheld from Gnocchi's resource metadata, which may be required
for security reasons e.g. for public clouds.

To handle updating existing attributes, changes have been made to
the ``upgrade_resource_types`` function to make it check if any
existing attributes are identical to the specified schema, and
update the resource type accordingly.

Change-Id: I26875b876be6401164442d2f5e52e2eb9b8e703c
This commit is contained in:
Callum Dickinson
2025-03-20 10:59:00 +13:00
parent f5e74cd77f
commit 703ada2c05
2 changed files with 25 additions and 2 deletions

View File

@@ -218,6 +218,15 @@ resources_update_operations = [
"value": {"type": "string", "min_length": 0, "max_length": 255,
"required": False} # Only containers have a storage policy
}]},
{"desc": "make host optional for instance",
"type": "update_attribute_type",
"resource_type": "instance",
"data": [{
"op": "add", # Usually update, the attribute likely already exists
"path": "/attributes/host",
"value": {"type": "string", "min_length": 0, "max_length": 255,
"required": False} # Allow the hypervisor to be withheld
}]},
]
@@ -243,8 +252,15 @@ def upgrade_resource_types(conf):
if ops['type'] == 'update_attribute_type':
rt = gnocchi.resource_type.get(name=ops['resource_type'])
first_op = ops['data'][0]
attrib = first_op['path'].replace('/attributes', '')
if first_op['op'] == 'add' and attrib in rt['attributes']:
attrib = first_op['path'].replace('/attributes/', '')
# Options are only used when adding/updating attributes.
# Make a shallow copy of the new value type, and remove options
# from the copy to make sure it isn't included in checks.
value = first_op['value'].copy()
value.pop('options', None)
if (first_op['op'] == 'add'
and attrib in rt['attributes']
and value == rt['attributes'][attrib]):
continue
if first_op['op'] == 'remove' and attrib not in rt['attributes']:
continue

View File

@@ -0,0 +1,7 @@
---
upgrade:
- |
The ``instance`` resource type has been updated to make the ``host``
resource attribute optional. This allows the hypervisor a compute instance
is running on to be withheld from Gnocchi's resource metadata, which may
be required for security reasons e.g. for public clouds.