landb 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ require 'savon'
2
+ require 'landb/landb_client'
3
+ require 'landb/landb_responce'
@@ -0,0 +1,133 @@
1
+ class LandbClient
2
+
3
+ def self.instance
4
+ @@instance ||= LandbClient.new
5
+ end
6
+
7
+ def self.set_config(config)
8
+ @@config = config
9
+ end
10
+
11
+ def self.flush
12
+ @@instance = LandbClient.new
13
+ end
14
+
15
+ def initialize
16
+ # Get the configurations. arguments are: wsdl, username, password.
17
+ if !@@config
18
+ return
19
+ end
20
+
21
+ # Make HTTPI silent.
22
+ HTTPI.log = false
23
+
24
+ # Make savon silent.
25
+ Savon.configure do |savon_config|
26
+ savon_config.log = false
27
+ end
28
+
29
+ # Getting the SOAP client with WSDL specifications.
30
+ @client = Savon.client(@@config["wsdl"])
31
+ @client.http.auth.ssl.verify_mode = :none
32
+
33
+
34
+ # Create a hash that contains all the operations and arguments that SOAP server supports.
35
+ # e.g. The get_auth_token action is taking 3 arguments: Login, Password, Type.
36
+ @operations_to_arguments_hash = get_all_operations_and_arguments
37
+
38
+ # For each action the SOAP server supports, we create a dynamic method for our class.
39
+ @client.wsdl.soap_actions.each do |method_name|
40
+ # Ruby method "send" helps us to create the methods of the class.
41
+ self.class.send :define_method, method_name do |arg|
42
+
43
+ # Error handling for arguments.
44
+ arg = arg.to_a unless arg.instance_of? Array
45
+
46
+ if arg.count != @operations_to_arguments_hash[method_name].count
47
+ puts "Arguments are: #{@operations_to_arguments_hash[method_name].inspect}"
48
+ puts 'Arguments should be in an array. e.g. ["test", "test2"]'
49
+ return
50
+ end
51
+
52
+ # For each action we have, we must integrate it with Savon.
53
+ response = @client.request(:message, method_name) do |soap|
54
+ # We are dynamically creating the content of the SOAP request.
55
+ soap.header = {"Auth" => {"token" => @auth_token } } if @auth_token
56
+
57
+ soap.body = {}
58
+
59
+ i = 0
60
+ # The operations_to_arguments_hash hash has all the needed arguments for the request.
61
+ # We populate the soap arguments with the user arguments.
62
+ @operations_to_arguments_hash[method_name].each do |soap_argument|
63
+ soap.body.merge! soap_argument => arg[i]
64
+ i+=1
65
+ end
66
+
67
+ # Savon tutorials infoms that if you are using ruby<1.9 there might be problems with the order.
68
+ # This is a workaround to this problem.
69
+ soap.body.merge! :order! => @operations_to_arguments_hash[method_name]
70
+
71
+ end
72
+
73
+ # We are setting the auth token automatically when existes.
74
+ if response.to_hash[:get_auth_token_response]
75
+ @auth_token = response.to_hash[:get_auth_token_response][:token]
76
+ end
77
+
78
+ LandbResponse.new(response.to_hash["#{method_name}_response".to_sym])
79
+ end
80
+ end
81
+
82
+ # There is a bug in the current version of Savon.(?)(i am using 1.1.0) This is a workarround.
83
+ # Ref: https://github.com/rubiii/savon/pull/275
84
+ @client = Savon.client do |wsdl|
85
+ wsdl.endpoint = @client.wsdl.endpoint.to_s
86
+ wsdl.namespace = @client.wsdl.namespace
87
+ end
88
+ @client.http.auth.ssl.verify_mode = :none
89
+
90
+ # Initialize the token, so the client would be ready for usage
91
+ # only if name and password have been provided
92
+
93
+ if (!@@config['username'].nil? || !@@config['password'].nil?)
94
+ self.get_auth_token [@@config["username"], @@config["password"], "NICE"]
95
+ end
96
+
97
+ end
98
+
99
+ def print_auth_token
100
+ @auth_token
101
+ end
102
+
103
+ def help_all_operations
104
+ @operations_to_arguments_hash.keys
105
+ end
106
+
107
+ def help_arguments_for_operation(operation)
108
+ @operations_to_arguments_hash[operation.to_sym]
109
+ end
110
+
111
+ private
112
+
113
+ # This method is mapping all operations, that are declared at WSDL, and their arguments in order.
114
+ # e.g. Gets get_auth_token and find that the arguments of this operation is: [:login, :password, :type].
115
+ def get_all_operations_and_arguments
116
+ hash = {}
117
+
118
+ @client.wsdl.parser.document.xpath('wsdl:definitions/wsdl:binding/wsdl:operation').each do |action|
119
+ action.children.each do |child_node|
120
+ next unless child_node.element? && child_node.name == "input"
121
+
122
+ child_node.children.each do |att|
123
+ hash[action.attributes["name"].value.snakecase.to_sym] ||= []
124
+ next unless att.element?
125
+ next if att.attributes["parts"].nil?
126
+ hash[action.attributes["name"].value.snakecase.to_sym] = att.attributes["parts"].value.split(" ").collect {|p| p.snakecase.to_sym }
127
+ end
128
+ end
129
+ end
130
+
131
+ hash
132
+ end
133
+ end
@@ -0,0 +1,36 @@
1
+ class LandbResponse
2
+
3
+ def initialize(hash)
4
+ hash.each do |k,v|
5
+ next if k.to_s =~ /@|:/
6
+
7
+ if v.instance_of? Hash
8
+ self.instance_variable_set("@#{k}", LandbResponse.new(v)) ## create and initialize an instance variable for this hash.
9
+ else
10
+ self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair.
11
+ end
12
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
13
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
14
+ end
15
+ end
16
+
17
+ def get_values_for_paths paths_array
18
+ responces = []
19
+
20
+ paths_array.each do |path|
21
+ responces << get_value_for_path(path)
22
+ end
23
+
24
+ responces
25
+ end
26
+
27
+ def get_value_for_path path_array
28
+ responce_runner = self
29
+
30
+ path_array.each do |domain|
31
+ responce_runner = responce_runner.send domain.to_sym
32
+ end
33
+
34
+ responce_runner
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: landb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anastasios Andronidis, Daniel Lobato Garcia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This is a WSDL dynamic wrapper for lanDB to puppet use. It's writen from
15
+ Anastasis Andronidis as an OpenLab Summer Student project
16
+ email: anastasis90@yahoo.gr
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/landb/landb_responce.rb
22
+ - lib/landb/landb_client.rb
23
+ - lib/landb.rb
24
+ homepage: https://gitgw.cern.ch/gitweb/?p=gem-landb.git;a=summary;js=1
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: This is a WSDL dynamic wrapper for lanDB to puppet use.
48
+ test_files: []