vcloud-core 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.0.10 (2014-03-17)
2
+
3
+ Features:
4
+
5
+ - separates out the query runner tool that interfaces with fog from the CLI tool
6
+
7
+ Deprecated:
8
+
9
+ - Vcloud::Query.get_all_results should no longer be used - use Vcloud::QueryRunner.run instead
10
+
1
11
  ## 0.0.9 (2014-03-10)
2
12
 
3
13
  Features:
data/bin/vcloud-query CHANGED
@@ -41,10 +41,6 @@ class App
41
41
  options[:output_format] = v.downcase
42
42
  end
43
43
 
44
- on("--mock", "Fog Mock mode")
45
- on("--verbose", "Verbose output")
46
- on("--debug", "Debugging output")
47
-
48
44
  arg :type, :optional
49
45
 
50
46
  description '
data/lib/vcloud/core.rb CHANGED
@@ -16,6 +16,7 @@ require 'vcloud/core/vapp'
16
16
  require 'vcloud/core/vapp_template'
17
17
  require 'vcloud/core/org_vdc_network'
18
18
  require 'vcloud/core/query'
19
+ require 'vcloud/core/query_runner'
19
20
 
20
21
  module Vcloud
21
22
  module Core
@@ -12,8 +12,8 @@ module Vcloud
12
12
  end
13
13
 
14
14
  def self.get_ids_by_name(name)
15
- q = Query.new('edgeGateway', :filter => "name==#{name}")
16
- unless res = q.get_all_results
15
+ q = QueryRunner.new
16
+ unless res = q.run('edgeGateway', :filter => "name==#{name}")
17
17
  raise "Error finding edgeGateway by name #{name}"
18
18
  end
19
19
  res.collect do |record|
@@ -3,101 +3,59 @@ require 'csv'
3
3
  module Vcloud
4
4
  class Query
5
5
 
6
- attr_reader :type
7
- attr_reader :options
8
-
9
- def initialize(type=nil, options={})
6
+ def initialize(type=nil, options={}, query_runner = Vcloud::QueryRunner.new)
10
7
  @type = type
11
8
  @options = options
12
9
  @options[:output_format] ||= 'tsv'
13
- Fog.mock! if ENV['FOG_MOCK'] || options[:mock]
14
- @fsi = Vcloud::Fog::ServiceInterface.new
15
- end
16
-
17
- def filter
18
- options[:filter]
19
- end
20
-
21
- def output_format
22
- options[:output_format]
23
- end
24
-
25
- def fields
26
- options[:fields]
10
+ @query_runner = query_runner
27
11
  end
28
12
 
29
13
  def run()
30
-
31
- puts "options:" if @options[:debug]
32
- pp @options if @options[:debug]
33
-
34
14
  if @type.nil?
35
- output_potential_query_types
15
+ output_available_query_types
36
16
  else
37
17
  output_query_results
38
18
  end
39
19
  end
40
20
 
21
+ # <b>DEPRECATED:</b> Please use <tt>Vcloud::QueryRunner.run</tt> instead.
41
22
  def get_all_results
42
- results = []
43
- (1..get_num_pages).each do |page|
44
- results += get_results_page(page) || []
45
- end
46
- results
47
- end
48
-
49
- private
50
-
51
- def get_num_pages
52
- body = @fsi.get_execute_query(type=@type, @options)
53
- last_page = body[:lastPage] || 1
54
- raise "Invalid lastPage (#{last_page}) in query results" unless last_page.is_a? Integer
55
- return last_page.to_i
56
- end
57
-
58
- def get_results_page(page)
59
- raise "Must supply a page number" if page.nil?
60
-
61
- begin
62
- body = @fsi.get_execute_query(type=@type, @options.merge({:page=>page}))
63
- pp body if @options[:debug]
64
- rescue ::Fog::Compute::VcloudDirector::BadRequest, ::Fog::Compute::VcloudDirector::Forbidden => e
65
- raise "Access denied: #{e.message}"
66
- end
67
-
68
- records = body.keys.detect {|key| key.to_s =~ /Record|Reference$/}
69
- body[records] = [body[records]] if body[records].is_a?(Hash)
70
- return nil if body[records].nil? || body[records].empty?
71
- body[records]
72
-
23
+ warn "[DEPRECATION] `Vcloud::Query::get_all_results` is deprecated. Please use `Vcloud::QueryRunner.run` instead."
24
+ @query_runner.run(@type, @options)
73
25
  end
74
26
 
27
+ private
75
28
  def output_query_results
76
- results = get_all_results
29
+ results = @query_runner.run(@type, @options)
77
30
  output_header(results)
78
31
  output_results(results)
79
32
  end
80
33
 
81
- def output_potential_query_types
34
+ def output_available_query_types
35
+ available_query_types = @query_runner.available_query_types
36
+ available_queries = collate_formats_for_types(available_query_types)
37
+ print_query_types(available_queries)
38
+ end
82
39
 
83
- query_list = @fsi.get_execute_query
84
- queries = {}
85
- type_width = 0
86
- query_list[:Link].select do |link|
87
- link[:rel] == 'down'
88
- end.map do |link|
89
- href = Nokogiri::XML.fragment(link[:href])
90
- query = CGI.parse(URI.parse(href.text).query)
91
- [query['type'].first, query['format'].first]
92
- end.each do |type, format|
93
- queries[type] ||= []
40
+ def collate_formats_for_types(available_queries)
41
+ queries = Hash.new { |h, k| h[k]=[] }
42
+ available_queries.each do |type, format|
94
43
  queries[type] << format
95
- type_width = [type_width, type.size].max
96
44
  end
45
+ queries
46
+ end
47
+
48
+ def print_query_types(queries)
49
+ type_width = longest_query_type(queries)
50
+
97
51
  queries.keys.sort.each do |type|
98
52
  puts "%-#{type_width}s %s" % [type, queries[type].sort.join(',')]
99
53
  end
54
+ end
100
55
 
56
+ def longest_query_type(queries)
57
+ return 0 if queries.keys.empty?
58
+ queries.keys.max_by{|key| key.length}.length
101
59
  end
102
60
 
103
61
  def output_header(results)
@@ -134,9 +92,7 @@ module Vcloud
134
92
  else
135
93
  raise "Unsupported output format #{@options[:output_format]}"
136
94
  end
137
-
138
95
  end
139
96
 
140
97
  end
141
98
  end
142
-
@@ -0,0 +1,52 @@
1
+ module Vcloud
2
+
3
+ class QueryRunner
4
+ def initialize
5
+ @fsi = Vcloud::Fog::ServiceInterface.new
6
+ end
7
+
8
+ def run(type=nil, options={})
9
+ get_all_results(type, options)
10
+ end
11
+
12
+ def available_query_types
13
+ query_list = @fsi.get_execute_query()
14
+ query_list[:Link].select do |link|
15
+ link[:rel] == 'down'
16
+ end.map do |link|
17
+ href = Nokogiri::XML.fragment(link[:href])
18
+ query = CGI.parse(URI.parse(href.text).query)
19
+ [query['type'].first, query['format'].first]
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def get_all_results(type, options)
26
+ results = []
27
+ (1..get_num_pages(type, options)).each do |page|
28
+ results += get_results_page(page, type, options) || []
29
+ end
30
+ results
31
+ end
32
+
33
+ def get_num_pages(type, options)
34
+ body = @fsi.get_execute_query(type, options)
35
+ last_page = body[:lastPage] || 1
36
+ raise "Invalid lastPage (#{last_page}) in query results" unless last_page.is_a? Integer
37
+ last_page.to_i
38
+ end
39
+
40
+ def get_results_page(page, type, options)
41
+ body = @fsi.get_execute_query(type, options.merge({:page=>page}))
42
+
43
+ record_key = key_of_first_record_or_reference(body)
44
+ body[record_key] = [body[record_key]] if body[record_key].is_a?(Hash)
45
+ body[record_key]
46
+ end
47
+
48
+ def key_of_first_record_or_reference(body)
49
+ body.keys.detect { |key| key.to_s =~ /Record|Reference$/ }
50
+ end
51
+ end
52
+ end
@@ -13,8 +13,8 @@ module Vcloud
13
13
  end
14
14
 
15
15
  def self.get_by_name(name)
16
- q = Query.new('vApp', :filter => "name==#{name}")
17
- unless res = q.get_all_results
16
+ q = QueryRunner.new
17
+ unless res = q.run('vApp', :filter => "name==#{name}")
18
18
  raise "Error finding vApp by name #{name}"
19
19
  end
20
20
  case res.size
@@ -25,11 +25,8 @@ module Vcloud
25
25
 
26
26
  def self.get_ids_by_name_and_catalog name, catalog_name
27
27
  raise "provide Catalog and vAppTemplate name" unless name && catalog_name
28
- q = Query.new(
29
- 'vAppTemplate',
30
- :filter => "name==#{name};catalogName==#{catalog_name}"
31
- )
32
- unless query_results = q.get_all_results
28
+ q = QueryRunner.new
29
+ unless query_results = q.run('vAppTemplate', :filter => "name==#{name};catalogName==#{catalog_name}")
33
30
  raise "Error retreiving #{q.type} query '#{q.filter}'"
34
31
  end
35
32
  query_results.collect do |record|
@@ -12,8 +12,8 @@ module Vcloud
12
12
  end
13
13
 
14
14
  def self.get_by_name(name)
15
- q = Query.new('orgVdc', :filter => "name==#{name}")
16
- unless res = q.get_all_results
15
+ q = QueryRunner.new
16
+ unless res = q.run('orgVdc', :filter => "name==#{name}")
17
17
  raise "Error finding vDC by name #{name}"
18
18
  end
19
19
  raise "vDc #{name} not found" unless res.size == 1
@@ -1,5 +1,5 @@
1
1
  module Vcloud
2
2
  module Core
3
- VERSION = '0.0.9'
3
+ VERSION = '0.0.10'
4
4
  end
5
5
  end
@@ -139,12 +139,12 @@ module Vcloud
139
139
  end
140
140
 
141
141
  def get_storage_profile_href_by_name(storage_profile_name, vapp_name)
142
- q = Query.new('vApp', :filter => "name==#{vapp_name}")
143
- vdc_results = q.get_all_results
142
+ q = QueryRunner.new
143
+ vdc_results = q.run('vApp', :filter => "name==#{vapp_name}")
144
144
  vdc_name = vdc_results.first[:vdcName]
145
145
 
146
- q = Query.new('orgVdcStorageProfile', :filter => "name==#{storage_profile_name};vdcName==#{vdc_name}")
147
- sp_results = q.get_all_results
146
+ q = QueryRunner.new
147
+ sp_results = q.run('orgVdcStorageProfile', :filter => "name==#{storage_profile_name};vdcName==#{vdc_name}")
148
148
 
149
149
  if sp_results.empty? or !sp_results.first.has_key?(:href)
150
150
  raise "storage profile not found"
@@ -48,8 +48,9 @@ module Vcloud
48
48
  q_results = [
49
49
  { :name => 'edgegw-test-1', :href => "/#{@edgegw_id}" }
50
50
  ]
51
- mock_query = double(:query, :get_all_results => q_results)
52
- Vcloud::Query.should_receive(:new).with('edgeGateway', :filter => "name==edgegw-test-1").and_return(mock_query)
51
+ mock_query = double(:query_runner)
52
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
53
+ mock_query.should_receive(:run).with('edgeGateway', :filter => "name==edgegw-test-1").and_return(q_results)
53
54
  @obj = EdgeGateway.get_by_name('edgegw-test-1')
54
55
  expect(@obj.class).to be(Vcloud::Core::EdgeGateway)
55
56
  end
@@ -58,16 +59,18 @@ module Vcloud
58
59
  q_results = [
59
60
  { :name => 'edgegw-test-1', :href => "/#{@edgegw_id}" }
60
61
  ]
61
- mock_query = double(:query, :get_all_results => q_results)
62
- Vcloud::Query.should_receive(:new).with('edgeGateway', :filter => "name==edgegw-test-1").and_return(mock_query)
62
+ mock_query = double(:query_runner)
63
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
64
+ mock_query.should_receive(:run).with('edgeGateway', :filter => "name==edgegw-test-1").and_return(q_results)
63
65
  @obj = EdgeGateway.get_by_name('edgegw-test-1')
64
66
  expect(@obj.id) == @edgegw_id
65
67
  end
66
68
 
67
69
  it "should raise an error if no edgegw with that name exists" do
68
70
  q_results = [ ]
69
- mock_query = double(:query, :get_all_results => q_results)
70
- Vcloud::Query.should_receive(:new).with('edgeGateway', :filter => "name==edgegw-test-1").and_return(mock_query)
71
+ mock_query = double(:query_runner)
72
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
73
+ mock_query.should_receive(:run).with('edgeGateway', :filter => "name==edgegw-test-1").and_return(q_results)
71
74
  expect{ EdgeGateway.get_by_name('edgegw-test-1') }.to raise_exception(RuntimeError, "edgeGateway edgegw-test-1 not found")
72
75
  end
73
76
 
@@ -110,7 +110,7 @@ module Vcloud
110
110
  q_results = [
111
111
  { :name => @net_name, :href => "/#{@net_id}" }
112
112
  ]
113
- @mock_net_query = double(:query, :get_all_results => q_results)
113
+ @mock_net_query = double(:query_runner, :run => q_results)
114
114
  @config = {
115
115
  :name => @net_name,
116
116
  :vdc_name => @vdc_name,
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vcloud::QueryRunner do
4
+ before(:each) do
5
+ @mock_fog_interface = StubFogInterface.new
6
+ Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
7
+ @query_runner = Vcloud::QueryRunner.new()
8
+ end
9
+
10
+ context '#available_query_types' do
11
+ it 'should return empty array if no data' do
12
+ @mock_fog_interface.stub(:get_execute_query).and_return({:Link => {}})
13
+
14
+ result = @query_runner.available_query_types
15
+
16
+ result.size.should == 0
17
+ end
18
+
19
+ it 'should parse the query types returned' do
20
+ @mock_fog_interface.stub(:get_execute_query).and_return(
21
+ {:Link => [
22
+ {:rel => 'down',
23
+ :href => 'query?type=alice&#38;format=references'},
24
+ ]})
25
+
26
+ result = @query_runner.available_query_types
27
+
28
+ result.size.should == 1
29
+ result[0][0].should == 'alice'
30
+ result[0][1].should == 'references'
31
+ end
32
+
33
+ it 'should return the set of data' do
34
+ @mock_fog_interface.stub(:get_execute_query).and_return(
35
+ {:Link => [
36
+ {:rel => 'down',
37
+ :href => 'query?type=alice&#38;format=references'},
38
+ {:rel => 'down',
39
+ :href => 'query?type=bob&#38;format=references'},
40
+ ]})
41
+
42
+ result = @query_runner.available_query_types
43
+
44
+ result.size.should == 2
45
+ end
46
+ end
47
+
48
+ context '#run' do
49
+ it 'should return no results when fog returns no results' do
50
+ @mock_fog_interface.stub(:get_execute_query).and_return({})
51
+
52
+ result = @query_runner.run()
53
+
54
+ result.should == []
55
+ end
56
+
57
+ it 'return no results when fog results do not include a "Record" or a "Reference"' do
58
+ @mock_fog_interface.stub(:get_execute_query).and_return(
59
+ {
60
+ :WibbleBlob => {:field1 => 'Stuff 1'}
61
+ }
62
+ )
63
+
64
+ result = @query_runner.run()
65
+
66
+ result.size.should == 0
67
+ end
68
+
69
+ it 'should return a single result when fog returns a single record' do
70
+ fields = {:field1 => 'Stuff 1'}
71
+ @mock_fog_interface.stub(:get_execute_query).and_return(
72
+ {
73
+ :WibbleRecord => [fields]
74
+ }
75
+ )
76
+
77
+ result = @query_runner.run()
78
+
79
+ result.size.should == 1
80
+ result[0].should == fields
81
+ end
82
+
83
+ it 'should return a single result when fog returns a single reference' do
84
+ fields = {:field1 => 'Stuff 1'}
85
+ @mock_fog_interface.stub(:get_execute_query).and_return(
86
+ {
87
+ :WibbleReference => [fields]
88
+ }
89
+ )
90
+
91
+ result = @query_runner.run()
92
+
93
+ result.size.should == 1
94
+ result[0].should == fields
95
+ end
96
+
97
+ it 'should wrap single result from fog in list' do
98
+ fields = {:field1 => 'Stuff 1'}
99
+ @mock_fog_interface.stub(:get_execute_query).and_return(
100
+ {
101
+ :WibbleRecord => fields
102
+ }
103
+ )
104
+
105
+ result = @query_runner.run()
106
+
107
+ result.size.should == 1
108
+ result[0].should == fields
109
+ end
110
+
111
+ it 'should return all results in a record returned by fog' do
112
+ fields = {:field1 => 'Stuff 1'}
113
+ more_fields = {:field1 => 'More Stuff 1'}
114
+ @mock_fog_interface.stub(:get_execute_query).and_return(
115
+ {
116
+ :WibbleRecord => [fields, more_fields]
117
+ }
118
+ )
119
+
120
+ result = @query_runner.run()
121
+
122
+ result.size.should == 2
123
+ result[0].should == fields
124
+ result[1].should == more_fields
125
+ end
126
+
127
+ it 'should return the first item if more than one records provided' do
128
+ fields1 = {:field1 => 'Stuff 1'}
129
+ fields2 = {:field1 => 'Stuff 2'}
130
+ @mock_fog_interface.stub(:get_execute_query).and_return(
131
+ {
132
+ :WibbleRecord => [fields1],
133
+ :WobbleRecord => [fields2]
134
+ }
135
+ )
136
+
137
+ result = @query_runner.run()
138
+
139
+ result.size.should == 1
140
+ result[0].should == fields1
141
+ end
142
+
143
+ it 'should raise error if lastPage is not an integer' do
144
+ @mock_fog_interface.stub(:get_execute_query).and_return(
145
+ {
146
+ :lastPage => :qwerty,
147
+ :WibbleRecord => []
148
+ }
149
+ )
150
+
151
+ expect { @query_runner.run() }.to raise_error('Invalid lastPage (qwerty) in query results')
152
+ end
153
+
154
+ it 'should get each page and collect the results' do
155
+ fields = {:field1 => 'Stuff 1'}
156
+ @mock_fog_interface.stub(:get_execute_query).and_return(
157
+ {
158
+ :lastPage => 2,
159
+ :WibbleRecord => [fields]
160
+ }
161
+
162
+ )
163
+ result = @query_runner.run()
164
+
165
+ result.size.should == 2
166
+ result[0].should == fields
167
+ result[1].should == fields
168
+ end
169
+ end
170
+ end
@@ -3,38 +3,19 @@ require 'spec_helper'
3
3
  describe Vcloud::Query do
4
4
  context "attributes" do
5
5
 
6
- context "our object should have methods" do
7
- before(:each) do
8
- @mock_fog_interface = StubFogInterface.new
9
- Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
10
- @query = Vcloud::Query.new()
11
- end
12
- it { @query.should respond_to(:run) }
13
- end
6
+ context "#run called with no type set on construction" do
14
7
 
15
- context "#run with no type set" do
16
-
17
- before(:each) do
18
- @mock_fog_interface = StubFogInterface.new
19
- Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
20
- @query = @query = Vcloud::Query.new()
21
- end
8
+ it "should get and reformat query types" do
9
+ query_runner = double(Vcloud::QueryRunner)
10
+ allow(query_runner).to receive(:available_query_types) {
11
+ [
12
+ ['alice', 'references'],
13
+ ['alice', 'records'],
14
+ ['bob', 'records']
15
+ ]
16
+ }
22
17
 
23
- it "should call output_potential_query_types when run not provided with a type" do
24
- @query.should_receive(:output_potential_query_types)
25
- @query.run()
26
- end
27
-
28
- it "should output viable types when run not provided with a type" do
29
- @mock_fog_interface.stub(:get_execute_query).and_return(
30
- { :Link => [
31
- {:rel=>"down",
32
- :href=>"query?type=alice&#38;format=references"},
33
- {:rel=>"down",
34
- :href=>"query?type=alice&#38;format=records"},
35
- {:rel=>"down",
36
- :href=>"query?type=bob&#38;format=records"},
37
- ]})
18
+ @query = Vcloud::Query.new(nil, {}, query_runner)
38
19
 
39
20
  @query.should_receive(:puts).with("alice records,references")
40
21
  @query.should_receive(:puts).with("bob records")
@@ -45,23 +26,26 @@ describe Vcloud::Query do
45
26
  end
46
27
 
47
28
  context "gracefully handle zero results" do
29
+
48
30
  before(:each) do
49
- @mock_fog_interface = StubFogInterface.new
50
- Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
51
- @query = Vcloud::Query.new('bob')
52
- @mock_fog_interface.stub(:get_execute_query).and_return({})
31
+ @query_runner = double(Vcloud::QueryRunner)
32
+ allow(@query_runner).to receive(:run) { {} }
53
33
  end
54
34
 
55
35
  it "should not output when given tsv output_format" do
56
- @query = Vcloud::Query.new('bob', :output_format => 'tsv')
57
- @query.should_not_receive(:puts)
58
- @query.run()
36
+ query = Vcloud::Query.new('bob', {:output_format => 'tsv'}, @query_runner)
37
+
38
+ query.should_not_receive(:puts)
39
+
40
+ query.run()
59
41
  end
60
42
 
61
43
  it "should not output when given csv output_format" do
62
- @query = Vcloud::Query.new('bob', :output_format => 'csv')
63
- @query.should_not_receive(:puts)
64
- @query.run()
44
+ query = Vcloud::Query.new('bob', {:output_format => 'csv'}, @query_runner)
45
+
46
+ query.should_not_receive(:puts)
47
+
48
+ query.run()
65
49
  end
66
50
 
67
51
  end
@@ -69,43 +53,36 @@ describe Vcloud::Query do
69
53
  context "get results with a single response page" do
70
54
 
71
55
  before(:each) do
72
- @mock_fog_interface = StubFogInterface.new
73
- Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
74
- @query = Vcloud::Query.new('bob')
75
- @mock_fog_interface.stub(:get_execute_query).and_return( {
76
- :WibbleRecord=>
77
- [{:field1=>"Stuff 1",
78
- :field2=>"Stuff 2",
79
- :field3=>"Stuff 3",
80
- },
81
- {:field1=>"More Stuff 1",
82
- :field2=>"More Stuff 2",
83
- :field3=>"More Stuff 3",
84
- },
85
- ]
86
- } )
56
+ @query_runner = double(Vcloud::QueryRunner)
57
+ allow(@query_runner).to receive(:run) {
58
+ [
59
+ {:field1 => "Stuff 1", :field2 => "Stuff 2"},
60
+ {:field1 => "More Stuff 1", :field2 => "More Stuff 2"}
61
+ ]
62
+ }
87
63
  end
88
64
 
89
65
  it "should output a query in tsv when run with a type" do
90
- @query = Vcloud::Query.new('bob', :output_format => 'tsv')
91
- @query.should_receive(:puts).with("field1\tfield2\tfield3")
92
- @query.should_receive(:puts).with("Stuff 1\tStuff 2\tStuff 3")
93
- @query.should_receive(:puts).with("More Stuff 1\tMore Stuff 2\tMore Stuff 3")
66
+ @query = Vcloud::Query.new('bob', {:output_format => 'tsv'}, @query_runner)
67
+
68
+ @query.should_receive(:puts).with("field1\tfield2")
69
+ @query.should_receive(:puts).with("Stuff 1\tStuff 2")
70
+ @query.should_receive(:puts).with("More Stuff 1\tMore Stuff 2")
71
+
94
72
  @query.run()
95
73
  end
96
74
 
97
75
  it "should output a query in csv when run with a type" do
98
- @query = Vcloud::Query.new('bob', :output_format => 'csv')
99
- @query.should_receive(:puts).with("field1,field2,field3\n")
100
- @query.should_receive(:puts).with("Stuff 1,Stuff 2,Stuff 3\nMore Stuff 1,More Stuff 2,More Stuff 3\n")
76
+ @query = Vcloud::Query.new('bob', {:output_format => 'csv'}, @query_runner)
77
+
78
+ @query.should_receive(:puts).with("field1,field2\n")
79
+ @query.should_receive(:puts).with("Stuff 1,Stuff 2\nMore Stuff 1,More Stuff 2\n")
80
+
101
81
  @query.run()
102
82
  end
103
83
 
104
- # it "should output a query in yaml when run with a type"
105
-
106
84
  end
107
85
 
108
86
  end
109
87
 
110
88
  end
111
-
@@ -52,16 +52,18 @@ module Vcloud
52
52
  q_results = [
53
53
  { :name => 'vapp-test-1', :href => @vapp_id }
54
54
  ]
55
- mock_query = double(:query, :get_all_results => q_results)
56
- Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==vapp-test-1").and_return(mock_query)
55
+ mock_query = double(:query)
56
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
57
+ mock_query.should_receive(:run).with('vApp', :filter => "name==vapp-test-1").and_return(q_results)
57
58
  obj = Vapp.get_by_name('vapp-test-1')
58
59
  expect(obj.class).to be(Vcloud::Core::Vapp)
59
60
  end
60
61
 
61
62
  it "should raise an error if no vApp with that name exists" do
62
63
  q_results = [ ]
63
- mock_query = double(:query, :get_all_results => q_results)
64
- Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==vapp-test-1").and_return(mock_query)
64
+ mock_query = double(:query_runner)
65
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
66
+ mock_query.should_receive(:run).with('vApp', :filter => "name==vapp-test-1").and_return(q_results)
65
67
  expect{ Vapp.get_by_name('vapp-test-1') }.to raise_exception(RuntimeError)
66
68
  end
67
69
 
@@ -70,8 +72,9 @@ module Vcloud
70
72
  { :name => 'vapp-test-1', :href => @vapp_id },
71
73
  { :name => 'vapp-test-1', :href => '/bogus' },
72
74
  ]
73
- mock_query = double(:query, :get_all_results => q_results)
74
- Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==vapp-test-1").and_return(mock_query)
75
+ mock_query = double(:query)
76
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
77
+ mock_query.should_receive(:run).with('vApp', :filter => "name==vapp-test-1").and_return(q_results)
75
78
  expect{ Vapp.get_by_name('vapp-test-1') }.to raise_exception(RuntimeError)
76
79
  end
77
80
 
@@ -50,10 +50,11 @@ module Vcloud
50
50
 
51
51
  it 'should raise a RuntimeError if there is no template' do
52
52
  q_results = [ ]
53
- mock_query = double(:query, :get_all_results => q_results)
54
- Vcloud::Query.should_receive(:new).
53
+ mock_query = double(:query_runner)
54
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
55
+ mock_query.should_receive(:run).
55
56
  with('vAppTemplate', :filter => "name==test_template;catalogName==test_catalog").
56
- and_return(mock_query)
57
+ and_return(q_results)
57
58
  expect { VappTemplate.get('test_catalog', 'test_template') }.
58
59
  to raise_error('Could not find template vApp')
59
60
  end
@@ -65,10 +66,11 @@ module Vcloud
65
66
  { :name => 'test_template',
66
67
  :href => "/vappTemplate-12345678-90ab-cdef-0123-4567890ab002" },
67
68
  ]
68
- mock_query = double(:query, :get_all_results => q_results)
69
- Vcloud::Query.should_receive(:new).
69
+ mock_query = double(:query_runner)
70
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
71
+ mock_query.should_receive(:run).
70
72
  with('vAppTemplate', :filter => "name==test_template;catalogName==test_catalog").
71
- and_return(mock_query)
73
+ and_return(q_results)
72
74
  expect { VappTemplate.get('test_catalog', 'test_template') }.
73
75
  to raise_error('Template test_template is not unique in catalog test_catalog')
74
76
  end
@@ -78,10 +80,11 @@ module Vcloud
78
80
  { :name => 'test_template',
79
81
  :href => "/vappTemplate-12345678-90ab-cdef-0123-4567890abcde" }
80
82
  ]
81
- mock_query = double(:query, :get_all_results => q_results)
82
- Vcloud::Query.should_receive(:new).
83
+ mock_query = double(:query)
84
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
85
+ mock_query.should_receive(:run).
83
86
  with('vAppTemplate', :filter => "name==test_template;catalogName==test_catalog").
84
- and_return(mock_query)
87
+ and_return(q_results)
85
88
  test_template = VappTemplate.get('test_catalog', 'test_template')
86
89
  test_template.id.should == 'vappTemplate-12345678-90ab-cdef-0123-4567890abcde'
87
90
  end
@@ -46,16 +46,18 @@ module Vcloud
46
46
  q_results = [
47
47
  { :name => 'vdc-test-1', :href => @vdc_id }
48
48
  ]
49
- mock_query = double(:query, :get_all_results => q_results)
50
- Vcloud::Query.should_receive(:new).with('orgVdc', :filter => "name==vdc-test-1").and_return(mock_query)
49
+ mock_query = double(:query_runner)
50
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
51
+ mock_query.should_receive(:run).with('orgVdc', :filter => "name==vdc-test-1").and_return(q_results)
51
52
  obj = Vdc.get_by_name('vdc-test-1')
52
53
  expect(obj.class).to be(Vcloud::Core::Vdc)
53
54
  end
54
55
 
55
56
  it "should raise an error if no vDC with that name exists" do
56
57
  q_results = [ ]
57
- mock_query = double(:query, :get_all_results => q_results)
58
- Vcloud::Query.should_receive(:new).with('orgVdc', :filter => "name==vdc-test-1").and_return(mock_query)
58
+ mock_query = double(:query_runner)
59
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_query)
60
+ mock_query.should_receive(:run).with('orgVdc', :filter => "name==vdc-test-1").and_return(q_results)
59
61
  expect{ Vdc.get_by_name('vdc-test-1') }.to raise_exception(RuntimeError, "vDc vdc-test-1 not found")
60
62
  end
61
63
 
@@ -293,15 +293,19 @@ module Vcloud
293
293
  vdc_results = [
294
294
  { :vdcName => 'vdc-test-1' }
295
295
  ]
296
- mock_vdc_query = double(:query, :get_all_results => vdc_results)
296
+ mock_vdc_query = double(:query_runner)
297
297
 
298
298
  storage_profile_results = [
299
299
  { :href => 'test-href' }
300
300
  ]
301
- mock_sp_query = double(:query, :get_all_results => storage_profile_results)
301
+ mock_sp_query = double(:query_runner)
302
302
 
303
- Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==#{@vapp_name}").and_return(mock_vdc_query)
304
- Vcloud::Query.should_receive(:new).with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").and_return(mock_sp_query)
303
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_vdc_query)
304
+ mock_vdc_query.should_receive(:run).with('vApp', :filter => "name==#{@vapp_name}").and_return(vdc_results)
305
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_sp_query)
306
+ mock_sp_query.should_receive(:run).
307
+ with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").
308
+ and_return(storage_profile_results)
305
309
 
306
310
  generated_storage_profile = { name: 'storage_profile_name', href: 'test-href' }
307
311
  @fog_interface.should_receive(:put_vm).with(@vm_id, @vm_name, { :StorageProfile => generated_storage_profile} ).and_return(true)
@@ -313,13 +317,17 @@ module Vcloud
313
317
  vdc_results = [
314
318
  { :vdcName => 'vdc-test-1' }
315
319
  ]
316
- mock_vdc_query = double(:query, :get_all_results => vdc_results)
320
+ mock_vdc_query = double(:query_runner)
317
321
 
318
322
  storage_profile_results = []
319
- mock_sp_query = double(:query, :get_all_results => storage_profile_results)
323
+ mock_sp_query = double(:query_runner)
320
324
 
321
- Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==#{@vapp_name}").and_return(mock_vdc_query)
322
- Vcloud::Query.should_receive(:new).with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").and_return(mock_sp_query)
325
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_vdc_query)
326
+ mock_vdc_query.should_receive(:run).with('vApp', :filter => "name==#{@vapp_name}").and_return(vdc_results)
327
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_sp_query)
328
+ mock_sp_query.should_receive(:run).
329
+ with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").
330
+ and_return(storage_profile_results)
323
331
 
324
332
  expect{ @vm.update_storage_profile(storage_profile) }.to raise_error("storage profile not found" )
325
333
  end
@@ -329,13 +337,17 @@ module Vcloud
329
337
  vdc_results = [
330
338
  { :vdcName => 'vdc-test-1' }
331
339
  ]
332
- mock_vdc_query = double(:query, :get_all_results => vdc_results)
340
+ mock_vdc_query = double(:query_runner)
333
341
 
334
342
  storage_profile_results = [ { :id => 'test-href' }]
335
- mock_sp_query = double(:query, :get_all_results => storage_profile_results)
336
-
337
- Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==#{@vapp_name}").and_return(mock_vdc_query)
338
- Vcloud::Query.should_receive(:new).with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").and_return(mock_sp_query)
343
+ mock_sp_query = double(:query_runner)
344
+
345
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_vdc_query)
346
+ mock_vdc_query.should_receive(:run).with('vApp', :filter => "name==#{@vapp_name}").and_return(vdc_results)
347
+ Vcloud::QueryRunner.should_receive(:new).and_return(mock_sp_query)
348
+ mock_sp_query.should_receive(:run).
349
+ with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").
350
+ and_return(storage_profile_results)
339
351
 
340
352
  expect{ @vm.update_storage_profile(storage_profile) }.to raise_error("storage profile not found" )
341
353
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vcloud-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-10 00:00:00.000000000 Z
12
+ date: 2014-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fog
@@ -134,6 +134,7 @@ files:
134
134
  - lib/vcloud/core/metadata_helper.rb
135
135
  - lib/vcloud/core/org_vdc_network.rb
136
136
  - lib/vcloud/core/query.rb
137
+ - lib/vcloud/core/query_runner.rb
137
138
  - lib/vcloud/core/vapp.rb
138
139
  - lib/vcloud/core/vapp_template.rb
139
140
  - lib/vcloud/core/vdc.rb
@@ -159,6 +160,7 @@ files:
159
160
  - spec/vcloud/core/edge_gateway_spec.rb
160
161
  - spec/vcloud/core/metadata_helper_spec.rb
161
162
  - spec/vcloud/core/org_vdc_network_spec.rb
163
+ - spec/vcloud/core/query_runner_spec.rb
162
164
  - spec/vcloud/core/query_spec.rb
163
165
  - spec/vcloud/core/vapp_spec.rb
164
166
  - spec/vcloud/core/vapp_template_spec.rb
@@ -188,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
190
  version: '0'
189
191
  segments:
190
192
  - 0
191
- hash: -3857077396882591731
193
+ hash: 4238244327631333059
192
194
  requirements: []
193
195
  rubyforge_project:
194
196
  rubygems_version: 1.8.23
@@ -211,6 +213,7 @@ test_files:
211
213
  - spec/vcloud/core/edge_gateway_spec.rb
212
214
  - spec/vcloud/core/metadata_helper_spec.rb
213
215
  - spec/vcloud/core/org_vdc_network_spec.rb
216
+ - spec/vcloud/core/query_runner_spec.rb
214
217
  - spec/vcloud/core/query_spec.rb
215
218
  - spec/vcloud/core/vapp_spec.rb
216
219
  - spec/vcloud/core/vapp_template_spec.rb