address review feedback

This commit is contained in:
Mahmood Ali 2020-10-22 11:49:37 -04:00
parent 9c0a93a604
commit d3a17b5c82
8 changed files with 80 additions and 81 deletions

View File

@ -60,16 +60,21 @@ type ScalingRequest struct {
// ScalingPolicy is the user-specified API object for an autoscaling policy
type ScalingPolicy struct {
ID string `hcl:"id,optional"`
Namespace string `hcl:"namespace,optional"`
Type string `hcl:"type,optional"`
Target map[string]string `hcl:"target,optional"`
Min *int64 `hcl:"min,optional"`
Max *int64 `hcl:"max,optional"`
Policy map[string]interface{} `hcl:"policy,block"`
Enabled *bool `hcl:"enabled,optional"`
CreateIndex uint64 `hcl:"create_index,optional"`
ModifyIndex uint64 `hcl:"modify_index,optional"`
/* fields set by user in HCL config */
Min *int64 `hcl:"min,optional"`
Max *int64 `hcl:"max,optional"`
Policy map[string]interface{} `hcl:"policy,block"`
Enabled *bool `hcl:"enabled,optional"`
/* fields set by server */
ID string
Namespace string
Type string
Target map[string]string
CreateIndex uint64
ModifyIndex uint64
}
// ScalingPolicyListStub is used to return a subset of scaling policy information

View File

@ -523,3 +523,25 @@ func (w *uiErrorWriter) Close() error {
}
return nil
}
// parseVars decodes a slice of `<key>=<val>` or `<key>` strings into a golang map.
//
// `<key>` without corresponding value, is mapped to the `<key>` environment variable.
func parseVars(vars []string) map[string]string {
if len(vars) == 0 {
return nil
}
result := make(map[string]string, len(vars))
for _, v := range vars {
parts := strings.SplitN(v, "=", 2)
k := parts[0]
if len(parts) == 2 {
result[k] = parts[1]
} else {
result[k] = os.Getenv(k)
}
}
return result
}

View File

@ -392,3 +392,14 @@ func TestUiErrorWriter(t *testing.T) {
expectedErr += "and thensome more\n"
require.Equal(t, expectedErr, errBuf.String())
}
func TestParseVars(t *testing.T) {
input := []string{"key1=val1", "HOME", "key2=321"}
expected := map[string]string{
"key1": "val1",
"HOME": os.Getenv("HOME"),
"key2": "321",
}
require.Equal(t, expected, parseVars(input))
}

View File

@ -328,22 +328,3 @@ func parseCheckIndex(input string) (uint64, bool, error) {
u, err := strconv.ParseUint(input, 10, 64)
return u, true, err
}
func parseVars(vars []string) map[string]string {
if len(vars) == 0 {
return nil
}
result := make(map[string]string, len(vars))
for _, v := range vars {
parts := strings.SplitN(v, "=", 2)
k := parts[0]
if len(parts) == 2 {
result[k] = parts[1]
} else {
result[k] = os.Getenv(k)
}
}
return result
}

View File

@ -84,23 +84,21 @@ func Functions(basedir string, allowFS bool) map[string]function.Function {
"split": stdlib.SplitFunc,
"strrev": stdlib.ReverseFunc,
"substr": stdlib.SubstrFunc,
//"timestamp": pkrfunction.TimestampFunc,
"timeadd": stdlib.TimeAddFunc,
"title": stdlib.TitleFunc,
"trim": stdlib.TrimFunc,
"trimprefix": stdlib.TrimPrefixFunc,
"trimspace": stdlib.TrimSpaceFunc,
"trimsuffix": stdlib.TrimSuffixFunc,
"try": tryfunc.TryFunc,
"upper": stdlib.UpperFunc,
"urlencode": encoding.URLEncodeFunc,
"uuidv4": uuid.V4Func,
"uuidv5": uuid.V5Func,
"values": stdlib.ValuesFunc,
//"vault": pkrfunction.VaultFunc,
"yamldecode": ctyyaml.YAMLDecodeFunc,
"yamlencode": ctyyaml.YAMLEncodeFunc,
"zipmap": stdlib.ZipmapFunc,
"timeadd": stdlib.TimeAddFunc,
"title": stdlib.TitleFunc,
"trim": stdlib.TrimFunc,
"trimprefix": stdlib.TrimPrefixFunc,
"trimspace": stdlib.TrimSpaceFunc,
"trimsuffix": stdlib.TrimSuffixFunc,
"try": tryfunc.TryFunc,
"upper": stdlib.UpperFunc,
"urlencode": encoding.URLEncodeFunc,
"uuidv4": uuid.V4Func,
"uuidv5": uuid.V5Func,
"values": stdlib.ValuesFunc,
"yamldecode": ctyyaml.YAMLDecodeFunc,
"yamlencode": ctyyaml.YAMLEncodeFunc,
"zipmap": stdlib.ZipmapFunc,
// filesystem calls
"abspath": guardFS(allowFS, filesystem.AbsPathFunc),
@ -133,12 +131,3 @@ func guardFS(allowFS bool, fn function.Function) function.Function {
return function.New(spec)
}
// var unimplFunc = function.New(&function.Spec{
// Type: func([]cty.Value) (cty.Type, error) {
// return cty.DynamicPseudoType, fmt.Errorf("function not yet implemented")
// },
// Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
// return cty.DynamicVal, fmt.Errorf("function not yet implemented")
// },
// })

View File

@ -18,11 +18,12 @@ import (
// meta { ... }
// }
// ```
// to
//
// ```
// config {
// meta { ... }
// meta = { ... } # <- attribute now
// }
// ```
func BlocksAsAttrs(body hcl.Body) hcl.Body {

View File

@ -38,21 +38,6 @@ func TestEquivalentToHCL1(t *testing.T) {
}
}
func TestParse_Variables(t *testing.T) {
hcl := `
job "example" {
datacenters = [for s in ["dc1", "dc2"] : upper(s)]
region = vars.region_var
}
`
out, err := ParseWithArgs("input.hcl", strings.NewReader(hcl), map[string]string{"region_var": "aug"}, true)
require.NoError(t, err)
require.Equal(t, []string{"DC1", "DC2"}, out.Datacenters)
require.Equal(t, "aug", *out.Region)
}
func TestParse_VarsAndFunctions(t *testing.T) {
hcl := `
job "example" {

View File

@ -60,16 +60,21 @@ type ScalingRequest struct {
// ScalingPolicy is the user-specified API object for an autoscaling policy
type ScalingPolicy struct {
ID string `hcl:"id,optional"`
Namespace string `hcl:"namespace,optional"`
Type string `hcl:"type,optional"`
Target map[string]string `hcl:"target,optional"`
Min *int64 `hcl:"min,optional"`
Max *int64 `hcl:"max,optional"`
Policy map[string]interface{} `hcl:"policy,block"`
Enabled *bool `hcl:"enabled,optional"`
CreateIndex uint64 `hcl:"create_index,optional"`
ModifyIndex uint64 `hcl:"modify_index,optional"`
/* fields set by user in HCL config */
Min *int64 `hcl:"min,optional"`
Max *int64 `hcl:"max,optional"`
Policy map[string]interface{} `hcl:"policy,block"`
Enabled *bool `hcl:"enabled,optional"`
/* fields set by server */
ID string
Namespace string
Type string
Target map[string]string
CreateIndex uint64
ModifyIndex uint64
}
// ScalingPolicyListStub is used to return a subset of scaling policy information