Merge "Add method to create network resource tags"

This commit is contained in:
Zuul 2024-12-14 23:19:57 +00:00 committed by Gerrit Code Review
commit 91f7249c0e
12 changed files with 80 additions and 24 deletions

View File

@ -9,7 +9,11 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack import exceptions
from openstack import resource
from openstack import utils
class NetworkResource(resource.Resource):
@ -39,3 +43,19 @@ class NetworkResource(resource.Resource):
if if_revision is not None:
req.headers['If-Match'] = "revision_number=%d" % if_revision
return req
class TagMixinNetwork(tag.TagMixin):
def add_tags(self, session, tags):
"""Create the tags on the resource
:param session: The session to use for making this request.
:param tags: List with tags to be set on the resource
"""
tags = tags or []
url = utils.urljoin(self.base_path, self.id, 'tags')
session = self._get_session(session)
response = session.post(url, json={'tags': tags})
exceptions.raise_from_response(response)
self._body.attributes.update({'tags': tags})
return self

View File

@ -5518,6 +5518,19 @@ class Proxy(proxy.Proxy):
self._check_tag_support(resource)
return resource.set_tags(self, tags)
def add_tags(self, resource, tags):
"""Add tags to a specified resource
:param resource: :class:`~openstack.resource.Resource` instance.
:param tags: New tags to be set.
:type tags: "list"
:returns: The updated resource
:rtype: :class:`~openstack.resource.Resource`
"""
self._check_tag_support(resource)
return resource.add_tags(self, tags)
def add_tag(self, resource, tag):
"""Add one single tag to a specified resource

View File

@ -9,12 +9,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class FloatingIP(_base.NetworkResource, tag.TagMixin):
class FloatingIP(_base.NetworkResource, _base.TagMixinNetwork):
name_attribute = "floating_ip_address"
resource_name = "floating ip"
resource_key = 'floatingip'
@ -43,7 +43,7 @@ class FloatingIP(_base.NetworkResource, tag.TagMixin):
'sort_key',
'sort_dir',
tenant_id='project_id',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -9,12 +9,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class Network(_base.NetworkResource, tag.TagMixin):
class Network(_base.NetworkResource, _base.TagMixinNetwork):
resource_key = 'network'
resources_key = 'networks'
base_path = '/networks'
@ -43,7 +43,7 @@ class Network(_base.NetworkResource, tag.TagMixin):
provider_network_type='provider:network_type',
provider_physical_network='provider:physical_network',
provider_segmentation_id='provider:segmentation_id',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -10,12 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class Port(_base.NetworkResource, tag.TagMixin):
class Port(_base.NetworkResource, _base.TagMixinNetwork):
resource_key = 'port'
resources_key = 'ports'
base_path = '/ports'
@ -53,7 +52,7 @@ class Port(_base.NetworkResource, tag.TagMixin):
is_admin_state_up='admin_state_up',
is_port_security_enabled='port_security_enabled',
security_group_ids='security_groups',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -9,14 +9,14 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack import exceptions
from openstack.network.v2 import _base
from openstack import resource
from openstack import utils
class Router(_base.NetworkResource, tag.TagMixin):
class Router(_base.NetworkResource, _base.TagMixinNetwork):
resource_key = 'router'
resources_key = 'routers'
base_path = '/routers'
@ -40,7 +40,7 @@ class Router(_base.NetworkResource, tag.TagMixin):
is_admin_state_up='admin_state_up',
is_distributed='distributed',
is_ha='ha',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -9,12 +9,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class SecurityGroup(_base.NetworkResource, tag.TagMixin):
class SecurityGroup(_base.NetworkResource, _base.TagMixinNetwork):
resource_key = 'security_group'
resources_key = 'security_groups'
base_path = '/security-groups'
@ -37,7 +37,7 @@ class SecurityGroup(_base.NetworkResource, tag.TagMixin):
'revision_number',
'sort_dir',
'sort_key',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -9,12 +9,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class SecurityGroupRule(_base.NetworkResource, tag.TagMixin):
class SecurityGroupRule(_base.NetworkResource, _base.TagMixinNetwork):
resource_key = 'security_group_rule'
resources_key = 'security_group_rules'
base_path = '/security-group-rules'
@ -43,7 +43,7 @@ class SecurityGroupRule(_base.NetworkResource, tag.TagMixin):
'sort_dir',
'sort_key',
ether_type='ethertype',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -9,12 +9,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class Subnet(_base.NetworkResource, tag.TagMixin):
class Subnet(_base.NetworkResource, _base.TagMixinNetwork):
resource_key = 'subnet'
resources_key = 'subnets'
base_path = '/subnets'
@ -44,7 +44,7 @@ class Subnet(_base.NetworkResource, tag.TagMixin):
is_dhcp_enabled='enable_dhcp',
subnet_pool_id='subnetpool_id',
use_default_subnet_pool='use_default_subnetpool',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -9,11 +9,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.common import tag
from openstack.network.v2 import _base
from openstack import resource
class SubnetPool(resource.Resource, tag.TagMixin):
class SubnetPool(resource.Resource, _base.TagMixinNetwork):
resource_key = 'subnetpool'
resources_key = 'subnetpools'
base_path = '/subnetpools'
@ -37,7 +38,7 @@ class SubnetPool(resource.Resource, tag.TagMixin):
'sort_key',
'sort_dir',
is_shared='shared',
**tag.TagMixin._tag_query_parameters,
**_base.TagMixinNetwork._tag_query_parameters,
)
# Properties

View File

@ -88,3 +88,21 @@ class TestTagNeutron(base.BaseFunctionalTest):
self.user_cloud.network.remove_all_tags(sot)
sot = self.get_command(self.ID)
self.assertEqual([], sot.tags)
def test_add_tags(self):
sot = self.get_command(self.ID)
self.assertEqual([], sot.tags)
self.user_cloud.network.add_tags(sot, ["red", "green"])
self.user_cloud.network.add_tags(sot, ["blue", "yellow"])
sot = self.get_command(self.ID)
self.assertEqual(["blue", "green", "red", "yellow"], sot.tags)
# The operation is idempotent.
self.user_cloud.network.add_tags(sot, ["blue", "yellow"])
sot = self.get_command(self.ID)
self.assertEqual(["blue", "green", "red", "yellow"], sot.tags)
self.user_cloud.network.add_tags(sot, [])
sot = self.get_command(self.ID)
self.assertEqual(["blue", "green", "red", "yellow"], sot.tags)

View File

@ -0,0 +1,5 @@
---
features:
- |
Added a method to create (POST) tags in the network resources. This method
is idempotent.