Skip to content

Commit

Permalink
feat: finding variables and resolving identifiers works; started on s…
Browse files Browse the repository at this point in the history
…etting values by identifier
  • Loading branch information
lukasjarosch committed Aug 13, 2022
1 parent a92dcab commit 21d0890
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 207 deletions.
33 changes: 30 additions & 3 deletions data.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package skipper

import (
"log"
"strings"
)

//type Data map[string]interface{}
type Data map[string]interface{}

const IdentifierSeparator = "."
Expand All @@ -20,6 +20,7 @@ func (d Data) Get(k string) Data {
return d[k].(Data)
}

// HasValueAtIdentifier returns true if the given identifier path points to a value.
func (d Data) HasValueAtIdentifier(path string) bool {
if d.GetByIdentifier(path) == nil {
return false
Expand All @@ -28,13 +29,15 @@ func (d Data) HasValueAtIdentifier(path string) bool {
}

// GetByIdentifier returns a value given a dot-separated identifier.
// Note: array indices (foo.bar.2) are NOT supported.
// You can only index map values which do have a string index (which is everything except arrays).
// TODO: add support for array indexing (e.g. 'foo.bar.3.baz')
func (d Data) GetByIdentifier(path string) interface{} {
var segments = strings.Split(path, IdentifierSeparator)

obj := d

for i, v := range segments {

// we found the last segment of the identifier path
if i == len(segments)-1 {
return obj[v]
}
Expand All @@ -50,6 +53,30 @@ func (d Data) GetByIdentifier(path string) interface{} {
return obj
}

func (d *Data) SetByIdentifier(path string, value interface{}) {
var segments = strings.Split(path, IdentifierSeparator)

obj := (*d)

for i, v := range segments {

// we found the last segment of the identifier path
if i == len(segments)-1 {
log.Println("REPLACE HERE", obj[v])
obj[v] = value
(*d) = obj
return
}

switch obj[v].(type) {
case Data:
obj = Data(obj[v].(Data))
}
}
}

// MergeReplace merges the existing Data with the given.
// If a key already exists, the passed data has precedence and it's value will be used.
func (d Data) MergeReplace(data Data) Data {
out := make(Data, len(d))
for k, v := range d {
Expand Down
2 changes: 1 addition & 1 deletion examples/terraform/compiled/dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
REPLACED
rg-dev-terraform-example

something: ${gitlab:base_url}/${gitlab:project_id}
something: 1234

foo-bar-baz

Expand Down
6 changes: 3 additions & 3 deletions examples/terraform/compiled/dev/terraform/00_setup.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ terraform {
}
}
backend "http" {
address = "<no value>/api/v4/projects/REPLACED/terraform/state/dev-state"
lock_address = "<no value>/api/v4/projects/REPLACED/terraform/state/dev-state/lock"
unlock_address = "<no value>/api/v4/projects/REPLACED/terraform/state/dev-state/lock"
address = "https://gitlab.example.com/api/v4/projects/REPLACED/terraform/state/dev-state"
lock_address = "https://gitlab.example.com/api/v4/projects/REPLACED/terraform/state/dev-state/lock"
unlock_address = "https://gitlab.example.com/api/v4/projects/REPLACED/terraform/state/dev-state/lock"
lock_method = "POST"
unlock_method = "DELETE"
retry_wait_min = 5
Expand Down
24 changes: 13 additions & 11 deletions examples/terraform/inventory/classes/gitlab.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
gitlab:
#base_url: https://gitlab.example.com
#
# project_id: 1234
something: ${gitlab:base_url}/${gitlab:project_id}
another: ${gitlab:base_url}/something/else
evenMore: ${gitlab:project_id}
foo:
bar: baz
this:
- is
- an
base_url: https://gitlab.example.com
project_id: 1234
something: ${gitlab:base_url}/${gitlab:project_id}
another: ${gitlab:base_url}/something/else
evenMore: ${gitlab:project_id}
foo:
- yay: ${gitlab:bar:2}
- baz: ${gitlab:foo:1}
- yay: baz
- this:
- is
- an
- value
22 changes: 20 additions & 2 deletions inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,29 @@ func (inv *Inventory) Load(classPath, targetPath string) error {
for _, class := range inv.classFiles {
// Determine which variables exist in the Data struct and store them (remove duplicates from this)

variables := FindVariables(class.Data()).Deduplicate()
variables := FindVariables(class.Data())
log.Printf("class '%s' contains %d variables", class.Name, len(variables))

if len(variables) == 0 {
break
continue
}

for _, variable := range variables {

// If the variable points to something which could not be resolved, just skip it
if !class.Data().HasValueAtIdentifier(variable.NameAsIdentifier()) {
continue
}

log.Printf("Variable '%s' at position '%s' points to value '%v'",
variable.Name, variable.Identifier, class.Data().GetByIdentifier(variable.NameAsIdentifier()))

sourceValue := class.Data().GetByIdentifier(variable.Identifier)
targetValue := class.Data().GetByIdentifier(variable.NameAsIdentifier())

class.Data().SetByIdentifier(variable.Identifier, targetValue)

log.Println(sourceValue, targetValue, strings.ReplaceAll(fmt.Sprint(sourceValue), "${"+variable.Name+"}", fmt.Sprint(targetValue)))
}

/*
Expand Down
Loading

0 comments on commit 21d0890

Please sign in to comment.