From 24d82ebe4e03ae606222df68a6648d36aac982a4 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Sun, 3 May 2020 13:45:54 -0400 Subject: [PATCH] Add conversion test and fix some stuff --- api/client.go | 13 +++++---- api/hosts/awsec2_host_catalog.go | 6 ++-- api/hosts/host.go | 2 +- api/hosts/host_catalog.go | 2 +- api/hosts/host_set.go | 2 +- api/hosts/static_host_catalog.go | 6 ++-- api/internal/genapi/genapi.go | 4 +-- api/internal/tests/detail_template_test.go | 34 ++++++++++++++++++++++ api/util.go | 6 ++++ go.mod | 1 + 10 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 api/internal/tests/detail_template_test.go diff --git a/api/client.go b/api/client.go index 9b712159af..d7c54108e5 100644 --- a/api/client.go +++ b/api/client.go @@ -6,7 +6,6 @@ import ( "crypto/tls" "encoding/json" "fmt" - "io" "net" "net/http" "net/url" @@ -528,7 +527,7 @@ func (c *Client) Clone() (*Client, error) { func copyHeaders(in http.Header) http.Header { ret := make(http.Header) - for k, v := range in.headers { + for k, v := range in { for _, val := range v { ret[k] = append(ret[k], val) } @@ -685,10 +684,12 @@ func (c *Client) Do(r *http.Request) (*http.Response, error) { } var result *http.Response - resp, err := client.Do(req) - if resp != nil { - result = &Response{Response: resp} - } + result, err = client.Do(req) + /* + if resp != nil { + result = &Response{Response: resp} + } + */ if err != nil { if strings.Contains(err.Error(), "tls: oversized") { err = errwrap.Wrapf( diff --git a/api/hosts/awsec2_host_catalog.go b/api/hosts/awsec2_host_catalog.go index 1308fded92..07771c5d42 100644 --- a/api/hosts/awsec2_host_catalog.go +++ b/api/hosts/awsec2_host_catalog.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2020-05-03 11:13:42.5271736 -0400 EDT m=+0.049578501 +// 2020-05-03 13:43:08.126021 -0400 EDT m=+0.032030001 package hosts import ( @@ -22,9 +22,9 @@ type AwsEc2HostCatalog struct { Rotate *bool `json:"rotate,omitempty"` } -func (s *HostCatalog) AsAwsEc2HostCatalog() (*AwsEc2HostCatalog, error) { +func (s HostCatalog) AsAwsEc2HostCatalog() (*AwsEc2HostCatalog, error) { out := &AwsEc2HostCatalog{ - HostCatalog: s, + HostCatalog: &s, } decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ Result: out, diff --git a/api/hosts/host.go b/api/hosts/host.go index 4a7a88f890..3d3b8f56c6 100644 --- a/api/hosts/host.go +++ b/api/hosts/host.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2020-05-03 11:13:42.5265109 -0400 EDT m=+0.048915801 +// 2020-05-03 13:43:08.1253741 -0400 EDT m=+0.031383201 package hosts import ( diff --git a/api/hosts/host_catalog.go b/api/hosts/host_catalog.go index 6ab949d580..17d2483ae7 100644 --- a/api/hosts/host_catalog.go +++ b/api/hosts/host_catalog.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2020-05-03 11:13:42.5269139 -0400 EDT m=+0.049318801 +// 2020-05-03 13:43:08.1257653 -0400 EDT m=+0.031774501 package hosts import ( diff --git a/api/hosts/host_set.go b/api/hosts/host_set.go index 9c22d4b3db..346edab20b 100644 --- a/api/hosts/host_set.go +++ b/api/hosts/host_set.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2020-05-03 11:13:42.5267614 -0400 EDT m=+0.049166301 +// 2020-05-03 13:43:08.1256396 -0400 EDT m=+0.031648601 package hosts import ( diff --git a/api/hosts/static_host_catalog.go b/api/hosts/static_host_catalog.go index 2d749f282e..1d248671f4 100644 --- a/api/hosts/static_host_catalog.go +++ b/api/hosts/static_host_catalog.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2020-05-03 11:13:42.5270389 -0400 EDT m=+0.049443801 +// 2020-05-03 13:43:08.1258801 -0400 EDT m=+0.031889201 package hosts import ( @@ -13,9 +13,9 @@ type StaticHostCatalog struct { *HostCatalog } -func (s *HostCatalog) AsStaticHostCatalog() (*StaticHostCatalog, error) { +func (s HostCatalog) AsStaticHostCatalog() (*StaticHostCatalog, error) { out := &StaticHostCatalog{ - HostCatalog: s, + HostCatalog: &s, } decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ Result: out, diff --git a/api/internal/genapi/genapi.go b/api/internal/genapi/genapi.go index 2a94847379..0b69a06c07 100644 --- a/api/internal/genapi/genapi.go +++ b/api/internal/genapi/genapi.go @@ -445,9 +445,9 @@ type {{ .DetailName }} struct { {{ .StructFields }} } -func (s *{{ .ParentName }}) As{{ .DetailName }}() (*{{ .DetailName }}, error) { +func (s {{ .ParentName }}) As{{ .DetailName }}() (*{{ .DetailName }}, error) { out := &{{ .DetailName }}{ - {{ .ParentName }}: s, + {{ .ParentName }}: &s, } decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ Result: out, diff --git a/api/internal/tests/detail_template_test.go b/api/internal/tests/detail_template_test.go new file mode 100644 index 0000000000..3cc48a1be9 --- /dev/null +++ b/api/internal/tests/detail_template_test.go @@ -0,0 +1,34 @@ +package tests + +import ( + "testing" + "time" + + "github.com/hashicorp/watchtower/api" + "github.com/hashicorp/watchtower/api/hosts" + "github.com/stretchr/testify/assert" +) + +func TestDetailTemplating(t *testing.T) { + lt := time.Now() + c := hosts.HostCatalog{ + Path: api.String("path"), + CreatedTime: lt, + Attributes: map[string]interface{}{ + "regions": []string{"a", "b"}, + "access_key": "access", + "secret_key": "secret", + "rotate": true, + }, + } + + ac, err := c.AsAwsEc2HostCatalog() + assert.NoError(t, err) + assert.Equal(t, &hosts.AwsEc2HostCatalog{ + HostCatalog: &c, + Regions: []string{"a", "b"}, + AccessKey: api.String("access"), + SecretKey: api.String("secret"), + Rotate: api.Bool(true), + }, ac) +} diff --git a/api/util.go b/api/util.go index 950685f37a..e7c1ca6f8b 100644 --- a/api/util.go +++ b/api/util.go @@ -1,5 +1,11 @@ package api +func Bool(in bool) *bool { + ret := new(bool) + *ret = in + return ret +} + func String(in string) *string { ret := new(string) *ret = in diff --git a/go.mod b/go.mod index 9fd63cfa92..db23fb27a7 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,7 @@ require ( github.com/ryanuber/columnize v2.1.0+incompatible github.com/ryanuber/go-glob v1.0.0 github.com/spf13/viper v1.6.3 // indirect + github.com/stretchr/testify v1.5.1 go.mongodb.org/mongo-driver v1.3.2 // indirect golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect