profitbricks-sdk-ruby 3.0.2 → 4.0.0

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.
data/lib/profitbricks.rb CHANGED
@@ -22,4 +22,10 @@ module ProfitBricks
22
22
  require 'profitbricks/location'
23
23
  require 'profitbricks/request'
24
24
  require 'profitbricks/wait_for'
25
+ require 'profitbricks/contract'
26
+ require 'profitbricks/group'
27
+ require 'profitbricks/user'
28
+ require 'profitbricks/share'
29
+ require 'profitbricks/resource'
30
+ require 'profitbricks/usermanagement'
25
31
  end
@@ -0,0 +1,29 @@
1
+ module ProfitBricks
2
+ # Contract Resource class
3
+ class Contract < ProfitBricks::Model
4
+
5
+ class << self
6
+ # Retrieve a contract.
7
+ def get(options = {})
8
+ response = ProfitBricks.request(
9
+ method: :get,
10
+ path: "/contracts",
11
+ expects: 200,
12
+ query: options
13
+ )
14
+ instantiate_objects(response)
15
+ end
16
+
17
+ # Retrieve a contract by contract number.
18
+ def get_by_contract_id(contract_id)
19
+ response = ProfitBricks.request(
20
+ method: :get,
21
+ path: "/contracts",
22
+ headers: { 'PB-Contract-Number' => "#{contract_id}" },
23
+ expects: 200
24
+ )
25
+ instantiate_objects(response)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -84,10 +84,32 @@ module ProfitBricks
84
84
 
85
85
  class << self
86
86
  def create(options = {})
87
+ entities = {}
88
+
89
+ # Retrieve servers collection if present and generate appropriate JSON.
90
+ if options.key?(:servers)
91
+ entities[:servers] = collect_entities(options.delete(:servers))
92
+ end
93
+
94
+ # Retrieve volumes collection if present and generate appropriate JSON.
95
+ if options.key?(:volumes)
96
+ entities[:volumes] = collect_entities(options.delete(:volumes))
97
+ end
98
+
99
+ # Retrieve volumes collection if present and generate appropriate JSON.
100
+ if options.key?(:loadbalancers)
101
+ entities[:loadbalancers] = collect_entities(options.delete(:loadbalancers))
102
+ end
103
+
104
+ # Retrieve volumes collection if present and generate appropriate JSON.
105
+ if options.key?(:lans)
106
+ entities[:lans] = collect_entities(options.delete(:lans))
107
+ end
108
+
87
109
  response = ProfitBricks.request(
88
110
  method: :post,
89
111
  path: '/datacenters',
90
- body: { properties: options }.to_json,
112
+ body: { properties: options, entities: entities }.to_json,
91
113
  expects: 202
92
114
  )
93
115
  instantiate_objects(response)
@@ -111,5 +133,25 @@ module ProfitBricks
111
133
  instantiate_objects(response)
112
134
  end
113
135
  end
136
+
137
+ private
138
+
139
+ def self.collect_entities(entities)
140
+ if entities.is_a?(Array) && entities.length > 0
141
+ items = []
142
+ entities.each do |entity|
143
+ if entity.key?(:volumes)
144
+ subentities = collect_entities(entity.delete(:volumes))
145
+ items << {
146
+ properties: entity,
147
+ entities: { volumes: subentities }
148
+ }
149
+ else
150
+ items << { properties: entity }
151
+ end
152
+ end
153
+ { items: items }
154
+ end
155
+ end
114
156
  end
115
157
  end
@@ -2,5 +2,6 @@ module ProfitBricks
2
2
  class Errors
3
3
  class Error < StandardError; end
4
4
  class OperationFailure < StandardError; end
5
+ class Unauthorized < StandardError; end
5
6
  end
6
7
  end
@@ -0,0 +1,76 @@
1
+ module ProfitBricks
2
+ # Group class
3
+ class Group < ProfitBricks::Model
4
+ # Delete the group.
5
+ def delete
6
+ response = ProfitBricks.request(
7
+ method: :delete,
8
+ path: "/um/groups/#{id}",
9
+ expects: 202
10
+ )
11
+ self.requestId = response[:requestId]
12
+ self
13
+ end
14
+
15
+ # Update the group.
16
+ def update(options = {})
17
+ response = ProfitBricks.request(
18
+ method: :put,
19
+ path: "/um/groups/#{id}",
20
+ expects: 202,
21
+ body: { properties: options }.to_json
22
+ )
23
+ if response
24
+ self.requestId = response['requestId']
25
+ @properties = @properties.merge(response['properties'])
26
+ end
27
+ self
28
+ end
29
+
30
+ # Add an user to the group
31
+ def add_user(user_id)
32
+ ProfitBricks::User.add_to_group(id, user_id)
33
+ end
34
+
35
+ # Remove an user from the group
36
+ def remove_user(user_id)
37
+ ProfitBricks::User.remove_from_group(id, user_id)
38
+ end
39
+
40
+ class << self
41
+ # Create a new group.
42
+ def create(options = {})
43
+ response = ProfitBricks.request(
44
+ method: :post,
45
+ path: '/um/groups/',
46
+ expects: 202,
47
+ body: { properties: options }.to_json
48
+ )
49
+ add_parent_identities(response)
50
+ instantiate_objects(response)
51
+ end
52
+
53
+ # List all groups.
54
+ def list(options = {})
55
+ response = ProfitBricks.request(
56
+ method: :get,
57
+ path: '/um/groups/',
58
+ expects: 200,
59
+ query: options
60
+ )
61
+ instantiate_objects(response)
62
+ end
63
+
64
+ # Retrieve a group.
65
+ def get(group_id,options = {})
66
+ response = ProfitBricks.request(
67
+ method: :get,
68
+ path: "/um/groups/#{group_id}",
69
+ expects: 200,
70
+ query: options
71
+ )
72
+ instantiate_objects(response)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,12 +1,11 @@
1
1
  module ProfitBricks
2
2
  # Image class
3
3
  class Image < ProfitBricks::Model
4
-
5
4
  # Delete the image.
6
5
  def delete
7
6
  ProfitBricks.request(
8
7
  method: :delete,
9
- path: "/images/#{self.id}",
8
+ path: "/images/#{id}",
10
9
  expects: 202
11
10
  )
12
11
  end
@@ -15,7 +14,7 @@ module ProfitBricks
15
14
  def update(options = {})
16
15
  response = ProfitBricks.request(
17
16
  method: :patch,
18
- path: "/images/#{self.id}",
17
+ path: "/images/#{id}",
19
18
  expects: 202,
20
19
  body: options.to_json
21
20
  )
@@ -27,13 +26,14 @@ module ProfitBricks
27
26
  end
28
27
 
29
28
  class << self
30
-
31
29
  # List all images.
32
- def list
30
+ def list(options = {})
31
+
33
32
  response = ProfitBricks.request(
34
33
  method: :get,
35
- path: '/images',
36
- expects: 200
34
+ path: '/images/',
35
+ expects: 200,
36
+ query: options
37
37
  )
38
38
  instantiate_objects(response)
39
39
  end
@@ -28,25 +28,20 @@ module ProfitBricks
28
28
  self
29
29
  end
30
30
 
31
- # List LAN members.
32
- def list_members
33
- response = ProfitBricks.request(
34
- method: :get,
35
- path: "/datacenters/#{self.datacenterId}/lans/#{self.id}/nics",
36
- expects: 200,
37
- )
38
- self.class.instantiate_objects(response)
39
- end
40
-
41
31
  class << self
42
32
 
43
33
  # Create a new LAN.
44
34
  def create(datacenter_id, options = {})
35
+ entities = {}
36
+ # Retrieve nics collection if present and generate appropriate JSON.
37
+ if options.key?('nics')
38
+ entities[:nics] =collect_entities(options.delete('nics'))
39
+ end
45
40
  response = ProfitBricks.request(
46
41
  method: :post,
47
42
  path: "/datacenters/#{datacenter_id}/lans",
48
43
  expects: 202,
49
- body: { properties: options }.to_json
44
+ body: { properties: options, entities: entities }.to_json
50
45
  )
51
46
  add_parent_identities(response)
52
47
  instantiate_objects(response)
@@ -74,5 +69,17 @@ module ProfitBricks
74
69
  instantiate_objects(response)
75
70
  end
76
71
  end
72
+
73
+ private
74
+
75
+ def self.collect_entities(entities)
76
+ items = []
77
+ if entities.is_a?(Array) && entities.length > 0
78
+ entities.each do |entity|
79
+ items << { id: entity }
80
+ end
81
+ {items: items}
82
+ end
83
+ end
77
84
  end
78
85
  end
@@ -27,8 +27,14 @@ module ProfitBricks
27
27
 
28
28
  def reload
29
29
  # Remove URL host and prefix path from href.
30
+ # Example: server
30
31
  path = URI(self.href).path
32
+ # => /cloudapi/v3/datacenters/UUID/servers/UUID
31
33
  path.sub!(ProfitBricks::Config.path_prefix, '')
34
+ # => //datacenters/UUID/servers/UUID
35
+ # => raises Excon::Error::NotFound
36
+ path.sub!(/\/{2,}/, '/')
37
+ # => /datacenters/UUID/servers/UUID
32
38
 
33
39
  response = ProfitBricks.request(
34
40
  method: :get,
@@ -9,21 +9,20 @@ module ProfitBricks
9
9
  ProfitBricks::Config.debug = false
10
10
  ProfitBricks::Config.protocol = 'https'
11
11
  ProfitBricks::Config.port = '443'
12
- ProfitBricks::Config.path_prefix = 'cloudapi/v3'
12
+ ProfitBricks::Config.path_prefix = 'cloudapi/v4'
13
13
  yield ProfitBricks::Config
14
14
 
15
15
  if ProfitBricks::Config.host
16
16
  url = construct_url
17
17
  else
18
- url = ProfitBricks::Config.url || 'https://api.profitbricks.com/cloudapi/v3'
18
+ url = ProfitBricks::Config.url || 'https://api.profitbricks.com/cloudapi/v4'
19
19
  end
20
20
 
21
21
  params = {
22
22
  user: ProfitBricks::Config.username,
23
23
  password: ProfitBricks::Config.password,
24
24
  debug: ProfitBricks::Config.debug,
25
- omit_default_port: true,
26
- query: { depth: ProfitBricks::Config.depth }
25
+ omit_default_port: true
27
26
  }
28
27
 
29
28
  @client = Excon.new(url, params)
@@ -49,6 +48,7 @@ module ProfitBricks
49
48
  def self.request(params)
50
49
  begin
51
50
  params = add_headers(params)
51
+ ProfitBricks.client.params[:query] = append_query(params)
52
52
  response = ProfitBricks.client.request(prepend_path_prefix(params))
53
53
  rescue Excon::Errors::Unauthorized => error
54
54
  raise error, parse_json(error.response.body)['messages']
@@ -56,6 +56,8 @@ module ProfitBricks
56
56
  raise error, parse_json(error.response.body)['messages']
57
57
  rescue Excon::Errors::InternalServerError => error
58
58
  raise error, parse_json(error.response.body)['messages']
59
+ rescue Excon::Error::Forbidden => error
60
+ raise error, parse_json(error.response.body)['messages']
59
61
  end
60
62
  add_request_id(response)
61
63
  end
@@ -91,10 +93,10 @@ module ProfitBricks
91
93
  params[:headers] ||= {}
92
94
  params[:headers].merge!(ProfitBricks::Config.headers) if ProfitBricks::Config.headers
93
95
 
94
- if params[:headers]["User-Agent"]
95
- params[:headers]["User-Agent"] = "profitbricks-ruby-sdk/#{ProfitBricks::VERSION} " + params[:headers]["User-Agent"]
96
+ if params[:headers]['User-Agent']
97
+ params[:headers]['User-Agent'] = "profitbricks-ruby-sdk/#{ProfitBricks::VERSION} " + params[:headers]['User-Agent']
96
98
  else
97
- params[:headers]["User-Agent"] = "profitbricks-ruby-sdk/#{ProfitBricks::VERSION}"
99
+ params[:headers]['User-Agent'] = "profitbricks-ruby-sdk/#{ProfitBricks::VERSION}"
98
100
  end
99
101
 
100
102
  unless params[:headers].key?('Content-Type')
@@ -116,6 +118,11 @@ module ProfitBricks
116
118
  params
117
119
  end
118
120
 
121
+ def self.append_query(params)
122
+ params[:query] ||= {}
123
+ params[:query].merge!(depth: ProfitBricks::Config.depth) if !params[:query][:depth]
124
+ end
125
+
119
126
  def self.construct_url
120
127
  "#{ProfitBricks::Config.protocol}://" \
121
128
  "#{ProfitBricks::Config.host}:" \
@@ -0,0 +1,42 @@
1
+ module ProfitBricks
2
+ # Resource class
3
+ class Resource < ProfitBricks::Model
4
+ class << self
5
+ # List all resources.
6
+ def list(options = {})
7
+ response = ProfitBricks.request(
8
+ method: :get,
9
+ path: '/um/resources',
10
+ expects: 200,
11
+ query: options
12
+ )
13
+ add_parent_identities(response)
14
+ instantiate_objects(response)
15
+ end
16
+
17
+ def list_by_type(resource_type, options = {})
18
+ response = ProfitBricks.request(
19
+ method: :get,
20
+ path: "/um/resources/#{resource_type}",
21
+ expects: 200,
22
+ query: options
23
+ )
24
+
25
+ add_parent_identities(response)
26
+ instantiate_objects(response)
27
+ end
28
+
29
+ # Retrieve a resource
30
+ def get(resource_type, resource_id, options = {})
31
+ response = ProfitBricks.request(
32
+ method: :get,
33
+ path: "/um/resources/#{resource_type}/#{resource_id}",
34
+ expects: 200,
35
+ query: options
36
+ )
37
+
38
+ instantiate_objects(response)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,66 @@
1
+ module ProfitBricks
2
+ # Share class
3
+ class Share < ProfitBricks::Model
4
+
5
+ class << self
6
+ # Delete the share.
7
+ def delete(group_id,resource_id)
8
+ response = ProfitBricks.request(
9
+ method: :delete,
10
+ path: "/um/groups/#{group_id}/shares/#{resource_id}",
11
+ expects: 202
12
+ )
13
+ return true
14
+ end
15
+
16
+ # Update the share.
17
+ def update(group_id,resource_id ,options = {})
18
+ response = ProfitBricks.request(
19
+ method: :put,
20
+ path: "/um/groups/#{group_id}/shares/#{resource_id}",
21
+ expects: 200,
22
+ body: { properties: options }.to_json
23
+ )
24
+
25
+ add_parent_identities(response)
26
+ instantiate_objects(response)
27
+ end
28
+
29
+ # Create a new share.
30
+ def create(group_id, options = {})
31
+ response = ProfitBricks.request(
32
+ method: :post,
33
+ path: "/um/groups/#{group_id}/shares/#{options[:resource_id]}",
34
+ expects: 202,
35
+ body: { properties: options}.to_json
36
+ )
37
+ add_parent_identities(response)
38
+ instantiate_objects(response)
39
+ end
40
+
41
+ # List all shares under a group.
42
+ def list(group_id, options = {})
43
+ response = ProfitBricks.request(
44
+ method: :get,
45
+ path: "/um/groups/#{group_id}/shares",
46
+ expects: 200,
47
+ query: options
48
+ )
49
+ add_parent_identities(response)
50
+ instantiate_objects(response)
51
+ end
52
+
53
+ # Retrieve a share under a group.
54
+ def get(group_id, share_id, options = {})
55
+ response = ProfitBricks.request(
56
+ method: :get,
57
+ path: "/um/groups/#{group_id}/shares/#{share_id}",
58
+ expects: 200,
59
+ query: options
60
+ )
61
+ add_parent_identities(response)
62
+ instantiate_objects(response)
63
+ end
64
+ end
65
+ end
66
+ end