cloudally 0.1.0 → 0.1.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 +4 -4
- data/.env.template +1 -1
- data/.gitignore +1 -0
- data/Gemfile +9 -0
- data/README.md +74 -0
- data/Rakefile +14 -0
- data/cloudally.gemspec +1 -3
- data/lib/cloudally/api.rb +31 -32
- data/lib/cloudally/authentication.rb +53 -65
- data/lib/cloudally/client/partners.rb +65 -62
- data/lib/cloudally/client.rb +11 -11
- data/lib/cloudally/configuration.rb +85 -101
- data/lib/cloudally/connection.rb +40 -45
- data/lib/cloudally/request.rb +121 -117
- data/lib/cloudally/version.rb +5 -4
- data/lib/cloudally.rb +27 -26
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76a1428d689b8aa5ac9b8a64093758d98ff334cc533aa57c0cb08df831099e43
|
4
|
+
data.tar.gz: f03c7bc9f4e557ffebca931ab1f090e8c95ded15c821a289d031e1d9a16fa6e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce92512da8c61a97b77306b45b77ddd8640f2ac1563caa907f074c07ca2661ed012b1a54b2c1ec52188c6e06536a0f7bb1859f8653f5352dea8860f986554d06
|
7
|
+
data.tar.gz: 58d6b69f4688391de8de608b956648f99d0104c3dc2c742d495a63bbca092618ff4cac7f286c1d9a58f05cfbbb07030af59df90570f1b69cc4941a3fc66db0e4
|
data/.env.template
CHANGED
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Cloudally API
|
2
|
+
|
3
|
+
|
4
|
+
This is a wrapper for the CloudAlly portal API v1. You can see the API endpoints here https://api.cloudally.com/documentation
|
5
|
+
|
6
|
+
Currently only the GET requests for the Partner Portal API are implemented.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'cloudally'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install cloudally
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Before you start making the requests to API provide the client id and client secret and email/password using the configuration wrapping.
|
27
|
+
|
28
|
+
```
|
29
|
+
CloudAlly.configure do |config|
|
30
|
+
config.client_id = ENV["CLOUDALLY_CLIENT_ID"]
|
31
|
+
config.client_secret = ENV["CLOUDALLY_CLIENT_SECRET"]
|
32
|
+
config.username = ENV["CLOUDALLY_USER"]
|
33
|
+
config.password = ENV["CLOUDALLY_PASSWORD"]
|
34
|
+
config.logger = Logger.new( "./cloudally-http.log" )
|
35
|
+
end
|
36
|
+
CloudAlly.partner_login
|
37
|
+
client = CloudAlly.client
|
38
|
+
```
|
39
|
+
|
40
|
+
## Resources
|
41
|
+
### Authentication
|
42
|
+
```
|
43
|
+
client.partner_login
|
44
|
+
```
|
45
|
+
|Resource|API endpoint|Description|
|
46
|
+
|:--|:--|:--|
|
47
|
+
|.auth_partner or .partner_login|/auth/partner|Authenticate partner|
|
48
|
+
|.auth or .login|/auth|Authenticate portal user|
|
49
|
+
|.auth_refresh|/auth/refresh|Refresh authentication token|
|
50
|
+
|
51
|
+
### Partner Portal
|
52
|
+
Endpoint for partner related requests https://api.cloudally.com/documentation#/Partner%20Portal
|
53
|
+
```
|
54
|
+
partner = client.get_partner
|
55
|
+
puts partner.email
|
56
|
+
```
|
57
|
+
|
58
|
+
|Resource|API endpoint|
|
59
|
+
|:--|:--|
|
60
|
+
|.partners or .get_partner|/v1/partners|
|
61
|
+
|.partner_bills|/v1/partners/bills
|
62
|
+
|.partner_status or .get_status_by_partner|/v1/partners/status|
|
63
|
+
|.partner_tasks|/v1/partners/tasks|
|
64
|
+
|.partner_resellers or .get_resellers_list |/v1/partners/resellers|
|
65
|
+
|.partner_resellers( partner_id ) or .get_reseller_by_partner_id( partner_id )|/v1/partners/resellers/{partner_id}|
|
66
|
+
|.partner_users or .get_users_by_partner|/v1/partners/users|
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jancotanis/cloudally.
|
71
|
+
|
72
|
+
## License
|
73
|
+
|
74
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
task default: %i[test rubocop]
|
data/cloudally.gemspec
CHANGED
@@ -9,8 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = "gems@jancology.com"
|
10
10
|
s.license = "MIT"
|
11
11
|
|
12
|
-
|
13
|
-
s.summary = %q{A Ruby wrapper for the CloudAlly Partner Portal REST APIs (readonly)}
|
12
|
+
s.summary = "A Ruby wrapper for the CloudAlly Partner Portal REST APIs (readonly)"
|
14
13
|
s.homepage = "https://rubygems.org/gems/cloudally"
|
15
14
|
|
16
15
|
s.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
@@ -33,5 +32,4 @@ Gem::Specification.new do |s|
|
|
33
32
|
s.add_development_dependency "dotenv"
|
34
33
|
s.add_development_dependency "minitest"
|
35
34
|
s.add_development_dependency "rubocop"
|
36
|
-
|
37
35
|
end
|
data/lib/cloudally/api.rb
CHANGED
@@ -1,32 +1,31 @@
|
|
1
|
-
require File.expand_path('
|
2
|
-
require File.expand_path('
|
3
|
-
require File.expand_path('
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
include
|
29
|
-
include
|
30
|
-
|
31
|
-
|
32
|
-
end
|
1
|
+
require File.expand_path('connection', __dir__)
|
2
|
+
require File.expand_path('request', __dir__)
|
3
|
+
require File.expand_path('authentication', __dir__)
|
4
|
+
|
5
|
+
module CloudAlly
|
6
|
+
# @private
|
7
|
+
class API
|
8
|
+
# @private
|
9
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
10
|
+
|
11
|
+
# Creates a new API
|
12
|
+
def initialize(options = {})
|
13
|
+
options = CloudAlly.options.merge(options)
|
14
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
15
|
+
send("#{key}=", options[key])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
conf = {}
|
21
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
22
|
+
conf[key] = send key
|
23
|
+
end
|
24
|
+
conf
|
25
|
+
end
|
26
|
+
|
27
|
+
include Connection
|
28
|
+
include Request
|
29
|
+
include Authentication
|
30
|
+
end
|
31
|
+
end
|
@@ -1,65 +1,53 @@
|
|
1
|
-
module CloudAlly
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
at = nil
|
55
|
-
CloudAlly.configure do |config|
|
56
|
-
at = config.access_token = response[ "accessToken" ]
|
57
|
-
config.token_type = response[ "tokenType" ]
|
58
|
-
config.refresh_token = response[ "refreshToken" ]
|
59
|
-
config.token_expires = response[ "expiresIn" ]
|
60
|
-
end
|
61
|
-
raise Exception.new 'Could not find valid accessToken; response '+response.to_s if at == '' || at.nil?
|
62
|
-
at
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
1
|
+
module CloudAlly
|
2
|
+
# Defines HTTP request methods
|
3
|
+
module Authentication
|
4
|
+
# Authorize to the CloudAlly portal and return access_token
|
5
|
+
def auth(options = {})
|
6
|
+
params = access_token_params.merge(options)
|
7
|
+
response = post("/auth", params)
|
8
|
+
# return access_token
|
9
|
+
process_token(response.body)
|
10
|
+
end
|
11
|
+
alias login auth
|
12
|
+
|
13
|
+
# Return an access token from authorization
|
14
|
+
def auth_refresh(token)
|
15
|
+
params = { refreshToken: token }
|
16
|
+
|
17
|
+
response = post("/auth/refresh", params)
|
18
|
+
# return access_token
|
19
|
+
process_token(response.body)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Authorize to the partner portal and return access_token
|
23
|
+
def auth_partner(options = {})
|
24
|
+
params = access_token_params.merge(options)
|
25
|
+
response = post("/auth/partner", params)
|
26
|
+
# return access_token
|
27
|
+
process_token(response.body)
|
28
|
+
end
|
29
|
+
alias partner_login auth_partner
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def access_token_params
|
34
|
+
{
|
35
|
+
email: username,
|
36
|
+
password: password
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def process_token(response)
|
41
|
+
at = nil
|
42
|
+
CloudAlly.configure do |config|
|
43
|
+
at = config.access_token = response["accessToken"]
|
44
|
+
config.token_type = response["tokenType"]
|
45
|
+
config.refresh_token = response["refreshToken"]
|
46
|
+
config.token_expires = response["expiresIn"]
|
47
|
+
end
|
48
|
+
raise StandardError.new 'Could not find valid accessToken; response ' + response.to_s if at == '' || at.nil?
|
49
|
+
|
50
|
+
at
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,66 +1,69 @@
|
|
1
|
-
|
2
1
|
module CloudAlly
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class Client
|
3
|
+
# Defines methods related to partners
|
4
|
+
module PartnerPortal
|
5
|
+
# Get CloudAlly Partner settings.
|
6
|
+
#
|
7
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
8
|
+
def partners
|
9
|
+
get("partners")
|
10
|
+
end
|
11
|
+
alias get_partner partners
|
12
|
+
|
13
|
+
# Get Partner bills.
|
14
|
+
#
|
15
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
16
|
+
def partner_bills
|
17
|
+
get_paged("partners/bills")
|
18
|
+
end
|
19
|
+
|
20
|
+
# Get Partner bills.
|
21
|
+
#
|
22
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
23
|
+
def partner_status
|
24
|
+
get_paged("partners/status")
|
25
|
+
end
|
26
|
+
alias get_status_by_partner partner_status
|
27
|
+
|
28
|
+
# Get Partner tasks.
|
29
|
+
#
|
30
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
31
|
+
def partner_tasks
|
32
|
+
get_paged("partners/tasks")
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get Partner resellers.
|
36
|
+
#
|
37
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
38
|
+
def partner_resellers(partner_id = nil)
|
39
|
+
if partner_id
|
40
|
+
get_paged("partners/resellers/#{partner_id}")
|
41
|
+
else
|
42
|
+
get_paged("partners/resellers")
|
43
|
+
end
|
44
|
+
end
|
6
45
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
alias :get_partner :partners
|
46
|
+
# Get Partner resellers.
|
47
|
+
#
|
48
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
49
|
+
def get_resellers_list
|
50
|
+
partner_resellers()
|
51
|
+
end
|
14
52
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
# Get Partner bills.
|
22
|
-
#
|
23
|
-
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
24
|
-
def partner_status
|
25
|
-
get_paged( "partners/status" )
|
26
|
-
end
|
27
|
-
alias :get_status_by_partner :partner_status
|
28
|
-
# Get Partner tasks.
|
29
|
-
#
|
30
|
-
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
31
|
-
def partner_tasks
|
32
|
-
get_paged( "partners/tasks" )
|
33
|
-
end
|
34
|
-
# Get Partner resellers.
|
35
|
-
#
|
36
|
-
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
37
|
-
def partner_resellers( partner_id=nil )
|
38
|
-
if partner_id
|
39
|
-
get_paged( "partners/resellers/#{partner_id}" )
|
40
|
-
else
|
41
|
-
get_paged( "partners/resellers" )
|
42
|
-
end
|
43
|
-
end
|
44
|
-
# Get Partner resellers.
|
45
|
-
#
|
46
|
-
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
47
|
-
def get_resellers_list
|
48
|
-
partner_resellers()
|
49
|
-
end
|
50
|
-
# Get Partner resellers.
|
51
|
-
#
|
52
|
-
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
53
|
-
def get_reseller_by_partner_id partner_id
|
54
|
-
partner_resellers( partner_id )
|
55
|
-
end
|
56
|
-
# Get Partner users.
|
57
|
-
#
|
58
|
-
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
59
|
-
def partner_users
|
60
|
-
get_paged( "partners/users" )
|
61
|
-
end
|
62
|
-
alias :get_users_by_partner :partner_users
|
53
|
+
# Get Partner resellers.
|
54
|
+
#
|
55
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
56
|
+
def get_reseller_by_partner_id partner_id
|
57
|
+
partner_resellers(partner_id)
|
58
|
+
end
|
63
59
|
|
64
|
-
|
65
|
-
|
66
|
-
|
60
|
+
# Get Partner users.
|
61
|
+
#
|
62
|
+
# @see https://api.cloudally.com/documentation#/Partner%20Portal
|
63
|
+
def partner_users
|
64
|
+
get_paged("partners/users")
|
65
|
+
end
|
66
|
+
alias get_users_by_partner partner_users
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/cloudally/client.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
module CloudAlly
|
2
|
-
# Wrapper for the CloudAlly REST API
|
3
|
-
#
|
4
|
-
# @note All methods have been separated into modules and follow the same grouping used in api docs
|
5
|
-
# @see https://api.cloudally.com/documentation
|
6
|
-
class Client < API
|
7
|
-
Dir[File.expand_path('
|
8
|
-
|
9
|
-
include CloudAlly::Client::PartnerPortal
|
10
|
-
end
|
11
|
-
end
|
1
|
+
module CloudAlly
|
2
|
+
# Wrapper for the CloudAlly REST API
|
3
|
+
#
|
4
|
+
# @note All methods have been separated into modules and follow the same grouping used in api docs
|
5
|
+
# @see https://api.cloudally.com/documentation
|
6
|
+
class Client < API
|
7
|
+
Dir[File.expand_path('client/*.rb', __dir__)].each { |f| require f }
|
8
|
+
|
9
|
+
include CloudAlly::Client::PartnerPortal
|
10
|
+
end
|
11
|
+
end
|
@@ -1,103 +1,87 @@
|
|
1
|
-
require_relative "./version"
|
2
|
-
module CloudAlly
|
3
|
-
# Defines constants and methods related to configuration
|
4
|
-
module Configuration
|
5
|
-
# An array of valid keys in the options hash when configuring a {CloudAlly::API}
|
1
|
+
require_relative "./version"
|
2
|
+
module CloudAlly
|
3
|
+
# Defines constants and methods related to configuration
|
4
|
+
module Configuration
|
5
|
+
# An array of valid keys in the options hash when configuring a {CloudAlly::API}
|
6
|
+
|
7
|
+
VALID_OPTIONS_KEYS = [
|
8
|
+
:access_token,
|
9
|
+
:token_type,
|
10
|
+
:refresh_token,
|
11
|
+
:token_expires,
|
12
|
+
:client_id,
|
13
|
+
:client_secret,
|
14
|
+
:connection_options,
|
15
|
+
:username,
|
16
|
+
:password,
|
17
|
+
:endpoint,
|
18
|
+
:logger,
|
19
|
+
:format,
|
20
|
+
:page_size,
|
21
|
+
:user_agent
|
22
|
+
].freeze
|
23
|
+
|
24
|
+
|
25
|
+
# By default, don't set any connection options
|
26
|
+
DEFAULT_CONNECTION_OPTIONS = {}
|
27
|
+
|
28
|
+
# The endpoint that will be used to connect if none is set
|
29
|
+
#
|
30
|
+
# @note There is no reason to use any other endpoint at this time
|
31
|
+
DEFAULT_ENDPOINT = "https://api.cloudally.com/v1/".freeze
|
32
|
+
|
33
|
+
# The response format appended to the path and sent in the 'Accept' header if none is set
|
34
|
+
#
|
35
|
+
# @note JSON is the only available format at this time
|
36
|
+
DEFAULT_FORMAT = :json
|
37
|
+
|
38
|
+
# The page size for paged rest responses
|
39
|
+
#
|
40
|
+
# @note default JSON is the only available format at this time
|
41
|
+
DEFAULT_PAGE_SIZE = 500
|
42
|
+
|
43
|
+
# The user agent that will be sent to the API endpoint if none is set
|
44
|
+
DEFAULT_USER_AGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
|
45
|
+
|
46
|
+
# @private
|
47
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
48
|
+
|
49
|
+
# When this module is extended, set all configuration options to their default values
|
50
|
+
def self.extended(base)
|
51
|
+
base.reset
|
52
|
+
end
|
53
|
+
|
54
|
+
# Convenience method to allow configuration options to be set in a block
|
55
|
+
def configure
|
56
|
+
yield self
|
57
|
+
end
|
58
|
+
|
59
|
+
# Create a hash of options and their values
|
60
|
+
def options
|
61
|
+
VALID_OPTIONS_KEYS.inject({}) do |option, key|
|
62
|
+
option.merge!(key => send(key))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Reset all configuration options to defaults
|
67
|
+
def reset
|
68
|
+
self.access_token = nil
|
69
|
+
self.token_type = nil
|
70
|
+
self.refresh_token = nil
|
71
|
+
self.token_expires = nil
|
72
|
+
self.client_id = nil
|
73
|
+
self.client_secret = nil
|
74
|
+
self.username = nil
|
75
|
+
self.password = nil
|
6
76
|
|
7
|
-
VALID_OPTIONS_KEYS = [
|
8
|
-
:access_token,
|
9
|
-
:token_type,
|
10
|
-
:refresh_token,
|
11
|
-
:token_expires,
|
12
|
-
:client_id,
|
13
|
-
:client_secret,
|
14
|
-
:connection_options,
|
15
|
-
:username,
|
16
|
-
:password,
|
17
|
-
:endpoint,
|
18
|
-
:logger,
|
19
|
-
:format,
|
20
|
-
:page_size,
|
21
|
-
:user_agent
|
22
|
-
].freeze
|
23
|
-
|
24
|
-
# By default, don't set a user access token
|
25
|
-
DEFAULT_ACCESS_TOKEN = nil
|
26
|
-
DEFAULT_TOKEN_TYPE = nil
|
27
|
-
|
28
|
-
# By default, don't set an application ID
|
29
|
-
DEFAULT_CLIENT_ID = nil
|
30
|
-
|
31
|
-
# By default, don't set an application secret
|
32
|
-
DEFAULT_CLIENT_SECRET = nil
|
33
|
-
|
34
|
-
# By default, don't set application username
|
35
|
-
DEFAULT_USERNAME = nil
|
36
|
-
|
37
|
-
# By default, don't set application password
|
38
|
-
DEFAULT_PASSWORD = nil
|
39
|
-
|
40
|
-
# By default, don't set any connection options
|
41
|
-
DEFAULT_CONNECTION_OPTIONS = {}
|
42
|
-
|
43
|
-
# The endpoint that will be used to connect if none is set
|
44
|
-
#
|
45
|
-
# @note There is no reason to use any other endpoint at this time
|
46
|
-
DEFAULT_ENDPOINT = "https://api.cloudally.com/v1/".freeze
|
47
|
-
|
48
|
-
# By default, don't turn on logging
|
49
|
-
DEFAULT_LOGGER = nil
|
50
|
-
|
51
|
-
# The response format appended to the path and sent in the 'Accept' header if none is set
|
52
|
-
#
|
53
|
-
# @note JSON is the only available format at this time
|
54
|
-
DEFAULT_FORMAT = :json
|
55
|
-
|
56
|
-
# The page size for paged rest responses
|
57
|
-
#
|
58
|
-
# @note default JSON is the only available format at this time
|
59
|
-
DEFAULT_PAGE_SIZE = 500
|
60
|
-
|
61
|
-
# The user agent that will be sent to the API endpoint if none is set
|
62
|
-
DEFAULT_USER_AGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
|
63
|
-
|
64
|
-
# @private
|
65
|
-
attr_accessor *VALID_OPTIONS_KEYS
|
66
|
-
|
67
|
-
# When this module is extended, set all configuration options to their default values
|
68
|
-
def self.extended(base)
|
69
|
-
base.reset
|
70
|
-
end
|
71
|
-
|
72
|
-
# Convenience method to allow configuration options to be set in a block
|
73
|
-
def configure
|
74
|
-
yield self
|
75
|
-
end
|
76
|
-
|
77
|
-
# Create a hash of options and their values
|
78
|
-
def options
|
79
|
-
VALID_OPTIONS_KEYS.inject({}) do |option, key|
|
80
|
-
option.merge!(key => send(key))
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Reset all configuration options to defaults
|
85
|
-
def reset
|
86
|
-
self.access_token = DEFAULT_ACCESS_TOKEN
|
87
|
-
self.token_type = DEFAULT_TOKEN_TYPE
|
88
|
-
self.client_id = DEFAULT_CLIENT_ID
|
89
|
-
self.client_secret = DEFAULT_CLIENT_SECRET
|
90
|
-
self.username = DEFAULT_USERNAME
|
91
|
-
self.password = DEFAULT_PASSWORD
|
92
|
-
self.connection_options = DEFAULT_CONNECTION_OPTIONS
|
93
|
-
self.endpoint = DEFAULT_ENDPOINT
|
94
77
|
self.logger = DEFAULT_LOGGER
|
95
|
-
|
96
|
-
self.
|
97
|
-
self.
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
78
|
+
|
79
|
+
self.connection_options = DEFAULT_CONNECTION_OPTIONS
|
80
|
+
self.endpoint = DEFAULT_ENDPOINT
|
81
|
+
self.format = DEFAULT_FORMAT
|
82
|
+
self.page_size = DEFAULT_PAGE_SIZE
|
83
|
+
self.user_agent = DEFAULT_USER_AGENT
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/cloudally/connection.rb
CHANGED
@@ -1,45 +1,40 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module CloudAlly
|
4
|
+
# @private
|
5
|
+
module Connection
|
6
|
+
private
|
7
|
+
|
8
|
+
def connection
|
9
|
+
options = {
|
10
|
+
:headers => {
|
11
|
+
'Accept' => "application/#{format}; charset=utf-8",
|
12
|
+
'User-Agent' => user_agent
|
13
|
+
},
|
14
|
+
:url => endpoint
|
15
|
+
}.merge(connection_options)
|
16
|
+
|
17
|
+
Faraday::Connection.new(options) do |connection|
|
18
|
+
connection.use Faraday::Response::RaiseError
|
19
|
+
connection.adapter Faraday.default_adapter
|
20
|
+
|
21
|
+
connection.authorization :Bearer, access_token if access_token
|
22
|
+
connection.headers['client-id'] = client_id
|
23
|
+
connection.headers['client-secret'] = client_secret
|
24
|
+
connection.response :json, :content_type => /\bjson$/
|
25
|
+
connection.use Faraday::Request::UrlEncoded
|
26
|
+
|
27
|
+
if logger
|
28
|
+
connection.response :logger, logger, { headers: true, bodies: true } do |l|
|
29
|
+
# filter json content
|
30
|
+
l.filter(/(\"password\"\:\")(.+?)(\".*)/, '\1[REMOVED]\3')
|
31
|
+
l.filter(/(\"accessToken\"\:\")(.+?)(\".*)/, '\1[REMOVED]\3')
|
32
|
+
# filter header content
|
33
|
+
l.filter(/(client-secret\:.)([^&]+)/, '\1[REMOVED]')
|
34
|
+
l.filter(/(Authorization\:.)([^&]+)/, '\1[REMOVED]')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/cloudally/request.rb
CHANGED
@@ -1,117 +1,121 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module CloudAlly
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
1
|
+
require 'uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module CloudAlly
|
5
|
+
# Defines HTTP request methods
|
6
|
+
module Request
|
7
|
+
class Entity
|
8
|
+
attr_reader :attributes
|
9
|
+
|
10
|
+
def initialize attributes
|
11
|
+
@attributes = attributes.clone.transform_keys(&:to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method_sym, *arguments, &block)
|
15
|
+
len = arguments.length
|
16
|
+
if method = method_sym[/.*(?==\z)/m]
|
17
|
+
# assignment
|
18
|
+
if len != 1
|
19
|
+
raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
|
20
|
+
end
|
21
|
+
@attributes[method] = arguments[0]
|
22
|
+
elsif @attributes.include? method_sym.to_s
|
23
|
+
r = @attributes[method_sym.to_s]
|
24
|
+
case r
|
25
|
+
when Hash
|
26
|
+
r = @attributes[method_sym.to_s] = self.class.new(r)
|
27
|
+
when Array
|
28
|
+
# make deep copy
|
29
|
+
@attributes[method_sym.to_s] = r = r.map { |item|
|
30
|
+
self.class.new(item)
|
31
|
+
} if r.length > 0 && r[0].is_a?(Hash)
|
32
|
+
r
|
33
|
+
else
|
34
|
+
r
|
35
|
+
end
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def respond_to?(method_sym, include_private = false)
|
42
|
+
if @attributes.include? method_sym.to_s
|
43
|
+
true
|
44
|
+
else
|
45
|
+
super
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Perform an HTTP GET request and return entity
|
51
|
+
def get(path, options = {})
|
52
|
+
response = request(:get, path, options)
|
53
|
+
:json.eql?(format) ? Entity.new(response.body) : response.body
|
54
|
+
end
|
55
|
+
|
56
|
+
# Perform an HTTP GET request for paged date sets responsind to
|
57
|
+
# Name Description
|
58
|
+
# pageSize The number of records to display per page
|
59
|
+
# page The page number
|
60
|
+
# nextPageToken Next page token
|
61
|
+
def get_paged(path, options = {}, &block)
|
62
|
+
raise! ArgumentError,
|
63
|
+
"Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
|
64
|
+
result = []
|
65
|
+
page = 1
|
66
|
+
total = page + 1
|
67
|
+
nextPage = ""
|
68
|
+
while page <= total
|
69
|
+
# https://api.cloudally.com/v3/api-docs/v1
|
70
|
+
followingPage = { pageSize: page_size }
|
71
|
+
followingPage.merge!({ page: page, nextPageToken: nextPage }) unless nextPage.empty?
|
72
|
+
|
73
|
+
response = request(:get, path, options.merge(followingPage))
|
74
|
+
data = response.body
|
75
|
+
d = data["data"].map { |e| Entity.new(e) }
|
76
|
+
if block_given?
|
77
|
+
yield(d)
|
78
|
+
else
|
79
|
+
result += d
|
80
|
+
end
|
81
|
+
page += 1
|
82
|
+
total = data["totalPages"].to_i
|
83
|
+
nextPage = data["nextPageToken"]
|
84
|
+
end
|
85
|
+
d unless block_given?
|
86
|
+
end
|
87
|
+
|
88
|
+
# Perform an HTTP POST request
|
89
|
+
def post(path, options = {})
|
90
|
+
request(:post, path, options)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Perform an HTTP PUT request
|
94
|
+
def put(path, options = {})
|
95
|
+
request(:put, path, options)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Perform an HTTP DELETE request
|
99
|
+
def delete(path, options = {})
|
100
|
+
request(:delete, path, options)
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
# Perform an HTTP request
|
106
|
+
def request(method, path, options)
|
107
|
+
response = connection().send(method) do |request|
|
108
|
+
uri = URI::Parser.new
|
109
|
+
case method
|
110
|
+
when :get, :delete
|
111
|
+
request.url(uri.escape(path), options)
|
112
|
+
when :post, :put
|
113
|
+
request.headers['Content-Type'] = "application/#{format}"
|
114
|
+
request.path = uri.escape(path)
|
115
|
+
request.body = options.to_json unless options.empty?
|
116
|
+
end
|
117
|
+
end
|
118
|
+
response
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/cloudally/version.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CloudAlly
|
4
|
+
VERSION = '0.1.1'
|
5
|
+
end
|
data/lib/cloudally.rb
CHANGED
@@ -1,26 +1,27 @@
|
|
1
|
-
require File.expand_path('
|
2
|
-
require File.expand_path('
|
3
|
-
require File.expand_path('
|
4
|
-
require File.expand_path('
|
5
|
-
|
6
|
-
module CloudAlly
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
1
|
+
require File.expand_path('cloudally/configuration', __dir__)
|
2
|
+
require File.expand_path('cloudally/api', __dir__)
|
3
|
+
require File.expand_path('cloudally/client', __dir__)
|
4
|
+
require File.expand_path('cloudally/version', __dir__)
|
5
|
+
|
6
|
+
module CloudAlly
|
7
|
+
extend Configuration
|
8
|
+
|
9
|
+
# Alias for CloudAlly::Client.new
|
10
|
+
#
|
11
|
+
# @return [CloudAlly::Client]
|
12
|
+
def self.client(options = {})
|
13
|
+
CloudAlly::Client.new(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Delegate to CloudAlly::Client
|
17
|
+
def self.method_missing(method, *args, &block)
|
18
|
+
return super unless client.respond_to?(method)
|
19
|
+
|
20
|
+
client.send(method, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Delegate to CloudAlly::Client
|
24
|
+
def self.respond_to?(method, include_all = false)
|
25
|
+
client.respond_to?(method, include_all) || super
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloudally
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janco Tanis
|
@@ -81,6 +81,9 @@ files:
|
|
81
81
|
- ".env.template"
|
82
82
|
- ".gitignore"
|
83
83
|
- CHANGELOG.md
|
84
|
+
- Gemfile
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
84
87
|
- cloudally.gemspec
|
85
88
|
- lib/cloudally.rb
|
86
89
|
- lib/cloudally/api.rb
|