Skip to content

Commit

Permalink
lookup the correct network_id for a given network
Browse files Browse the repository at this point in the history
Fixes: #1104
  • Loading branch information
evgeni committed Feb 3, 2021
1 parent a071f17 commit 9526041
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1104-network_id-lookup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- host - look up the correct network id for a network given as part of ``interfaces_attributes`` (https://github.com/theforeman/foreman-ansible-modules/issues/1104)
11 changes: 11 additions & 0 deletions plugins/modules/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,17 @@ def main():
if not module.desired_absent:
module.auto_lookup_entities()

if 'compute_resource' in module.foreman_params and 'interfaces_attributes' in module.foreman_params:
avn_params = {'id': module.foreman_params['compute_resource']['id']}
available_networks = module.resource_action('compute_resources', 'available_networks', params=avn_params,
ignore_check_mode=True, record_change=False)['results']

for interface in module.foreman_params['interfaces_attributes']:
if 'compute_attributes' in interface and 'network' in interface['compute_attributes']:
network_name = interface['compute_attributes']['network']
network_id = next((network['id'] for network in available_networks if network['name'] == network_name), network_name)
interface['compute_attributes']['network'] = network_id

# We use different APIs for creating a host with interfaces
# and updating it, so let's differentiate based on entity being present or not
if entity and 'interfaces_attributes' in module.foreman_params:
Expand Down

0 comments on commit 9526041

Please sign in to comment.