Skip to content

Implement full support for network configuration - #196

Open
davispuh wants to merge 1 commit into
fog:masterfrom
davispuh:network
Open

Implement full support for network configuration#196
davispuh wants to merge 1 commit into
fog:masterfrom
davispuh:network

Conversation

@davispuh

Copy link
Copy Markdown
Contributor

This PR implements full support for network configuration.
I basically implemented pretty much all of libvirt Network functionality.

Example usage:

require "fog/libvirt"

compute = Fog::Compute.new(provider: "Libvirt", libvirt_uri: "qemu:///system")

# Create new network
network = compute.networks.new(name: "example",
                               forward: { mode: :nat },
                               dns: {
                                 hosts: [
                                   { ip: "192.168.25.25", hostnames: ["example.org"] },
                                 ],
                                 txts: [
                                   { name: "example", value: "some text" },
                                 ],
                               },
                               ips: [
                                 { address: "192.168.40.1", netmask: "255.255.255.0",
                                   dhcp: { ranges: [{ start: "192.168.40.100", end: "192.168.40.200" }] } },
                                 { address: "2001:db8::1", prefix: 64, family: :ipv6,
                                   dhcp: { ranges: [{ start: "2001:db8::100", end: "2001:db8::200" }] } },
                               ])
network.save

network.start
network.enable_autostart

# Modify existing
existing = compute.networks.all(:name => "example").first
existing.ips[0].dhcp.ranges << ("192.168.40.210".."192.168.40.230")
existing.dns.txts[0].value = "new val"
existing.dns.hosts = []

# these changes will be applied without destroy/recreate because libvirt supports that in some cases
# in other cases this will delete network and recreate with new changes (will have same name and uuid)
existing.save

existing.stop

# convert persistent network to transient
existing.persistent = false
existing.save

puts existing.to_xml

existing.destroy

I also added comprehensive tests that exercise all functionality. By specifying LIBVIRT_URI=qemu:///session env var you can run them against actual libvirt instance (qemu:///system also works)

Copilot AI review requested due to automatic review settings July 27, 2026 20:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds end-to-end network configuration support to the fog-libvirt compute provider by introducing a richer Network model that can parse/build libvirt network XML and persist changes via full re-defines and (when possible) libvirt “section update” fragment updates.

Changes:

  • Expanded Fog::Libvirt::Compute::Network to support most libvirt network XML features (parse + generate + incremental updates).
  • Added new compute requests to define/create/update networks, update autostart, and apply section updates.
  • Replaced the old Shindo network model test with a comprehensive Minitest suite covering XML round-tripping, lifecycle, and fragment updates.

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/libvirt/models/compute/network_tests.rb Removes legacy Shindo network model coverage (superseded by Minitest suite).
minitests/network/network_test.rb Adds extensive Minitest coverage for network XML, lifecycle, and partial updates.
lib/fog/libvirt/requests/compute/update_network.rb Implements full network replacement logic (destroy/undefine + create/define).
lib/fog/libvirt/requests/compute/update_network_section.rb Adds libvirt network section update request wrapper (modify/add/delete).
lib/fog/libvirt/requests/compute/update_network_autostart.rb Adds request to toggle network autostart flag.
lib/fog/libvirt/requests/compute/list_networks.rb Enhances listing to include state flags and preloaded parsed XML attributes.
lib/fog/libvirt/requests/compute/define_network.rb Adds request to define a persistent network from XML.
lib/fog/libvirt/requests/compute/create_network.rb Adds request to create a transient network from XML.
lib/fog/libvirt/models/compute/util/util.rb Adds XML/key normalization helpers and generic casting/compare utilities.
lib/fog/libvirt/models/compute/network/vlan.rb Adds VLAN model + XML parse/build support.
lib/fog/libvirt/models/compute/network/vlan_tag.rb Adds VLAN tag model + normalization.
lib/fog/libvirt/models/compute/network/virtualport.rb Adds virtualport model + XML parse/build support.
lib/fog/libvirt/models/compute/network/route.rb Adds route model + XML parse/build support.
lib/fog/libvirt/models/compute/network/portgroup.rb Adds portgroup model with fragment-update support.
lib/fog/libvirt/models/compute/network/pci_address.rb Adds PCI address model for forward/address sections.
lib/fog/libvirt/models/compute/network/nat.rb Adds NAT model including range normalization for addresses/ports.
lib/fog/libvirt/models/compute/network/ip.rb Adds IP model with nested DHCP + fragment-only detection.
lib/fog/libvirt/models/compute/network/forward.rb Adds forward model with NAT/interfaces/addresses support and fragment updates.
lib/fog/libvirt/models/compute/network/forward_interface.rb Adds forward interface model + fragment update behavior.
lib/fog/libvirt/models/compute/network/domain.rb Adds domain model + XML parse/build support.
lib/fog/libvirt/models/compute/network/dnsmasq.rb Adds dnsmasq namespace/options model.
lib/fog/libvirt/models/compute/network/dns.rb Adds DNS model + fragment-update orchestration for hosts/txt/srv.
lib/fog/libvirt/models/compute/network/dns_txt.rb Adds DNS TXT record model + fragment update support.
lib/fog/libvirt/models/compute/network/dns_srv.rb Adds DNS SRV record model + fragment update support.
lib/fog/libvirt/models/compute/network/dns_host.rb Adds DNS host record model + fragment update support.
lib/fog/libvirt/models/compute/network/dns_forwarder.rb Adds DNS forwarder model + normalization.
lib/fog/libvirt/models/compute/network/dhcp.rb Adds DHCP model (ranges/hosts/bootp) + fragment updates.
lib/fog/libvirt/models/compute/network/dhcp_range.rb Adds DHCP range model + XML parse/build.
lib/fog/libvirt/models/compute/network/dhcp_lease.rb Adds DHCP lease model + XML parse/build.
lib/fog/libvirt/models/compute/network/dhcp_host.rb Adds DHCP host model + XML parse/build.
lib/fog/libvirt/models/compute/network/bridge.rb Adds bridge model + XML parse/build.
lib/fog/libvirt/models/compute/network/bandwidth.rb Adds bandwidth model + XML parse/build.
lib/fog/libvirt/models/compute/network.rb Major network model expansion: attributes, XML round-trip, save/reload, fragment updates.
lib/fog/libvirt/models/compute/attribute_model.rb Introduces base model for attribute-only nested objects + equality semantics.
lib/fog/libvirt/compute.rb Registers new compute requests for network define/create/update operations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/fog/libvirt/models/compute/network/nat.rb
Comment thread lib/fog/libvirt/models/compute/network/route.rb

@ekohl ekohl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a very large PR. I'd appreciate splitting it in 2 parts: first rewrite the existing network tests from shindo to minitest. Then the big addition of networking. That way we can first work out the test setup part and it's easier to verify the existing tests are rewritten.

Comment thread lib/fog/libvirt/models/compute/util/util.rb
Comment thread minitests/network/network_test.rb Outdated
Comment thread minitests/network/network_test.rb Outdated
@davispuh

Copy link
Copy Markdown
Contributor Author

This is a very large PR. I'd appreciate splitting it in 2 parts: first rewrite the existing network tests from shindo to minitest. Then the big addition of networking. That way we can first work out the test setup part and it's easier to verify the existing tests are rewritten.

For network tests there's basically nothing, look at tests/libvirt/models/compute/network_tests.rb that's only file I deleted. It doesn't really test much and I didn't touch dhcp_leases but yeah can easily do that with another PR.

@davispuh davispuh mentioned this pull request Jul 27, 2026
@davispuh
davispuh force-pushed the network branch 3 times, most recently from c84047a to 0acc1dc Compare July 29, 2026 19:49
@davispuh

Copy link
Copy Markdown
Contributor Author

I just pushed updates:

  1. Rebased so now it has changes we talked about
  2. Fixed RuboCop issues (syntax/style and split methods into several smaller ones)
  3. Implemented support for older libvirt versions (like in CI)
  4. Fixed issues with Ruby 2.7 in CI (maybe drop it?). Had to add hash_except in Util because I can't live without #except() which is Ruby 3.0+
  5. Implemented clone / dup for models to do deep copy which is very convenient.

Now I think this is complete. All tests pass :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants