Class: AbideDevUtils::Ppt::ApiClient

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

Constant Summary collapse

CT_JSON =
'application/json'
API_DEFS =
{
  codemanager: {
    port: 8170,
    version: 'v1',
    base: 'code-manager',
    paths: [
      {
        path: 'deploys',
        verbs: %w[post],
        x_auth: true
      }
    ]
  },
  classifier1: {
    port: 4433,
    version: 'v1',
    base: 'classifier-api',
    paths: [
      {
        path: 'groups',
        verbs: %w[get post],
        x_auth: true
      }
    ]
  },
  orchestrator: {
    port: 8143,
    version: 'v1',
    base: 'orchestrator',
    paths: [
      {
        path: 'command/deploy',
        verbs: %w[post],
        x_auth: true
      },
      {
        path: 'command/task',
        verbs: %w[post],
        x_auth: true
      },
      {
        path: 'jobs',
        verbs: %w[get],
        x_auth: true
      }
    ]
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, auth_token: nil, content_type: CT_JSON, custom_ports: {}, verbose: false) ⇒ ApiClient

Returns a new instance of ApiClient.



65
66
67
68
69
70
71
72
# File 'lib/abide_dev_utils/ppt/api.rb', line 65

def initialize(hostname, auth_token: nil, content_type: CT_JSON, custom_ports: {}, verbose: false)
  @hostname = hostname
  @auth_token = auth_token
  @content_type = content_type
  @custom_ports = custom_ports
  @verbose = verbose
  define_api_methods
end

Instance Attribute Details

#auth_token=(value) ⇒ Object (writeonly)

Sets the attribute auth_token

Parameters:

  • value

    the value to set the attribute auth_token to.



12
13
14
# File 'lib/abide_dev_utils/ppt/api.rb', line 12

def auth_token=(value)
  @auth_token = value
end

#content_typeObject

Returns the value of attribute content_type.



13
14
15
# File 'lib/abide_dev_utils/ppt/api.rb', line 13

def content_type
  @content_type
end

#custom_portsObject (readonly)

Returns the value of attribute custom_ports.



11
12
13
# File 'lib/abide_dev_utils/ppt/api.rb', line 11

def custom_ports
  @custom_ports
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



11
12
13
# File 'lib/abide_dev_utils/ppt/api.rb', line 11

def hostname
  @hostname
end

#tls_cert_verifyObject



94
95
96
# File 'lib/abide_dev_utils/ppt/api.rb', line 94

def tls_cert_verify
  @tls_cert_verify = defined?(@tls_cert_verify) ? @tls_cert_verify : false
end

Instance Method Details

#auth_token?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/abide_dev_utils/ppt/api.rb', line 90

def auth_token?
  defined?(@auth_token) && !@auth_token.nil? && !@auth_token.empty?
end

#login(username, password: nil, lifetime: '1h', label: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/abide_dev_utils/ppt/api.rb', line 74

def (username, password: nil, lifetime: '1h', label: nil)
  label = "AbideDevUtils token for #{username} - lifetime #{lifetime}" if label.nil?
  password = IO.console.getpass 'Password: ' if password.nil?
  data = {
    'login' => username,
    'password' => password,
    'lifetime' => lifetime,
    'label' => label
  }
  uri = URI("https://#{@hostname}:4433/rbac-api/v1/auth/token")
  result = http_request(uri, post_request(uri, x_auth: false, **data), json_out: true)
  @auth_token = result['token']
  log_verbose("Successfully logged in? #{auth_token?}")
  auth_token?
end

#no_verboseObject



102
103
104
# File 'lib/abide_dev_utils/ppt/api.rb', line 102

def no_verbose
  @verbose = false
end

#verbose!Object



106
107
108
# File 'lib/abide_dev_utils/ppt/api.rb', line 106

def verbose!
  @verbose = true
end

#verbose?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/abide_dev_utils/ppt/api.rb', line 98

def verbose?
  @verbose
end