-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provides native functions for go-jsonnet.
- Loading branch information
Showing
5 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |