Class: Terraspace::Terraform::Api::Http

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util::Logging
Defined in:
lib/terraspace/terraform/api/http.rb,
lib/terraspace/terraform/api/http/concern.rb

Defined Under Namespace

Modules: Concern

Constant Summary collapse

API =
ENV['TERRAFORM_API'] || 'https://app.terraform.io/api/v2'

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Instance Method Details

#build_request(klass, url, data = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/terraspace/terraform/api/http.rb', line 35

def build_request(klass, url, data={})
  req = klass.new(url) # url includes query string and uri.path does not, must used url
  set_headers!(req)
  if [Net::HTTP::Delete, Net::HTTP::Patch, Net::HTTP::Post, Net::HTTP::Put].include?(klass)
    text = JSON.dump(data)
    req.body = text
    req.content_length = text.bytesize
  end
  req
end

#delete(path, data = {}) ⇒ Object



22
23
24
# File 'lib/terraspace/terraform/api/http.rb', line 22

def delete(path, data={})
  request(Net::HTTP::Delete, path, data)
end

#get(path) ⇒ Object



10
11
12
# File 'lib/terraspace/terraform/api/http.rb', line 10

def get(path)
  request(Net::HTTP::Get, path)
end

#http(url) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/terraspace/terraform/api/http.rb', line 51

def http(url)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = http.read_timeout = 30
  http.use_ssl = true if uri.scheme == 'https'
  http
end

#load_json(res) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/terraspace/terraform/api/http.rb', line 65

def load_json(res)
  if ok?(res.code)
    JSON.load(res.body)
  else
    logger.info "Error: Non-successful http response status code: #{res.code}"
    logger.info "headers: #{res.each_header.to_h.inspect}"
    raise "TFC API called failed"
  end
end

#ok?(http_code) ⇒ Boolean

Note: 422 is Unprocessable Entity. This means an invalid data payload was sent. We want that to error and raise

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/terraspace/terraform/api/http.rb', line 77

def ok?(http_code)
  http_code =~ /^20/ ||
  http_code =~ /^40/
end

#patch(path, data = {}) ⇒ Object



18
19
20
# File 'lib/terraspace/terraform/api/http.rb', line 18

def patch(path, data={})
  request(Net::HTTP::Patch, path, data)
end

#post(path, data = {}) ⇒ Object



14
15
16
# File 'lib/terraspace/terraform/api/http.rb', line 14

def post(path, data={})
  request(Net::HTTP::Post, path, data)
end

#request(klass, path, data = {}) ⇒ Object

Always translate raw json response to ruby Hash



27
28
29
30
31
32
33
# File 'lib/terraspace/terraform/api/http.rb', line 27

def request(klass, path, data={})
  url = url(path)
  http = http(url)
  req = build_request(klass, url, data)
  resp = http.request(req) # send request
  load_json(resp)
end

#set_headers!(req) ⇒ Object



46
47
48
49
# File 'lib/terraspace/terraform/api/http.rb', line 46

def set_headers!(req)
  req['Authorization'] = "Bearer #{token}"
  req['Content-Type'] = "application/vnd.api+json"
end

#tokenObject



82
83
84
# File 'lib/terraspace/terraform/api/http.rb', line 82

def token
  Token.get
end

#url(path) ⇒ Object

API does not include the /. IE: app.terraform.io/api/v2



61
62
63
# File 'lib/terraspace/terraform/api/http.rb', line 61

def url(path)
  "#{API}/#{path}"
end