Class: Logstream::CloudAPIV2

Inherits:
Object
  • Object
show all
Defined in:
lib/logstream/cloudapi_v2.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

CLOUDAPI_ENDPOINT =
'https://cloud.acquia.com/api'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ CloudAPIV2

Returns a new instance of CloudAPIV2.



11
12
13
14
# File 'lib/logstream/cloudapi_v2.rb', line 11

def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



8
9
10
# File 'lib/logstream/cloudapi_v2.rb', line 8

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



8
9
10
# File 'lib/logstream/cloudapi_v2.rb', line 8

def client_secret
  @client_secret
end

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/logstream/cloudapi_v2.rb', line 8

def endpoint
  @endpoint
end

Instance Method Details

#get(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstream/cloudapi_v2.rb', line 16

def get(path)
  bearer_token = get_token
  uri = URI.parse("#{CLOUDAPI_ENDPOINT}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request['Authorization'] = "Bearer #{bearer_token}"
  response = http.request(request)
  parsed = JSON.parse(response.body) rescue nil
  case response.code.to_i
  when 200
    raise Error, "Unexpected reply #{response.body}" unless parsed
    parsed
  else
    raise Error, "HTTP #{response.code}: #{response.body}"
  end
end

#get_application_environments(application_uuid) ⇒ Object

Raises:



34
35
36
37
38
39
# File 'lib/logstream/cloudapi_v2.rb', line 34

def get_application_environments(application_uuid)
  response = get("/applications/#{application_uuid}/environments") #, { :query => { "filter" => "name%3D#{env}"}})
  raise Error, "No Environments found." if response['total'] == 0
  raise Error, "Unexpected reply #{response}" unless response['_embedded']['items']
  response['_embedded']['items']
end

#get_envirornment_logstream(environment_uuid) ⇒ Object

Raises:



41
42
43
44
45
# File 'lib/logstream/cloudapi_v2.rb', line 41

def get_envirornment_logstream(environment_uuid)
  response = get("/environments/#{environment_uuid}/logstream")
  raise Error, "Unexpected reply #{response}" unless response['logstream']
  response['logstream']
end

#get_tokenObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/logstream/cloudapi_v2.rb', line 47

def get_token
  uri = URI.parse("https://accounts.acquia.com/api/auth/oauth/token")
  response = Net::HTTP.post_form(uri, 'client_id' => @client_id, 'client_secret' => @client_secret, 'grant_type' => 'client_credentials')
  parsed = JSON.parse(response.body) rescue nil
  case response.code.to_i
  when 200
    raise Error, "Unexpected reply #{response.body}" unless parsed["access_token"]
    parsed["access_token"]
  else
    raise Error, "HTTP #{response.code}: #{response.body}"
  end
end