Src C0 Coverage Information - RCov

lib/config_handler.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/config_handler.rb 239 188
86.61%
85.11%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 #
2 #   Copyright [2011] [Red Hat, Inc.]
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #   http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #  limitations under the License.
15 #
16 require 'sinatra/base'
17 require 'fileutils'
18 require 'nokogiri'
19 require 'open-uri'
20 
21 require 'lib/model'
22 
23 class String
24   require 'base64'
25   def to_b64
26     Base64.encode64(self).delete("\n")
27   end
28   alias b64_encode to_b64
29 
30   def b64_decode
31     Base64.decode64(self)
32   end
33 end
34 
35 module ConfigServer
36 
37   # There is a silly conventions used in this class.  Everywhere that "data" is
38   # passed as argument to a method, it's assumed that it hasn't been validated
39   # yet.  If it's called "config", then it has been validated.  To be validated
40   # simply means that the "data" was validated against a RelaxNG schema.
41 
42   class InstanceConfigs
43     attr_reader :version
44     def initialize(settings)
45       @settings = settings
46       @logger = $LOGGER
47       @version = @settings.version || "0.2.0"
48     end
49 
50     def exists?(uuid)
51       Model::Instance.exists?(uuid)
52     end
53 
54     def deployment_exists?(uuid)
55       Model::Deployable.exists?(uuid)
56     end
57 
58     def get_configs(uuid, options={})
59       options[:as] ||= :text
60 
61       if exists?(uuid)
62         instance = Model::Instance.find(uuid)
63         configs = case options[:as]
64           when :xml
65             "<node-config ver='#{@version}'>\n" +
66             "  <services>\n" +
67             instance.services.map do |name, params|
68               "    <service name='#{name}'>\n" +
69               "      <parameters>\n" +
70               params.map do |pname, val|
71                 "        <parameter name='#{pname}'>\n" +
72                 "          <value><![CDATA[#{val}]]></value>\n" +
73                 "        </parameter>"
74               end.join("\n") +
75               "\n" +
76               "      </parameters>\n" +
77               "    </service>\n"
78             end.join +
79             "  </services>\n" +
80             "</node-config>\n"
81           when :text
82             services = instance.services
83             log "uuid: #{uuid}"
84             log "services: #{services.inspect}"
85             "|" +
86             services.map do |svc_name,params|
87               "service|#{svc_name}" +
88               if not params.empty?
89                 "|parameters|" +
90                 params.map do |p_name,value|
91                   "#{p_name}&" + [value].pack("m0").delete("\n")
92                 end.join("|")
93               else
94                 ""
95               end
96             end.join("|") +
97             "|"
98           when :textold
99             #old format for now
100             #|classes&service_name&service_name|parameters|param1&b64(va11)|param2&b64(val2)|
101             #new format soon
102             #|service&<s1>|parameters|<p1name>&b64(<v1>)|<p2name>&b64(<v2>)|service&<s2>|parameters|<p1name>&b64(<v1>)|<p2name>&b64(<v2>)|
103             "|classes&" +
104 	    instance.services.keys.join("&") +
105             "|parameters|" +
106             instance.services.values.map do |params|
107               params.map do |pname, val|
108                 "#{pname}&" + [val].pack("m0").delete("\n")
109               end.join("|")
110             end.join("|") +
111             "|"
112           else
113             ""
114         end
115         return configs, instance.required_parameters_remaining?
116       end
117     end
118 
119     def get_provides(uuid, options={})
120       options[:as] ||= :text
121       if exists?(uuid)
122         instance = Model::Instance.find(uuid)
123         parameters = instance.provided_parameters(:only_empty => true)
124         process_provides(parameters, options)
125       end
126     end
127 
128     def get_ip(uuid, options={})
129       Model::Instance.find(uuid).ip if exists?(uuid)
130     end
131 
132     def delete(uuid)
133       Model::Instance.find(uuid).delete! if exists?(uuid)
134     end
135 
136     def delete_deployment(uuid)
137       Model::Deployable.find(uuid).delete!
138     end
139 
140     def create(uuid, data)
141       xml = Model::Instance.validate(uuid, data)
142       instance = Model::Instance.new(uuid, xml)
143       register_with_oauth(instance)
144     end
145 
146     def update(uuid, data, ip, options={})
147       return nil if not exists?(uuid)
148       log "update #{uuid} with #{ip} and #{data}"
149 
150       instance = Model::Instance.find(uuid)
151       instance.ip = ip
152 
153       params = parse_audrey_data(data)
154       instance.provided_parameters_values = params
155 
156       provided_params = instance.provided_parameters(
157         :only_with_values => true,
158         :include_values => true)
159       log "provided_params: #{provided_params.inspect}"
160 
161       dep = instance.deployable
162       assembly_identifiers = [instance.uuid, instance.assembly_name]
163       dep.instances_with_assembly_dependencies(assembly_identifiers).each do |uuid|
164         log "found a dependency"
165         other = Model::Instance.find(uuid)
166         params = {}
167         required_params = other.required_parameters(:raw => true)
168         log "required_params: #{required_params.to_xml}"
169         match_string = assembly_identifiers.map {|id| "(@assembly='#{id}')"}.join("or")
170         required_params.xpath("//required-parameter[#{match_string}]").each do |p|
171           log "found a required param match: #{p.to_xml}"
172           if provided_params.key?(p['parameter'])
173             params[p['name']] = provided_params[p['parameter']]
174           end
175         end
176         other.required_parameters_values = params if not params.empty?
177       end
178 
179       options[:as] ||= :text
180       parameters = instance.provided_parameters(:only_empty => true)
181       process_provides(parameters, options)
182     end
183 
184     def get_file(uuid)
185       if exists?(uuid)
186         instance = Model::Instance.find(uuid)
187         instance.file
188       end
189     end
190 
191     def save_file(uuid, file)
192       if exists?(uuid)
193         instance = Model::Instance.find(uuid)
194         instance.file = file
195       end
196     end
197 
198     private
199     def logger
200       @logger
201     end
202 
203     def log(msg)
204       logger.info msg
205     end
206 
207     def parse_audrey_data(data)
208       return {} if data.nil? or "|&|" == data
209       log "parse_audrey_data(#{data})"
210       (data.split "|").map do |d|
211         next if d.nil? or d.empty?
212         k, v = d.split "&"
213         {k => v.b64_decode}
214       end.compact.inject(:merge)
215     end
216 
217     def process_provides(provides, opts={})
218       case opts[:as]
219         when :xml
220           "<parameters>\n" +
221           provides.map do |p|
222             "  <parameter name='#{p}'/>"
223           end.join("\n") +
224           "\n</parameters>"
225         when :text
226           "|" + provides.join('&') + "|"
227         else
228           ""
229       end
230     end
231 
232     def register_with_oauth(instance)
233       username = instance.uuid
234       if secret = instance.secret
235         Model::Consumer.create(username, secret)
236       end
237     end
238   end
239 end

Generated on Wed Dec 14 16:00:31 -0500 2011 with rcov 0.9.11