Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actions/recipe.go: Add custom function map for generating v5 UUID #434

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions actions/image_partition_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ checks in boot time. By default is set to `true` allowing checks on boot.
ext2, ext3, ext4 and xfs.

- partuuid -- GPT partition UUID string.
A version 5 UUID can be easily generated using the uuid5 template function
{{ uuid5 $namespace $data }} $namespace should be a valid UUID and $data can be
any string, to generate reproducible UUID value pass a fixed value of namespace
and data.

- extendedoptions -- list of additional filesystem extended options which need
to be enabled for the partition.
Expand Down
8 changes: 8 additions & 0 deletions actions/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following custom template functions are available:

- sector: Returns the argument * 512 (convential sector size) e.g. `{{ sector 64 }}`
- escape: Shell escape the argument `{{ escape $var }}`
- uuid5: Generates fixed UUID value `{{ uuid5 $random-uuid $text }}`
- functions from [slim-sprig](https://go-task.github.io/slim-sprig/)

Mandatory properties for recipe:
Expand Down Expand Up @@ -88,6 +89,7 @@ import (
"log"
"strings"
"reflect"
"github.com/google/uuid"
)

/* the YamlAction just embed the Action interface and implements the
Expand Down Expand Up @@ -158,6 +160,11 @@ func escape(s string) string {
return shellescape.Quote(s)
}

func uuid5(namespace string, data string) string {
id := uuid.NewSHA1(uuid.MustParse(namespace), []byte(data))
return id.String()
}

func DumpActionStruct(iface interface{}) string {
var a []string

Expand Down Expand Up @@ -245,6 +252,7 @@ func (r *Recipe) Parse(file string, printRecipe bool, dump bool, templateVars ..
funcs := template.FuncMap{
"sector": sector,
"escape": escape,
"uuid5": uuid5,
}
t.Funcs(funcs)

Expand Down
15 changes: 15 additions & 0 deletions tests/templating/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ actions:
description: sprig conversion function
chroot: false
command: test 42 -eq {{ 0x2a | int }}

# expected.uuid5 contains value generated with $namespace and $text1
{{ $namespace := or .namespace "6c9d1418-8fa9-11ee-b4ca-325096b39f47" }}
{{ $text1 := or .text1 "ver1.0" }}
{{ $text2 := or .text2 "ver2.0" }}
{{ $expected := or .expected "3deb2a0e-692c-5a37-93c0-1123948bf5ae" }}

- action: run
description: test uuid5 generate same value when input is same
chroot: false
command: test {{ uuid5 $namespace $text1 }} = {{ $expected }}
- action: run
description: test uuid5 generate different value when input is different
chroot: false
command: test {{ uuid5 $namespace $text2 }} != {{ $expected }}