Skip to content

Commit

Permalink
add JsonnetNatevieFuncs
Browse files Browse the repository at this point in the history
provides native functions for go-jsonnet.
  • Loading branch information
fujiwara committed Jun 13, 2024
1 parent 1aee386 commit b79fbee
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.30.4
github.com/aws/aws-sdk-go-v2/service/sts v1.18.4
github.com/google/go-cmp v0.6.0
github.com/google/go-jsonnet v0.20.0
github.com/hashicorp/go-tfe v1.2.0
github.com/itchyny/gojq v0.12.11
github.com/manifoldco/promptui v0.9.0
Expand Down Expand Up @@ -54,7 +55,7 @@ require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -75,7 +76,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/nsf/termbox-go v0.0.0-20181027232701-60ab7e3d12ed // indirect
Expand All @@ -101,4 +102,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
16 changes: 14 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -146,6 +147,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-jsonnet v0.20.0 h1:WG4TTSARuV7bSm4PMB4ohjxe33IHT5WVTrJSU33uT4g=
github.com/google/go-jsonnet v0.20.0/go.mod h1:VbgWF9JX7ztlv770x/TolZNGGFfiHEVx9G6ca2eUmeA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw=
Expand Down Expand Up @@ -188,9 +191,11 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand All @@ -215,6 +220,7 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/simeji/jid v0.7.6 h1:AfqyVUyxhoMXjECfQ+0UoxLL+atEMM4DF6GjqKg+klM=
github.com/simeji/jid v0.7.6/go.mod h1:aWC2wZw1IZvbeAh/UcbA86BznfPyxsMSVTcvzWX18q0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -275,6 +281,8 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -341,11 +349,15 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
4 changes: 2 additions & 2 deletions tfstate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const (
defaultFuncName = "tfstate"
)

// FuncMap provides a tamplate.FuncMap tfstate based on URL and provide
// FuncMap provides a template.FuncMap tfstate based on URL and provide
func FuncMap(ctx context.Context, stateLoc string) (template.FuncMap, error) {
return FuncMapWithName(ctx, defaultFuncName, stateLoc)
}

// FuncMapWithName provides a tamplate.FuncMap. can lockup values from tfstate.
// FuncMapWithName provides a template.FuncMap. can lockup values from tfstate.
func FuncMapWithName(ctx context.Context, name string, stateLoc string) (template.FuncMap, error) {
state, err := ReadURL(ctx, stateLoc)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions tfstate/jsonnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tfstate

import (
"context"
"fmt"

"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
)

// JsonnetNativeFuncs provides the native functions for go-jsonnet.
func JsonnetNativeFuncs(ctx context.Context, prefix, stateLoc string) ([]*jsonnet.NativeFunction, error) {
state, err := ReadURL(ctx, stateLoc)
if err != nil {
return nil, fmt.Errorf("failed to read tfstate: %s %w", stateLoc, err)
}
return []*jsonnet.NativeFunction{
{
Name: prefix + "tfstate",
Params: []ast.Identifier{"address"},
Func: func(args []interface{}) (interface{}, error) {
if len(args) != 1 {
return nil, fmt.Errorf("tfstate expects 1 argument")
}
addr, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("tfstate expects string argument")
}
attrs, err := state.Lookup(addr)
if err != nil {
return nil, fmt.Errorf("failed to lookup %s in tfstate: %w", addr, err)
}
if attrs.Value == nil {
return nil, fmt.Errorf("%s is not found in tfstate", addr)
}
return attrs.Value, nil
},
},
}, nil
}
56 changes: 56 additions & 0 deletions tfstate/jsonnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tfstate_test

import (
"bytes"
"context"
"encoding/json"
"testing"

"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/google/go-cmp/cmp"
"github.com/google/go-jsonnet"
)

func TestJsonnetNativeFunc(t *testing.T) {
ctx := context.Background()
funcs, err := tfstate.JsonnetNativeFuncs(ctx, "myfunc_", "./test/terraform.tfstate")
if err != nil {
t.Fatal(err)
}
vm := jsonnet.MakeVM()
for _, fn := range funcs {
vm.NativeFunction(fn)
}
out, err := vm.EvaluateAnonymousSnippet("test.jsonnet", `
local tfstate = std.native("myfunc_tfstate");
{
arn: tfstate("aws_acm_certificate.main.arn"), // string
subject_alternative_names: tfstate("aws_acm_certificate.main.subject_alternative_names"), // array
subject_alternative_names_0: tfstate("aws_acm_certificate.main.subject_alternative_names[0]"), // string
tags: tfstate("aws_acm_certificate.main.tags"), // object
tags_env: tfstate("aws_acm_certificate.main.tags").env, // string
}`+"\n")
if err != nil {
t.Fatal(err)
}
ob := new(bytes.Buffer)
if err := json.Indent(ob, []byte(out), "", " "); err != nil {
t.Fatal(err)
}
eb := new(bytes.Buffer)
expect := `{
"arn": "arn:aws:acm:ap-northeast-1:123456789012:certificate/4986a36e-7027-4265-864b-1fe32f96d774",
"subject_alternative_names": ["*.example.com"],
"subject_alternative_names_0": "*.example.com",
"tags": {
"env": "world"
},
"tags_env": "world"
}` + "\n"
if err := json.Indent(eb, []byte(expect), "", " "); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(ob.String(), eb.String()); diff != "" {
t.Errorf("unexpected output: %s", diff)
}
}

0 comments on commit b79fbee

Please sign in to comment.