Mailinglist Archive: yast-commit (1901 mails)

< Previous Next >
[yast-commit] <rest-service> master : added DNS and routing (controllers, models, tests)
  • From: Michal Zugec <mzugec@xxxxxxx>
  • Date: Sun, 30 Aug 2009 12:51:11 +0200
  • Message-id: <E1MjUOi-0001C4-LK@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit 828a29d2a65e9ef4a218a91fded1c6623c741f91
Author: Michal Zugec <mzugec@xxxxxxx>
Date: Sun Aug 30 12:51:11 2009 +0200

added DNS and routing (controllers, models, tests)
---
plugins/network/app/controllers/dns_controller.rb | 45 ++++++++++++++++
.../network/app/controllers/routes_controller.rb | 49 +++++++++++++++++
plugins/network/app/models/dns.rb | 56 ++++++++++++++++++++
plugins/network/app/models/routes.rb | 51 ++++++++++++++++++
plugins/network/config/resources/dns.yml | 4 ++
.../test/functional/routes_controller_test.rb | 21 +++++++
plugins/network/test/unit/routes_test.rb | 25 +++++++++
7 files changed, 251 insertions(+), 0 deletions(-)

diff --git a/plugins/network/app/controllers/dns_controller.rb
b/plugins/network/app/controllers/dns_controller.rb
new file mode 100644
index 0000000..6e92ee0
--- /dev/null
+++ b/plugins/network/app/controllers/dns_controller.rb
@@ -0,0 +1,45 @@
+# = Hostname controller
+# Provides access to hostname settings for authenticated users.
+# Main goal is checking permissions.
+class DnsController < ApplicationController
+
+ before_filter :login_required
+
+ # Sets hostname settings. Requires write permissions for network YaPI.
+ def update
+ unless permission_check( "org.opensuse.yast.modules.yapi.network.write")
+ render ErrorResult.error(403, 1, "no permission") and return
+ end
+
+ root = params[:hostname]
+ if root == nil
+ render ErrorResult.error(404, 2, "format or internal error") and return
+ end
+
+ @dns = DNS.new(root)
+ @dns.save
+ render :show
+ end
+
+ # See update
+ def create
+ update
+ end
+
+ # Shows hostname settings. Requires read permission for network YaPI.
+ def show
+
+ unless permission_check( "org.opensuse.yast.modules.yapi.network.read")
+ render ErrorResult.error( 403, 1, "no permission" ) and return
+ end
+
+ @dns = DNS.find
+
+ respond_to do |format|
+ format.html { render :xml => @dns.to_xml( :root => "dns", :dasherize =>
false ) }
+ format.xml { render :xml => @dns.to_xml( :root => "dns", :dasherize =>
false ) }
+ format.json { render :json => @dns.to_json }
+ end
+ end
+
+end
diff --git a/plugins/network/app/controllers/routes_controller.rb
b/plugins/network/app/controllers/routes_controller.rb
new file mode 100644
index 0000000..2c91e6f
--- /dev/null
+++ b/plugins/network/app/controllers/routes_controller.rb
@@ -0,0 +1,49 @@
+# = Routing controller
+# Provides access to hostname settings for authenticated users.
+# Main goal is checking permissions.
+class RoutesController < ApplicationController
+
+ before_filter :login_required
+
+ # Sets hostname settings. Requires write permissions for network YaPI.
+ def update
+ unless permission_check( "org.opensuse.yast.modules.yapi.network.write")
+ render ErrorResult.error(403, 1, "no permission") and return
+ end
+
+ root = params[:routing]
+ if root == nil
+ render ErrorResult.error(404, 2, "format or internal error") and return
+ end
+
+ @routing = Routing.new(root)
+ @routing.save
+ render :show
+ end
+
+ # See update
+ def create
+ update
+ end
+
+ # Shows hostname settings. Requires read permission for network YaPI.
+ def show
+
+ unless permission_check( "org.opensuse.yast.modules.yapi.network.read")
+ render ErrorResult.error( 403, 1, "no permission" ) and return
+ end
+
+ @routes = Routes.find
+
+ respond_to do |format|
+ format.html { render :xml => @routes.to_xml( :root => "routes",
:dasherize => false ) }
+ format.xml { render :xml => @routes.to_xml( :root => "routes",
:dasherize => false ) }
+ format.json { render :json => @routes.to_json }
+ end
+ end
+
+ def index
+ show
+ end
+
+end
diff --git a/plugins/network/app/models/dns.rb
b/plugins/network/app/models/dns.rb
new file mode 100644
index 0000000..6a03736
--- /dev/null
+++ b/plugins/network/app/models/dns.rb
@@ -0,0 +1,56 @@
+# = Hostname model
+# Provides set and gets resources from YaPI network module.
+# Main goal is handle YaPI specific calls and data formats. Provides cleaned
+# and well defined data.
+class DNS
+
+ # the short hostname
+ attr_accessor :domains
+ # the domain name
+ attr_accessor :servers
+
+ private
+
+ public
+
+ def initialize(kwargs)
+ @domains = kwargs["dnsdomains"]
+ @servers = kwargs["dnsservers"]
+ end
+
+ # fills time instance with data from YaPI.
+ #
+ # +warn+: Doesn't take any parameters.
+ def DNS.find
+ response = YastService.Call("YaPI::NETWORK::Read") # hostname: true
+ ret = DNS.new(response["dns"])
+ return ret
+ end
+
+ # Saves data from model to system via YaPI. Saves only setted data,
+ # so it support partial safe (e.g. save only new timezone if rest of fields
is not set).
+ def save
+ settings = {
+ "domains" => @domains,
+ "servers" => @servers,
+ }
+ YastService.Call("YaPI::NETWORK::Write",{"hostname" => settings})
+ # TODO success or not?
+ end
+
+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+
+ xml.hostname do
+ xml.domains @domains
+ xml.servers @servers
+ end
+ end
+
+ def to_json( options = {} )
+ hash = Hash.from_xml(to_xml())
+ return hash.to_json
+ end
+
+end
diff --git a/plugins/network/app/models/routes.rb
b/plugins/network/app/models/routes.rb
new file mode 100644
index 0000000..388417d
--- /dev/null
+++ b/plugins/network/app/models/routes.rb
@@ -0,0 +1,51 @@
+# = Routing model
+# Provides set and gets resources from YaPI network module.
+# Main goal is handle YaPI specific calls and data formats. Provides cleaned
+# and well defined data.
+class Routes
+
+ # default route
+ attr_accessor :default
+
+ private
+
+ public
+
+ def initialize(kwargs)
+ @default = kwargs["default"]
+ end
+
+ # fills route instance with data from YaPI.
+ #
+ # +warn+: Doesn't take any parameters.
+ def Routes.find
+ response = YastService.Call("YaPI::NETWORK::Read") # hostname: true
+ ret = Routes.new(response["routes"])
+ return ret
+ end
+
+ # Saves data from model to system via YaPI. Saves only setted data,
+ # so it support partial safe (e.g. save only new timezone if rest of fields
is not set).
+ def save
+ settings = {
+ "default" => @default,
+ }
+ YastService.Call("YaPI::NETWORK::Write",{"routes" => settings})
+ # TODO success or not?
+ end
+
+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+
+ xml.hostname do
+ xml.default @default
+ end
+ end
+
+ def to_json( options = {} )
+ hash = Hash.from_xml(to_xml())
+ return hash.to_json
+ end
+
+end
diff --git a/plugins/network/config/resources/dns.yml
b/plugins/network/config/resources/dns.yml
new file mode 100644
index 0000000..1d2bf08
--- /dev/null
+++ b/plugins/network/config/resources/dns.yml
@@ -0,0 +1,4 @@
+interface: org.opensuse.yast.modules.yapi.network.dns
+policy: org.opensuse.yast.modules.yapi.dns
+controller: network/dns
+singular: true
diff --git a/plugins/network/test/functional/routes_controller_test.rb
b/plugins/network/test/functional/routes_controller_test.rb
new file mode 100644
index 0000000..c330871
--- /dev/null
+++ b/plugins/network/test/functional/routes_controller_test.rb
@@ -0,0 +1,21 @@
+require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
+require 'test/unit'
+require 'rubygems'
+require 'hostname'
+require "scr"
+require 'mocha'
+require File.expand_path( File.join("test","plugin_basic_tests"),
RailsParent.parent )
+
+class RoutesControllerTest < Test::Unit::TestCase
+
+ # this is test only for mocked data - not very useful
+ # we need to extend it to test both model and controller
+ # to extract routes data from all YaPI map
+ #
+ def test_show
+ Routes.expects(:find).returns({:routes=>{:default=>'10.20.30.40'}})
+ Routes.find
+ end
+
+end
+
diff --git a/plugins/network/test/unit/routes_test.rb
b/plugins/network/test/unit/routes_test.rb
new file mode 100644
index 0000000..4b815aa
--- /dev/null
+++ b/plugins/network/test/unit/routes_test.rb
@@ -0,0 +1,25 @@
+require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
+
+class RoutesTest < ActiveSupport::TestCase
+
+ RESPONSE_FULL = {
+ 'interfaces'=>{
+ 'eth0'=>{'bootproto'=>'dhcp'},
+ 'eth1'=>{'bootproto'=>'static',
'ipaddr'=>'192.168.3.27/24'}},
+ 'routes'=>{'default'=>'10.20.7.254'},
+ 'dns'=>{'dnsservers'=>'10.20.0.15 10.20.0.8',
'dnsdomains'=>'suse.cz suse.de'},
+ 'hostname'=>{'name'=>'linux', 'domain'=>'suse.cz'}
+ }
+
+ def setup
+ YastService.stubs(:Call).with("YaPI::NETWORK::Read").returns(RESPONSE_FULL)
+ end
+
+ def test_getter
+ hostname=Routes.find
+# assert_equal("linux", hostname.name)
+# assert_equal("suse.cz", hostname.domain)
+ end
+
+end
+
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages