deltacloud-client 0.0.9.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/documentation.rb CHANGED
@@ -1,98 +1,62 @@
1
- require 'lib/deltacloud'
1
+ #
2
+ # Copyright (C) 2010 Red Hat, Inc.
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one or more
5
+ # contributor license agreements. See the NOTICE file distributed with
6
+ # this work for additional information regarding copyright ownership. The
7
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance with the
9
+ # License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
+ # License for the specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ module DeltaCloud
20
+ class Documentation
21
+
22
+ attr_reader :api, :description, :params, :collection_operations
23
+ attr_reader :collection, :operation
24
+
25
+ def initialize(api, opts={})
26
+ @description, @api = opts[:description], api
27
+ @params = parse_parameters(opts[:params]) if opts[:params]
28
+ @collection_operations = opts[:operations] if opts[:operations]
29
+ @collection = opts[:collection]
30
+ @operation = opts[:operation]
31
+ self
32
+ end
2
33
 
3
- skip_methods = [ "id=", "uri=" ]
34
+ def operations
35
+ @collection_operations.collect { |o| api.documentation(@collection, o) }
36
+ end
4
37
 
5
- begin
6
- @dc=DeltaCloud.new('mockuser', 'mockpassword', 'http://localhost:3001/api')
7
- rescue
8
- puts "Please make sure that Deltacloud API is running with Mock driver"
9
- exit(1)
10
- end
38
+ class OperationParameter
39
+ attr_reader :name
40
+ attr_reader :type
41
+ attr_reader :required
42
+ attr_reader :description
11
43
 
12
- @dc.entry_points.keys.each do |ep|
13
- @dc.send(ep)
14
- end
44
+ def initialize(data)
45
+ @name, @type, @required, @description = data[:name], data[:type], data[:required], data[:description]
46
+ end
15
47
 
16
- class_list = DeltaCloud::classes.collect { |c| DeltaCloud::module_eval("::DeltaCloud::API::#{c}")}
48
+ def to_comment
49
+ " # @param [#{@type}, #{@name}] #{@description}"
50
+ end
17
51
 
18
- def read_method_description(c, method)
19
- if method =~ /es$/
20
- " # Read #{c.downcase} collection from Deltacloud API"
21
- else
22
- case method
23
- when "uri"
24
- " # Return URI to API for this object"
25
- when "action_urls"
26
- " # Return available actions API URL"
27
- when "client"
28
- " # Return instance of API client"
29
- else
30
- " # Get #{method} attribute value from #{c.downcase}"
31
52
  end
32
- end
33
- end
34
53
 
35
- def read_parameters(c, method)
36
- out = []
37
- if method =~ /es$/
38
- out << " # @param [String, #id] Filter by ID"
39
- end
40
- out.join("\n")
41
- end
54
+ private
42
55
 
43
- def read_return_value(c, method)
44
- if method =~ /es$/
45
- rt = "Array"
46
- else
47
- rt = "String"
48
- end
49
- " # @return [String] Value of #{method}"
50
- end
51
-
52
- out = []
53
-
54
- class_list.each do |c|
55
- class_name = "#{c}".gsub(/^DeltaCloud::/, '')
56
- out << "module DeltaCloud"
57
- out << " class API"
58
- @dc.entry_points.keys.each do |ep|
59
- out << "# Return #{ep.to_s.classify} object with given id\n"
60
- out << "# "
61
- out << "# #{@dc.documentation(ep.to_s).description.split("\n").join("\n# ")}"
62
- out << "# @return [#{ep.to_s.classify}]"
63
- out << "def #{ep.to_s.gsub(/s$/, '')}"
64
- out << "end"
65
- out << "# Return collection of #{ep.to_s.classify} objects"
66
- out << "# "
67
- out << "# #{@dc.documentation(ep.to_s).description.split("\n").join("\n# ")}"
68
- @dc.documentation(ep.to_s, 'index').params.each do |p|
69
- out << p.to_comment
56
+ def parse_parameters(params)
57
+ params.collect { |p| OperationParameter.new(p) }
70
58
  end
71
- out << "# @return [Array] [#{ep.to_s.classify}]"
72
- out << "def #{ep}(opts={})"
73
- out << "end"
74
- end
75
- out << " end"
76
- out << " class #{class_name}"
77
- c.instance_methods(false).each do |method|
78
- next if skip_methods.include?(method)
79
- params = read_parameters(class_name, method)
80
- retval = read_return_value(class_name, method)
81
- out << read_method_description(class_name, method)
82
- out << params if params
83
- out << retval if retval
84
- out << " def #{method}"
85
- out << " # This method was generated dynamically from API"
86
- out << " end\n"
59
+
87
60
  end
88
- out << " end"
89
- out << "end"
90
- end
91
61
 
92
- FileUtils.rm_r('doc') rescue nil
93
- FileUtils.mkdir_p('doc')
94
- File.open('doc/deltacloud.rb', 'w') do |f|
95
- f.puts(out.join("\n"))
96
62
  end
97
- system("yardoc -m markdown --readme README --title 'Deltacloud Client Library' 'lib/*.rb' 'doc/deltacloud.rb' --verbose")
98
- FileUtils.rm('doc/deltacloud.rb')
@@ -0,0 +1,64 @@
1
+ #
2
+ # Copyright (C) 2009 Red Hat, Inc.
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one or more
5
+ # contributor license agreements. See the NOTICE file distributed with
6
+ # this work for additional information regarding copyright ownership. The
7
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance with the
9
+ # License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
+ # License for the specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ module DeltaCloud
20
+
21
+ module HWP
22
+
23
+ class Property
24
+ attr_reader :name, :unit, :value, :kind
25
+
26
+ def initialize(xml, name)
27
+ @name, @kind, @value, @unit = xml['name'], xml['kind'].to_sym, xml['value'], xml['unit']
28
+ declare_ranges(xml)
29
+ self
30
+ end
31
+
32
+ def present?
33
+ ! @value.nil?
34
+ end
35
+
36
+ private
37
+
38
+ def declare_ranges(xml)
39
+ case xml['kind']
40
+ when 'range' then
41
+ self.class.instance_eval do
42
+ attr_reader :range
43
+ end
44
+ @range = { :from => xml.xpath('range').first['first'], :to => xml.xpath('range').first['last'] }
45
+ when 'enum' then
46
+ self.class.instance_eval do
47
+ attr_reader :options
48
+ end
49
+ @options = xml.xpath('enum/entry').collect { |e| e['value'] }
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ # FloatProperty is like Property but return value is Float instead of String.
56
+ class FloatProperty < Property
57
+ def initialize(xml, name)
58
+ super(xml, name)
59
+ @value = @value.to_f if @value
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,29 @@
1
+ module DeltaCloud
2
+ module InstanceState
3
+
4
+ class State
5
+ attr_reader :name
6
+ attr_reader :transitions
7
+
8
+ def initialize(name)
9
+ @name, @transitions = name, []
10
+ end
11
+ end
12
+
13
+ class Transition
14
+ attr_reader :to
15
+ attr_reader :action
16
+
17
+ def initialize(to, action)
18
+ @to = to
19
+ @action = action
20
+ end
21
+
22
+ def auto?
23
+ @action.nil?
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,53 @@
1
+ #
2
+ # Copyright (C) 2010 Red Hat, Inc.
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one or more
5
+ # contributor license agreements. See the NOTICE file distributed with
6
+ # this work for additional information regarding copyright ownership. The
7
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance with the
9
+ # License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
+ # License for the specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ class String
20
+
21
+ unless method_defined?(:classify)
22
+ # Create a class name from string
23
+ def classify
24
+ self.singularize.camelize
25
+ end
26
+ end
27
+
28
+ unless method_defined?(:camelize)
29
+ # Camelize converts strings to UpperCamelCase
30
+ def camelize
31
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
32
+ end
33
+ end
34
+
35
+ unless method_defined?(:singularize)
36
+ # Strip 's' character from end of string
37
+ def singularize
38
+ self.gsub(/s$/, '')
39
+ end
40
+ end
41
+
42
+ # Convert string to float if string value seems like Float
43
+ def convert
44
+ return self.to_f if self.strip =~ /^([\d\.]+$)/
45
+ self
46
+ end
47
+
48
+ # Simply converts whitespaces and - symbols to '_' which is safe for Ruby
49
+ def sanitize
50
+ self.strip.gsub(/(\W+)/, '_')
51
+ end
52
+
53
+ end
@@ -31,13 +31,15 @@ describe "hardware_profiles" do
31
31
  it_should_behave_like "all resources"
32
32
 
33
33
  it "should allow retrieval of all hardware profiles" do
34
- DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
35
- hardware_profiles = client.hardware_profiles
36
- hardware_profiles.should_not be_empty
37
- hardware_profiles.each do |hwp|
38
- hwp.uri.should_not be_nil
39
- hwp.uri.should be_a(String)
40
- prop_check(hwp.architecture, String) if hwp.architecture
34
+ [API_URL, API_URL_REDIRECT].each do |entry_point|
35
+ DeltaCloud.new( API_NAME, API_PASSWORD, entry_point ) do |client|
36
+ hardware_profiles = client.hardware_profiles
37
+ hardware_profiles.should_not be_empty
38
+ hardware_profiles.each do |hwp|
39
+ hwp.uri.should_not be_nil
40
+ hwp.uri.should be_a(String)
41
+ prop_check(hwp.architecture, String) if hwp.architecture
42
+ end
41
43
  end
42
44
  end
43
45
  end
@@ -62,7 +64,7 @@ describe "hardware_profiles" do
62
64
  it "should allow fetching different hardware_profiles" do
63
65
  client = DeltaCloud.new( API_NAME, API_PASSWORD, API_URL )
64
66
  hwp1 = client.hardware_profile( 'm1-small' )
65
- hwp2 = client.hardware_profile( 'm1-xlarge' )
67
+ hwp2 = client.hardware_profile( 'm1-large' )
66
68
  hwp1.storage.value.should_not eql(hwp2.storage.value)
67
69
  hwp1.memory.value.should_not eql(hwp2.memory.value)
68
70
  end
data/specs/images_spec.rb CHANGED
@@ -24,19 +24,21 @@ describe "images" do
24
24
  it_should_behave_like "all resources"
25
25
 
26
26
  it "should allow retrieval of all images" do
27
- DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
28
- images = client.images
29
- images.should_not be_empty
30
- images.size.should eql( 3 )
31
- images.each do |image|
32
- image.uri.should_not be_nil
33
- image.uri.should be_a(String)
34
- image.description.should_not be_nil
35
- image.description.should be_a(String)
36
- image.architecture.should_not be_nil
37
- image.architecture.should be_a(String)
38
- image.owner_id.should_not be_nil
39
- image.owner_id.should be_a(String)
27
+ [API_URL, API_URL_REDIRECT].each do |entry_point|
28
+ DeltaCloud.new( API_NAME, API_PASSWORD, entry_point ) do |client|
29
+ images = client.images
30
+ images.should_not be_empty
31
+ images.size.should eql( 3 )
32
+ images.each do |image|
33
+ image.uri.should_not be_nil
34
+ image.uri.should be_a(String)
35
+ image.description.should_not be_nil
36
+ image.description.should be_a(String)
37
+ image.architecture.should_not be_nil
38
+ image.architecture.should be_a(String)
39
+ image.owner_id.should_not be_nil
40
+ image.owner_id.should be_a(String)
41
+ end
40
42
  end
41
43
  end
42
44
  end
@@ -28,12 +28,14 @@ describe "initializing the client" do
28
28
  end
29
29
 
30
30
  it "should discover entry points upon connection" do
31
- DeltaCloud.new( "name", "password", API_URL ) do |client|
32
- client.entry_points[:hardware_profiles].should eql( "#{API_URL}/hardware_profiles" )
33
- client.entry_points[:images].should eql( "#{API_URL}/images" )
34
- client.entry_points[:instances].should eql( "#{API_URL}/instances" )
35
- client.entry_points[:storage_volumes].should eql( "#{API_URL}/storage_volumes" )
36
- client.entry_points[:storage_snapshots].should eql( "#{API_URL}/storage_snapshots" )
31
+ [API_URL, API_URL_REDIRECT].each do |entry_point|
32
+ DeltaCloud.new( "name", "password", entry_point ) do |client|
33
+ client.entry_points[:hardware_profiles].should eql( "#{API_URL}/hardware_profiles" )
34
+ client.entry_points[:images].should eql( "#{API_URL}/images" )
35
+ client.entry_points[:instances].should eql( "#{API_URL}/instances" )
36
+ client.entry_points[:storage_volumes].should eql( "#{API_URL}/storage_volumes" )
37
+ client.entry_points[:storage_snapshots].should eql( "#{API_URL}/storage_snapshots" )
38
+ end
37
39
  end
38
40
  end
39
41
 
@@ -33,23 +33,25 @@ describe "instance-states" do
33
33
  it_should_behave_like "all resources"
34
34
 
35
35
  it "should allow retrieval of instance-state information" do
36
- DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
37
- instance_states = client.instance_states
38
- instance_states.should_not be_nil
39
- instance_states.should_not be_empty
36
+ [API_URL, API_URL_REDIRECT].each do |entry_point|
37
+ DeltaCloud.new( API_NAME, API_PASSWORD, entry_point ) do |client|
38
+ instance_states = client.instance_states
39
+ instance_states.should_not be_nil
40
+ instance_states.should_not be_empty
40
41
 
41
- instance_states[0].name.should eql( 'start' )
42
- instance_states[0].transitions.size.should eql( 1 )
43
- instance_states[0].transitions[0].should_not be_auto
42
+ instance_states[0].name.should eql( 'start' )
43
+ instance_states[0].transitions.size.should eql( 1 )
44
+ instance_states[0].transitions[0].should_not be_auto
44
45
 
45
- instance_states[1].name.should eql( 'pending' )
46
- instance_states[1].transitions.size.should eql( 1 )
47
- instance_states[1].transitions[0].should be_auto
46
+ instance_states[1].name.should eql( 'pending' )
47
+ instance_states[1].transitions.size.should eql( 1 )
48
+ instance_states[1].transitions[0].should be_auto
48
49
 
49
- instance_states[2].name.should eql( 'running' )
50
- instance_states[2].transitions.size.should eql( 2 )
51
- includes_transition( instance_states[2].transitions, :reboot, :running ).should be_true
52
- includes_transition( instance_states[2].transitions, :stop, :stopped ).should be_true
50
+ instance_states[2].name.should eql( 'running' )
51
+ instance_states[2].transitions.size.should eql( 2 )
52
+ includes_transition( instance_states[2].transitions, :reboot, :running ).should be_true
53
+ includes_transition( instance_states[2].transitions, :stop, :stopped ).should be_true
54
+ end
53
55
  end
54
56
  end
55
57
 
@@ -24,26 +24,28 @@ describe "instances" do
24
24
  it_should_behave_like "all resources"
25
25
 
26
26
  it "should allow retrieval of all instances" do
27
- DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
28
- instances = client.instances
29
- instances.should_not be_empty
30
- instances.each do |instance|
31
- instance.uri.should_not be_nil
32
- instance.uri.should be_a( String )
33
- instance.owner_id.should_not be_nil
34
- instance.owner_id.should be_a( String )
35
- instance.image.should_not be_nil
36
- instance.image.should be_a( DeltaCloud::API::Image )
37
- instance.hardware_profile.should_not be_nil
38
- instance.hardware_profile.should be_a( DeltaCloud::API::HardwareProfile )
39
- instance.state.should_not be_nil
40
- instance.state.should be_a( String )
41
- instance.public_addresses.should_not be_nil
42
- instance.public_addresses.should_not be_empty
43
- instance.public_addresses.should be_a( Array )
44
- instance.private_addresses.should_not be_nil
45
- instance.private_addresses.should_not be_empty
46
- instance.private_addresses.should be_a( Array )
27
+ [API_URL, API_URL_REDIRECT].each do |entry_point|
28
+ DeltaCloud.new( API_NAME, API_PASSWORD, entry_point ) do |client|
29
+ instances = client.instances
30
+ instances.should_not be_empty
31
+ instances.each do |instance|
32
+ instance.uri.should_not be_nil
33
+ instance.uri.should be_a( String )
34
+ instance.owner_id.should_not be_nil
35
+ instance.owner_id.should be_a( String )
36
+ instance.image.should_not be_nil
37
+ instance.image.should be_a( DeltaCloud::API::Image )
38
+ instance.hardware_profile.should_not be_nil
39
+ instance.hardware_profile.should be_a( DeltaCloud::API::HardwareProfile )
40
+ instance.state.should_not be_nil
41
+ instance.state.should be_a( String )
42
+ instance.public_addresses.should_not be_nil
43
+ instance.public_addresses.should_not be_empty
44
+ instance.public_addresses.should be_a( Array )
45
+ instance.private_addresses.should_not be_nil
46
+ instance.private_addresses.should_not be_empty
47
+ instance.private_addresses.should be_a( Array )
48
+ end
47
49
  end
48
50
  end
49
51
  end
@@ -83,7 +85,7 @@ describe "instances" do
83
85
 
84
86
  it "should allow creation of new instances with reasonable defaults" do
85
87
  DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
86
- instance = client.create_instance( 'img1', :name=>'TestInstance' )
88
+ instance = client.create_instance( 'img1', :name=>'TestInstance', :hardware_profile => 'm1-large' )
87
89
  instance.should_not be_nil
88
90
  instance.uri.should match( %r{#{API_URL}/instances/inst[0-9]+} )
89
91
  instance.id.should match( /inst[0-9]+/ )
@@ -96,7 +98,7 @@ describe "instances" do
96
98
 
97
99
  it "should allow creation of new instances with specific realm" do
98
100
  DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
99
- instance = client.create_instance( 'img1', :realm=>'eu' )
101
+ instance = client.create_instance( 'img1', :realm=>'eu', :hardware_profile => 'm1-large' )
100
102
  instance.should_not be_nil
101
103
  instance.uri.should match( %r{#{API_URL}/instances/inst[0-9]+} )
102
104
  instance.id.should match( /inst[0-9]+/ )
@@ -184,7 +186,8 @@ describe "instances" do
184
186
  instance = client.instance( 'inst1' )
185
187
  instance.should_not be_nil
186
188
  instance.state.should eql( "RUNNING" )
187
- lambda{instance.start}.should raise_error
189
+ instance.start!
190
+ instance.state.should eql( "RUNNING" )
188
191
  end
189
192
  end
190
193
  end