blob: d59842e423137f0dad15d50be45d9b215ed89162 [file] [log] [blame]
garciadeblas83775ba2025-07-23 18:35:24 +02001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18# Module with custom functions to manage a Pattern definition, taking into account its corresponding source template and the set of transformations specified for its constituent Bricks.
19
20
21use ../krm *
22use ./replace.nu
23use ./location.nu
24use ./brick.nu
25
26# Generate a ResourceList based on a Pattern instance model received from stdin.
27#
28# Initially, the ResourceList will be generated from the templates at the `source` location (replacing environment variables as needed), and then the transformations indicated by the Bricks will be applied.
29export def create [
30 environment: record = {} # Record with environment variables to load
31]: [
32 record -> record
33] {
34 let in_pattern: record = $in
35
36 # Get the pattern name and its parameters
37 let pattern_name: string = ($in_pattern | get "name")
38 let pattern_params: record = (
39 $in_pattern
40 | get -i "parameters"
41 | default {}
42 # If applicable, update placeholder values at the custom environment parameters
43 | replace vars (
44 $environment
45 | upsert $.PATTERN_NAME $pattern_name
46 )
47 )
48
49 # Update the environment to include the pattern name
50 let updated_environment: record = (
51 $environment
52 | upsert $.PATTERN_NAME $pattern_name
53 | merge $pattern_params
54 )
55
56 # Update the pattern record accordingly
57 let updated_pattern: record = (
58 $in_pattern
59 | replace vars $updated_environment
60 )
61
62 # Get other key parts
63 let src: string = (
64 $updated_pattern
65 | get "source"
66 | location to absolute path
67 )
68 let bricks: list<record> = ($updated_pattern | get "bricks")
69
70 # Generate ResourceList from source template folder
71 let rl: record = (
72 convert folder to resourcelist $src
73 | replace vars $updated_environment
74 )
75
76 # Apply transformations according to the specified bricks
77 with-env $updated_environment {
78 $bricks
79 | reduce --fold $rl {|elt, acc|
80 $acc | brick transform $elt $updated_environment
81 }
82 }
83}