steamcannon-deltacloud-core 0.0.7.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/COPYING +176 -0
  2. data/Rakefile +106 -0
  3. data/bin/deltacloudd +120 -0
  4. data/config.ru +5 -0
  5. data/deltacloud.rb +20 -0
  6. data/lib/deltacloud/base_driver/base_driver.rb +259 -0
  7. data/lib/deltacloud/base_driver/features.rb +173 -0
  8. data/lib/deltacloud/base_driver/mock_driver.rb +58 -0
  9. data/lib/deltacloud/base_driver.rb +20 -0
  10. data/lib/deltacloud/drivers/azure/azure_driver.rb +127 -0
  11. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +580 -0
  12. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +170 -0
  13. data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +50 -0
  14. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +343 -0
  15. data/lib/deltacloud/drivers/gogrid/test.rb +13 -0
  16. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob1.yml +5 -0
  17. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob2.yml +5 -0
  18. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob3.yml +5 -0
  19. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob4.yml +5 -0
  20. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob5.yml +5 -0
  21. data/lib/deltacloud/drivers/mock/data/buckets/bucket1.yml +2 -0
  22. data/lib/deltacloud/drivers/mock/data/buckets/bucket2.yml +2 -0
  23. data/lib/deltacloud/drivers/mock/data/images/img1.yml +3 -0
  24. data/lib/deltacloud/drivers/mock/data/images/img2.yml +3 -0
  25. data/lib/deltacloud/drivers/mock/data/images/img3.yml +3 -0
  26. data/lib/deltacloud/drivers/mock/data/instances/inst0.yml +16 -0
  27. data/lib/deltacloud/drivers/mock/data/instances/inst1.yml +9 -0
  28. data/lib/deltacloud/drivers/mock/data/instances/inst2.yml +9 -0
  29. data/lib/deltacloud/drivers/mock/data/storage_snapshots/snap1.yml +4 -0
  30. data/lib/deltacloud/drivers/mock/data/storage_snapshots/snap2.yml +4 -0
  31. data/lib/deltacloud/drivers/mock/data/storage_snapshots/snap3.yml +4 -0
  32. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol1.yml +6 -0
  33. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol2.yml +6 -0
  34. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol3.yml +6 -0
  35. data/lib/deltacloud/drivers/mock/mock_driver.rb +356 -0
  36. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +116 -0
  37. data/lib/deltacloud/drivers/opennebula/occi_client.rb +204 -0
  38. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +241 -0
  39. data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +130 -0
  40. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +290 -0
  41. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +258 -0
  42. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +85 -0
  43. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +166 -0
  44. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +295 -0
  45. data/lib/deltacloud/hardware_profile.rb +153 -0
  46. data/lib/deltacloud/helpers/application_helper.rb +122 -0
  47. data/lib/deltacloud/helpers/blob_stream.rb +51 -0
  48. data/lib/deltacloud/helpers/conversion_helper.rb +39 -0
  49. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +35 -0
  50. data/lib/deltacloud/helpers.rb +5 -0
  51. data/lib/deltacloud/method_serializer.rb +85 -0
  52. data/lib/deltacloud/models/base_model.rb +59 -0
  53. data/lib/deltacloud/models/blob.rb +26 -0
  54. data/lib/deltacloud/models/bucket.rb +24 -0
  55. data/lib/deltacloud/models/image.rb +27 -0
  56. data/lib/deltacloud/models/instance.rb +38 -0
  57. data/lib/deltacloud/models/instance_profile.rb +48 -0
  58. data/lib/deltacloud/models/key.rb +35 -0
  59. data/lib/deltacloud/models/realm.rb +26 -0
  60. data/lib/deltacloud/models/storage_snapshot.rb +27 -0
  61. data/lib/deltacloud/models/storage_volume.rb +28 -0
  62. data/lib/deltacloud/state_machine.rb +84 -0
  63. data/lib/deltacloud/validation.rb +70 -0
  64. data/lib/drivers.rb +51 -0
  65. data/lib/sinatra/accept_media_types.rb +128 -0
  66. data/lib/sinatra/lazy_auth.rb +56 -0
  67. data/lib/sinatra/rabbit.rb +279 -0
  68. data/lib/sinatra/respond_to.rb +238 -0
  69. data/lib/sinatra/static_assets.rb +83 -0
  70. data/lib/sinatra/url_for.rb +53 -0
  71. data/public/favicon.ico +0 -0
  72. data/public/images/grid.png +0 -0
  73. data/public/images/logo-wide.png +0 -0
  74. data/public/images/rails.png +0 -0
  75. data/public/images/topbar-bg.png +0 -0
  76. data/public/javascripts/application.js +32 -0
  77. data/public/javascripts/jquery-1.4.2.min.js +154 -0
  78. data/public/stylesheets/compiled/application.css +613 -0
  79. data/public/stylesheets/compiled/ie.css +31 -0
  80. data/public/stylesheets/compiled/print.css +27 -0
  81. data/public/stylesheets/compiled/screen.css +456 -0
  82. data/server.rb +516 -0
  83. data/support/fedora/deltacloudd +68 -0
  84. data/support/fedora/rubygem-deltacloud-core.spec +91 -0
  85. data/tests/api_test.rb +37 -0
  86. data/tests/hardware_profiles_test.rb +120 -0
  87. data/tests/images_test.rb +111 -0
  88. data/tests/instance_states_test.rb +51 -0
  89. data/tests/instances_test.rb +222 -0
  90. data/tests/realms_test.rb +78 -0
  91. data/tests/url_for_test.rb +50 -0
  92. data/views/accounts/index.html.haml +11 -0
  93. data/views/accounts/show.html.haml +30 -0
  94. data/views/api/show.html.haml +15 -0
  95. data/views/api/show.xml.haml +5 -0
  96. data/views/blobs/show.html.haml +20 -0
  97. data/views/blobs/show.xml.haml +7 -0
  98. data/views/buckets/index.html.haml +33 -0
  99. data/views/buckets/index.xml.haml +10 -0
  100. data/views/buckets/new.html.haml +13 -0
  101. data/views/buckets/show.html.haml +19 -0
  102. data/views/buckets/show.xml.haml +8 -0
  103. data/views/docs/collection.html.haml +37 -0
  104. data/views/docs/collection.xml.haml +14 -0
  105. data/views/docs/index.html.haml +15 -0
  106. data/views/docs/index.xml.haml +5 -0
  107. data/views/docs/operation.html.haml +31 -0
  108. data/views/docs/operation.xml.haml +10 -0
  109. data/views/errors/auth_exception.html.haml +8 -0
  110. data/views/errors/auth_exception.xml.haml +2 -0
  111. data/views/errors/backend_error.html.haml +19 -0
  112. data/views/errors/backend_error.xml.haml +8 -0
  113. data/views/errors/not_found.html.haml +6 -0
  114. data/views/errors/not_found.xml.haml +2 -0
  115. data/views/errors/validation_failure.html.haml +11 -0
  116. data/views/errors/validation_failure.xml.haml +7 -0
  117. data/views/hardware_profiles/index.html.haml +25 -0
  118. data/views/hardware_profiles/index.xml.haml +4 -0
  119. data/views/hardware_profiles/show.html.haml +19 -0
  120. data/views/hardware_profiles/show.xml.haml +18 -0
  121. data/views/images/index.html.haml +30 -0
  122. data/views/images/index.xml.haml +8 -0
  123. data/views/images/show.html.haml +21 -0
  124. data/views/images/show.xml.haml +5 -0
  125. data/views/instance_states/show.html.haml +31 -0
  126. data/views/instance_states/show.png.erb +45 -0
  127. data/views/instance_states/show.xml.haml +8 -0
  128. data/views/instances/index.html.haml +30 -0
  129. data/views/instances/index.xml.haml +21 -0
  130. data/views/instances/new.html.haml +55 -0
  131. data/views/instances/show.html.haml +43 -0
  132. data/views/instances/show.xml.haml +49 -0
  133. data/views/keys/index.html.haml +26 -0
  134. data/views/keys/index.xml.haml +4 -0
  135. data/views/keys/new.html.haml +8 -0
  136. data/views/keys/show.html.haml +22 -0
  137. data/views/keys/show.xml.haml +20 -0
  138. data/views/layout.html.haml +26 -0
  139. data/views/realms/index.html.haml +29 -0
  140. data/views/realms/index.xml.haml +10 -0
  141. data/views/realms/show.html.haml +15 -0
  142. data/views/realms/show.xml.haml +9 -0
  143. data/views/root/index.html.haml +4 -0
  144. data/views/storage_snapshots/index.html.haml +20 -0
  145. data/views/storage_snapshots/index.xml.haml +9 -0
  146. data/views/storage_snapshots/show.html.haml +14 -0
  147. data/views/storage_snapshots/show.xml.haml +7 -0
  148. data/views/storage_volumes/index.html.haml +21 -0
  149. data/views/storage_volumes/index.xml.haml +23 -0
  150. data/views/storage_volumes/show.html.haml +20 -0
  151. data/views/storage_volumes/show.xml.haml +24 -0
  152. metadata +367 -0
@@ -0,0 +1,170 @@
1
+ module RightAws
2
+ class MockEc2
3
+
4
+ def initialize(opts={})
5
+ end
6
+
7
+ def describe_images(id)
8
+ load_fixtures_for(:images).select { |i| i[:aws_id].eql?(id) }
9
+ end
10
+
11
+ def describe_images_by_owner(id)
12
+ load_fixtures_for(:images).select { |i| i[:aws_owner].eql?(id) }
13
+ end
14
+
15
+ def describe_images(opts={})
16
+ load_fixtures_for(:images)
17
+ end
18
+
19
+ def describe_availability_zones(opts={})
20
+ load_fixtures_for(:realms)
21
+ end
22
+
23
+ def describe_instances(opts={})
24
+ instances = load_fixtures_for(:instances)
25
+ instances.each_with_index do |instance, i|
26
+ instances[i] = update_delayed_state(instance)
27
+ return [instance] if opts and opts[:id] and instance[:aws_instance_id].eql?(opts[:id])
28
+ end
29
+ update_fixtures_for(:instances, instances)
30
+ instances
31
+ end
32
+
33
+ def run_instances(image_id, min_count, max_count, group_ids, key_name, user_data='', addressing_type = nil, instance_type = nil, kernel_id = nil, ramdisk_id = nil, availability_zone = nil, block_device_mappings = nil)
34
+
35
+ instances = load_fixtures_for(:instances)
36
+ image = load_fixtures_for(:images).select { |img| img[:aws_id].eql?(image_id) }.first
37
+
38
+ if availability_zone
39
+ realm = load_fixtures_for(:realms).select { |realm| realm[:zone_name].eql?(availability_zone) }.first
40
+ else
41
+ realm = load_fixtures_for(:realms).first
42
+ end
43
+
44
+ raise Exception unless image
45
+ raise Exception unless realm
46
+
47
+ instance = { }
48
+ instance[:aws_image_id] = image[:aws_id]
49
+ instance[:aws_availability_zone] = realm[:zone_name]
50
+ instance[:aws_instance_type] = instance_type
51
+ instance[:aws_owner] = user_data
52
+ instance[:aws_state] = 'pending'
53
+ instance[:aws_reason] = ''
54
+ instance[:dns_name] = "#{random_dns}-01-C9.usma2.compute.amazonaws.com"
55
+ instance[:private_dns_name] = "#{random_dns}-02-P9.usma2.compute.amazonaws.com"
56
+ instance[:aws_state_code] = "0"
57
+ instance[:aws_key_name] = "staging"
58
+ instance[:aws_kernel_id] = "aki-be3adfd7"
59
+ instance[:aws_ramdisk_id] = "ari-ce34gad7"
60
+ instance[:aws_groups] = ["default"]
61
+ instance[:aws_instance_id] = random_instance_id
62
+ instance[:aws_reservation_id] = "r-aabbccdd"
63
+ instance[:aws_launch_time] = instance_time_format
64
+
65
+ instances << instance
66
+
67
+ update_fixtures_for(:instances, instances)
68
+
69
+ return [instance]
70
+ end
71
+
72
+
73
+ def terminate_instances(id)
74
+ update_instance_state(id, 'stopping', '80')
75
+ end
76
+
77
+ def reboot_instances(id)
78
+ update_instance_state(id, 'pending', '0')
79
+ end
80
+
81
+ alias :destroy_instance :terminate_instances
82
+
83
+ def describe_snapshots(opts={})
84
+ load_fixtures_for(:storage_snapshot)
85
+ end
86
+
87
+ def describe_volumes(opts={})
88
+ load_fixtures_for(:storage_volume)
89
+ end
90
+
91
+ private
92
+
93
+ def driver_dir
94
+ File::expand_path(File::join(File::dirname(__FILE__), '../../../../features/support/ec2'))
95
+ end
96
+
97
+ def fixtures_path
98
+ File::expand_path(File::join(driver_dir, 'fixtures'))
99
+ end
100
+
101
+ def load_fixtures_for(collection)
102
+ YAML.load_file(File::join(fixtures_path, "#{collection}.yaml"))
103
+ end
104
+
105
+ def update_fixtures_for(collection, new_data)
106
+ File.open(File::join(fixtures_path, "#{collection}.yaml"), 'w' ) do |out|
107
+ YAML.dump(new_data, out)
108
+ end
109
+ return new_data
110
+ end
111
+
112
+ def instance_time_format
113
+ DateTime.now.to_s.gsub(/\+(.+)$/, '.000Z')
114
+ end
115
+
116
+ def random_instance_id
117
+ id_1 = ("%.4s" % Time.now.to_i.to_s.reverse).reverse
118
+ id_2 = ("%.3s" % Time.now.to_i.to_s.reverse)
119
+ "i-#{id_1}f#{id_2}"
120
+ end
121
+
122
+ def random_dns
123
+ "domU-#{rand(90)+10}-#{rand(90)+10}-#{rand(90)+10}-#{rand(90)+10}"
124
+ end
125
+
126
+ def update_delayed_state(instance)
127
+ time = DateTime.now - DateTime.parse(instance[:aws_launch_time])
128
+ hours, minutes, seconds, frac = Date.day_fraction_to_time(time)
129
+
130
+ if (minutes>(rand(2)+1) or hours>0) and instance[:aws_state].eql?('pending')
131
+ instance[:aws_state], instance[:aws_state_code] = 'running', '16'
132
+ end
133
+
134
+ if (minutes>(rand(1)+1) or hours>0) and instance[:aws_state].eql?('stopping')
135
+ instance[:aws_state], instance[:aws_state_code] = 'stopped', '80'
136
+ end
137
+
138
+ return instance
139
+ end
140
+
141
+ def update_instance_state(id, state, state_code)
142
+ instance = describe_instances(:id => id).first
143
+ if instance
144
+ instance[:aws_state], instance[:aws_state_code] = state, state_code
145
+ instance[:aws_launch_time] = instance_time_format
146
+ instances = load_fixtures_for(:instances)
147
+ instances.each_with_index do |inst, i|
148
+ instances[i] = instance if inst[:aws_instance_id].eql?(id)
149
+ end
150
+ update_fixtures_for(:instances, instances)
151
+ return instance
152
+ else
153
+ raise Exception
154
+ end
155
+ end
156
+
157
+ end
158
+ end
159
+
160
+ Deltacloud::Drivers::EC2::EC2Driver.class_eval do
161
+ alias_method :original_new_client, :new_client
162
+
163
+ def new_client(credentials, opts={})
164
+ if credentials.user != 'mockuser' and credentials.password != 'mockpassword'
165
+ raise Deltacloud::AuthException.new
166
+ end
167
+ RightAws::MockEc2.new
168
+ end
169
+
170
+ end
@@ -0,0 +1,50 @@
1
+ require 'digest/md5'
2
+ require 'cgi'
3
+ require 'open-uri'
4
+ require 'json'
5
+
6
+ class GoGridClient
7
+
8
+ def initialize(server='https://api.gogrid.com/api',
9
+ apikey='YOUR API KEY',
10
+ secret='YOUR SHARED SECRET',
11
+ format='json',
12
+ version='1.5')
13
+ @server = server
14
+ @secret = secret
15
+ @default_params = {'format'=>format, 'v'=>version,'api_key' => apikey}
16
+ end
17
+
18
+ def getRequestURL(method,params)
19
+ requestURL = @server+'/'+method+'?'
20
+ call_params = @default_params.merge(params)
21
+ call_params['sig']=getSignature(@default_params['api_key'],@secret)
22
+ requestURL = requestURL+encode_params(call_params)
23
+ end
24
+
25
+ def getSignature(key,secret)
26
+ Digest::MD5.hexdigest(key+secret+"%.0f"%Time.new.to_f)
27
+ end
28
+
29
+ def sendAPIRequest(method,params={})
30
+ open(getRequestURL(method,params)).read
31
+ end
32
+
33
+ def request(method, params={}, version=nil)
34
+ if version
35
+ @default_params['v'] = version
36
+ else
37
+ @default_params['v'] = '1.5'
38
+ end
39
+ begin
40
+ JSON::parse(sendAPIRequest(method, params))
41
+ rescue Exception => e
42
+ STDERR.puts("ERROR: #{e.message}")
43
+ end
44
+ end
45
+
46
+ def encode_params(params)
47
+ params.map {|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
48
+ end
49
+
50
+ end
@@ -0,0 +1,343 @@
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
+ require 'deltacloud/base_driver'
20
+ require 'deltacloud/drivers/gogrid/gogrid_client'
21
+
22
+ class Instance
23
+ attr_accessor :username
24
+ attr_accessor :password
25
+ attr_accessor :authn_error
26
+
27
+ def authn_feature_failed?
28
+ return true unless authn_error.nil?
29
+ end
30
+ end
31
+
32
+ module Deltacloud
33
+ module Drivers
34
+ module Gogrid
35
+
36
+ class GogridDriver < Deltacloud::BaseDriver
37
+
38
+ feature :instances, :authentication_password
39
+
40
+ define_hardware_profile 'server' do
41
+ cpu 2
42
+ memory [512, 1024, 2048, 4096, 8192]
43
+ storage 10
44
+ end
45
+
46
+ def supported_collections
47
+ DEFAULT_COLLECTIONS.reject! { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
48
+ DEFAULT_COLLECTIONS + [ :keys ]
49
+ end
50
+
51
+ def images(credentials, opts=nil)
52
+ imgs = []
53
+ if opts and opts[:id]
54
+ safely do
55
+ imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
56
+ end
57
+ else
58
+ safely do
59
+ imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
60
+ convert_image(image, credentials.user)
61
+ end
62
+ end
63
+ end
64
+ imgs = filter_on( imgs, :architecture, opts )
65
+ imgs.sort_by{|e| [e.owner_id, e.description]}
66
+ end
67
+
68
+ def realms(credentials, opts=nil)
69
+ safely do
70
+ new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
71
+ convert_realm(realm)
72
+ end
73
+ end
74
+ end
75
+
76
+ def create_instance(credentials, image_id, opts=nil)
77
+ server_ram = nil
78
+ if opts[:hwp_memory]
79
+ mem = opts[:hwp_memory].to_i
80
+ server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
81
+ else
82
+ server_ram = "512MB"
83
+ end
84
+ client = new_client(credentials)
85
+ name = (opts[:name] && opts[:name]!='') ? opts[:name] : get_random_instance_name
86
+ safely do
87
+ instance = client.request('grid/server/add', {
88
+ 'name' => name,
89
+ 'image' => image_id,
90
+ 'server.ram' => server_ram,
91
+ 'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
92
+ })['list'].first
93
+ if instance
94
+ login_data = get_login_data(client, instance[:id])
95
+ if login_data['username'] and login_data['password']
96
+ instance['username'] = login_data['username']
97
+ instance['password'] = login_data['password']
98
+ inst = convert_instance(instance, credentials.user)
99
+ else
100
+ inst = convert_instance(instance, credentials.user)
101
+ inst.authn_error = "Unable to fetch password"
102
+ end
103
+ return inst
104
+ else
105
+ return nil
106
+ end
107
+ end
108
+ end
109
+
110
+ def list_instances(credentials, id)
111
+ instances = []
112
+ safely do
113
+ new_client(credentials).request('grid/server/list')['list'].collect do |instance|
114
+ if id.nil? or instance['name'] == id
115
+ instances << convert_instance(instance, credentials.user)
116
+ end
117
+ end
118
+ end
119
+ instances
120
+ end
121
+
122
+ def instances(credentials, opts=nil)
123
+ instances = []
124
+ if opts and opts[:id]
125
+ begin
126
+ client = new_client(credentials)
127
+ instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
128
+ login_data = get_login_data(client, instance['id'])
129
+ if login_data['username'] and login_data['password']
130
+ instance['username'] = login_data['username']
131
+ instance['password'] = login_data['password']
132
+ inst = convert_instance(instance, credentials.user)
133
+ else
134
+ inst = convert_instance(instance, credentials.user)
135
+ inst.authn_error = "Unable to fetch password"
136
+ end
137
+ instances = [inst]
138
+ rescue Exception => e
139
+ if e.message == "400 Bad Request"
140
+ # in the case of a VM that we just made, the grid/server/get method
141
+ # throws a "400 Bad Request error". In this case we try again by
142
+ # getting a full listing a filtering on the id. This could
143
+ # potentially take a long time, but I don't see another way to get
144
+ # information about a newly created instance
145
+ instances = list_instances(credentials, opts[:id])
146
+ end
147
+ end
148
+ else
149
+ instances = list_instances(credentials, nil)
150
+ end
151
+ instances = filter_on( instances, :state, opts )
152
+ instances
153
+ end
154
+
155
+ def reboot_instance(credentials, id)
156
+ safely do
157
+ new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
158
+ end
159
+ end
160
+
161
+ def destroy_instance(credentials, id)
162
+ safely do
163
+ new_client(credentials).request('grid/server/delete', { 'name' => id})
164
+ end
165
+ end
166
+
167
+ def stop_instance(credentials, id)
168
+ safely do
169
+ new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
170
+ end
171
+ end
172
+
173
+ def start_instance(credentials, id)
174
+ safely do
175
+ new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
176
+ end
177
+ end
178
+
179
+ def key(credentials, opts=nil)
180
+ keys(credentials, opts).first
181
+ end
182
+
183
+ def keys(credentials, opts=nil)
184
+ gogrid = new_client( credentials )
185
+ creds = []
186
+ safely do
187
+ gogrid.request('support/password/list')['list'].each do |password|
188
+ creds << convert_key(password)
189
+ end
190
+ end
191
+ return creds
192
+ end
193
+
194
+ def valid_credentials?(credentials)
195
+ client = new_client(credentials)
196
+ # FIXME: We need to do this call to determine if
197
+ # GoGrid is working with given credentials. There is no
198
+ # other way to check, if given credentials are valid or not.
199
+ return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
200
+ true
201
+ end
202
+
203
+ define_instance_states do
204
+ start.to( :pending ) .automatically
205
+ pending.to( :running ) .automatically
206
+ running.to( :stopped ) .on( :stop )
207
+ stopped.to( :running ) .on( :start )
208
+ running.to( :finish ) .on( :destroy )
209
+ stopped.to( :finish ) .on( :destroy )
210
+ end
211
+
212
+ private
213
+
214
+ def new_client(credentials)
215
+ GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
216
+
217
+ end
218
+
219
+ def get_login_data(client, instance_id)
220
+ login_data = {}
221
+ begin
222
+ client.request('support/password/list')['list'].each do |passwd|
223
+ next unless passwd['server']
224
+ if passwd['server']['id'] == instance_id
225
+ login_data['username'], login_data['password'] = passwd['username'], passwd['password']
226
+ break
227
+ end
228
+ end
229
+ rescue Exception => e
230
+ login_data[:error] = e.message
231
+ end
232
+ return login_data
233
+ end
234
+
235
+ def convert_key(password)
236
+ Key.new({
237
+ :id => password['id'],
238
+ :username => password['username'],
239
+ :password => password['password'],
240
+ :credential_type => :password
241
+ })
242
+ end
243
+
244
+ def convert_image(gg_image, owner_id=nil)
245
+ Image.new( {
246
+ :id=>gg_image['id'],
247
+ :name => gg_image['friendlyName'],
248
+ :description=> convert_description(gg_image),
249
+ :owner_id=>gg_image['owner']['name'],
250
+ :architecture=>convert_arch(gg_image['description']),
251
+ } )
252
+ end
253
+
254
+ def convert_description(image)
255
+ if image['price'].eql?(0)
256
+ image['description']
257
+ else
258
+ "#{image['description']} (#{image['price']}$)"
259
+ end
260
+ end
261
+
262
+ def convert_realm(realm)
263
+ Realm.new(
264
+ :id => realm['id'],
265
+ :name => realm['name'],
266
+ :state => :unlimited,
267
+ :storage => :unlimited
268
+ )
269
+ end
270
+
271
+ def convert_arch(description)
272
+ description.include?('64-bit') ? 'x86_64' : 'i386'
273
+ end
274
+
275
+ def convert_instance(instance, owner_id)
276
+ opts = {}
277
+ unless instance['ram']['id'] == "1"
278
+ mem = instance['ram']['name']
279
+ if mem == "512MB"
280
+ opts[:hwp_memory] = "512"
281
+ else
282
+ opts[:hwp_memory] = (mem.to_i * 1024).to_s
283
+ end
284
+ end
285
+ prof = InstanceProfile.new("server", opts)
286
+
287
+ hwp_name = instance['image']['name']
288
+ state = convert_server_state(instance['state']['name'], instance['id'])
289
+
290
+ Instance.new(
291
+ # note that we use 'name' as the id here, because newly created instances
292
+ # don't get a real ID until later on. The name is good enough; from
293
+ # what I can tell, 'name' per user is unique, so it should be sufficient
294
+ # to uniquely identify this instance.
295
+ :id => instance['name'],
296
+ :owner_id => owner_id,
297
+ :image_id => instance['image']['id'],
298
+ :instance_profile => prof,
299
+ :name => instance['name'],
300
+ :realm_id => instance['ip']['datacenter']['id'],
301
+ :state => state,
302
+ :actions => instance_actions_for(state),
303
+ :public_addresses => [ instance['ip']['ip'] ],
304
+ :private_addresses => [],
305
+ :username => instance['username'],
306
+ :password => instance['password']
307
+ )
308
+ end
309
+
310
+ def get_random_instance_name
311
+ "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
312
+ end
313
+
314
+ def convert_server_state(state, id)
315
+ return 'PENDING' unless id
316
+ state.eql?('Off') ? 'STOPPED' : 'RUNNING'
317
+ end
318
+
319
+ def get_free_ip_from_realm(credentials, realm_id)
320
+ ip = ""
321
+ safely do
322
+ ip = new_client(credentials).request('grid/ip/list', {
323
+ 'ip.type' => '1',
324
+ 'ip.state' => '1',
325
+ 'datacenter' => realm_id
326
+ })['list'].first['ip']
327
+ end
328
+ return ip
329
+ end
330
+
331
+ def safely(&block)
332
+ begin
333
+ block.call
334
+ rescue Exception => e
335
+ raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
336
+ end
337
+ end
338
+
339
+ end
340
+
341
+ end
342
+ end
343
+ end
@@ -0,0 +1,13 @@
1
+ require 'gogrid_client'
2
+ require 'ap'
3
+
4
+ user='fbb1de3897597ccf'
5
+ password='ngieth10'
6
+
7
+ client=GoGridClient.new('https://api.gogrid.com/api', user, password)
8
+
9
+ ap client.request('grid/ip/list', {
10
+ 'ip.type' => '1',
11
+ 'ip.state' => '1',
12
+ 'datacenter' => '1'
13
+ })
@@ -0,0 +1,5 @@
1
+ :bucket: bucket1
2
+ :content_length: 12627
3
+ :content_type: text/plain
4
+ :last_modified: 2010-09-23 16:44:54 +0100
5
+ :content: "content of blob 1"
@@ -0,0 +1,5 @@
1
+ :bucket: bucket1
2
+ :content_length: 4420
3
+ :content_type: text/html
4
+ :last_modified: 2010-09-23 16:55:05 +0100
5
+ :content: "content of blob 2"
@@ -0,0 +1,5 @@
1
+ :bucket: bucket1
2
+ :content_length: 98732
3
+ :content_type: text/plain
4
+ :last_modified: 2010-08-14 02:14:31 +0100
5
+ :content: "content of blob 3"
@@ -0,0 +1,5 @@
1
+ :bucket: bucket2
2
+ :content_length: 983232
3
+ :content_type: application/octet-stream
4
+ :last_modified: 2010-09-21 06:17:24 +0100
5
+ :content: "content of blob 4"
@@ -0,0 +1,5 @@
1
+ :bucket: bucket2
2
+ :content_length: 651922
3
+ :content_type: application/pdf
4
+ :last_modified: 2009-07-21 11:12:13 +0100
5
+ :content: "content of blob 5"
@@ -0,0 +1,2 @@
1
+ :blob_list: [blob1, blob2, blob3]
2
+ :size: 3
@@ -0,0 +1,2 @@
1
+ :blob_list: [blob4, blob5]
2
+ :size: 2
@@ -0,0 +1,3 @@
1
+ :description: Fedora 10
2
+ :owner_id: fedoraproject
3
+ :architecture: x86_64
@@ -0,0 +1,3 @@
1
+ :description: Fedora 10
2
+ :owner_id: fedoraproject
3
+ :architecture: i386
@@ -0,0 +1,3 @@
1
+ :description: JBoss
2
+ :owner_id: mockuser
3
+ :architecture: i386
@@ -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