Skip to content

Commit bcc9bac

Browse files
committed
Optimize model.destroy_units()
Currently it's calling the API for each unit to be destroyed. This changes it so that we're only calling the API only once.
1 parent 92562c2 commit bcc9bac

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

juju/model.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,32 +2157,30 @@ async def destroy_unit(self, unit_id, destroy_storage=False, dry_run=False, forc
21572157
"""Destroy units by name.
21582158
21592159
"""
2160-
connection = self.connection()
2161-
app_facade = client.ApplicationFacade.from_connection(connection)
2162-
2163-
# Get the corresponding unit tag
2164-
unit_tag = tag.unit(unit_id)
2165-
if unit_tag is None:
2166-
log.error("Error converting %s to a valid unit tag", unit_id)
2167-
return JujuUnitError("Error converting %s to a valid unit tag", unit_id)
2168-
2169-
log.debug(
2170-
'Destroying unit %s', unit_id)
2171-
2172-
return await app_facade.DestroyUnit(units=[{
2173-
'unit-tag': unit_tag,
2174-
'destroy-storage': destroy_storage,
2175-
'force': force,
2176-
'max-wait': max_wait,
2177-
'dry-run': dry_run,
2178-
}])
2160+
await self.destroy_units(unit_id, destroy_storage, dry_run, force, max_wait)
21792161

21802162
async def destroy_units(self, *unit_names, destroy_storage=False, dry_run=False, force=False, max_wait=None):
21812163
"""Destroy several units at once.
21822164
21832165
"""
2184-
for u in unit_names:
2185-
await self.destroy_unit(u, destroy_storage, dry_run, force, max_wait)
2166+
connection = self.connection()
2167+
app_facade = client.ApplicationFacade.from_connection(connection)
2168+
2169+
units_to_destroy = []
2170+
for unit_id in unit_names:
2171+
unit_tag = tag.unit(unit_id)
2172+
if unit_tag is None:
2173+
log.error("Error converting %s to a valid unit tag", unit_id)
2174+
raise JujuUnitError("Error converting %s to a valid unit tag", unit_id)
2175+
units_to_destroy.append({
2176+
'unit-tag': unit_tag,
2177+
'destroy-storage': destroy_storage,
2178+
'force': force,
2179+
'max-wait': max_wait,
2180+
'dry-run': dry_run,
2181+
})
2182+
log.debug('Destroying units %s', unit_names)
2183+
return await app_facade.DestroyUnit(units=units_to_destroy)
21862184

21872185
def download_backup(self, archive_id, target_filename=None):
21882186
"""Download a backup archive file.

0 commit comments

Comments
 (0)