Skip to content

Commit

Permalink
Merge accaef5 into 37d99ac
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekBleschke committed Aug 6, 2019
2 parents 37d99ac + accaef5 commit 90afa45
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/ralph/virtual/management/commands/openstack_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,34 @@ def _cleanup(self):
for project_id in (set(self.ralph_projects.keys()) - set(
self.openstack_projects.keys())
):
self._delete_object(CloudProject.objects.get(
project_id=project_id))
cloud_project = CloudProject.objects.get(project_id=project_id)
children_count = cloud_project.children.count()
if children_count == 0:
self._delete_object(cloud_project)
logger.debug(
'Deleted Cloud Project (name: {}, id: {})'.format(
cloud_project.name,
cloud_project.id,
),
extra={
'cloud_project_name': cloud_project.name,
'cloud_project_id': cloud_project.id
}
)
self.summary['del_projects'] += 1
else:
logger.warning(
'Cloud project name: {} id: {} cant\'t be deleted '
'because it has {} children'.format(
cloud_project.name,
cloud_project.id,
children_count
),
extra={
'cloud_project_name': cloud_project.name,
'cloud_project_id': cloud_project.id
}
)

for del_flavor in (
set(self.ralph_flavors) - set(self.openstack_flavors)):
Expand Down Expand Up @@ -760,6 +786,7 @@ def _print_summary(self):
logger.info(msg)

def handle(self, *args, **options):
logger.info('Openstack sync started...')
match_ironic = options.get('match_ironic_physical_hosts')

if not nova_client_exists:
Expand All @@ -781,7 +808,6 @@ def handle(self, *args, **options):
self.ralph_serial_number_param = options[
'asset_serial_number_parameter'
]
self.stdout.write("syncing...")

self._get_cloud_provider()
self._process_openstack_instances()
Expand Down
16 changes: 16 additions & 0 deletions src/ralph/virtual/tests/test_openstack_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ def test_check_complete(self):
host_id='host_id1',
)

def test_cleanup_doesnt_remove_cloud_projects_with_children(self):
project = CloudProjectFactory(project_id='im_not_here')
host = CloudHostFactory(
host_id='host_id123',
parent=project,
cloudflavor=self.cloud_flavor[1]
)
self.cmd._get_ralph_data()

self.cmd._cleanup()

try:
CloudProject.objects.get(project_id='im_not_here')
except ObjectDoesNotExist:
self.fail('Project "im_not_here" was deleted.')

def test_delete_cloud_instance_cleanup_ip(self):
ips_count = IPAddress.objects.count()
self.cmd._cleanup_servers({}, self.cloud_project.project_id)
Expand Down

0 comments on commit 90afa45

Please sign in to comment.