bbrowning-deltacloud-client 0.0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ require 'lib/deltacloud'
2
+
3
+ skip_methods = [ "id=", "uri=" ]
4
+
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
11
+
12
+ @dc.entry_points.keys.each do |ep|
13
+ @dc.send(ep)
14
+ end
15
+ class_list = @dc.classes
16
+
17
+ def read_method_description(c, method)
18
+ if method =~ /es$/
19
+ " # Read #{c.downcase} collection from Deltacloud API"
20
+ else
21
+ case method
22
+ when "uri"
23
+ " # Return URI to API for this object"
24
+ when "action_urls"
25
+ " # Return available actions API URL"
26
+ when "client"
27
+ " # Return instance of API client"
28
+ else
29
+ " # Get #{method} attribute value from #{c.downcase}"
30
+ end
31
+ end
32
+ end
33
+
34
+ def read_parameters(c, method)
35
+ out = []
36
+ if method =~ /es$/
37
+ out << " # @param [String, #id] Filter by ID"
38
+ end
39
+ out.join("\n")
40
+ end
41
+
42
+ def read_return_value(c, method)
43
+ if method =~ /es$/
44
+ rt = "Array"
45
+ else
46
+ rt = "String"
47
+ end
48
+ " # @return [String] Value of #{method}"
49
+ end
50
+
51
+ out = []
52
+
53
+ class_list.each do |c|
54
+ class_name = "#{c}".gsub(/^DeltaCloud::/, '')
55
+ out << "module DeltaCloud"
56
+ out << " class API"
57
+ @dc.entry_points.keys.each do |ep|
58
+ out << "# Return #{ep.to_s.classify} object with given id\n"
59
+ out << "# "
60
+ out << "# *#{@dc.documentation(ep.to_s).description}*"
61
+ out << "# @return [#{ep.to_s.classify}]"
62
+ out << "def #{ep.to_s.gsub(/s$/, '')}"
63
+ out << "end"
64
+ out << "# Return collection of #{ep.to_s.classify} objects"
65
+ out << "# "
66
+ out << "# *#{@dc.documentation(ep.to_s).description}*"
67
+ @dc.documentation(ep.to_s, 'index').params.each do |p|
68
+ out << p.to_comment
69
+ end
70
+ out << "# @return [Array] [#{ep.to_s.classify}]"
71
+ out << "def #{ep}(opts={})"
72
+ out << "end"
73
+ end
74
+ out << " end"
75
+ out << " class #{class_name}"
76
+ c.instance_methods(false).each do |method|
77
+ next if skip_methods.include?(method)
78
+ params = read_parameters(class_name, method)
79
+ retval = read_return_value(class_name, method)
80
+ out << read_method_description(class_name, method)
81
+ out << params if params
82
+ out << retval if retval
83
+ out << " def #{method}"
84
+ out << " # This method was generated dynamically from API"
85
+ out << " end\n"
86
+ end
87
+ out << " end"
88
+ out << "end"
89
+ end
90
+
91
+ FileUtils.rm_r('doc') rescue nil
92
+ FileUtils.mkdir_p('doc')
93
+ File.open('doc/deltacloud.rb', 'w') do |f|
94
+ f.puts(out.join("\n"))
95
+ end
96
+ system("yardoc -m markdown --readme README --title 'Deltacloud Client Library' 'lib/*.rb' 'doc/deltacloud.rb' --verbose")
97
+ FileUtils.rm('doc/deltacloud.rb')
@@ -0,0 +1,86 @@
1
+ module DeltaCloud
2
+ module PlainFormatter
3
+ module FormatObject
4
+
5
+ class Base
6
+ def initialize(obj)
7
+ @obj = obj
8
+ end
9
+ end
10
+
11
+ class Image < Base
12
+ def format
13
+ sprintf("%-10s | %-20s | %-6s | %-20s | %15s",
14
+ @obj.id[0,10],
15
+ @obj.name ? @obj.name[0, 20]: 'unknown',
16
+ @obj.architecture[0,6],
17
+ @obj.description[0,20],
18
+ @obj.owner_id[0,15]
19
+ )
20
+ end
21
+ end
22
+
23
+ class Realm < Base
24
+ def format
25
+ sprintf("%-10s | %-15s | %-5s | %10s GB",
26
+ @obj.id[0, 10],
27
+ @obj.name[0, 15],
28
+ @obj.state[0,5],
29
+ @obj.limit.to_s[0,10]
30
+ )
31
+ end
32
+ end
33
+
34
+ class HardwareProfile < Base
35
+ def format
36
+ sprintf("%-15s | %-6s | %10s | %10s ", @obj.id[0, 15],
37
+ @obj.architecture.value[0,6], @obj.memory.value.to_s[0,10], @obj.storage.value.to_s[0,10])
38
+ end
39
+ end
40
+
41
+ class Instance < Base
42
+ def format
43
+ sprintf("%-15s | %-15s | %-15s | %10s | %32s | %32s",
44
+ @obj.id ? @obj.id[0,15] : '-',
45
+ @obj.name ? @obj.name[0,15] : 'unknown',
46
+ @obj.image.name ? @obj.image.name[0,15] : 'unknown',
47
+ @obj.state ? @obj.state.to_s[0,10] : 'unknown',
48
+ @obj.public_addresses.join(',')[0,32],
49
+ @obj.private_addresses.join(',')[0,32]
50
+ )
51
+ end
52
+ end
53
+
54
+ class StorageVolume < Base
55
+ def format
56
+ sprintf("%-10s | %15s GB | %-10s | %-10s | %-15s",
57
+ @obj.id[0,10],
58
+ @obj.capacity ? @obj.capacity.to_s[0,15] : 'unknown',
59
+ @obj.device ? @obj.device[0,10] : 'unknown',
60
+ @obj.respond_to?('state') ? @obj.state[0,10] : 'unknown',
61
+ @obj.instance ? @obj.instance.name[0,15] : 'unknown'
62
+ )
63
+ end
64
+ end
65
+
66
+ class StorageSnapshot < Base
67
+ def format
68
+ sprintf("%-10s | %-15s | %-6s | %15s",
69
+ @obj.id[0,10],
70
+ @obj.storage_volume.respond_to?('name') ? @obj.storage_volume.name[0, 15] : 'unknown',
71
+ @obj.state ? @obj.state[0,10] : 'unknown',
72
+ @obj.created ? @obj.created[0,19] : 'unknown'
73
+ )
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ def format(obj)
80
+ object_name = obj.class.name.classify.gsub(/^DeltaCloud::/, '')
81
+ format_class = DeltaCloud::PlainFormatter::FormatObject.const_get(object_name)
82
+ format_class.new(obj).format
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,4 @@
1
+ :description: Fedora 10
2
+ :owner_id: fedoraproject
3
+ :architecture: x86_64
4
+ :id: img1
@@ -0,0 +1,4 @@
1
+ :description: Fedora 10
2
+ :owner_id: fedoraproject
3
+ :architecture: i386
4
+ :id: img2
@@ -0,0 +1,4 @@
1
+ :description: JBoss
2
+ :owner_id: mockuser
3
+ :architecture: i386
4
+ :id: img3
@@ -0,0 +1,16 @@
1
+ ---
2
+ :realm_id: us
3
+ :public_addresses:
4
+ - img1.inst0.public.com
5
+ :state: RUNNING
6
+ :name: "Mock Instance With Profile Change"
7
+ :private_addresses:
8
+ - img1.inst0.private.com
9
+ :image_id: img1
10
+ :instance_profile: !ruby/object:InstanceProfile
11
+ id: m1-large
12
+ memory: "12288"
13
+ :owner_id: mockuser
14
+ :actions:
15
+ - :reboot
16
+ - :stop
@@ -0,0 +1,9 @@
1
+ :name: MockUserInstance
2
+ :state: RUNNING
3
+ :image_id: img3
4
+ :owner_id: mockuser
5
+ :public_addresses: [ img3.inst1.public.com ]
6
+ :private_addresses: [ img3.inst1.private.com ]
7
+ :realm_id: us
8
+ :instance_profile: !ruby/object:InstanceProfile
9
+ id: m1-small
@@ -0,0 +1,9 @@
1
+ :name: AnotherInstance
2
+ :state: RUNNING
3
+ :image_id: img1
4
+ :owner_id: anotheruser
5
+ :public_addresses: [ img1.inst2.public.com ]
6
+ :private_addresses: [ img1.inst2.private.com ]
7
+ :realm_id: us
8
+ :instance_profile: !ruby/object:InstanceProfile
9
+ id: m1-large
@@ -0,0 +1,4 @@
1
+ :owner_id: fedoraproject
2
+ :created: Wed Jul 29 18:15:24 UTC 2009
3
+ :state: COMPLETED
4
+ :storage_volume_id: vol1
@@ -0,0 +1,4 @@
1
+ :owner_id: mockuser
2
+ :created: Wed Jul 29 18:15:24 UTC 2009
3
+ :state: COMPLETED
4
+ :storage_volume_id: vol2
@@ -0,0 +1,4 @@
1
+ :owner_id: mockuser
2
+ :created: Wed Jul 29 18:15:24 UTC 2009
3
+ :state: COMPLETED
4
+ :storage_volume_id: vol2
@@ -0,0 +1,6 @@
1
+ :owner_id: fedoraproject
2
+ :created: Thu Jul 30 14:35:11 UTC 2009
3
+ :state: AVAILABLE
4
+ :capacity: 1
5
+ :device:
6
+ :instance_id:
@@ -0,0 +1,6 @@
1
+ :owner_id: mockuser
2
+ :created: Thu Jul 30 14:35:11 UTC 2009
3
+ :state: AVAILABLE
4
+ :capacity: 1
5
+ :device:
6
+ :instance_id:
@@ -0,0 +1,6 @@
1
+ :owner_id: mockuser
2
+ :created: Thu Jul 30 14:35:11 UTC 2009
3
+ :state: IN-USE
4
+ :capacity: 1
5
+ :device: /dev/sda1
6
+ :instance_id: inst1
@@ -0,0 +1,72 @@
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
+
20
+ require 'specs/spec_helper'
21
+
22
+ def prop_check(prop, value_class)
23
+ if prop.present?
24
+ prop.value.should_not be_nil
25
+ prop.value.should be_a(value_class)
26
+ end
27
+ end
28
+
29
+ describe "hardware_profiles" do
30
+
31
+ it_should_behave_like "all resources"
32
+
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)
41
+ prop_check(hwp.storage, Float)
42
+ prop_check(hwp.memory, Float)
43
+ end
44
+ end
45
+ end
46
+
47
+ it "should allow filtering of hardware_profiles by architecture" do
48
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
49
+ hardware_profiles = client.hardware_profiles( :architecture=>'i386' )
50
+ hardware_profiles.should_not be_empty
51
+ hardware_profiles.size.should eql( 2 )
52
+ hardware_profiles.first.architecture.value.should eql( 'i386' )
53
+ end
54
+ end
55
+
56
+ it "should allow fetching a hardware_profile by id" do
57
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
58
+ hwp = client.hardware_profile( 'm1-small' )
59
+ hwp.should_not be_nil
60
+ hwp.id.should eql( 'm1-small' )
61
+ end
62
+ end
63
+
64
+ it "should allow fetching a hardware_profile by URI" do
65
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
66
+ hwp = client.fetch_hardware_profile( API_URL + '/hardware_profiles/m1-small' )
67
+ hwp.should_not be_nil
68
+ hwp.id.should eql( 'm1-small' )
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,105 @@
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
+
20
+ require 'specs/spec_helper'
21
+
22
+ describe "images" do
23
+
24
+ it_should_behave_like "all resources"
25
+
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)
40
+ end
41
+ end
42
+ end
43
+
44
+ it "should allow retrieval of my own images" do
45
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
46
+ images = client.images( :owner_id=>:self )
47
+ images.should_not be_empty
48
+ images.size.should eql( 1 )
49
+ images.each do |image|
50
+ image.uri.should_not be_nil
51
+ image.uri.should be_a(String)
52
+ image.description.should_not be_nil
53
+ image.description.should be_a(String)
54
+ image.architecture.should_not be_nil
55
+ image.architecture.should be_a(String)
56
+ image.owner_id.should_not be_nil
57
+ image.owner_id.should be_a(String)
58
+ end
59
+ end
60
+ end
61
+
62
+ it "should allow retrieval of a single image by ID" do
63
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
64
+ image = client.image( 'img1' )
65
+ image.should_not be_nil
66
+ image.uri.should eql( API_URL + '/images/img1' )
67
+ image.id.should eql( 'img1' )
68
+ image.architecture.should eql( 'x86_64' )
69
+ end
70
+ end
71
+
72
+ it "should allow retrieval of a single image by URI" do
73
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
74
+ image = client.fetch_image( API_URL + '/images/img1' )
75
+ image.should_not be_nil
76
+ image.uri.should eql( API_URL + '/images/img1' )
77
+ image.id.should eql( 'img1' )
78
+ image.architecture.should eql( 'x86_64' )
79
+ end
80
+ end
81
+
82
+ describe "filtering by architecture" do
83
+ it "return matching images" do
84
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
85
+ images = client.images( :architecture=>'x86_64' )
86
+ images.should_not be_empty
87
+ images.each do |image|
88
+ image.architecture.should eql( 'x86_64' )
89
+ end
90
+ images = client.images( :architecture=>'i386' )
91
+ images.should_not be_empty
92
+ images.each do |image|
93
+ image.architecture.should eql( 'i386' )
94
+ end
95
+ end
96
+ end
97
+
98
+ it "should return an empty array for no matches" do
99
+ DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
100
+ images = client.images( :architecture=>'8088' )
101
+ images.should be_empty
102
+ end
103
+ end
104
+ end
105
+ end