Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend NetworkManager inventory #9

Merged
merged 1 commit into from
Sep 20, 2019

Conversation

mancabizjak
Copy link
Contributor

So far the provider's NetworkManager inventory supported only security groups.

With this commit we extend the NetworkManager inventory with cloud networks, cloud subnets and network ports. Cloud subnets and network ports are connected to security groups, while network
ports are in addition connected to VMs.

Copy link
Contributor

@miha-plesko miha-plesko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't believe the amazing quality level of your first PR on MIQ @mancabizjak , it totally feels like you're developing MIQ for months already and not just 5 days. Kudos 👍 Few small comments anyway

)
if network.subnets
cloud_subnets(network)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could capture the just-built network and pass it to the cloud_subnets function so that we don't lazy_find it over and over again:

network_obj = persister.cloud_networks.build(...)
cloud_subnets(network_obj, network.subnets) if network.subnets

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, missed that. Thanks, done 👍

:ems_ref => port.id.downcase,
:mac_address => port.mac_address,
:device => port.virtual_machine ? persister.vms.lazy_find(port.virtual_machine.id.downcase) : nil,
:security_groups => build_security_groups(port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need utility function here because we're just filling in a simple relation. I'd go with a simple IF statement at the top instead:

security_groups = []
if (security_group_id = port.network_security_group&.id&.downcase)
  security_groups = [persister.security_groups.lazy_find(security_group_id)]
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly how I used to have it initially, when I hooked security_groups to network ports only. But then I needed the same logic for subnets and I thought it made sense to extract it to a helper function (note that this is also mirrored by the assert_security_groups_binding which is used in both assert_specific_subnet and assert_specific_network_port).

I did modify the helper itself to make use of &. when accessing entity's security group though 👍

:name => port.name,
:ems_ref => port.id.downcase,
:mac_address => port.mac_address,
:device => port.virtual_machine ? persister.vms.lazy_find(port.virtual_machine.id.downcase) : nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:device => port.virtual_machine ? persister.vms.lazy_find(port.virtual_machine.id.downcase) : nil,
:device => persister.vms.lazy_find(port.virtual_machine&.id&.downcase),

"addressPrefix": "[variables('subnetPrefix')]",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 👍

expect(subnet).not_to be_nil
expect(ems_ref_suffix(subnet.ems_ref)).to match(%r{^/providers/microsoft.network/virtualnetworks/[^/]+/subnets/[^/]+$})

expect(subnet.cloud_network).not_to be_nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is actually redundant because we assert the exact value in the next line anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I removed a similar redundant case from assert_specific_network_port as well.

assert_security_groups_binding(network_port)
end

def assert_security_groups_binding(entity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this DRY approach you took here 👍

persister.cloud_subnets.build(
:name => subnet.name,
:ems_ref => subnet.id.downcase,
:cloud_network => persister.cloud_networks.lazy_find(network.id.downcase),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is called right after building the network you can save the lazy_find and pass in the persisted network object directly if you want.
Example:

persister_network = persister.cloud_networks.build()
cloud_subnets(persister_network, network.subnets) if network.subnets

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah @miha-plesko beat me to it #9 (comment)

security_groups].each do |name|
add_collection(network, name)

add_collection(cloud, :vms) do |builder|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this inside the loop above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, fixed.

@agrare
Copy link
Member

agrare commented Sep 20, 2019

I can't believe the amazing quality level of your first PR on MIQ @mancabizjak , it totally feels like you're developing MIQ for months already and not just 5 days. Kudos 👍

I completely agree. This is a great PR for anyone, for your first this is amazing great work.

So far the provider's NetworkManager inventory supported security
groups. With this commit we extend NetworkManager inventory with
cloud networks, cloud subnets and network ports. Cloud subnets
and network ports are connected to security groups, while network
ports are in addition connected to VMs.

Signed-off-by: Manca Bizjak <[email protected]>
@mancabizjak mancabizjak force-pushed the extend-network-inventory branch from bc827ac to 2bfe503 Compare September 20, 2019 12:35
@miq-bot
Copy link
Member

miq-bot commented Sep 20, 2019

Checked commit xlab-si@2bfe503 with ruby 2.4.6, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
8 files checked, 0 offenses detected
Everything looks fine. 🍰

Copy link
Contributor Author

@mancabizjak mancabizjak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your reviews and encouraging words @miha-plesko @agrare! Please see the changes I made according to your comments.

)
if network.subnets
cloud_subnets(network)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, missed that. Thanks, done 👍

:ems_ref => port.id.downcase,
:mac_address => port.mac_address,
:device => port.virtual_machine ? persister.vms.lazy_find(port.virtual_machine.id.downcase) : nil,
:security_groups => build_security_groups(port)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly how I used to have it initially, when I hooked security_groups to network ports only. But then I needed the same logic for subnets and I thought it made sense to extract it to a helper function (note that this is also mirrored by the assert_security_groups_binding which is used in both assert_specific_subnet and assert_specific_network_port).

I did modify the helper itself to make use of &. when accessing entity's security group though 👍

security_groups].each do |name|
add_collection(network, name)

add_collection(cloud, :vms) do |builder|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, fixed.

expect(subnet).not_to be_nil
expect(ems_ref_suffix(subnet.ems_ref)).to match(%r{^/providers/microsoft.network/virtualnetworks/[^/]+/subnets/[^/]+$})

expect(subnet.cloud_network).not_to be_nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I removed a similar redundant case from assert_specific_network_port as well.

Copy link
Member

@agrare agrare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM

@agrare agrare merged commit 2bfe503 into ManageIQ:master Sep 20, 2019
agrare added a commit that referenced this pull request Sep 20, 2019
@agrare agrare self-assigned this Sep 20, 2019
@agrare agrare added the enhancement New feature or request label Sep 20, 2019
@agrare agrare added this to the Sprint 121 Ending Sep 30, 2019 milestone Sep 20, 2019
@mancabizjak mancabizjak deleted the extend-network-inventory branch October 4, 2019 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants