diff options
author | Lingxian Kong <anlin.kong@gmail.com> | 2021-08-02 13:15:56 +1200 |
---|---|---|
committer | Lingxian Kong <anlin.kong@gmail.com> | 2021-08-02 14:25:54 +1200 |
commit | 5590ecdce036f7a2b11ff440dfd4eb637180abcc (patch) | |
tree | 5b778f8d093d92dd7e94c224a83e23deb294023b /trove/common | |
parent | 036948c516672e55c4a2c932c74bcdec80319108 (diff) | |
download | trove-5590ecdce036f7a2b11ff440dfd4eb637180abcc.tar.gz |
Show user network ID for getting instance
Change-Id: Ia1e9112ae69e04f8c3e9e9d1b4a0189c743d7448
Diffstat (limited to 'trove/common')
-rw-r--r-- | trove/common/cache.py | 57 | ||||
-rw-r--r-- | trove/common/neutron.py | 21 |
2 files changed, 78 insertions, 0 deletions
diff --git a/trove/common/cache.py b/trove/common/cache.py new file mode 100644 index 00000000..7c259e44 --- /dev/null +++ b/trove/common/cache.py @@ -0,0 +1,57 @@ +# Copyright 2021 Catalyst Cloud +# +# 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 of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""The code related to integration between oslo.cache module and trove.""" + +from oslo_cache import core +from oslo_config import cfg + + +def register_cache_configurations(conf): + """Register all configurations required for oslo.cache. + + The procedure registers all configurations required for oslo.cache. + It should be called before configuring of cache region + """ + core.configure(conf) + + ports_cache_group = cfg.OptGroup('instance_ports_cache') + ports_cache_opts = [ + cfg.IntOpt('expiration_time', default=86400, + help='TTL, in seconds, for any cached item in the ' + 'dogpile.cache region used for caching of the ' + 'instance ports.'), + cfg.BoolOpt("caching", default=True, + help='Toggle to enable/disable caching when getting trove ' + 'instance ports. Please note that the global toggle ' + 'for oslo.cache(enabled=True in [cache] group) ' + 'must be enabled to use this feature.') + ] + conf.register_group(ports_cache_group) + conf.register_opts(ports_cache_opts, group=ports_cache_group) + + return conf + + +# variable that stores an initialized cache region for trove +_REGION = None + + +def get_cache_region(): + global _REGION + if not _REGION: + _REGION = core.configure_cache_region( + conf=register_cache_configurations(cfg.CONF), + region=core.create_region()) + return _REGION diff --git a/trove/common/neutron.py b/trove/common/neutron.py index 9f82eb16..d60eda57 100644 --- a/trove/common/neutron.py +++ b/trove/common/neutron.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. import netaddr +from oslo_cache import core from oslo_log import log as logging from neutronclient.common import exceptions as neutron_exceptions +from trove.common import cache from trove.common import cfg from trove.common import clients from trove.common import exception @@ -26,6 +28,11 @@ MGMT_CIDRS = None NEUTRON_EXTENSION_CACHE = {} PROJECT_ID_EXT_ALIAS = 'project-id' +MEMOIZE_PORTS = core.get_memoization_decorator( + conf=CONF, + region=cache.get_cache_region(), + group="instance_ports_cache") + def check_extension_enabled(client, extension_alias): """Check if an extension is enabled in Neutron.""" @@ -93,6 +100,20 @@ def check_subnet_router(client, subnet_id): f"associated with router.") +@MEMOIZE_PORTS +def get_instance_ports(client, instance_id): + """Get ports attached to the trove instance. + + After the trove instance is created, the attached ports are not changed. + """ + LOG.info(f'Getting ports for instance {instance_id}') + return client.list_ports(device_id=instance_id)['ports'] + + +def get_port_fips(client, port_id): + return client.list_floatingips(port_id=port_id)['floatingips'] + + def create_port(client, name, description, network_id, security_groups, is_public=False, subnet_id=None, ip=None, is_mgmt=False, project_id=None): |