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

Import functions from Terraform #4

Merged
merged 35 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1325850
import cidr functions from terraform
azr Jan 23, 2020
ddecdf5
test cidr folder
azr Jan 23, 2020
a0249ba
test crypto folder
azr Jan 24, 2020
2601cd9
add crypto files
azr Jan 24, 2020
04faee9
add crypto files from terraform
azr Jan 24, 2020
4b02bba
split files a little
azr Jan 24, 2020
f8bec26
Update config.yml
azr Jan 24, 2020
bd0862c
add filesystem files
azr Jan 24, 2020
514f4c6
Update config.yml
azr Jan 24, 2020
a314ad1
document better
azr Jan 24, 2020
2f4b52e
update comments
azr Jan 24, 2020
72a6d70
rewrap comments
azr Jan 24, 2020
094b4ac
Merge branch 'init-crypto' into mergeall
azr Jan 24, 2020
476681a
Merge branch 'init-cidr' into mergeall
azr Jan 24, 2020
5699632
split files in their respective pkgs
azr Jan 31, 2020
4f0cd73
remove file funcs from crypto
azr Jan 31, 2020
660c686
tidy crypto's modules
azr Jan 31, 2020
b080ee4
Update config.yml
azr Feb 3, 2020
343223b
add url encode funcs
azr Feb 3, 2020
59c998e
tidy go modules
azr Feb 3, 2020
fc7196f
add test script
azr Feb 3, 2020
0fba4f8
fail if there are any diffs
azr Feb 3, 2020
aaecbb1
Update config.yml
azr Feb 3, 2020
6fa7916
Update config.yml
azr Feb 3, 2020
8368378
fix test in TestFileSet
azr Feb 3, 2020
03c7bd8
Update .test.sh
azr Feb 3, 2020
eeb5774
fix test in TestDirname for windows
azr Feb 3, 2020
68349bf
refactor test cases
azr Feb 3, 2020
64565d4
test uuid folder
azr Feb 3, 2020
c92509f
Update filesystem_test.go
azr Feb 3, 2020
fe97005
V4: use "github.com/google/uuid".NewRandom instead of "github.com/has…
azr Feb 20, 2020
6f9b6d7
Delete hello.tmpl
azr Feb 20, 2020
6bf392c
Revert "Delete hello.tmpl"
azr Feb 20, 2020
29d7e30
Update hello.tmpl
azr Feb 20, 2020
d968fa0
make it one module
azr Apr 23, 2020
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
14 changes: 9 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,31 @@ commands:
steps:
- run: mkdir ~/goroot && curl https://dl.google.com/go/go<< parameters.GOVERSION >>.windows-amd64.zip --output ~/go<< parameters.GOVERSION >>.windows-amd64.zip
- run: unzip ~/go<< parameters.GOVERSION >>.windows-amd64.zip -d ~/goroot
go-test:
go-tests:
parameters:
FOLDER:
type: string
GO:
type: string
default: go
steps:
- run: cd << parameters.FOLDER >> && << parameters.GO >> env && << parameters.GO >> test .

- run: << parameters.GO >> test ./...
jobs:
test-linux:
executor: golang
working_directory: /go/src/github.com/hashicorp/go-cty-funcs
steps:
- checkout
- go-tests

test-darwin:
executor: darwin
working_directory: ~/go/src/github.com/hashicorp/go-cty-funcs
steps:
- checkout
- install-go-unix:
GOOS: darwin
- go-tests:
GO: ~/goroot/go/bin/go
test-windows:
executor:
name: win/vs2019
Expand All @@ -59,6 +61,8 @@ jobs:
steps:
- checkout
- install-go-windows
- go-tests:
GO: ~/goroot/go/bin/go
check-fmt:
executor: golang
steps:
Expand Down
13 changes: 13 additions & 0 deletions .test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set -e
clear
go fmt ./...
goimports -w */*.go
for f in */
do
pushd $f > /dev/null
go mod tidy
go test .
popd > /dev/null
done

git diff --exit-code --ignore-space-change --ignore-space-at-eol
49 changes: 49 additions & 0 deletions cidr/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cidr

import (
"fmt"
"net"

"github.com/apparentlymart/go-cidr/cidr"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/gocty"
)

// HostFunc is a function that calculates a full host IP address within a given
// IP network address prefix.
var HostFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "prefix",
Type: cty.String,
},
{
Name: "hostnum",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var hostNum int
if err := gocty.FromCtyValue(args[1], &hostNum); err != nil {
return cty.UnknownVal(cty.String), err
}
_, network, err := net.ParseCIDR(args[0].AsString())
if err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err)
}

ip, err := cidr.Host(network, hostNum)
if err != nil {
return cty.UnknownVal(cty.String), err
}

return cty.StringVal(ip.String()), nil
},
})

// Host calculates a full host IP address within a given IP network address prefix.
func Host(prefix, hostnum cty.Value) (cty.Value, error) {
return HostFunc.Call([]cty.Value{prefix, hostnum})
}
79 changes: 79 additions & 0 deletions cidr/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cidr

import (
"fmt"
"testing"

"github.com/zclconf/go-cty/cty"
)

func TestHost(t *testing.T) {
tests := []struct {
Prefix cty.Value
Hostnum cty.Value
Want cty.Value
Err bool
}{
{
cty.StringVal("192.168.1.0/24"),
cty.NumberIntVal(5),
cty.StringVal("192.168.1.5"),
false,
},
{
cty.StringVal("192.168.1.0/24"),
cty.NumberIntVal(-5),
cty.StringVal("192.168.1.251"),
false,
},
{
cty.StringVal("192.168.1.0/24"),
cty.NumberIntVal(-256),
cty.StringVal("192.168.1.0"),
false,
},
{
cty.StringVal("192.168.1.0/30"),
cty.NumberIntVal(255),
cty.UnknownVal(cty.String),
true, // 255 doesn't fit in two bits
},
{
cty.StringVal("192.168.1.0/30"),
cty.NumberIntVal(-255),
cty.UnknownVal(cty.String),
true, // 255 doesn't fit in two bits
},
{
cty.StringVal("not-a-cidr"),
cty.NumberIntVal(6),
cty.UnknownVal(cty.String),
true, // not a valid CIDR mask
},
{
cty.StringVal("10.256.0.0/8"),
cty.NumberIntVal(6),
cty.UnknownVal(cty.String),
true, // can't have an octet >255
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("cidrhost(%#v, %#v)", test.Prefix, test.Hostnum), func(t *testing.T) {
got, err := Host(test.Prefix, test.Hostnum)

if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
34 changes: 34 additions & 0 deletions cidr/netmask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cidr

import (
"fmt"
"net"

"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)

// NetmaskFunc is a function that converts an IPv4 address prefix given in CIDR
// notation into a subnet mask address.
var NetmaskFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "prefix",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
_, network, err := net.ParseCIDR(args[0].AsString())
if err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err)
}

return cty.StringVal(net.IP(network.Mask).String()), nil
},
})

// Netmask converts an IPv4 address prefix given in CIDR notation into a subnet mask address.
func Netmask(prefix cty.Value) (cty.Value, error) {
return NetmaskFunc.Call([]cty.Value{prefix})
}
66 changes: 66 additions & 0 deletions cidr/netmask_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cidr

import (
"fmt"
"testing"

"github.com/zclconf/go-cty/cty"
)

func TestNetmask(t *testing.T) {
tests := []struct {
Prefix cty.Value
Want cty.Value
Err bool
}{
{
cty.StringVal("192.168.1.0/24"),
cty.StringVal("255.255.255.0"),
false,
},
{
cty.StringVal("192.168.1.0/32"),
cty.StringVal("255.255.255.255"),
false,
},
{
cty.StringVal("0.0.0.0/0"),
cty.StringVal("0.0.0.0"),
false,
},
{
cty.StringVal("1::/64"),
cty.StringVal("ffff:ffff:ffff:ffff::"),
false,
},
{
cty.StringVal("not-a-cidr"),
cty.UnknownVal(cty.String),
true, // not a valid CIDR mask
},
{
cty.StringVal("110.256.0.0/8"),
cty.UnknownVal(cty.String),
true, // can't have an octet >255
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("cidrnetmask(%#v)", test.Prefix), func(t *testing.T) {
got, err := Netmask(test.Prefix)

if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
66 changes: 66 additions & 0 deletions cidr/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cidr

import (
"fmt"
"net"

"github.com/apparentlymart/go-cidr/cidr"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/gocty"
)

// SubnetFunc is a function that calculates a subnet address within a given
// IP network address prefix.
var SubnetFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "prefix",
Type: cty.String,
},
{
Name: "newbits",
Type: cty.Number,
},
{
Name: "netnum",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var newbits int
if err := gocty.FromCtyValue(args[1], &newbits); err != nil {
return cty.UnknownVal(cty.String), err
}
var netnum int
if err := gocty.FromCtyValue(args[2], &netnum); err != nil {
return cty.UnknownVal(cty.String), err
}

_, network, err := net.ParseCIDR(args[0].AsString())
if err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err)
}

// For portability with 32-bit systems where the subnet number will be
// a 32-bit int, we only allow extension of 32 bits in one call even if
// we're running on a 64-bit machine. (Of course, this is significant
// only for IPv6.)
if newbits > 32 {
return cty.UnknownVal(cty.String), fmt.Errorf("may not extend prefix by more than 32 bits")
}

newNetwork, err := cidr.Subnet(network, newbits, netnum)
if err != nil {
return cty.UnknownVal(cty.String), err
}

return cty.StringVal(newNetwork.String()), nil
},
})

// Subnet calculates a subnet address within a given IP network address prefix.
func Subnet(prefix, newbits, netnum cty.Value) (cty.Value, error) {
return SubnetFunc.Call([]cty.Value{prefix, newbits, netnum})
}
Loading