code42 0.2.4 → 0.2.5
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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -0
- data/code42.gemspec +2 -2
- data/lib/code42.rb +1 -0
- data/lib/code42/api/computer.rb +12 -0
- data/lib/code42/api/org.rb +34 -5
- data/lib/code42/api/user.rb +8 -0
- data/lib/code42/client.rb +15 -13
- data/lib/code42/connection.rb +39 -17
- data/lib/code42/error.rb +2 -0
- data/lib/code42/org.rb +52 -2
- data/lib/code42/user.rb +27 -6
- data/lib/code42/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e4779f663016dbb2239a99c9cee391ce4c03627
|
4
|
+
data.tar.gz: 599b65430440a9aea5cc384675af10739f6e7b8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7ef9ef0c96aff020be4c584cae265ed8ed365834d9effdfa582e4e98b4cc6c240d4bcc399fc17ef5d23325c440df223b488524081e4c1caacb1fa54196f10ba
|
7
|
+
data.tar.gz: 630b13729bb81af29b7fb072cce7da8981127f170a9666403637e70554cb0be4d2efa7f4ec971d79e6c04d7e9bac511b7b46a4fb69176bedfb76b26d92e86034
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/code42.gemspec
CHANGED
@@ -16,9 +16,9 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.add_development_dependency 'webmock', '~> 1.11.0'
|
17
17
|
gem.add_development_dependency 'vcr', '~> 2.5.0'
|
18
18
|
gem.add_development_dependency 'rake'
|
19
|
-
gem.add_dependency 'faraday', '~> 0.
|
19
|
+
gem.add_dependency 'faraday', '~> 0.9.0'
|
20
20
|
gem.add_dependency 'activesupport', '>= 3.2.0'
|
21
|
-
gem.add_dependency 'faraday_middleware', '~> 0.
|
21
|
+
gem.add_dependency 'faraday_middleware', '~> 0.9.1'
|
22
22
|
|
23
23
|
gem.files = `git ls-files`.split($/)
|
24
24
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/lib/code42.rb
CHANGED
@@ -4,6 +4,7 @@ require "active_support/core_ext/hash/keys"
|
|
4
4
|
require "active_support/core_ext/string/inflections"
|
5
5
|
require "active_support/core_ext/object/blank"
|
6
6
|
require "active_support/core_ext/object/try"
|
7
|
+
require "active_support/notifications"
|
7
8
|
require "code42/version"
|
8
9
|
require "code42/client"
|
9
10
|
require "code42/settings"
|
data/lib/code42/api/computer.rb
CHANGED
@@ -17,6 +17,18 @@ module Code42
|
|
17
17
|
params.merge!(key: 'computers')
|
18
18
|
objects_from_response(Code42::Computer, :get, 'computer', params)
|
19
19
|
end
|
20
|
+
|
21
|
+
|
22
|
+
# Block a computer from backing up
|
23
|
+
# @return [Code42::Computer] The blocked computer
|
24
|
+
# @params id [Integer, String] The computer ID you want to block
|
25
|
+
def block_computer(id)
|
26
|
+
put("computerblock/#{id}")
|
27
|
+
end
|
28
|
+
|
29
|
+
def unblock_computer(id)
|
30
|
+
delete("computerblock/#{id}")
|
31
|
+
end
|
20
32
|
end
|
21
33
|
end
|
22
34
|
end
|
data/lib/code42/api/org.rb
CHANGED
@@ -28,19 +28,31 @@ module Code42
|
|
28
28
|
object_from_response(Code42::Org, :get, "org/#{id}", params)
|
29
29
|
end
|
30
30
|
|
31
|
-
# Returns an org for a given name
|
31
|
+
# Returns an org for a given name. If multiple orgs are found, the first found will
|
32
|
+
# be returned.
|
32
33
|
# @return [Code42::Org] The requested org
|
33
34
|
# @param name [String] A Code42 org name
|
35
|
+
# @param params [Hash] Additional params used by the server for filtering, output, etc.
|
34
36
|
# FIXME: This needs to change when the API implements a better way.
|
35
|
-
def find_org_by_name(name)
|
36
|
-
search_orgs(name).select { |o| o.name == name }.first
|
37
|
+
def find_org_by_name(name, params = {})
|
38
|
+
search_orgs(name, params).select { |o| o.name == name }.first
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the first org that starts with a given name.
|
42
|
+
# Since orgs are renamed when they are deactivated, it is impossible
|
43
|
+
# to guarantee a match by name alone.
|
44
|
+
# @param name [String] A Code42 org name
|
45
|
+
# @param params [Hash] Additional params used by the server for filtering, output, etc.
|
46
|
+
def find_inactive_org_by_name(name, params = {})
|
47
|
+
search_orgs(name, params.merge!(:active => false)).first
|
37
48
|
end
|
38
49
|
|
39
50
|
# Searches orgs for a query string
|
40
51
|
# @return [Array] An array of matching orgs
|
41
52
|
# @param query [String] A string to search for
|
42
|
-
def search_orgs(query)
|
43
|
-
|
53
|
+
def search_orgs(query, params = {})
|
54
|
+
params.merge!(q: query)
|
55
|
+
orgs(params)
|
44
56
|
end
|
45
57
|
|
46
58
|
# Returns a list of up to 100 orgs
|
@@ -55,6 +67,23 @@ module Code42
|
|
55
67
|
object_from_response(Code42::Org, :put, "org/#{id}", attrs)
|
56
68
|
end
|
57
69
|
|
70
|
+
def activate_org(id = 'my', attrs = {})
|
71
|
+
object_from_response(Code42::Org, :delete, "orgDeactivation/#{id}", attrs)
|
72
|
+
end
|
73
|
+
|
74
|
+
def deactivate_org(id = 'my', attrs = {})
|
75
|
+
object_from_response(Code42::Org, :put, "orgDeactivation/#{id}", attrs)
|
76
|
+
end
|
77
|
+
|
78
|
+
def block_org(id = 'my', attrs = {})
|
79
|
+
object_from_response(Code42::Org, :put, "orgBlock/#{id}", attrs)
|
80
|
+
end
|
81
|
+
|
82
|
+
def unblock_org(id = 'my', attrs = {})
|
83
|
+
object_from_response(Code42::Org, :delete, "orgBlock/#{id}", attrs)
|
84
|
+
end
|
85
|
+
|
86
|
+
|
58
87
|
end
|
59
88
|
end
|
60
89
|
end
|
data/lib/code42/api/user.rb
CHANGED
data/lib/code42/client.rb
CHANGED
@@ -16,6 +16,8 @@ module Code42
|
|
16
16
|
self.settings = options
|
17
17
|
end
|
18
18
|
|
19
|
+
def last_response; connection.last_response; end
|
20
|
+
|
19
21
|
def settings=(options)
|
20
22
|
@settings = Settings.new(options)
|
21
23
|
end
|
@@ -61,16 +63,16 @@ module Code42
|
|
61
63
|
end
|
62
64
|
|
63
65
|
def connection
|
64
|
-
@connection
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
66
|
+
@connection ||= begin
|
67
|
+
connection = Connection.new
|
68
|
+
settings.debug && connection.use(Faraday::Response::Logger)
|
69
|
+
connection
|
70
|
+
end
|
71
|
+
@connection.host = settings.host
|
72
|
+
@connection.port = settings.port
|
73
|
+
@connection.scheme = settings.scheme
|
74
|
+
@connection.path_prefix = settings.api_root
|
75
|
+
@connection.verify_https = settings.verify_https
|
74
76
|
if settings.username && settings.password
|
75
77
|
@connection.username = settings.username
|
76
78
|
@connection.password = settings.password
|
@@ -87,7 +89,7 @@ module Code42
|
|
87
89
|
klass = fetch_class(klass)
|
88
90
|
options = klass.serialize(options)
|
89
91
|
response = send(request_method.to_sym, path, options)
|
90
|
-
return nil unless response_has_data?(response
|
92
|
+
return nil unless response_has_data?(response)
|
91
93
|
klass.from_response(response['data'], self)
|
92
94
|
end
|
93
95
|
|
@@ -98,7 +100,7 @@ module Code42
|
|
98
100
|
response = send(request_method.to_sym, path, options)
|
99
101
|
return nil unless response_has_data?(response)
|
100
102
|
response = response['data']
|
101
|
-
response = response[key] if key
|
103
|
+
response = response[key] if !response.empty? && key
|
102
104
|
objects_from_array(klass, response)
|
103
105
|
end
|
104
106
|
|
@@ -107,7 +109,7 @@ module Code42
|
|
107
109
|
end
|
108
110
|
|
109
111
|
def response_has_data?(response)
|
110
|
-
|
112
|
+
response && response['data']
|
111
113
|
end
|
112
114
|
|
113
115
|
def collection_from_response(collection_klass, object_klass, request_method, path, options = {})
|
data/lib/code42/connection.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'bundler/setup'
|
1
2
|
require 'faraday'
|
2
3
|
require 'faraday_middleware'
|
3
4
|
require 'logger'
|
@@ -5,7 +6,8 @@ require 'code42/error'
|
|
5
6
|
|
6
7
|
module Code42
|
7
8
|
class Connection
|
8
|
-
attr_accessor :host, :port, :scheme, :path_prefix, :username, :password, :adapter, :token, :verify_https, :logger
|
9
|
+
attr_accessor :host, :port, :scheme, :path_prefix, :username, :password, :adapter, :token, :verify_https, :logger, :last_response
|
10
|
+
|
9
11
|
def initialize(options = {})
|
10
12
|
self.host = options[:host]
|
11
13
|
self.port = options[:port]
|
@@ -15,24 +17,34 @@ module Code42
|
|
15
17
|
self.password = options[:password]
|
16
18
|
self.token = options[:token] if options[:token]
|
17
19
|
self.verify_https = !options[:verify_https].nil? ? options[:verify_https] : true
|
20
|
+
end
|
21
|
+
|
22
|
+
extend Forwardable
|
18
23
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
24
|
+
delegate %i(host port path_prefix scheme) => :adapter
|
25
|
+
delegate %i(host= port= path_prefix= scheme=) => :adapter
|
26
|
+
|
27
|
+
def verify_https=(verify_https)
|
23
28
|
adapter.ssl[:verify] = verify_https
|
24
29
|
end
|
25
30
|
|
31
|
+
def verify_https
|
32
|
+
adapter.ssl[:verify]
|
33
|
+
end
|
34
|
+
|
26
35
|
def logger
|
27
36
|
@logger ||= Logger.new(STDOUT)
|
28
37
|
end
|
29
38
|
|
30
39
|
def adapter
|
31
|
-
|
32
|
-
|
33
|
-
|
40
|
+
@adapter ||= Faraday.new do |f|
|
41
|
+
f.request :multipart
|
42
|
+
f.request :json
|
43
|
+
|
44
|
+
f.adapter :net_http
|
45
|
+
|
46
|
+
f.response :json
|
34
47
|
end
|
35
|
-
@adapter
|
36
48
|
end
|
37
49
|
|
38
50
|
def has_valid_credentials?
|
@@ -58,7 +70,12 @@ module Code42
|
|
58
70
|
|
59
71
|
def make_request(method, *args)
|
60
72
|
begin
|
61
|
-
response = self.send(method, *args)
|
73
|
+
@last_response = response = self.send(method, *args)
|
74
|
+
ActiveSupport::Notifications.instrument('code42.request', {
|
75
|
+
method: method,
|
76
|
+
args: args,
|
77
|
+
response: response
|
78
|
+
})
|
62
79
|
rescue Faraday::Error::ConnectionFailed
|
63
80
|
raise Code42::Error::ConnectionFailed
|
64
81
|
end
|
@@ -71,13 +88,11 @@ module Code42
|
|
71
88
|
end
|
72
89
|
|
73
90
|
def put(path, data)
|
74
|
-
adapter.
|
75
|
-
adapter.put path, data.to_json
|
91
|
+
adapter.put path, data
|
76
92
|
end
|
77
93
|
|
78
94
|
def post(path, data)
|
79
|
-
adapter.
|
80
|
-
adapter.post path, data.to_json
|
95
|
+
adapter.post path, data
|
81
96
|
end
|
82
97
|
|
83
98
|
def delete(path, data)
|
@@ -96,22 +111,29 @@ module Code42
|
|
96
111
|
|
97
112
|
def check_for_errors(response)
|
98
113
|
if response.status == 401
|
99
|
-
raise Code42::Error::AuthenticationError.new(
|
114
|
+
raise Code42::Error::AuthenticationError.new(description_from_response(response), response.status)
|
115
|
+
elsif response.status == 403
|
116
|
+
raise Code42::Error::AuthorizationError.new(description_from_response(response), response.status)
|
100
117
|
elsif response.status == 404
|
101
|
-
raise Code42::Error::ResourceNotFound.new(
|
118
|
+
raise Code42::Error::ResourceNotFound.new(description_from_response(response), response.status)
|
102
119
|
elsif response.status >= 400 && response.status < 600
|
103
120
|
body = response.body.is_a?(Array) ? response.body.first : response.body
|
104
121
|
raise exception_from_body(body, response.status)
|
105
122
|
end
|
106
123
|
end
|
107
124
|
|
125
|
+
def description_from_response(response)
|
126
|
+
response.try { |resp| resp.body.try { |body| body.first['description'] } }
|
127
|
+
end
|
128
|
+
|
108
129
|
def exception_from_body(body, status = nil)
|
109
130
|
return Code42::Error.new("Status: #{status}") if body.nil? || !body.has_key?('name')
|
110
131
|
exception_name = body['name'].downcase.camelize
|
111
132
|
if Code42::Error.const_defined?(exception_name)
|
112
133
|
klass = Code42::Error.const_get(exception_name)
|
113
134
|
else
|
114
|
-
|
135
|
+
# Generic server error if no specific error is caught.
|
136
|
+
klass = Code42::Error::ServerError
|
115
137
|
end
|
116
138
|
klass.new(body['description'], status)
|
117
139
|
end
|
data/lib/code42/error.rb
CHANGED
@@ -8,6 +8,7 @@ module Code42
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class Error::AuthenticationError < Error; end
|
11
|
+
class Error::AuthorizationError < Error; end
|
11
12
|
class Error::UsernameNotEmail < Error; end
|
12
13
|
class Error::EmailInvalid < Error; end
|
13
14
|
class Error::InvalidUsername < Error; end
|
@@ -18,4 +19,5 @@ module Code42
|
|
18
19
|
class Error::OrgNameTooShort < Error; end
|
19
20
|
class Error::ConnectionFailed < Error; end
|
20
21
|
class Error::ResourceNotFound < Error; end
|
22
|
+
class Error::ServerError < Error; end
|
21
23
|
end
|
data/lib/code42/org.rb
CHANGED
@@ -9,13 +9,47 @@ module Code42
|
|
9
9
|
attribute :created_at, :from => 'creationDate', :as => DateTime
|
10
10
|
attribute :updated_at, :from => 'modificationDate', :as => DateTime
|
11
11
|
attribute :customer_id
|
12
|
+
attribute :active
|
13
|
+
attribute :blocked
|
12
14
|
|
13
15
|
def self.create(attrs = {})
|
14
16
|
client.create_org(attrs)
|
15
17
|
end
|
16
18
|
|
17
|
-
def self.
|
18
|
-
|
19
|
+
def self.find_by_id(id, attrs = {})
|
20
|
+
find_active_by_id(id, attrs) || find_inactive_by_id(id, attrs)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.find_active_by_id(id, attrs = {})
|
24
|
+
client.org(id, attrs)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.find_inactive_by_id(id, attrs = {})
|
28
|
+
client.org(id, attrs.merge(active: false))
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.find_by_name(name, attrs = {})
|
32
|
+
find_active_by_name(name, attrs) || find_inactive_by_name(name, attrs)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.find_active_by_name(name, attrs = {})
|
36
|
+
client.find_org_by_name(name, attrs.merge(active: true))
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.find_inactive_by_name(name, attrs = {})
|
40
|
+
client.find_inactive_org_by_name name, attrs
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.find_all_orgs
|
44
|
+
find_all_active_orgs + find_all_inactive_orgs
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.find_all_active_orgs
|
48
|
+
client.orgs
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.find_all_inactive_orgs
|
52
|
+
client.orgs(active: false)
|
19
53
|
end
|
20
54
|
|
21
55
|
def users
|
@@ -26,8 +60,24 @@ module Code42
|
|
26
60
|
client.update_org(id, attrs)
|
27
61
|
end
|
28
62
|
|
63
|
+
def activate
|
64
|
+
client.activate_org(id)
|
65
|
+
client.org id
|
66
|
+
end
|
67
|
+
|
29
68
|
def deactivate
|
30
69
|
client.deactivate_org(id)
|
70
|
+
client.org(id, active: false)
|
71
|
+
end
|
72
|
+
|
73
|
+
def block
|
74
|
+
client.block_org(id)
|
75
|
+
client.org id
|
76
|
+
end
|
77
|
+
|
78
|
+
def unblock
|
79
|
+
client.unblock_org(id)
|
80
|
+
client.org id
|
31
81
|
end
|
32
82
|
|
33
83
|
def create_user(attrs = {})
|
data/lib/code42/user.rb
CHANGED
@@ -8,18 +8,27 @@ module Code42
|
|
8
8
|
attribute :org_id
|
9
9
|
attribute :updated_at, :from => 'modificationDate', :as => DateTime
|
10
10
|
attribute :created_at, :from => 'creationDate', :as => DateTime
|
11
|
+
attribute :role, :from => 'roleName' # from plan_user_Dto
|
11
12
|
attribute :status, :username, :email, :first_name, :last_name, :quota_in_bytes, :org_id, :org_uid, :org_name, :active, :blocked, :email_promo, :invited, :org_type, :username_is_an_email, :customer_id
|
12
13
|
|
13
14
|
def self.create(attributes)
|
14
15
|
client.create_user(attributes)
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
client.
|
18
|
+
def self.find_by_id(id)
|
19
|
+
client.find_user_by_id id
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.find_by_name(user_name)
|
23
|
+
client.user user_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.find_all_users
|
27
|
+
client.users + client.users(active: false)
|
19
28
|
end
|
20
29
|
|
21
|
-
def
|
22
|
-
client.
|
30
|
+
def update(attributes)
|
31
|
+
client.update_user(id, attributes)
|
23
32
|
end
|
24
33
|
|
25
34
|
# Returns the org associated with this user
|
@@ -42,11 +51,23 @@ module Code42
|
|
42
51
|
end
|
43
52
|
|
44
53
|
def block
|
45
|
-
client.block_user(
|
54
|
+
client.block_user(id)
|
55
|
+
client.user id
|
46
56
|
end
|
47
57
|
|
48
58
|
def unblock
|
49
|
-
client.unblock_user(
|
59
|
+
client.unblock_user(id)
|
60
|
+
client.user id
|
61
|
+
end
|
62
|
+
|
63
|
+
def activate
|
64
|
+
client.activate_user(id)
|
65
|
+
client.user id
|
66
|
+
end
|
67
|
+
|
68
|
+
def deactivate
|
69
|
+
client.deactivate_user(id)
|
70
|
+
client.user(id, active: false)
|
50
71
|
end
|
51
72
|
end
|
52
73
|
end
|
data/lib/code42/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code42
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code 42
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.
|
75
|
+
version: 0.9.0
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.
|
82
|
+
version: 0.9.0
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: activesupport
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - ~>
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 0.
|
103
|
+
version: 0.9.1
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - ~>
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 0.
|
110
|
+
version: 0.9.1
|
111
111
|
description: Provides a Ruby interface to the Code42 API
|
112
112
|
email:
|
113
113
|
- dev-ruby@code42.com
|