Class: Puppet::Transport::NexusRestApi

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/transport/nexus_rest_api.rb

Overview

The main connection class to a NexusRestApi endpoint

Instance Method Summary collapse

Constructor Details

#initialize(context, connection_info) ⇒ NexusRestApi

Initialise this transport with a set of credentials



8
9
10
11
# File 'lib/puppet/transport/nexus_rest_api.rb', line 8

def initialize(context, connection_info)
  @connection_info = connection_info
  verify(context)
end

Instance Method Details

#build_optionsObject

Return the options hash with basic auth credentials used by the http client



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/transport/nexus_rest_api.rb', line 19

def build_options
  if File.exist?(@connection_info[:tmp_pw_file])
    username = 'admin'
    password = File.read(@connection_info[:tmp_pw_file])
  else
    username = @connection_info[:username]
    password = @connection_info[:password].unwrap
  end

  {
    basic_auth: {
      user: username,
      password: password
    }
  }
end

#build_uri(endpoint) ⇒ Object

Build the complete rest api uri



14
15
16
# File 'lib/puppet/transport/nexus_rest_api.rb', line 14

def build_uri(endpoint)
  URI("http://#{@connection_info[:address]}:#{@connection_info[:port]}/service/rest/v1/#{endpoint}")
end

#close(_context) ⇒ Object

Close the connection and release all resources



107
108
109
# File 'lib/puppet/transport/nexus_rest_api.rb', line 107

def close(_context)
  @client = nil
end

#delete_request(_context, endpoint) ⇒ Object

JSON delete request against the given api endpoint



84
85
86
87
88
89
90
91
92
# File 'lib/puppet/transport/nexus_rest_api.rb', line 84

def delete_request(_context, endpoint)
  Puppet.runtime[:http].delete(
    build_uri(endpoint),
    headers: {
      'Content-Type' => 'application/json'
    },
    options: build_options,
  )
end

#facts(_context) ⇒ Object

Retrieve facts from the target and return in a hash



102
103
104
# File 'lib/puppet/transport/nexus_rest_api.rb', line 102

def facts(_context)
  {}
end

#get_request(_context, endpoint) ⇒ Object

JSON get request against the given api endpoint



37
38
39
40
41
42
43
44
45
# File 'lib/puppet/transport/nexus_rest_api.rb', line 37

def get_request(_context, endpoint)
  Puppet.runtime[:http].get(
    build_uri(endpoint),
    headers: {
      'Content-Type' => 'application/json'
    },
    options: build_options,
  )
end

#post_request(_context, endpoint, data) ⇒ Object

JSON post request against the given api endpoint



72
73
74
75
76
77
78
79
80
81
# File 'lib/puppet/transport/nexus_rest_api.rb', line 72

def post_request(_context, endpoint, data)
  Puppet.runtime[:http].post(
    build_uri(endpoint),
    Puppet::Util::Json.dump(data),
    headers: {
      'Content-Type' => 'application/json'
    },
    options: build_options,
  )
end

#put_request(_context, endpoint, data) ⇒ Object

JSON put request against the given api endpoint



60
61
62
63
64
65
66
67
68
69
# File 'lib/puppet/transport/nexus_rest_api.rb', line 60

def put_request(_context, endpoint, data)
  Puppet.runtime[:http].put(
    build_uri(endpoint),
    Puppet::Util::Json.dump(data),
    headers: {
      'Content-Type' => 'application/json'
    },
    options: build_options,
  )
end

#put_request_text(_context, endpoint, data) ⇒ Object

Plaintext put request against the given api endpoint



48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/transport/nexus_rest_api.rb', line 48

def put_request_text(_context, endpoint, data)
  Puppet.runtime[:http].put(
    build_uri(endpoint),
    data,
    headers: {
      'Content-Type' => 'text/plain'
    },
    options: build_options,
  )
end

#verify(context) ⇒ Object

Verifies that the stored credentials are valid, and that we can talk to the target



95
96
97
98
99
# File 'lib/puppet/transport/nexus_rest_api.rb', line 95

def verify(context)
  context.debug("Checking connection to #{@connection_info[:address]}:#{@connection_info[:port]}")

  raise 'authentication error' unless get_request(context, 'status').success?
end