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

Issue with handling array of dictionary (NetworkManager.Settings) #142

Closed
logicog opened this issue Oct 13, 2023 · 3 comments
Closed

Issue with handling array of dictionary (NetworkManager.Settings) #142

logicog opened this issue Oct 13, 2023 · 3 comments

Comments

@logicog
Copy link

logicog commented Oct 13, 2023

The following code

#!/usr/bin/env ruby

require "dbus"

bus = DBus::SystemBus.instance

@nm_service = bus.service("org.freedesktop.NetworkManager")
@nm_settings = @nm_service.object("/org/freedesktop/NetworkManager/Settings")
@nm_settings_iface = DBus::ProxyObjectInterface.new(@nm_settings, "org.freedesktop.NetworkManager.Settings")
@nm_settings_iface.define_method("AddConnection", "in connection:a{sa{sv}}, out path:o");
new_c = { "connection" => {"id" => "LAN", "type"=>"802-3-ethernet"},
          "ipv4" => {"method"=>"manual", "address-data" => [{"address" => "192.168.2.1", "prefix" => 24}]  } }

p "Creating new LAN connection"
p new_c
clan_path = @nm_settings_iface.AddConnection(new_c)

gives the following error:

$ ./new_con.rb 
"Creating new LAN connection"
{"connection"=>{"id"=>"LAN", "type"=>"802-3-ethernet"}, "ipv4"=>{"method"=>"manual", "address-data"=>[{"address"=>"192.168.2.1", "prefix"=>24}]}}
/var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:95:in `block in send_sync_or_async': ipv4.address-data: can't set property of type 'aa{sv}' from value of type 'av'; caused by 3 sender=:1.10 -> dest=:1.8432 serial=495323 reply_serial=3 path=; interface=; member= error_name=org.freedesktop.NetworkManager.Settings.Connection.InvalidProperty (DBus::Error)
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:284:in `process'
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:228:in `send_sync'
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:94:in `send_sync_or_async'
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/proxy_object_interface.rb:70:in `block (2 levels) in define_method_from_descriptor'
        from ./new_con.rb:16:in `<main>'
/var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:95:in `block in send_sync_or_async': ipv4.address-data: can't set property of type 'aa{sv}' from value of type 'av' (DBus::Error)
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:284:in `process'
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:228:in `send_sync'
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/connection.rb:94:in `send_sync_or_async'
        from /var/lib/gems/3.1.0/gems/ruby-dbus-0.23.1/lib/dbus/proxy_object_interface.rb:70:in `block (2 levels) in define_method_from_descriptor'
        from ./new_con.rb:16:in `<main>'

The expected behaviour is that the address-data array: [{"address" => "192.168.2.1", "prefix" => 24}] is indeed sent via DBus as 'aa{sv}' and not as 'av'

@mvidner
Copy link
Owner

mvidner commented Oct 13, 2023

Thanks for the report!

The problem is that ruby-dbus has no way of knowing what type the NetworkManager API is actually expecting.
This is a known problem, please see #124 for a slightly more general description, also using the org.freedesktop.NetworkManager.Settings interface

You need to explicitly set the data type, like this:

new_c = {
  "connection" => {
    "id" => "LAN",
    "type" => "802-3-ethernet"
  },
  "ipv4" => {
    "method" => "manual",
    "address-data" => DBus::Data.make_typed("aa{sv}", [{ "address" => "192.168.2.1", "prefix" => 24 }])
  }
}

which then gives me another error:

ipv4.addresses: this property cannot be empty for 'method=manual'; ... error_name=org.freedesktop.NetworkManager.Settings.Connection.MissingProperty

but that is left for you to solve, consulting the NM API documentation

@logicog
Copy link
Author

logicog commented Oct 13, 2023

Thanks so much for the quick help! I will dig into this further!

@mvidner mvidner changed the title Issue with handling array of dictionary Issue with handling array of dictionary (NetworkManager.Settings) Nov 9, 2023
@mvidner
Copy link
Owner

mvidner commented Nov 9, 2023

@logicog feel free to reopen this if you have related questions or problems

@mvidner mvidner closed this as completed Nov 9, 2023
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

No branches or pull requests

2 participants