Class: Envirobly::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/envirobly/api.rb

Constant Summary collapse

HOST =
ENV["ENVIROBLY_API_HOST"].presence || "on.envirobly.com"
USER_AGENT =
"Envirobly CLI v#{Envirobly::VERSION}"
CONTENT_TYPE =
"application/json"
MAX_RETRIES =
30
SHORT_RETRY_INTERVAL =
2.seconds
LONG_RETRY_INTERVAL =
6.seconds

Instance Method Summary collapse

Constructor Details

#initialize(access_token: Envirobly::AccessToken.new, exit_on_error: true) ⇒ Api

Returns a new instance of Api.



13
14
15
16
# File 'lib/envirobly/api.rb', line 13

def initialize(access_token: Envirobly::AccessToken.new, exit_on_error: true)
  @exit_on_error = exit_on_error
  @access_token = access_token
end

Instance Method Details

#create_deployment(params) ⇒ Object



27
28
29
# File 'lib/envirobly/api.rb', line 27

def create_deployment(params)
  post_as_json(api_v1_deployments_url, params:, headers: authorization_headers)
end

#create_service_shell_connection(params) ⇒ Object



31
32
33
# File 'lib/envirobly/api.rb', line 31

def create_service_shell_connection(params)
  post_as_json(api_v1_service_shell_connections_url, params:, headers: authorization_headers)
end

#get_as_json(url, headers: {}) ⇒ Object



70
71
72
# File 'lib/envirobly/api.rb', line 70

def get_as_json(url, headers: {})
  request(url, type: Net::HTTP::Get, headers:)
end

#get_deployment_with_delay_and_retry(url, tries = 1) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/envirobly/api.rb', line 50

def get_deployment_with_delay_and_retry(url, tries = 1)
  sleep SHORT_RETRY_INTERVAL * tries
  response = get_as_json URI(url)

  if response.success?
    response
  elsif MAX_RETRIES <= tries
    $stderr.puts "Max retries exhausted while waiting for deployment credentials. Aborting."
    exit 1
  else
    if tries > 3
      sleep LONG_RETRY_INTERVAL
    else
      sleep SHORT_RETRY_INTERVAL
    end

    get_deployment_with_delay_and_retry(url, tries + 1)
  end
end

#list_accountsObject



35
36
37
# File 'lib/envirobly/api.rb', line 35

def list_accounts
  get_as_json api_v1_accounts_url, headers: authorization_headers
end

#list_instance_types(region) ⇒ Object



43
44
45
# File 'lib/envirobly/api.rb', line 43

def list_instance_types(region)
  get_as_json api_v1_instance_types_url(region), headers: authorization_headers
end

#list_regionsObject



39
40
41
# File 'lib/envirobly/api.rb', line 39

def list_regions
  get_as_json api_v1_regions_url, headers: authorization_headers
end

#post_as_json(url, params: {}, headers: {}) ⇒ Object



74
75
76
77
78
# File 'lib/envirobly/api.rb', line 74

def post_as_json(url, params: {}, headers: {})
  request(url, type: Net::HTTP::Post, headers:) do |request|
    request.body = params.to_json
  end
end

#put_as_json(url, params: {}, headers: {}) ⇒ Object



80
81
82
83
84
# File 'lib/envirobly/api.rb', line 80

def put_as_json(url, params: {}, headers: {})
  request(url, type: Net::HTTP::Put, headers:) do |request|
    request.body = params.to_json
  end
end

#validate_shape(params) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/envirobly/api.rb', line 18

def validate_shape(params)
  post_as_json(api_v1_shape_validations_url, params:, headers: authorization_headers).tap do |response|
    unless response.success?
      $stderr.puts "Validation request responded with #{response.code}. Aborting."
      exit 1
    end
  end
end