summaryrefslogtreecommitdiff
path: root/trove
diff options
context:
space:
mode:
authorLingxian Kong <anlin.kong@gmail.com>2021-08-02 13:15:56 +1200
committerLingxian Kong <anlin.kong@gmail.com>2021-08-02 14:25:54 +1200
commit5590ecdce036f7a2b11ff440dfd4eb637180abcc (patch)
tree5b778f8d093d92dd7e94c224a83e23deb294023b /trove
parent036948c516672e55c4a2c932c74bcdec80319108 (diff)
downloadtrove-5590ecdce036f7a2b11ff440dfd4eb637180abcc.tar.gz
Show user network ID for getting instance
Change-Id: Ia1e9112ae69e04f8c3e9e9d1b4a0189c743d7448
Diffstat (limited to 'trove')
-rw-r--r--trove/common/cache.py57
-rw-r--r--trove/common/neutron.py21
-rw-r--r--trove/instance/models.py17
3 files changed, 89 insertions, 6 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):
diff --git a/trove/instance/models.py b/trove/instance/models.py
index e00bff0c..b2659d08 100644
--- a/trove/instance/models.py
+++ b/trove/instance/models.py
@@ -146,7 +146,7 @@ def load_simple_instance_addresses(context, db_info):
addresses = []
user_ports = []
client = clients.create_neutron_client(context, db_info.region_id)
- ports = client.list_ports(device_id=db_info.compute_instance_id)['ports']
+ ports = neutron.get_instance_ports(client, db_info.compute_instance_id)
for port in ports:
if port['network_id'] not in CONF.management_networks:
LOG.debug('Found user port %s for instance %s', port['id'],
@@ -157,12 +157,17 @@ def load_simple_instance_addresses(context, db_info):
# TODO(lxkong): IPv6 is not supported
if netutils.is_valid_ipv4(ip.get('ip_address')):
addresses.append(
- {'address': ip['ip_address'], 'type': 'private'})
-
- fips = client.list_floatingips(port_id=port['id'])
- if len(fips['floatingips']) == 0:
+ {
+ 'address': ip['ip_address'],
+ 'type': 'private',
+ 'network': port['network_id']
+ }
+ )
+
+ fips = neutron.get_port_fips(client, port['id'])
+ if len(fips) == 0:
continue
- fip = fips['floatingips'][0]
+ fip = fips[0]
addresses.append(
{'address': fip['floating_ip_address'], 'type': 'public'})