fog-brightbox 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/fog-brightbox.gemspec +28 -0
- data/lib/fog/brightbox/compute/image_selector.rb +45 -0
- data/lib/fog/brightbox/compute/shared.rb +230 -0
- data/lib/fog/brightbox/compute.rb +305 -0
- data/lib/fog/brightbox/core.rb +11 -0
- data/lib/fog/brightbox/models/compute/account.rb +69 -0
- data/lib/fog/brightbox/models/compute/accounts.rb +29 -0
- data/lib/fog/brightbox/models/compute/api_client.rb +37 -0
- data/lib/fog/brightbox/models/compute/api_clients.rb +24 -0
- data/lib/fog/brightbox/models/compute/application.rb +24 -0
- data/lib/fog/brightbox/models/compute/applications.rb +28 -0
- data/lib/fog/brightbox/models/compute/cloud_ip.rb +68 -0
- data/lib/fog/brightbox/models/compute/cloud_ips.rb +34 -0
- data/lib/fog/brightbox/models/compute/collaboration.rb +49 -0
- data/lib/fog/brightbox/models/compute/collaborations.rb +47 -0
- data/lib/fog/brightbox/models/compute/database_server.rb +97 -0
- data/lib/fog/brightbox/models/compute/database_servers.rb +28 -0
- data/lib/fog/brightbox/models/compute/database_snapshot.rb +52 -0
- data/lib/fog/brightbox/models/compute/database_snapshots.rb +24 -0
- data/lib/fog/brightbox/models/compute/database_type.rb +19 -0
- data/lib/fog/brightbox/models/compute/database_types.rb +26 -0
- data/lib/fog/brightbox/models/compute/firewall_policies.rb +29 -0
- data/lib/fog/brightbox/models/compute/firewall_policy.rb +63 -0
- data/lib/fog/brightbox/models/compute/firewall_rule.rb +54 -0
- data/lib/fog/brightbox/models/compute/firewall_rules.rb +24 -0
- data/lib/fog/brightbox/models/compute/flavor.rb +32 -0
- data/lib/fog/brightbox/models/compute/flavors.rb +28 -0
- data/lib/fog/brightbox/models/compute/image.rb +64 -0
- data/lib/fog/brightbox/models/compute/images.rb +28 -0
- data/lib/fog/brightbox/models/compute/load_balancer.rb +107 -0
- data/lib/fog/brightbox/models/compute/load_balancers.rb +28 -0
- data/lib/fog/brightbox/models/compute/server.rb +217 -0
- data/lib/fog/brightbox/models/compute/server_group.rb +94 -0
- data/lib/fog/brightbox/models/compute/server_groups.rb +29 -0
- data/lib/fog/brightbox/models/compute/servers.rb +61 -0
- data/lib/fog/brightbox/models/compute/user.rb +43 -0
- data/lib/fog/brightbox/models/compute/user_collaboration.rb +44 -0
- data/lib/fog/brightbox/models/compute/user_collaborations.rb +31 -0
- data/lib/fog/brightbox/models/compute/users.rb +27 -0
- data/lib/fog/brightbox/models/compute/zone.rb +22 -0
- data/lib/fog/brightbox/models/compute/zones.rb +29 -0
- data/lib/fog/brightbox/oauth2.rb +166 -0
- data/lib/fog/brightbox/requests/compute/accept_user_collaboration.rb +21 -0
- data/lib/fog/brightbox/requests/compute/activate_console_server.rb +21 -0
- data/lib/fog/brightbox/requests/compute/add_listeners_load_balancer.rb +24 -0
- data/lib/fog/brightbox/requests/compute/add_nodes_load_balancer.rb +24 -0
- data/lib/fog/brightbox/requests/compute/add_servers_server_group.rb +34 -0
- data/lib/fog/brightbox/requests/compute/apply_to_firewall_policy.rb +24 -0
- data/lib/fog/brightbox/requests/compute/create_api_client.rb +23 -0
- data/lib/fog/brightbox/requests/compute/create_application.rb +23 -0
- data/lib/fog/brightbox/requests/compute/create_cloud_ip.rb +24 -0
- data/lib/fog/brightbox/requests/compute/create_collaboration.rb +23 -0
- data/lib/fog/brightbox/requests/compute/create_database_server.rb +25 -0
- data/lib/fog/brightbox/requests/compute/create_firewall_policy.rb +26 -0
- data/lib/fog/brightbox/requests/compute/create_firewall_rule.rb +29 -0
- data/lib/fog/brightbox/requests/compute/create_image.rb +30 -0
- data/lib/fog/brightbox/requests/compute/create_load_balancer.rb +26 -0
- data/lib/fog/brightbox/requests/compute/create_server.rb +29 -0
- data/lib/fog/brightbox/requests/compute/create_server_group.rb +23 -0
- data/lib/fog/brightbox/requests/compute/delete_api_client.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_application.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_cloud_ip.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_collaboration.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_database_server.rb +26 -0
- data/lib/fog/brightbox/requests/compute/delete_database_snapshot.rb +26 -0
- data/lib/fog/brightbox/requests/compute/delete_firewall_policy.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_firewall_rule.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_image.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_load_balancer.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_server.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_server_group.rb +28 -0
- data/lib/fog/brightbox/requests/compute/delete_user_collaboration.rb +28 -0
- data/lib/fog/brightbox/requests/compute/get_account.rb +29 -0
- data/lib/fog/brightbox/requests/compute/get_api_client.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_application.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_authenticated_user.rb +17 -0
- data/lib/fog/brightbox/requests/compute/get_cloud_ip.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_collaboration.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_database_server.rb +19 -0
- data/lib/fog/brightbox/requests/compute/get_database_snapshot.rb +19 -0
- data/lib/fog/brightbox/requests/compute/get_database_type.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_firewall_policy.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_firewall_rule.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_image.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_interface.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_load_balancer.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_scoped_account.rb +15 -0
- data/lib/fog/brightbox/requests/compute/get_server.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_server_group.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_server_type.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_user.rb +29 -0
- data/lib/fog/brightbox/requests/compute/get_user_collaboration.rb +21 -0
- data/lib/fog/brightbox/requests/compute/get_zone.rb +21 -0
- data/lib/fog/brightbox/requests/compute/list_accounts.rb +17 -0
- data/lib/fog/brightbox/requests/compute/list_api_clients.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_applications.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_cloud_ips.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_collaborations.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_database_servers.rb +17 -0
- data/lib/fog/brightbox/requests/compute/list_database_snapshots.rb +17 -0
- data/lib/fog/brightbox/requests/compute/list_database_types.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_firewall_policies.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_images.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_load_balancers.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_server_groups.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_server_types.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_servers.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_user_collaborations.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_users.rb +19 -0
- data/lib/fog/brightbox/requests/compute/list_zones.rb +19 -0
- data/lib/fog/brightbox/requests/compute/map_cloud_ip.rb +24 -0
- data/lib/fog/brightbox/requests/compute/move_servers_server_group.rb +36 -0
- data/lib/fog/brightbox/requests/compute/reject_user_collaboration.rb +21 -0
- data/lib/fog/brightbox/requests/compute/remove_firewall_policy.rb +24 -0
- data/lib/fog/brightbox/requests/compute/remove_listeners_load_balancer.rb +24 -0
- data/lib/fog/brightbox/requests/compute/remove_nodes_load_balancer.rb +24 -0
- data/lib/fog/brightbox/requests/compute/remove_servers_server_group.rb +34 -0
- data/lib/fog/brightbox/requests/compute/resend_collaboration.rb +21 -0
- data/lib/fog/brightbox/requests/compute/reset_ftp_password_account.rb +32 -0
- data/lib/fog/brightbox/requests/compute/reset_ftp_password_scoped_account.rb +18 -0
- data/lib/fog/brightbox/requests/compute/reset_password_database_server.rb +19 -0
- data/lib/fog/brightbox/requests/compute/reset_secret_api_client.rb +25 -0
- data/lib/fog/brightbox/requests/compute/reset_secret_application.rb +21 -0
- data/lib/fog/brightbox/requests/compute/shutdown_server.rb +21 -0
- data/lib/fog/brightbox/requests/compute/snapshot_database_server.rb +19 -0
- data/lib/fog/brightbox/requests/compute/snapshot_server.rb +21 -0
- data/lib/fog/brightbox/requests/compute/start_server.rb +21 -0
- data/lib/fog/brightbox/requests/compute/stop_server.rb +21 -0
- data/lib/fog/brightbox/requests/compute/unmap_cloud_ip.rb +21 -0
- data/lib/fog/brightbox/requests/compute/update_account.rb +61 -0
- data/lib/fog/brightbox/requests/compute/update_api_client.rb +26 -0
- data/lib/fog/brightbox/requests/compute/update_application.rb +26 -0
- data/lib/fog/brightbox/requests/compute/update_cloud_ip.rb +27 -0
- data/lib/fog/brightbox/requests/compute/update_database_server.rb +25 -0
- data/lib/fog/brightbox/requests/compute/update_database_snapshot.rb +26 -0
- data/lib/fog/brightbox/requests/compute/update_firewall_policy.rb +26 -0
- data/lib/fog/brightbox/requests/compute/update_firewall_rule.rb +31 -0
- data/lib/fog/brightbox/requests/compute/update_image.rb +31 -0
- data/lib/fog/brightbox/requests/compute/update_load_balancer.rb +29 -0
- data/lib/fog/brightbox/requests/compute/update_scoped_account.rb +31 -0
- data/lib/fog/brightbox/requests/compute/update_server.rb +28 -0
- data/lib/fog/brightbox/requests/compute/update_server_group.rb +26 -0
- data/lib/fog/brightbox/requests/compute/update_user.rb +29 -0
- data/lib/fog/brightbox/version.rb +5 -0
- data/lib/fog/brightbox.rb +1 -0
- metadata +261 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'fog/core/collection'
|
|
2
|
+
require 'fog/brightbox/models/compute/server_group'
|
|
3
|
+
|
|
4
|
+
module Fog
|
|
5
|
+
module Compute
|
|
6
|
+
class Brightbox
|
|
7
|
+
|
|
8
|
+
class ServerGroups < Fog::Collection
|
|
9
|
+
|
|
10
|
+
model Fog::Compute::Brightbox::ServerGroup
|
|
11
|
+
|
|
12
|
+
def all
|
|
13
|
+
data = service.list_server_groups
|
|
14
|
+
load(data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def get(identifier)
|
|
18
|
+
return nil if identifier.nil? || identifier == ""
|
|
19
|
+
data = service.get_server_group(identifier)
|
|
20
|
+
new(data)
|
|
21
|
+
rescue Excon::Errors::NotFound
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require 'fog/core/collection'
|
|
2
|
+
require 'fog/brightbox/models/compute/server'
|
|
3
|
+
|
|
4
|
+
module Fog
|
|
5
|
+
module Compute
|
|
6
|
+
class Brightbox
|
|
7
|
+
|
|
8
|
+
class Servers < Fog::Collection
|
|
9
|
+
|
|
10
|
+
model Fog::Compute::Brightbox::Server
|
|
11
|
+
|
|
12
|
+
def all
|
|
13
|
+
data = service.list_servers
|
|
14
|
+
load(data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Creates a server and maps an Cloud IP
|
|
18
|
+
#
|
|
19
|
+
# By default the public SSH key you have registered with
|
|
20
|
+
# Brightbox is already made available in an AWS compatible
|
|
21
|
+
# metdata service.
|
|
22
|
+
#
|
|
23
|
+
# @todo Support uploading of arbitary SSH keys
|
|
24
|
+
#
|
|
25
|
+
# @param [Hash] options
|
|
26
|
+
# @option options [String] name Name for the server
|
|
27
|
+
# @option options [String] flavor_id Identifier for virtual hardware type to request
|
|
28
|
+
# @option options [String] image_id Identifier for image to use when creating
|
|
29
|
+
# @option options [String] zone_id Identifer for preferred zone to locate server in
|
|
30
|
+
# @option options [Array<String>] server_groups List of group identifiers for the server to join
|
|
31
|
+
#
|
|
32
|
+
# @return Fog::Compute::Brightbox::Server
|
|
33
|
+
#
|
|
34
|
+
def bootstrap(options = {})
|
|
35
|
+
server = create(options)
|
|
36
|
+
|
|
37
|
+
# Ensure server is now available
|
|
38
|
+
server.wait_for { ready? }
|
|
39
|
+
|
|
40
|
+
# To get a public IP address we need to map a cloud IP address
|
|
41
|
+
cip = service.cloud_ips.allocate
|
|
42
|
+
cip.map(server)
|
|
43
|
+
cip.wait_for { mapped? }
|
|
44
|
+
|
|
45
|
+
# Reload so the public IP is now available
|
|
46
|
+
server.reload
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def get(identifier)
|
|
50
|
+
return nil if identifier.nil? || identifier == ""
|
|
51
|
+
data = service.get_server(identifier)
|
|
52
|
+
new(data)
|
|
53
|
+
rescue Excon::Errors::NotFound
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'fog/core/model'
|
|
2
|
+
|
|
3
|
+
module Fog
|
|
4
|
+
module Compute
|
|
5
|
+
class Brightbox
|
|
6
|
+
|
|
7
|
+
class User < Fog::Model
|
|
8
|
+
|
|
9
|
+
identity :id
|
|
10
|
+
attribute :resource_type
|
|
11
|
+
attribute :url
|
|
12
|
+
|
|
13
|
+
attribute :name
|
|
14
|
+
attribute :email_address
|
|
15
|
+
attribute :ssh_key
|
|
16
|
+
|
|
17
|
+
# Boolean flags
|
|
18
|
+
attribute :email_verified
|
|
19
|
+
attribute :messaging_pref
|
|
20
|
+
|
|
21
|
+
# Links - to be replaced
|
|
22
|
+
attribute :account_id, :aliases => "default_account", :squash => "id"
|
|
23
|
+
attribute :accounts
|
|
24
|
+
|
|
25
|
+
def save
|
|
26
|
+
requires :identity
|
|
27
|
+
|
|
28
|
+
options = {
|
|
29
|
+
:email_address => email_address,
|
|
30
|
+
:ssh_key => ssh_key,
|
|
31
|
+
:name => name
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
data = service.update_user(identity, options)
|
|
35
|
+
merge_attributes(data)
|
|
36
|
+
true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'fog/core/model'
|
|
2
|
+
|
|
3
|
+
module Fog
|
|
4
|
+
module Compute
|
|
5
|
+
class Brightbox
|
|
6
|
+
class UserCollaboration < Fog::Model
|
|
7
|
+
identity :id
|
|
8
|
+
attribute :status
|
|
9
|
+
attribute :email
|
|
10
|
+
attribute :role
|
|
11
|
+
attribute :role_label
|
|
12
|
+
attribute :account
|
|
13
|
+
attribute :user
|
|
14
|
+
attribute :inviter
|
|
15
|
+
|
|
16
|
+
def account_id
|
|
17
|
+
account['id'] || account[:id]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def accept
|
|
21
|
+
requires :identity
|
|
22
|
+
data = service.accept_user_collaboration(identity)
|
|
23
|
+
merge_attributes(data)
|
|
24
|
+
true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def reject
|
|
28
|
+
requires :identity
|
|
29
|
+
data = service.reject_user_collaboration(identity)
|
|
30
|
+
merge_attributes(data)
|
|
31
|
+
true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def destroy
|
|
35
|
+
requires :identity
|
|
36
|
+
data = service.destroy_user_collaboration(identity)
|
|
37
|
+
merge_attributes(data)
|
|
38
|
+
true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require "fog/core/collection"
|
|
2
|
+
require "fog/brightbox/models/compute/user_collaboration"
|
|
3
|
+
|
|
4
|
+
module Fog
|
|
5
|
+
module Compute
|
|
6
|
+
class Brightbox
|
|
7
|
+
class UserCollaborations < Fog::Collection
|
|
8
|
+
model Fog::Compute::Brightbox::UserCollaboration
|
|
9
|
+
|
|
10
|
+
def all
|
|
11
|
+
data = service.list_user_collaborations
|
|
12
|
+
load(data)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def get(identifier)
|
|
16
|
+
return nil if identifier.nil? || identifier == ""
|
|
17
|
+
data = service.get_user_collaboration(identifier)
|
|
18
|
+
new(data)
|
|
19
|
+
rescue Excon::Errors::NotFound
|
|
20
|
+
nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def destroy
|
|
24
|
+
requires :identity
|
|
25
|
+
service.destroy_user_collaboration(identity)
|
|
26
|
+
true
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'fog/core/collection'
|
|
2
|
+
require 'fog/brightbox/models/compute/user'
|
|
3
|
+
|
|
4
|
+
module Fog
|
|
5
|
+
module Compute
|
|
6
|
+
class Brightbox
|
|
7
|
+
|
|
8
|
+
class Users < Fog::Collection
|
|
9
|
+
|
|
10
|
+
model Fog::Compute::Brightbox::User
|
|
11
|
+
|
|
12
|
+
def all
|
|
13
|
+
data = service.list_users
|
|
14
|
+
load(data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def get(identifier = nil)
|
|
18
|
+
data = service.get_user(identifier)
|
|
19
|
+
new(data)
|
|
20
|
+
rescue Excon::Errors::NotFound
|
|
21
|
+
nil
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'fog/core/model'
|
|
2
|
+
|
|
3
|
+
module Fog
|
|
4
|
+
module Compute
|
|
5
|
+
class Brightbox
|
|
6
|
+
|
|
7
|
+
class Zone < Fog::Model
|
|
8
|
+
|
|
9
|
+
identity :id
|
|
10
|
+
attribute :url
|
|
11
|
+
attribute :resource_type
|
|
12
|
+
|
|
13
|
+
attribute :status
|
|
14
|
+
attribute :handle
|
|
15
|
+
|
|
16
|
+
attribute :description
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'fog/core/collection'
|
|
2
|
+
require 'fog/brightbox/models/compute/zone'
|
|
3
|
+
|
|
4
|
+
module Fog
|
|
5
|
+
module Compute
|
|
6
|
+
class Brightbox
|
|
7
|
+
|
|
8
|
+
class Zones < Fog::Collection
|
|
9
|
+
|
|
10
|
+
model Fog::Compute::Brightbox::Zone
|
|
11
|
+
|
|
12
|
+
def all
|
|
13
|
+
data = service.list_zones
|
|
14
|
+
load(data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def get(identifier)
|
|
18
|
+
return nil if identifier.nil? || identifier == ""
|
|
19
|
+
data = service.get_zone(identifier)
|
|
20
|
+
new(data)
|
|
21
|
+
rescue Excon::Errors::NotFound
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# This module covers Brightbox's partial implementation of OAuth 2.0
|
|
2
|
+
# and enables fog clients to implement several authentictication strategies
|
|
3
|
+
#
|
|
4
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-10
|
|
5
|
+
#
|
|
6
|
+
module Fog::Brightbox::OAuth2
|
|
7
|
+
|
|
8
|
+
# This builds the simplest form of requesting an access token
|
|
9
|
+
# based on the arguments passed in
|
|
10
|
+
#
|
|
11
|
+
# @param [Fog::Core::Connection] connection
|
|
12
|
+
# @param [CredentialSet] credentials
|
|
13
|
+
#
|
|
14
|
+
# @return [Excon::Response]
|
|
15
|
+
def request_access_token(connection, credentials)
|
|
16
|
+
token_strategy = credentials.best_grant_strategy
|
|
17
|
+
|
|
18
|
+
header_content = "#{credentials.client_id}:#{credentials.client_secret}"
|
|
19
|
+
encoded_credentials = Base64.encode64(header_content).chomp
|
|
20
|
+
|
|
21
|
+
connection.request(
|
|
22
|
+
:path => "/token",
|
|
23
|
+
:expects => 200,
|
|
24
|
+
:headers => {
|
|
25
|
+
'Authorization' => "Basic #{encoded_credentials}",
|
|
26
|
+
'Content-Type' => 'application/json'
|
|
27
|
+
},
|
|
28
|
+
:method => 'POST',
|
|
29
|
+
:body => Fog::JSON.encode(token_strategy.authorization_body_data)
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Encapsulates credentials required to request access tokens from the
|
|
34
|
+
# Brightbox authorisation servers
|
|
35
|
+
#
|
|
36
|
+
# @todo Interface to update certain credentials (after password change)
|
|
37
|
+
#
|
|
38
|
+
class CredentialSet
|
|
39
|
+
attr_reader :client_id, :client_secret, :username, :password
|
|
40
|
+
attr_reader :access_token, :refresh_token, :expires_in
|
|
41
|
+
#
|
|
42
|
+
# @param [String] client_id
|
|
43
|
+
# @param [String] client_secret
|
|
44
|
+
# @param [Hash] options
|
|
45
|
+
# @option options [String] :username
|
|
46
|
+
# @option options [String] :password
|
|
47
|
+
#
|
|
48
|
+
def initialize(client_id, client_secret, options = {})
|
|
49
|
+
@client_id = client_id
|
|
50
|
+
@client_secret = client_secret
|
|
51
|
+
@username = options[:username]
|
|
52
|
+
@password = options[:password]
|
|
53
|
+
@access_token = options[:access_token]
|
|
54
|
+
@refresh_token = options[:refresh_token]
|
|
55
|
+
@expires_in = options[:expires_in]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Returns true if user details are available
|
|
59
|
+
# @return [Boolean]
|
|
60
|
+
def user_details?
|
|
61
|
+
!!(@username && @password)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Is an access token available for these credentials?
|
|
65
|
+
def access_token?
|
|
66
|
+
!!@access_token
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Is a refresh token available for these credentials?
|
|
70
|
+
def refresh_token?
|
|
71
|
+
!!@refresh_token
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Updates the credentials with newer tokens
|
|
75
|
+
def update_tokens(access_token, refresh_token = nil, expires_in = nil)
|
|
76
|
+
@access_token = access_token
|
|
77
|
+
@refresh_token = refresh_token
|
|
78
|
+
@expires_in = expires_in
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Based on available credentials returns the best strategy
|
|
82
|
+
#
|
|
83
|
+
# @todo Add a means to dictate which should or shouldn't be used
|
|
84
|
+
#
|
|
85
|
+
def best_grant_strategy
|
|
86
|
+
if refresh_token?
|
|
87
|
+
RefreshTokenStrategy.new(self)
|
|
88
|
+
elsif user_details?
|
|
89
|
+
UserCredentialsStrategy.new(self)
|
|
90
|
+
else
|
|
91
|
+
ClientCredentialsStrategy.new(self)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# This strategy class is the basis for OAuth2 grant types
|
|
97
|
+
#
|
|
98
|
+
# @abstract Need to implement {#authorization_body_data} to return a
|
|
99
|
+
# Hash matching the expected parameter form for the OAuth request
|
|
100
|
+
#
|
|
101
|
+
# @todo Strategies should be able to validate if credentials are suitable
|
|
102
|
+
# so just client credentials cannot be used with user strategies
|
|
103
|
+
#
|
|
104
|
+
class GrantTypeStrategy
|
|
105
|
+
def initialize(credentials)
|
|
106
|
+
@credentials = credentials
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def authorization_body_data
|
|
110
|
+
raise "Not implemented"
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# This implements client based authentication/authorization
|
|
115
|
+
# based on the existing trust relationship using the `none`
|
|
116
|
+
# grant type.
|
|
117
|
+
#
|
|
118
|
+
class ClientCredentialsStrategy < GrantTypeStrategy
|
|
119
|
+
def authorization_body_data
|
|
120
|
+
{
|
|
121
|
+
"grant_type" => "none",
|
|
122
|
+
"client_id" => @credentials.client_id
|
|
123
|
+
}
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# This passes user details through so the returned token
|
|
128
|
+
# carries the privileges of the user not account limited
|
|
129
|
+
# by the client
|
|
130
|
+
#
|
|
131
|
+
class UserCredentialsStrategy < GrantTypeStrategy
|
|
132
|
+
def authorization_body_data
|
|
133
|
+
{
|
|
134
|
+
"grant_type" => "password",
|
|
135
|
+
"client_id" => @credentials.client_id,
|
|
136
|
+
"username" => @credentials.username,
|
|
137
|
+
"password" => @credentials.password
|
|
138
|
+
}
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# This strategy attempts to use a refresh_token gained during an earlier
|
|
143
|
+
# request to reuse the credentials given originally
|
|
144
|
+
#
|
|
145
|
+
class RefreshTokenStrategy < GrantTypeStrategy
|
|
146
|
+
def authorization_body_data
|
|
147
|
+
{
|
|
148
|
+
"grant_type" => "refresh_token",
|
|
149
|
+
"client_id" => @credentials.client_id,
|
|
150
|
+
"refresh_token" => @credentials.refresh_token
|
|
151
|
+
}
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
private
|
|
156
|
+
|
|
157
|
+
# This updates the current credentials if passed a valid response
|
|
158
|
+
#
|
|
159
|
+
# @param [CredentialSet] credentials Credentials to update
|
|
160
|
+
# @param [Excon::Response] response Response object to parse value from
|
|
161
|
+
#
|
|
162
|
+
def update_credentials_from_response(credentials, response)
|
|
163
|
+
response_data = Fog::JSON.decode(response.body)
|
|
164
|
+
credentials.update_tokens(response_data["access_token"], response_data["refresh_token"], response_data["expires_in"])
|
|
165
|
+
end
|
|
166
|
+
end
|