From 0bf1af9cfc7c33a265324f376c83409802de41eb Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Fri, 1 May 2020 09:53:15 -0400 Subject: [PATCH] Add all static host types and update to handle array fields --- Makefile | 2 +- api/hosts/static_host.go | 51 ++++++++++++ api/hosts/{host.go => static_host_catalog.go} | 22 ++--- api/hosts/static_host_set.go | 57 +++++++++++++ api/internal/genapi/genapi.go | 82 +++++++++++++------ go.mod | 2 + .../api/resources/hosts/static_host_set.pb.go | 10 +-- .../resources/hosts/v1/static_host_set.proto | 2 +- 8 files changed, 184 insertions(+), 44 deletions(-) create mode 100644 api/hosts/static_host.go rename api/hosts/{host.go => static_host_catalog.go} (62%) create mode 100644 api/hosts/static_host_set.go diff --git a/Makefile b/Makefile index 98b9ce8919..44204bfdaf 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ bootstrap: go generate -tags tools tools/tools.go api: - go generate -tags genapi api/internal/genapi/makeimports.go + APIGEN_BASEPATH=$(shell pwd) go generate -tags genapi api/internal/genapi/makeimports.go ### oplog requires protoc-gen-go v1.20.0 or later # GO111MODULE=on go get -u github.com/golang/protobuf/protoc-gen-go@v1.40 diff --git a/api/hosts/static_host.go b/api/hosts/static_host.go new file mode 100644 index 0000000000..ec784b7130 --- /dev/null +++ b/api/hosts/static_host.go @@ -0,0 +1,51 @@ +// Code generated by go generate; DO NOT EDIT. +// This file was generated by robots at +// 2020-05-01 09:51:58.7285456 -0400 EDT m=+0.014777201 +package hosts + +import ( + "encoding/json" + "time" + + "github.com/fatih/structs" + "github.com/hashicorp/watchtower/api/internal/strutil" +) + +type StaticHost struct { + defaultFields []string + + // Canonical path of the resource from the API's base URI + // Output only. + Path *string `json:"path,omitempty"` + // Friendly name, if set + FriendlyName *string `json:"friendly_name,omitempty"` + // The time this host was created + // Ouput only. + CreatedTime time.Time `json:"created_time,omitempty"` + // The time this host was last updated + // Output only. + UpdatedTime time.Time `json:"updated_time,omitempty"` + // Whether the host is disabled + Disabled *bool `json:"disabled,omitempty"` + // The address (DNS or IP name) used to reach the host + Address *string `json:"address,omitempty"` +} + +func (s *StaticHost) SetDefault(key string) { + s.defaultFields = strutil.AppendIfMissing(s.defaultFields, key) +} + +func (s *StaticHost) UnsetDefault(key string) { + s.defaultFields = strutil.StrListDelete(s.defaultFields, key) +} + +func (s StaticHost) MarshalJSON() ([]byte, error) { + m := structs.Map(s) + if m == nil { + m = make(map[string]interface{}) + } + for _, k := range s.defaultFields { + m[k] = nil + } + return json.Marshal(m) +} diff --git a/api/hosts/host.go b/api/hosts/static_host_catalog.go similarity index 62% rename from api/hosts/host.go rename to api/hosts/static_host_catalog.go index 2dc1d0effd..36279971d7 100644 --- a/api/hosts/host.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-04-30 11:21:04.8535614 -0400 EDT m=+0.008417801 +// 2020-05-01 09:51:58.7287406 -0400 EDT m=+0.014972301 package hosts import ( @@ -11,33 +11,33 @@ import ( "github.com/hashicorp/watchtower/api/internal/strutil" ) -type Host struct { +type StaticHostCatalog struct { defaultFields []string + // Canonical path of the resource from the API's base URI // Output only. - Uri *string `json:"uri,omitempty"` + Path *string `json:"path,omitempty"` + // Friendly name, if set FriendlyName *string `json:"friendly_name,omitempty"` - // The time this host was created. + // The time this host was created // Output only. CreatedTime time.Time `json:"created_time,omitempty"` - // The time this host was last updated. + // The time this host was last updated // Output only. UpdatedTime time.Time `json:"updated_time,omitempty"` - // Marks the host as disabled. Default is false. + // Whether the catalog is disabled Disabled *bool `json:"disabled,omitempty"` - // This field is required. - Address *string `json:"address,omitempty"` } -func (s *Host) SetDefault(key string) { +func (s *StaticHostCatalog) SetDefault(key string) { s.defaultFields = strutil.AppendIfMissing(s.defaultFields, key) } -func (s *Host) UnsetDefault(key string) { +func (s *StaticHostCatalog) UnsetDefault(key string) { s.defaultFields = strutil.StrListDelete(s.defaultFields, key) } -func (s Host) MarshalJSON() ([]byte, error) { +func (s StaticHostCatalog) MarshalJSON() ([]byte, error) { m := structs.Map(s) if m == nil { m = make(map[string]interface{}) diff --git a/api/hosts/static_host_set.go b/api/hosts/static_host_set.go new file mode 100644 index 0000000000..c8067fd0c6 --- /dev/null +++ b/api/hosts/static_host_set.go @@ -0,0 +1,57 @@ +// Code generated by go generate; DO NOT EDIT. +// This file was generated by robots at +// 2020-05-01 09:51:58.7287025 -0400 EDT m=+0.014934201 +package hosts + +import ( + "encoding/json" + "time" + + "github.com/fatih/structs" + "github.com/hashicorp/watchtower/api/internal/strutil" +) + +type StaticHostSet struct { + defaultFields []string + + // Canonical path of the resource from the API's base URI + // Output only. + Path *string `json:"path,omitempty"` + // Friendly name, if set + FriendlyName *string `json:"friendly_name,omitempty"` + // The time this host was created + // Output only. + CreatedTime time.Time `json:"created_time,omitempty"` + // The time this host was last updated + // Output only. + UpdatedTime time.Time `json:"updated_time,omitempty"` + // Whether the host set is disabled + Disabled *bool `json:"disabled,omitempty"` + // The total count of hosts in this host set + // Output only. + Size *int64 `json:"size,omitempty"` + // A list of hosts in this host set + // TODO: Figure out if this should be in the basic HostSet view and what + // view to use on the Hosts. + // Output only. + Hosts []*StaticHost `json:"hosts,omitempty"` +} + +func (s *StaticHostSet) SetDefault(key string) { + s.defaultFields = strutil.AppendIfMissing(s.defaultFields, key) +} + +func (s *StaticHostSet) UnsetDefault(key string) { + s.defaultFields = strutil.StrListDelete(s.defaultFields, key) +} + +func (s StaticHostSet) MarshalJSON() ([]byte, error) { + m := structs.Map(s) + if m == nil { + m = make(map[string]interface{}) + } + for _, k := range s.defaultFields { + m[k] = nil + } + return json.Marshal(m) +} diff --git a/api/internal/genapi/genapi.go b/api/internal/genapi/genapi.go index 8593778a84..f1c357b15e 100644 --- a/api/internal/genapi/genapi.go +++ b/api/internal/genapi/genapi.go @@ -33,10 +33,26 @@ var ( inputStructs = []*generateInfo{ { - "../../../internal/gen/controller/api/resource/host.pb.go", - "Host", - "../../hosts/host.go", - "Host", + os.Getenv("APIGEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/static_host.pb.go", + "StaticHost", + os.Getenv("APIGEN_BASEPATH") + "/api/hosts/static_host.go", + "StaticHost", + "hosts", + "", + }, + { + os.Getenv("APIGEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/static_host_set.pb.go", + "StaticHostSet", + os.Getenv("APIGEN_BASEPATH") + "/api/hosts/static_host_set.go", + "StaticHostSet", + "hosts", + "", + }, + { + os.Getenv("APIGEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/static_host_catalog.pb.go", + "StaticHostCatalog", + os.Getenv("APIGEN_BASEPATH") + "/api/hosts/static_host_catalog.go", + "StaticHostCatalog", "hosts", "", }, @@ -95,34 +111,38 @@ func createStructs() { os.Exit(1) return } + + // Tee up unexported field deletion var elideList []int - defer func() { - var cutCount int - // Remove unexported proto stuff - for _, val := range elideList { - loc := val - cutCount - st.Fields.List = append(st.Fields.List[:loc], st.Fields.List[loc+1:]...) - cutCount++ - } + { + defer func() { + var cutCount int + // Remove unexported proto stuff + for _, val := range elideList { + loc := val - cutCount + st.Fields.List = append(st.Fields.List[:loc], st.Fields.List[loc+1:]...) + cutCount++ + } - // Add default fields - st.Fields.List = append([]*ast.Field{&ast.Field{ - Names: []*ast.Ident{ - { - Name: "defaultFields", - Obj: &ast.Object{ - Kind: ast.Var, + // Add default fields + st.Fields.List = append([]*ast.Field{&ast.Field{ + Names: []*ast.Ident{ + { Name: "defaultFields", + Obj: &ast.Object{ + Kind: ast.Var, + Name: "defaultFields", + }, }, }, - }, - Type: &ast.ArrayType{ - Elt: &ast.Ident{ - Name: "string", + Type: &ast.ArrayType{ + Elt: &ast.Ident{ + Name: "string", + }, }, - }, - }}, st.Fields.List...) - }() + }}, st.Fields.List...) + }() + } for i, field := range st.Fields.List { if !field.Names[0].IsExported() { @@ -142,6 +162,8 @@ func createStructs() { case *ast.Ident: typ.Name = "*" + typ.Name goto TAGMODIFY + case *ast.ArrayType: + goto TAGMODIFY case *ast.StarExpr: switch nextTyp := typ.X.(type) { case *ast.Ident: @@ -181,6 +203,14 @@ func createStructs() { }, Tag: field.Tag, } + case "Int64Value": + st.Fields.List[i] = &ast.Field{ + Names: field.Names, + Type: &ast.Ident{ + Name: "*int64", + }, + Tag: field.Tag, + } default: fmt.Printf("unhandled wrappers selector sel name %q\n", selectorExpr.Sel.Name) os.Exit(1) diff --git a/go.mod b/go.mod index 9744bbe7e2..866767e8e8 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/armon/go-metrics v0.3.3 github.com/bufbuild/buf v0.11.0 github.com/fatih/color v1.9.0 + github.com/fatih/structs v1.1.0 github.com/favadi/protoc-go-inject-tag v1.0.0 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 github.com/go-openapi/runtime v0.19.15 // indirect @@ -38,6 +39,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/posener/complete v1.2.3 github.com/ryanuber/columnize v2.1.0+incompatible + github.com/ryanuber/go-glob v1.0.0 github.com/spf13/viper v1.6.3 // indirect go.mongodb.org/mongo-driver v1.3.2 // indirect golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e diff --git a/internal/gen/controller/api/resources/hosts/static_host_set.pb.go b/internal/gen/controller/api/resources/hosts/static_host_set.pb.go index 6538d8588e..77ceda972e 100644 --- a/internal/gen/controller/api/resources/hosts/static_host_set.pb.go +++ b/internal/gen/controller/api/resources/hosts/static_host_set.pb.go @@ -49,7 +49,7 @@ type StaticHostSet struct { Disabled *wrappers.BoolValue `protobuf:"bytes,5,opt,name=disabled,proto3" json:"disabled,omitempty"` // The total count of hosts in this host set // Output only. - Size *wrappers.Int32Value `protobuf:"bytes,6,opt,name=size,proto3" json:"size,omitempty"` + Size *wrappers.Int64Value `protobuf:"bytes,6,opt,name=size,proto3" json:"size,omitempty"` // A list of hosts in this host set // TODO: Figure out if this should be in the basic HostSet view and what // view to use on the Hosts. @@ -124,7 +124,7 @@ func (x *StaticHostSet) GetDisabled() *wrappers.BoolValue { return nil } -func (x *StaticHostSet) GetSize() *wrappers.Int32Value { +func (x *StaticHostSet) GetSize() *wrappers.Int64Value { if x != nil { return x.Size } @@ -174,7 +174,7 @@ var file_controller_api_resources_hosts_v1_static_host_set_proto_rawDesc = []byt 0x6f, 0x6c, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x08, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x12, 0x2f, 0x0a, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, - 0x66, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x04, 0x73, 0x69, + 0x66, 0x2e, 0x49, 0x6e, 0x74, 0x36, 0x34, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x43, 0x0a, 0x05, 0x68, 0x6f, 0x73, 0x74, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2d, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x68, 0x6f, 0x73, @@ -206,7 +206,7 @@ var file_controller_api_resources_hosts_v1_static_host_set_proto_goTypes = []int (*wrappers.StringValue)(nil), // 1: google.protobuf.StringValue (*timestamp.Timestamp)(nil), // 2: google.protobuf.Timestamp (*wrappers.BoolValue)(nil), // 3: google.protobuf.BoolValue - (*wrappers.Int32Value)(nil), // 4: google.protobuf.Int32Value + (*wrappers.Int64Value)(nil), // 4: google.protobuf.Int64Value (*StaticHost)(nil), // 5: controller.api.resources.hosts.v1.StaticHost } var file_controller_api_resources_hosts_v1_static_host_set_proto_depIdxs = []int32{ @@ -214,7 +214,7 @@ var file_controller_api_resources_hosts_v1_static_host_set_proto_depIdxs = []int 2, // 1: controller.api.resources.hosts.v1.StaticHostSet.created_time:type_name -> google.protobuf.Timestamp 2, // 2: controller.api.resources.hosts.v1.StaticHostSet.updated_time:type_name -> google.protobuf.Timestamp 3, // 3: controller.api.resources.hosts.v1.StaticHostSet.disabled:type_name -> google.protobuf.BoolValue - 4, // 4: controller.api.resources.hosts.v1.StaticHostSet.size:type_name -> google.protobuf.Int32Value + 4, // 4: controller.api.resources.hosts.v1.StaticHostSet.size:type_name -> google.protobuf.Int64Value 5, // 5: controller.api.resources.hosts.v1.StaticHostSet.hosts:type_name -> controller.api.resources.hosts.v1.StaticHost 6, // [6:6] is the sub-list for method output_type 6, // [6:6] is the sub-list for method input_type diff --git a/internal/proto/local/controller/api/resources/hosts/v1/static_host_set.proto b/internal/proto/local/controller/api/resources/hosts/v1/static_host_set.proto index 20ad7dc665..13da06733c 100644 --- a/internal/proto/local/controller/api/resources/hosts/v1/static_host_set.proto +++ b/internal/proto/local/controller/api/resources/hosts/v1/static_host_set.proto @@ -31,7 +31,7 @@ message StaticHostSet { // The total count of hosts in this host set // Output only. - google.protobuf.Int32Value size = 6; + google.protobuf.Int64Value size = 6; // A list of hosts in this host set // TODO: Figure out if this should be in the basic HostSet view and what