Class: PuppetX::Rustup::Provider::Collection::Subresources

Inherits:
PuppetX::Rustup::Provider::Collection
  • Object
show all
Defined in:
lib/puppet_x/rustup/provider/collection/subresources.rb

Overview

A subresource collection

Direct Known Subclasses

Targets, Toolchains

Instance Method Summary collapse

Instance Method Details

#group_subresources_by_toolchain(requested) { ... } ⇒ Object

Split subresource up by toolchain for management

This also verifies that none of the subresources were requested for toolchains with ensure => absent.

Parameters:

Yields:

  • A block that takes |toolchain, subresources|



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppet_x/rustup/provider/collection/subresources.rb', line 46

def group_subresources_by_toolchain(requested)
  by_toolchain = requested.group_by do |subresource|
    @provider.normalize_toolchain_or_default(subresource['toolchain'])
  end

  @provider.toolchains.system.each do |toolchain|
    yield toolchain['name'], by_toolchain.delete(toolchain['name']) || []
  end

  # Find subresources that were requested for uninstalled toolchains.
  missing_toolchains = []
  by_toolchain.each do |toolchain_name, subresources|
    if subresources.any? { |subresource| subresource['ensure'] != 'absent' }
      missing_toolchains << toolchain_name
    end
  end

  unless missing_toolchains.empty?
    raise Puppet::Error, "#{plural_name} were requested for toolchains " \
      "that are not installed: #{missing_toolchains.join(', ')}"
  end
end

#installed?(name) ⇒ Boolean

Is the passed subresource already installed?

Parameters:

  • name (String)

    Normalized subresource name.

Returns:

  • (Boolean)


13
14
15
# File 'lib/puppet_x/rustup/provider/collection/subresources.rb', line 13

def installed?(name)
  system.any? { |info| info['name'] == name }
end

#manage(requested, purge) ⇒ Object

Install and uninstall subresources as appropriate.

Note that this does not update the internal state after changing the system. You must call ‘load` after this if you need the state to be correct.

This takes ‘resource` as a parameter instead of using the set value of this collection because the value isn’t set if it is initially unchanged. That means if the values on the system change after `load` was called but before this method was called, we won’t be able to tell.



26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet_x/rustup/provider/collection/subresources.rb', line 26

def manage(requested, purge)
  system_grouped = system.group_by { |info| info['toolchain'] }
  group_subresources_by_toolchain(requested) do |toolchain, subresources|
    unmanaged = manage_group(system_grouped[toolchain] || [], subresources)

    if purge
      uninstall_all(unmanaged)
    end
  end
end

#manage_group(on_system, requested) ⇒ Object

Install or uninstall a group of subresources by comparing them to what’s already present on the system.

Returns subresourecs found on the system, but not declared in the resource.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet_x/rustup/provider/collection/subresources.rb', line 73

def manage_group(on_system, requested)
  requested.each do |subresource|
    subresource['normalized_name'] = normalize(subresource['name'])

    # Remove installed subresource from the on_system list.
    found = on_system.reject! do |info|
      info['name'] == subresource['normalized_name']
    end

    if subresource['ensure'] == 'absent'
      if found
        uninstall(subresource)
      end
    elsif found.nil? || subresource['ensure'] == 'latest'
      # ensure == 'present' implied
      install(subresource)
    end
  end

  # Return unmanaged subresources.
  on_system
end

#uninstall_all(subresources) ⇒ Object

Uninstall all subresourcecs in an array.



97
98
99
100
101
# File 'lib/puppet_x/rustup/provider/collection/subresources.rb', line 97

def uninstall_all(subresources)
  subresources.each do |subresource|
    uninstall(subresource)
  end
end