Skip to content

Commit a9a240a

Browse files
committed
clean/fip: Add new clean command
* Remove refs between floating-ip-pool and floatinip (which do not exist anymore).
1 parent 6b7d2c4 commit a9a240a

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from contrail_api_cli.command import Option
5+
from contrail_api_cli.utils import printo
6+
from contrail_api_cli.exceptions import ResourceNotFound
7+
8+
from ..utils import CassandraCommand, CheckCommand, PathCommand
9+
from refs import CleanRefs
10+
11+
class CleanFipRefs(CassandraCommand, CheckCommand, PathCommand):
12+
"""Clean broken references related to floating IPs.
13+
14+
Broken refs related to FIP can be found with gremlin (floating-ip-pool <->
15+
floating-ip).
16+
17+
The command will take all existing floating-ip-pool and checks the existence
18+
of each floating-ip it points to and removes the reference if that latter
19+
does not exist.
20+
21+
You can run the command by putting the floating-ip-pool path to be more
22+
precise or you can run it without any resources path, in that case it will
23+
iterates over all floating-ip-pool.
24+
25+
You can also specify the floating-ip uuids refs list you want to clean,
26+
and it will remove those floating-ip references only for the floating-ip-pool
27+
where the floating-ip is linked to.
28+
29+
"""
30+
description = "Clean broken references related to floating IPs"
31+
32+
refs = Option(help="list of floating IP UUIDs", nargs="*", default=[])
33+
34+
@property
35+
def resource_type(self):
36+
return "floating-ip-pool"
37+
38+
def _clean_refs(self, pool, fip, **kwargs):
39+
try:
40+
fip.fetch()
41+
except ResourceNotFound:
42+
CleanRefs(ref_type="children",
43+
target_type="floating_ip",
44+
paths=[pool.uuid, fip.uuid],
45+
**kwargs)
46+
47+
def __call__(self, refs, **kwargs):
48+
super(CleanFipRefs, self).__call__(**kwargs)
49+
for pool in self.resources:
50+
try:
51+
pool.fetch()
52+
if refs:
53+
fip_list = [fip for fip in pool.children.floating_ip if
54+
fip.uuid in refs]
55+
else:
56+
fip_list = pool.children.floating_ip
57+
58+
for fip in fip_list:
59+
# TODO: clean also refs between project and fip by using
60+
# projects = Collection('project',
61+
# fetch=True,
62+
# back_refs_uuid=['2f20ee42-c69a-4521-800c-d0a151bdf93d'])
63+
self._clean_refs(pool, fip, **kwargs)
64+
65+
except ResourceNotFound as e:
66+
printo(e)

contrail_api_cli_extra/clean/refs.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from contrail_api_cli.command import Option, Arg
99
from contrail_api_cli.utils import printo
1010

11-
from ..utils import server_type, CheckCommand
11+
from ..utils import server_type, CheckCommand, CassandraCommand
1212

1313

14-
class CleanRefs(CheckCommand):
14+
class CleanRefs(CheckCommand, CassandraCommand):
1515
"""Clean references in Contrail DB.
1616
1717
Broken refs can be found with gremlin.
@@ -57,18 +57,13 @@ class CleanRefs(CheckCommand):
5757
target_type = Option(help="resource type of the target",
5858
required=True)
5959

60-
cassandra_servers = Option(help="cassandra server list' (default: %(default)s)",
61-
nargs='+',
62-
type=server_type,
63-
default=['localhost:9160'])
64-
6560
def _remove_refs(self, paths):
6661
if len(paths) == 0:
6762
return
6863
source, target = paths[:2]
6964
# when the parent doesn't exists anymore,
7065
# we don't need to keep the source
71-
if not self.check:
66+
if not self.check and not self.dry_run:
7267
if self.ref_type == "parent":
7368
self.uuid_cf.remove(target)
7469
self.uuid_cf.remove(source)
@@ -85,9 +80,9 @@ def _read_file(self, resources_file):
8580
paths = paths + l.split()
8681
return paths
8782

88-
def __call__(self, paths=None, resources_file=None, ref_type=None, target_type=None, cassandra_servers=None, **kwargs):
83+
def __call__(self, paths=None, resources_file=None, ref_type=None, target_type=None, **kwargs):
8984
super(CleanRefs, self).__call__(**kwargs)
90-
pool = ConnectionPool('config_db_uuid', server_list=cassandra_servers)
85+
pool = ConnectionPool('config_db_uuid', server_list=self.cassandra_servers)
9186
self.uuid_cf = ColumnFamily(pool, 'obj_uuid_table')
9287
self.ref_type = ref_type
9388
self.target_type = target_type

contrail_api_cli_extra/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ def __call__(self, zk_server=None, **kwargs):
220220
super(ZKCommand, self).__call__(**kwargs)
221221

222222

223+
class CassandraCommand(Command):
224+
"""Inherit from this class to add `--cassandra-servers` options.
225+
226+
Cassandra servers list value is stored in `self.cassandra_servers`.
227+
"""
228+
cassandra_servers = Option(help="cassandra server list' (default: %(default)s)",
229+
nargs='+',
230+
type=server_type,
231+
default=['localhost:9160'])
232+
233+
def __call__(self, cassandra_servers=None, **kwargs):
234+
self.cassandra_servers = cassandra_servers
235+
super(CassandraCommand, self).__call__(**kwargs)
236+
237+
223238
class CheckCommand(Command):
224239
"""Inherit from this class to add `--check` and `--dry-run` options.
225240

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
'clean-si-scheduling = contrail_api_cli_extra.clean.si:CleanSIScheduling',
9999
'clean-subnet = contrail_api_cli_extra.clean.subnet:CleanSubnet',
100100
'clean-refs = contrail_api_cli_extra.clean.refs:CleanRefs',
101+
'clean-fip-refs = contrail_api_cli_extra.clean.fip:CleanFipRefs',
101102
]
102103
},
103104
classifiers=[

0 commit comments

Comments
 (0)