Created
November 9, 2012 01:24
-
-
Save jackhong/4043132 to your computer and use it in GitHub Desktop.
RC integration with Planetlab, some notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original ruby script | |
# | |
require "xmlrpc/client" | |
require "pp" | |
XMLRPC::Config.module_eval do | |
remove_const :ENABLE_NIL_PARSER | |
const_set :ENABLE_NIL_PARSER, true | |
end | |
ret = XMLRPC::Client.new2("http://demo.myslice.info:7080/") | |
# Authentication token | |
auth = {"AuthMethod" => "password", "Username" => "demo", "AuthString" => "demo"} | |
#ret = srv.Get(auth, "slice", [["slice_hrn", '=', "ple.upmc.myslicedemo"]], {}, ["slice_hrn", "resource.network", "resource.type", "resource.hrn", "resource.hostname"]) | |
begin | |
pp ret.call("Get", auth, "slice", [["slice_hrn",'=', "ple.upmc.myslicedemo"]],{}, ["slice_hrn"]) | |
rescue XMLRPC::FaultException => e | |
puts "Error:" | |
puts e.faultCode | |
puts e.faultString | |
end | |
# Put this into OMF RC proxy module | |
module OmfRc::ResourceProxy::Magic | |
include OmfRc::ResourceProxyDSL | |
register_proxy :magic, :create_by => :magic_factory | |
XMLRPC::Config.module_eval do | |
remove_const :ENABLE_NIL_PARSER | |
const_set :ENABLE_NIL_PARSER, true | |
end | |
property :auth_method | |
property :username | |
property :auth_string | |
property :api_url | |
request :slice do |resource, params| | |
auth = { | |
'AuthMethod' => resource.property.auth_method, | |
'Username' => resource.property.username, | |
'AuthString' => resource.property.auth_string | |
} | |
ret = XMLRPC::Client.new2(resource.api_url) | |
# say params passed in is { slice_hrn: "ple.upmc.myslicedemo" } | |
key = params.keys.first | |
begin | |
ret.call("Get", auth, "slice", [[key,'=', parasm[key]]],{}, [key]) | |
rescue XMLRPC::FaultException => e | |
raise RuntimeError, "Magic failed: #{e.faultCode} #{e.faultString}" | |
end | |
end | |
end | |
# A factory for creating new magic api request instance (a resource) | |
module OmfRc::ResourceProxy::MagicFactory | |
include OmfRc::ResourceProxyDSL | |
register_proxy :magic_factory | |
end | |
# In the factory script (like garage script) | |
factory = OmfRc::ResourceProxy.new(:magic_factory, user: 'u', password: 'pw', uid: 'my_magic') | |
factory.connect | |
# Then it is just matter of sending create/request/releaee message to factory | |
# as the ones showed in engine_test script | |
# So the key step here is to design a parent-child relationship based on your API functionality, think things around 'everything is a resource' | |
# You could map 'GET' to request and 'POST'/'PUT' to configure. | |
# Get it working first, then think about reduce duplicate code by refactoring common methods into a Utility module. | |
# All these should be covered by the that reosurece_proxy tutorial, and source code documenation, let me know if sth missing, so I can update them... . Actually, feel free to modify the docs yourself, that will be valuable contribution. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment