Class: CtRegisterMicroservice::ControlTower

Inherits:
Object
  • Object
show all
Defined in:
lib/ct_register_microservice/control_tower.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeControlTower

Returns a new instance of ControlTower.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ct_register_microservice/control_tower.rb', line 5

def initialize
  if CtRegisterMicroservice.config.ct_url.nil?
    raise MissingConfigError, 'Could not register microservice - No Control Tower URL defined'
  end
  if CtRegisterMicroservice.config.url.nil?
    raise MissingConfigError, 'Could not register microservice - No self URL defined'
  end
  if CtRegisterMicroservice.config.ct_token.nil?
    raise MissingConfigError, 'Could not register microservice - No Control Tower auth token found'
  end
  if CtRegisterMicroservice.config.swagger.nil?
    raise MissingConfigError, 'Could not register microservice - No swagger file defined'
  end
  if !File.exist?(CtRegisterMicroservice.config.swagger)
    raise MissingConfigError, 'Could not register microservice - Swagger file path ' + File.absolute_path(CtRegisterMicroservice.config.swagger) + 'does not match a file'
  end
  if CtRegisterMicroservice.config.name.nil?
    raise MissingConfigError, 'Could not register microservice - Microservice name not defined'
  end

  @options = OpenStruct.new
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def credentials
  @credentials
end

#ct_urlObject (readonly)

Returns the value of attribute ct_url.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def ct_url
  @ct_url
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def response
  @response
end

#swaggerObject (readonly)

Returns the value of attribute swagger.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def swagger
  @swagger
end

#urlObject (readonly)

Returns the value of attribute url.



28
29
30
# File 'lib/ct_register_microservice/control_tower.rb', line 28

def url
  @url
end

Instance Method Details

#initialize_optionsObject



54
55
56
# File 'lib/ct_register_microservice/control_tower.rb', line 54

def initialize_options
  @options = OpenStruct.new
end

#microservice_request(uri, method, headers = {}, body = nil) ⇒ Object

TODO: make sure it works as intended, add unit tests



44
45
46
47
48
49
50
51
52
# File 'lib/ct_register_microservice/control_tower.rb', line 44

def microservice_request(uri, method, headers = {}, body = nil)
  options.http_method = method
  options.endpoint = uri
  options.headers = headers
  options.headers['authorization'] = 'Bearer '+CtRegisterMicroservice.config.ct_token
  options.body = body
  result = make_call(options)
  result
end

#register_service(active = true) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ct_register_microservice/control_tower.rb', line 30

def register_service(active = true)
  options.http_method = "post"
  options.endpoint = "/api/v1/microservice"
  options.query_string = false
  options.body = {
    name: CtRegisterMicroservice.config.name,
    url: CtRegisterMicroservice.config.url,
    active: !!active
  }.to_json
  result = make_call(options)
  result
end