Class: Puppet::Provider::NexusRepository::NexusRepository

Inherits:
ResourceApi::SimpleProvider
  • Object
show all
Defined in:
lib/puppet/provider/nexus_repository/nexus_repository.rb

Overview

Implementation for the nexus_repository type using the Resource API.

Instance Method Summary collapse

Constructor Details

#initializeNexusRepository

Init connection to the rest api



9
10
11
12
13
14
# File 'lib/puppet/provider/nexus_repository/nexus_repository.rb', line 9

def initialize
  super
  local_device = Puppet::Util::NetworkDevice::Config.devices['localhost_nexus_rest_api']
  config_file = local_device['url'] unless local_device.nil?
  Puppet::ResourceApi::Transport.inject_device('nexus_rest_api', config_file) unless File.exist?(config_file)
end

Instance Method Details

#create(context, name, should) ⇒ Object

Create repositories not yet existing in nexus repository manager



55
56
57
58
59
60
61
# File 'lib/puppet/provider/nexus_repository/nexus_repository.rb', line 55

def create(context, name, should)
  attributes = should[:attributes]
  attributes[:name] = name
  res = context.transport.post_request(context, "repositories/#{should[:format]}/#{should[:type]}", attributes)

  context.err(res.body) unless res.success?
end

#delete(context, name) ⇒ Object

Delete repository which is set to absent



83
84
85
86
87
# File 'lib/puppet/provider/nexus_repository/nexus_repository.rb', line 83

def delete(context, name)
  res = context.transport.delete_request(context, "repositories/#{name}")

  context.err(res.body) unless res.success?
end

#get(context, names = nil) ⇒ Object

Return requested repositories as resources



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/provider/nexus_repository/nexus_repository.rb', line 17

def get(context, names = nil)
  res = context.transport.get_request(context, 'repositories')

  context.err(res.body) unless res.success?

  Puppet::Util::Json.load(res.body).map { |repo|
    next unless names.include?(repo['name']) # only query asked repository

    real_format = case repo['format']
                  when 'maven2'
                    'maven'
                  else
                    repo['format']
                  end

    res = context.transport.get_request(context, "repositories/#{real_format}/#{repo['type']}/#{repo['name']}")

    next unless res.success? # skip unsupported repository types

    attributes = Puppet::Util::Json.load(res.body)

    # Remove static elements as we can't change them without recreation of the resource
    attributes.delete('format')
    attributes.delete('name')
    attributes.delete('type')
    attributes.delete('url')

    {
      name: repo['name'],
      ensure: 'present',
      format: real_format,
      type: repo['type'],
      attributes: attributes
    }
  }.compact
end

#update(context, name, should) ⇒ Object

Update repository settings on existing repository



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/provider/nexus_repository/nexus_repository.rb', line 64

def update(context, name, should)
  attributes = should[:attributes]
  attributes[:name] = name
  res = context.transport.put_request(context, "repositories/#{should[:format]}/#{should[:type]}/#{name}", attributes)

  case should[:type]
  when 'group'
    context.transport.post_request(context, "repositories/#{name}/invalidate-cache", '')
  when 'hosted'
    context.transport.post_request(context, "repositories/#{name}/rebuild-index", '')
  when 'proxy'
    context.transport.post_request(context, "repositories/#{name}/invalidate-cache", '')
    context.transport.post_request(context, "repositories/#{name}/rebuild-index", '')
  end

  context.err(res.body) unless res.success?
end