fog-brightbox 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/fog-brightbox.gemspec +28 -0
  3. data/lib/fog/brightbox/compute/image_selector.rb +45 -0
  4. data/lib/fog/brightbox/compute/shared.rb +230 -0
  5. data/lib/fog/brightbox/compute.rb +305 -0
  6. data/lib/fog/brightbox/core.rb +11 -0
  7. data/lib/fog/brightbox/models/compute/account.rb +69 -0
  8. data/lib/fog/brightbox/models/compute/accounts.rb +29 -0
  9. data/lib/fog/brightbox/models/compute/api_client.rb +37 -0
  10. data/lib/fog/brightbox/models/compute/api_clients.rb +24 -0
  11. data/lib/fog/brightbox/models/compute/application.rb +24 -0
  12. data/lib/fog/brightbox/models/compute/applications.rb +28 -0
  13. data/lib/fog/brightbox/models/compute/cloud_ip.rb +68 -0
  14. data/lib/fog/brightbox/models/compute/cloud_ips.rb +34 -0
  15. data/lib/fog/brightbox/models/compute/collaboration.rb +49 -0
  16. data/lib/fog/brightbox/models/compute/collaborations.rb +47 -0
  17. data/lib/fog/brightbox/models/compute/database_server.rb +97 -0
  18. data/lib/fog/brightbox/models/compute/database_servers.rb +28 -0
  19. data/lib/fog/brightbox/models/compute/database_snapshot.rb +52 -0
  20. data/lib/fog/brightbox/models/compute/database_snapshots.rb +24 -0
  21. data/lib/fog/brightbox/models/compute/database_type.rb +19 -0
  22. data/lib/fog/brightbox/models/compute/database_types.rb +26 -0
  23. data/lib/fog/brightbox/models/compute/firewall_policies.rb +29 -0
  24. data/lib/fog/brightbox/models/compute/firewall_policy.rb +63 -0
  25. data/lib/fog/brightbox/models/compute/firewall_rule.rb +54 -0
  26. data/lib/fog/brightbox/models/compute/firewall_rules.rb +24 -0
  27. data/lib/fog/brightbox/models/compute/flavor.rb +32 -0
  28. data/lib/fog/brightbox/models/compute/flavors.rb +28 -0
  29. data/lib/fog/brightbox/models/compute/image.rb +64 -0
  30. data/lib/fog/brightbox/models/compute/images.rb +28 -0
  31. data/lib/fog/brightbox/models/compute/load_balancer.rb +107 -0
  32. data/lib/fog/brightbox/models/compute/load_balancers.rb +28 -0
  33. data/lib/fog/brightbox/models/compute/server.rb +217 -0
  34. data/lib/fog/brightbox/models/compute/server_group.rb +94 -0
  35. data/lib/fog/brightbox/models/compute/server_groups.rb +29 -0
  36. data/lib/fog/brightbox/models/compute/servers.rb +61 -0
  37. data/lib/fog/brightbox/models/compute/user.rb +43 -0
  38. data/lib/fog/brightbox/models/compute/user_collaboration.rb +44 -0
  39. data/lib/fog/brightbox/models/compute/user_collaborations.rb +31 -0
  40. data/lib/fog/brightbox/models/compute/users.rb +27 -0
  41. data/lib/fog/brightbox/models/compute/zone.rb +22 -0
  42. data/lib/fog/brightbox/models/compute/zones.rb +29 -0
  43. data/lib/fog/brightbox/oauth2.rb +166 -0
  44. data/lib/fog/brightbox/requests/compute/accept_user_collaboration.rb +21 -0
  45. data/lib/fog/brightbox/requests/compute/activate_console_server.rb +21 -0
  46. data/lib/fog/brightbox/requests/compute/add_listeners_load_balancer.rb +24 -0
  47. data/lib/fog/brightbox/requests/compute/add_nodes_load_balancer.rb +24 -0
  48. data/lib/fog/brightbox/requests/compute/add_servers_server_group.rb +34 -0
  49. data/lib/fog/brightbox/requests/compute/apply_to_firewall_policy.rb +24 -0
  50. data/lib/fog/brightbox/requests/compute/create_api_client.rb +23 -0
  51. data/lib/fog/brightbox/requests/compute/create_application.rb +23 -0
  52. data/lib/fog/brightbox/requests/compute/create_cloud_ip.rb +24 -0
  53. data/lib/fog/brightbox/requests/compute/create_collaboration.rb +23 -0
  54. data/lib/fog/brightbox/requests/compute/create_database_server.rb +25 -0
  55. data/lib/fog/brightbox/requests/compute/create_firewall_policy.rb +26 -0
  56. data/lib/fog/brightbox/requests/compute/create_firewall_rule.rb +29 -0
  57. data/lib/fog/brightbox/requests/compute/create_image.rb +30 -0
  58. data/lib/fog/brightbox/requests/compute/create_load_balancer.rb +26 -0
  59. data/lib/fog/brightbox/requests/compute/create_server.rb +29 -0
  60. data/lib/fog/brightbox/requests/compute/create_server_group.rb +23 -0
  61. data/lib/fog/brightbox/requests/compute/delete_api_client.rb +28 -0
  62. data/lib/fog/brightbox/requests/compute/delete_application.rb +28 -0
  63. data/lib/fog/brightbox/requests/compute/delete_cloud_ip.rb +28 -0
  64. data/lib/fog/brightbox/requests/compute/delete_collaboration.rb +28 -0
  65. data/lib/fog/brightbox/requests/compute/delete_database_server.rb +26 -0
  66. data/lib/fog/brightbox/requests/compute/delete_database_snapshot.rb +26 -0
  67. data/lib/fog/brightbox/requests/compute/delete_firewall_policy.rb +28 -0
  68. data/lib/fog/brightbox/requests/compute/delete_firewall_rule.rb +28 -0
  69. data/lib/fog/brightbox/requests/compute/delete_image.rb +28 -0
  70. data/lib/fog/brightbox/requests/compute/delete_load_balancer.rb +28 -0
  71. data/lib/fog/brightbox/requests/compute/delete_server.rb +28 -0
  72. data/lib/fog/brightbox/requests/compute/delete_server_group.rb +28 -0
  73. data/lib/fog/brightbox/requests/compute/delete_user_collaboration.rb +28 -0
  74. data/lib/fog/brightbox/requests/compute/get_account.rb +29 -0
  75. data/lib/fog/brightbox/requests/compute/get_api_client.rb +21 -0
  76. data/lib/fog/brightbox/requests/compute/get_application.rb +21 -0
  77. data/lib/fog/brightbox/requests/compute/get_authenticated_user.rb +17 -0
  78. data/lib/fog/brightbox/requests/compute/get_cloud_ip.rb +21 -0
  79. data/lib/fog/brightbox/requests/compute/get_collaboration.rb +21 -0
  80. data/lib/fog/brightbox/requests/compute/get_database_server.rb +19 -0
  81. data/lib/fog/brightbox/requests/compute/get_database_snapshot.rb +19 -0
  82. data/lib/fog/brightbox/requests/compute/get_database_type.rb +21 -0
  83. data/lib/fog/brightbox/requests/compute/get_firewall_policy.rb +21 -0
  84. data/lib/fog/brightbox/requests/compute/get_firewall_rule.rb +21 -0
  85. data/lib/fog/brightbox/requests/compute/get_image.rb +21 -0
  86. data/lib/fog/brightbox/requests/compute/get_interface.rb +21 -0
  87. data/lib/fog/brightbox/requests/compute/get_load_balancer.rb +21 -0
  88. data/lib/fog/brightbox/requests/compute/get_scoped_account.rb +15 -0
  89. data/lib/fog/brightbox/requests/compute/get_server.rb +21 -0
  90. data/lib/fog/brightbox/requests/compute/get_server_group.rb +21 -0
  91. data/lib/fog/brightbox/requests/compute/get_server_type.rb +21 -0
  92. data/lib/fog/brightbox/requests/compute/get_user.rb +29 -0
  93. data/lib/fog/brightbox/requests/compute/get_user_collaboration.rb +21 -0
  94. data/lib/fog/brightbox/requests/compute/get_zone.rb +21 -0
  95. data/lib/fog/brightbox/requests/compute/list_accounts.rb +17 -0
  96. data/lib/fog/brightbox/requests/compute/list_api_clients.rb +19 -0
  97. data/lib/fog/brightbox/requests/compute/list_applications.rb +19 -0
  98. data/lib/fog/brightbox/requests/compute/list_cloud_ips.rb +19 -0
  99. data/lib/fog/brightbox/requests/compute/list_collaborations.rb +19 -0
  100. data/lib/fog/brightbox/requests/compute/list_database_servers.rb +17 -0
  101. data/lib/fog/brightbox/requests/compute/list_database_snapshots.rb +17 -0
  102. data/lib/fog/brightbox/requests/compute/list_database_types.rb +19 -0
  103. data/lib/fog/brightbox/requests/compute/list_firewall_policies.rb +19 -0
  104. data/lib/fog/brightbox/requests/compute/list_images.rb +19 -0
  105. data/lib/fog/brightbox/requests/compute/list_load_balancers.rb +19 -0
  106. data/lib/fog/brightbox/requests/compute/list_server_groups.rb +19 -0
  107. data/lib/fog/brightbox/requests/compute/list_server_types.rb +19 -0
  108. data/lib/fog/brightbox/requests/compute/list_servers.rb +19 -0
  109. data/lib/fog/brightbox/requests/compute/list_user_collaborations.rb +19 -0
  110. data/lib/fog/brightbox/requests/compute/list_users.rb +19 -0
  111. data/lib/fog/brightbox/requests/compute/list_zones.rb +19 -0
  112. data/lib/fog/brightbox/requests/compute/map_cloud_ip.rb +24 -0
  113. data/lib/fog/brightbox/requests/compute/move_servers_server_group.rb +36 -0
  114. data/lib/fog/brightbox/requests/compute/reject_user_collaboration.rb +21 -0
  115. data/lib/fog/brightbox/requests/compute/remove_firewall_policy.rb +24 -0
  116. data/lib/fog/brightbox/requests/compute/remove_listeners_load_balancer.rb +24 -0
  117. data/lib/fog/brightbox/requests/compute/remove_nodes_load_balancer.rb +24 -0
  118. data/lib/fog/brightbox/requests/compute/remove_servers_server_group.rb +34 -0
  119. data/lib/fog/brightbox/requests/compute/resend_collaboration.rb +21 -0
  120. data/lib/fog/brightbox/requests/compute/reset_ftp_password_account.rb +32 -0
  121. data/lib/fog/brightbox/requests/compute/reset_ftp_password_scoped_account.rb +18 -0
  122. data/lib/fog/brightbox/requests/compute/reset_password_database_server.rb +19 -0
  123. data/lib/fog/brightbox/requests/compute/reset_secret_api_client.rb +25 -0
  124. data/lib/fog/brightbox/requests/compute/reset_secret_application.rb +21 -0
  125. data/lib/fog/brightbox/requests/compute/shutdown_server.rb +21 -0
  126. data/lib/fog/brightbox/requests/compute/snapshot_database_server.rb +19 -0
  127. data/lib/fog/brightbox/requests/compute/snapshot_server.rb +21 -0
  128. data/lib/fog/brightbox/requests/compute/start_server.rb +21 -0
  129. data/lib/fog/brightbox/requests/compute/stop_server.rb +21 -0
  130. data/lib/fog/brightbox/requests/compute/unmap_cloud_ip.rb +21 -0
  131. data/lib/fog/brightbox/requests/compute/update_account.rb +61 -0
  132. data/lib/fog/brightbox/requests/compute/update_api_client.rb +26 -0
  133. data/lib/fog/brightbox/requests/compute/update_application.rb +26 -0
  134. data/lib/fog/brightbox/requests/compute/update_cloud_ip.rb +27 -0
  135. data/lib/fog/brightbox/requests/compute/update_database_server.rb +25 -0
  136. data/lib/fog/brightbox/requests/compute/update_database_snapshot.rb +26 -0
  137. data/lib/fog/brightbox/requests/compute/update_firewall_policy.rb +26 -0
  138. data/lib/fog/brightbox/requests/compute/update_firewall_rule.rb +31 -0
  139. data/lib/fog/brightbox/requests/compute/update_image.rb +31 -0
  140. data/lib/fog/brightbox/requests/compute/update_load_balancer.rb +29 -0
  141. data/lib/fog/brightbox/requests/compute/update_scoped_account.rb +31 -0
  142. data/lib/fog/brightbox/requests/compute/update_server.rb +28 -0
  143. data/lib/fog/brightbox/requests/compute/update_server_group.rb +26 -0
  144. data/lib/fog/brightbox/requests/compute/update_user.rb +29 -0
  145. data/lib/fog/brightbox/version.rb +5 -0
  146. data/lib/fog/brightbox.rb +1 -0
  147. metadata +261 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e8e0a4337ce1090b15746deb6601ce046ab5a16
4
+ data.tar.gz: 6f1ed505b468ae1651713ef96e1d269583669501
5
+ SHA512:
6
+ metadata.gz: f834becf04d20fae09bb5492978320d47f5d67512ea8a8a6554c90943ba2bd7a7f102998cd473175da2ad310d3ca97ce28302e4be559fe7ad310ea502a8e6352
7
+ data.tar.gz: 35c867371334ce552d61429b20075cac591e87dcbb5a2b2819a6b8c87b51e10bae4d87dc37441dd5cb05084fc6eab562ae26546f4c151dd5571895888b8adfdf
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fog/brightbox/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fog-brightbox"
8
+ spec.version = Fog::Brightbox::VERSION
9
+ spec.authors = ["Paul Thornthwaite"]
10
+ spec.email = ["tokengeek@gmail.com"]
11
+ spec.description = %q{Module for the 'fog' gem to support Brightbox Cloud}
12
+ spec.summary = %q{This library can be used as a module for `fog` or as standalone provider
13
+ to use the Brightbox Cloud in applications}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "fog-core"
23
+ spec.add_dependency "fog-json"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "shindo"
28
+ end
@@ -0,0 +1,45 @@
1
+ module Fog
2
+ module Brightbox
3
+ module Compute
4
+ #
5
+ # This selects the preferred image to use based on a number of
6
+ # conditions
7
+ #
8
+ class ImageSelector
9
+ # Prepares a selector with the API output
10
+ #
11
+ # @param [Array<Hash>] images hash matching API output for {Fog::Compute::Brightbox#list_images}
12
+ #
13
+ def initialize(images)
14
+ @images = images
15
+ end
16
+
17
+ # Returns current identifier of the latest version of Ubuntu
18
+ #
19
+ # The order of preference is:
20
+ # * Only Official Brightbox images
21
+ # * Only Ubuntu images
22
+ # * Latest by name (alphanumeric sort)
23
+ # * Latest by creation date
24
+ #
25
+ # @note This performs a live query against the API
26
+ #
27
+ # @return [String] if image matches containing the identifier
28
+ # @return [NilClass] if no image matches
29
+ #
30
+ def latest_ubuntu
31
+ @images.select do |img|
32
+ img["official"] == true &&
33
+ img["arch"] == "i686" &&
34
+ img["name"] =~ /ubuntu/i
35
+ end.sort do |a, b|
36
+ # Reverse sort so "raring" > "precise" and "13.10" > "13.04"
37
+ b["name"].downcase <=> a["name"].downcase
38
+ end.first["id"]
39
+ rescue
40
+ nil
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,230 @@
1
+ require "fog/brightbox/oauth2"
2
+
3
+ module Fog
4
+ module Brightbox
5
+ module Compute
6
+
7
+ # The Shared module consists of code that was duplicated between the Real
8
+ # and Mock implementations.
9
+ #
10
+ module Shared
11
+ include Fog::Brightbox::OAuth2
12
+
13
+ API_URL = "https://api.gb1.brightbox.com/"
14
+
15
+ # Creates a new instance of the Brightbox Compute service
16
+ #
17
+ # @note If you create service using just a refresh token when it
18
+ # expires the service will no longer be able to authenticate.
19
+ #
20
+ # @param [Hash] options
21
+ # @option options [String] :brightbox_api_url
22
+ # Override the default (or configured) API endpoint
23
+ # @option options [String] :brightbox_auth_url
24
+ # Override the default (or configured) API authentication endpoint
25
+ # @option options [String] :brightbox_client_id
26
+ # Client identifier to authenticate with (overrides configured)
27
+ # @option options [String] :brightbox_secret
28
+ # Client secret to authenticate with (overrides configured)
29
+ # @option options [String] :brightbox_username
30
+ # Email or user identifier for user based authentication
31
+ # @option options [String] :brightbox_password
32
+ # Password for user based authentication
33
+ # @option options [String] :brightbox_account
34
+ # Account identifier to scope this connection to
35
+ # @option options [String] :connection_options
36
+ # Settings to pass to underlying {Fog::Core::Connection}
37
+ # @option options [Boolean] :persistent
38
+ # Sets a persistent HTTP {Fog::Core::Connection}
39
+ # @option options [String] :brightbox_access_token
40
+ # Sets the OAuth access token to use rather than requesting a new token
41
+ # @option options [String] :brightbox_refresh_token
42
+ # Sets the refresh token to use when requesting a newer access token
43
+ # @option options [String] (true) :brightbox_token_management
44
+ # Overide the existing behaviour to request access tokens if expired
45
+ #
46
+ def initialize(options)
47
+ # Currently authentication and api endpoints are the same but may change
48
+ @auth_url = options[:brightbox_auth_url] || Fog.credentials[:brightbox_auth_url] || API_URL
49
+ @auth_connection = Fog::Core::Connection.new(@auth_url)
50
+
51
+ @api_url = options[:brightbox_api_url] || Fog.credentials[:brightbox_api_url] || API_URL
52
+ @connection_options = options[:connection_options] || {}
53
+ @persistent = options[:persistent] || false
54
+ @connection = Fog::Core::Connection.new(@api_url, @persistent, @connection_options)
55
+
56
+ # Authentication options
57
+ client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
58
+ client_secret = options[:brightbox_secret] || Fog.credentials[:brightbox_secret]
59
+
60
+ username = options[:brightbox_username] || Fog.credentials[:brightbox_username]
61
+ password = options[:brightbox_password] || Fog.credentials[:brightbox_password]
62
+ @configured_account = options[:brightbox_account] || Fog.credentials[:brightbox_account]
63
+ # Request account can be changed at anytime and changes behaviour of future requests
64
+ @scoped_account = @configured_account
65
+
66
+ credential_options = { :username => username, :password => password }
67
+ @credentials = CredentialSet.new(client_id, client_secret, credential_options)
68
+
69
+ # If existing tokens have been cached, allow continued use of them in the service
70
+ @credentials.update_tokens(options[:brightbox_access_token], options[:brightbox_refresh_token])
71
+
72
+ @token_management = options.fetch(:brightbox_token_management, true)
73
+ end
74
+
75
+ # Sets the scoped account for future requests
76
+ # @param [String] scoped_account Identifier of the account to scope request to
77
+ def scoped_account=(scoped_account)
78
+ @scoped_account = scoped_account
79
+ end
80
+
81
+ # This returns the account identifier that the request should be scoped by
82
+ # based on the options passed to the request and current configuration
83
+ #
84
+ # @param [String] options_account Any identifier passed into the request
85
+ #
86
+ # @return [String, nil] The account identifier to scope the request to or nil
87
+ def scoped_account(options_account = nil)
88
+ [options_account, @scoped_account].compact.first
89
+ end
90
+
91
+ # Resets the scoped account back to intially configured one
92
+ def scoped_account_reset
93
+ @scoped_account = @configured_account
94
+ end
95
+
96
+ # Returns the scoped account being used for requests
97
+ #
98
+ # * For API clients this is the owning account
99
+ # * For User applications this is the account specified by either +account_id+
100
+ # option on the service or the +brightbox_account+ setting in your configuration
101
+ #
102
+ # @return [Fog::Compute::Brightbox::Account]
103
+ #
104
+ def account
105
+ account_data = get_scoped_account.merge(:service => self)
106
+ Fog::Compute::Brightbox::Account.new(account_data)
107
+ end
108
+
109
+ # Returns true if authentication is being performed as a user
110
+ # @return [Boolean]
111
+ def authenticating_as_user?
112
+ @credentials.user_details?
113
+ end
114
+
115
+ # Returns true if an access token is set
116
+ # @return [Boolean]
117
+ def access_token_available?
118
+ !! @credentials.access_token
119
+ end
120
+
121
+ # Returns the current access token or nil
122
+ # @return [String,nil]
123
+ def access_token
124
+ @credentials.access_token
125
+ end
126
+
127
+ # Returns the current refresh token or nil
128
+ # @return [String,nil]
129
+ def refresh_token
130
+ @credentials.refresh_token
131
+ end
132
+
133
+ # Returns the current token expiry time in seconds or nil
134
+ # @return [Number,nil]
135
+ def expires_in
136
+ @credentials.expires_in
137
+ end
138
+
139
+ # Requests a new access token
140
+ #
141
+ # @return [String] New access token
142
+ def get_access_token
143
+ begin
144
+ get_access_token!
145
+ rescue Excon::Errors::Unauthorized, Excon::Errors::BadRequest
146
+ @credentials.update_tokens(nil, nil)
147
+ end
148
+ @credentials.access_token
149
+ end
150
+
151
+ # Requests a new access token and raises if there is a problem
152
+ #
153
+ # @return [String] New access token
154
+ # @raise [Excon::Errors::BadRequest] The credentials are expired or incorrect
155
+ #
156
+ def get_access_token!
157
+ response = request_access_token(@auth_connection, @credentials)
158
+ update_credentials_from_response(@credentials, response)
159
+ @credentials.access_token
160
+ end
161
+
162
+ # Returns an identifier for the default image for use
163
+ #
164
+ # Currently tries to find the latest version of Ubuntu (i686) from
165
+ # Brightbox.
166
+ #
167
+ # Highly recommended that you actually select the image you want to run
168
+ # on your servers yourself!
169
+ #
170
+ # @return [String] if image is found, returns the identifier
171
+ # @return [NilClass] if no image is found or an error occurs
172
+ #
173
+ def default_image
174
+ return @default_image_id unless @default_image_id.nil?
175
+ @default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image
176
+ end
177
+
178
+ private
179
+
180
+ # This makes a request of the API based on the configured setting for
181
+ # token management.
182
+ #
183
+ # @param [Hash] options Excon compatible options
184
+ # @see https://github.com/geemus/excon/blob/master/lib/excon/connection.rb
185
+ #
186
+ # @return [Hash] Data of response body
187
+ #
188
+ def make_request(options)
189
+ if @token_management
190
+ managed_token_request(options)
191
+ else
192
+ authenticated_request(options)
193
+ end
194
+ end
195
+
196
+ # This request checks for access tokens and will ask for a new one if
197
+ # it receives Unauthorized from the API before repeating the request
198
+ #
199
+ # @param [Hash] options Excon compatible options
200
+ #
201
+ # @return [Excon::Response]
202
+ def managed_token_request(options)
203
+ get_access_token unless access_token_available?
204
+ authenticated_request(options)
205
+ rescue Excon::Errors::Unauthorized
206
+ get_access_token
207
+ authenticated_request(options)
208
+ end
209
+
210
+ # This request makes an authenticated request of the API using currently
211
+ # setup credentials.
212
+ #
213
+ # @param [Hash] options Excon compatible options
214
+ #
215
+ # @see https://github.com/geemus/excon/blob/master/lib/excon/connection.rb
216
+ #
217
+ # @return [Excon::Response]
218
+ def authenticated_request(options)
219
+ headers = options[:headers] || {}
220
+ headers.merge!("Authorization" => "OAuth #{@credentials.access_token}", "Content-Type" => "application/json")
221
+ options[:headers] = headers
222
+ # TODO: This is just a wrapper around a call to Excon::Connection#request
223
+ # so can be extracted from Compute by passing in the connection,
224
+ # credentials and options
225
+ @connection.request(options)
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1,305 @@
1
+ require 'fog/brightbox/core'
2
+ require 'fog/brightbox/compute/shared'
3
+ require 'fog/brightbox/compute/image_selector'
4
+
5
+ module Fog
6
+ module Compute
7
+ class Brightbox < Fog::Service
8
+
9
+ # Client credentials
10
+ requires :brightbox_client_id, :brightbox_secret
11
+
12
+ # API endpoint settings
13
+ recognizes :brightbox_auth_url, :brightbox_api_url
14
+
15
+ # User credentials (still requires client details)
16
+ recognizes :brightbox_username, :brightbox_password, :brightbox_account
17
+
18
+ # Cached tokens
19
+ recognizes :brightbox_access_token, :brightbox_refresh_token
20
+
21
+ # Automatic token management
22
+ recognizes :brightbox_token_management
23
+
24
+ # Excon connection settings
25
+ recognizes :persistent
26
+
27
+ model_path 'fog/brightbox/models/compute'
28
+ collection :accounts
29
+ model :account
30
+ collection :applications
31
+ model :application
32
+ collection :api_clients
33
+ model :api_client
34
+ collection :collaborations
35
+ model :collaboration
36
+ collection :servers
37
+ model :server
38
+ collection :server_groups
39
+ model :server_group
40
+ collection :firewall_policies
41
+ model :firewall_policy
42
+ collection :firewall_rules
43
+ model :firewall_rule
44
+ collection :flavors
45
+ model :flavor
46
+ collection :images
47
+ model :image
48
+ collection :load_balancers
49
+ model :load_balancer
50
+ collection :database_servers
51
+ model :database_server
52
+ collection :database_snapshots
53
+ model :database_snapshot
54
+ collection :database_types
55
+ model :database_type
56
+ collection :zones
57
+ model :zone
58
+ collection :cloud_ips
59
+ model :cloud_ip
60
+ collection :users
61
+ model :user
62
+ collection :user_collaborations
63
+ model :user_collaboration
64
+
65
+ request_path 'fog/brightbox/requests/compute'
66
+ request :accept_user_collaboration
67
+ request :activate_console_server
68
+ request :add_listeners_load_balancer
69
+ request :add_nodes_load_balancer
70
+ request :add_servers_server_group
71
+ request :apply_to_firewall_policy
72
+ request :accept_user_collaboration
73
+ request :remove_firewall_policy
74
+ request :create_api_client
75
+ request :create_application
76
+ request :create_cloud_ip
77
+ request :create_collaboration
78
+ request :create_firewall_policy
79
+ request :create_firewall_rule
80
+ request :create_image
81
+ request :create_load_balancer
82
+ request :create_database_server
83
+ request :create_server
84
+ request :create_server_group
85
+ request :delete_api_client
86
+ request :delete_application
87
+ request :delete_cloud_ip
88
+ request :delete_collaboration
89
+ request :delete_firewall_policy
90
+ request :delete_firewall_rule
91
+ request :delete_image
92
+ request :delete_load_balancer
93
+ request :delete_database_server
94
+ request :delete_database_snapshot
95
+ request :delete_server
96
+ request :delete_server_group
97
+ request :delete_user_collaboration
98
+ request :get_account
99
+ request :get_api_client
100
+ request :get_application
101
+ request :get_authenticated_user
102
+ request :get_cloud_ip
103
+ request :get_collaboration
104
+ request :get_firewall_policy
105
+ request :get_firewall_rule
106
+ request :get_image
107
+ request :get_interface
108
+ request :get_load_balancer
109
+ request :get_database_server
110
+ request :get_database_snapshot
111
+ request :get_database_type
112
+ request :get_scoped_account
113
+ request :get_server
114
+ request :get_server_group
115
+ request :get_server_type
116
+ request :get_user
117
+ request :get_user_collaboration
118
+ request :get_zone
119
+ request :list_accounts
120
+ request :list_api_clients
121
+ request :list_applications
122
+ request :list_cloud_ips
123
+ request :list_collaborations
124
+ request :list_firewall_policies
125
+ request :list_images
126
+ request :list_load_balancers
127
+ request :list_database_servers
128
+ request :list_database_snapshots
129
+ request :list_database_types
130
+ request :list_server_groups
131
+ request :list_server_types
132
+ request :list_servers
133
+ request :list_users
134
+ request :list_user_collaborations
135
+ request :list_zones
136
+ request :map_cloud_ip
137
+ request :move_servers_server_group
138
+ request :reject_user_collaboration
139
+ request :remove_listeners_load_balancer
140
+ request :remove_nodes_load_balancer
141
+ request :remove_servers_server_group
142
+ request :resend_collaboration
143
+ request :reset_ftp_password_account
144
+ request :reset_ftp_password_scoped_account
145
+ request :reset_password_database_server
146
+ request :reset_secret_api_client
147
+ request :reset_secret_application
148
+ request :resend_collaboration
149
+ request :reject_user_collaboration
150
+ request :shutdown_server
151
+ request :snapshot_database_server
152
+ request :snapshot_server
153
+ request :start_server
154
+ request :stop_server
155
+ request :unmap_cloud_ip
156
+ request :update_account
157
+ request :update_api_client
158
+ request :update_application
159
+ request :update_cloud_ip
160
+ request :update_firewall_policy
161
+ request :update_firewall_rule
162
+ request :update_image
163
+ request :update_load_balancer
164
+ request :update_database_server
165
+ request :update_database_snapshot
166
+ request :update_scoped_account
167
+ request :update_server
168
+ request :update_server_group
169
+ request :update_user
170
+
171
+ # The Mock Service allows you to run a fake instance of the Service
172
+ # which makes no real connections.
173
+ #
174
+ # @todo Implement
175
+ #
176
+ class Mock
177
+ include Fog::Brightbox::Compute::Shared
178
+
179
+ def request(method, path, expected_responses, parameters = {})
180
+ _request
181
+ end
182
+
183
+ def request_access_token(connection, credentials)
184
+ _request
185
+ end
186
+
187
+ private
188
+
189
+ def _request
190
+ raise Fog::Errors::MockNotImplemented
191
+ end
192
+
193
+ def select_default_image
194
+ "img-mockd"
195
+ end
196
+ end
197
+
198
+ # The Real Service actually makes real connections to the Brightbox
199
+ # service.
200
+ #
201
+ class Real
202
+ include Fog::Brightbox::Compute::Shared
203
+
204
+ # Makes an API request to the given path using passed options or those
205
+ # set with the service setup
206
+ #
207
+ # @todo Standard Fog behaviour is to return the Excon::Response but
208
+ # this was unintentionally changed to be the Hash version of the
209
+ # data in the body. This loses access to some details and should
210
+ # be corrected in a backwards compatible manner
211
+ #
212
+ # @overload request(params)
213
+ # @param [Hash] params Excon compatible options
214
+ # @option params [String] :body text to be sent over a socket
215
+ # @option params [Hash<Symbol, String>] :headers The default headers to supply in a request
216
+ # @option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
217
+ # @option params [String] :path appears after 'scheme://host:port/'
218
+ # @option params [Fixnum] :port The port on which to connect, to the destination host
219
+ # @option params [Hash] :query appended to the 'scheme://host:port/path/' in the form of '?key=value'
220
+ # @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
221
+ # @return [Excon::Response]
222
+ # @see https://github.com/geemus/excon/blob/master/lib/excon/connection.rb
223
+ #
224
+ # @overload request(method, path, expected_responses, params = {})
225
+ # @param [String] method HTTP method to use for the request
226
+ # @param [String] path The absolute path for the request
227
+ # @param [Array<Fixnum>] expected_responses HTTP response codes that have been successful
228
+ # @param [Hash] params Keys and values for JSON
229
+ # @option params [String] :account_id The scoping account if required
230
+ # @deprecated #request with multiple arguments is deprecated
231
+ # since it is inconsistent with original fog version.
232
+ # @return [Hash]
233
+ def request(*args)
234
+ if args.size == 1
235
+ authenticated_request(*args)
236
+ else
237
+ Fog::Logger.deprecation("#request with multiple parameters is deprecated, use #wrapped_request instead [light_black](#{caller.first})[/]")
238
+ wrapped_request(*args)
239
+ end
240
+ end
241
+
242
+ # Makes a request but with seperated arguments and parses the response to a hash
243
+ #
244
+ # @note #wrapped_request is the non-standard form of request introduced by mistake
245
+ #
246
+ # @param [String] method HTTP method to use for the request
247
+ # @param [String] path The absolute path for the request
248
+ # @param [Array<Fixnum>] expected_responses HTTP response codes that have been successful
249
+ # @param [Hash] parameters Keys and values for JSON
250
+ # @option parameters [String] :account_id The scoping account if required
251
+ #
252
+ # @return [Hash]
253
+ def wrapped_request(method, path, expected_responses, parameters = {})
254
+ _wrapped_request(method, path, expected_responses, parameters)
255
+ end
256
+
257
+ private
258
+
259
+ # Wrapped request is the non-standard form of request introduced by mistake
260
+ #
261
+ # @param [String] method HTTP method to use for the request
262
+ # @param [String] path The absolute path for the request
263
+ # @param [Array<Fixnum>] expected_responses HTTP response codes that have been successful
264
+ # @param [Hash] parameters Keys and values for JSON
265
+ # @option parameters [String] :account_id The scoping account if required
266
+ #
267
+ # @return [Hash]
268
+ def _wrapped_request(method, path, expected_responses, parameters = {})
269
+ request_options = {
270
+ :method => method.to_s.upcase,
271
+ :path => path,
272
+ :expects => expected_responses
273
+ }
274
+
275
+ # Select the account to scope for this request
276
+ account = scoped_account(parameters.fetch(:account_id, nil))
277
+ request_options[:query] = { :account_id => account } if account
278
+
279
+ request_options[:body] = Fog::JSON.encode(parameters) unless parameters.empty?
280
+
281
+ response = make_request(request_options)
282
+
283
+ # FIXME: We should revert to returning the Excon::Request after a suitable
284
+ # configuration option is in place to switch back to this incorrect behaviour
285
+ if response.body.empty?
286
+ response
287
+ else
288
+ Fog::JSON.decode(response.body)
289
+ end
290
+ end
291
+
292
+ # Queries the API and tries to select the most suitable official Image
293
+ # to use if the user chooses not to select their own.
294
+ #
295
+ # @return [String] if image is found, the image's identifier
296
+ # @return [NilClass] if no image found or an error occured
297
+ #
298
+ def select_default_image
299
+ Fog::Brightbox::Compute::ImageSelector.new(list_images).latest_ubuntu
300
+ end
301
+ end
302
+
303
+ end
304
+ end
305
+ end