Puppet Function: wmflib::resource::import

Defined in:
modules/wmflib/functions/resource/import.pp
Function type:
Puppet Language

Summary

Realise a set of exported resources but filter out duplicats.

Overview

wmflib::resource::import(Wmflib::Resource::Type $resource, Optional[String[1]] $resource_title = undef, Hash $parameters = {}, Boolean $merge_contents = false)Hash[String, Hash]

SPDX-License-Identifier: Apache-2.0 # this allow multiple hosts to export resources with the same title but we only realise one

When realising resources parameters are merged with undetermined preference
as such all resources should have the same set of parameters

Parameters:

  • resource (Wmflib::Resource::Type)

    the resource type to search for

  • resource_title (Optional[String[1]]) (defaults to: undef)

    a regex to use when seraching for the title

  • parameters (Hash) (defaults to: {})

    a hash of parameters to filter on

  • merge_content

    if true and resoure == 'File' merge the contents parameter

  • merge_contents (Boolean) (defaults to: false)

Returns:



10
11
12
13
14
15
16
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
53
54
# File 'modules/wmflib/functions/resource/import.pp', line 10

function wmflib::resource::import (
    Wmflib::Resource::Type $resource,
    Optional[String[1]]    $resource_title = undef,
    Hash                   $parameters     = {},
    Boolean                $merge_contents = false,
) >> Hash[String, Hash] {
    $_resource = wmflib::resource::capitalize($resource)
    $_title = $resource_title ? {
        undef   => '',
        # TODO: i suspect classes cant be exported but not sure
        'Class' => "and title = \"${wmflib::resource::capitalize($resource_title)}\"",
        default => "and title = \"${resource_title}\"",
    }
    $_paramters = $parameters.empty ? {
        true    => '',
        default => $parameters.reduce('') |$memo, $value| { "${memo} and parameters.${value[0]} = \"${value[1]}\"" }
    }
    $pql = @("PQL")
    resources[parameters, title] {
        type = "${_resource}" and exported = true
        ${_title}
        ${_paramters}
    }
    | PQL
    # Following is useful for debugging, we should add proper spec tests
    # notify { $pql: }
    $unique_resources = wmflib::puppetdb_query($pql).reduce({}) |$memo, $resource| {
        # TODO: when we export we prfix the title with the following
        # wmflib::resource::export||
        # feels like we could do something better here.
        $clean_title = $resource['title'].split('\|\|')[1]
        if $merge_contents and $clean_title in $memo and 'content' in $resource['parameters'] {
            $content = "${memo[$clean_title]['content']}${resource['parameters']['content']}"
            $parameters = $resource['parameters'] + { 'content' => $content }
        } else {
            $parameters = $resource['parameters']
        }
        $memo + { $clean_title => $parameters }
    }
    unless $unique_resources.empty {
        create_resources($resource.downcase, $unique_resources)
    }
    # Useful to return this for testing if nothing else
    $unique_resources
}