Migrate grants to more structured output (#194)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-07-16 22:18:27 -04:00
1 parent 1a1d3058c9
commit 99653727bb
19 files changed
+757 -642

No files matched your search

-43
View File
@@ -1,17 +1,7 @@
// Code generated by "make api"; DO NOT EDIT.
package api
import (
"encoding/json"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/watchtower/api/internal/strutil"
)
type Error struct {
defaultFields []string
// The HTTP Status code applicable to this error
Status int32 `json:"status,omitempty"`
@@ -23,36 +13,3 @@ type Error struct {
// different fields will be populated.
Details *ErrorDetails `json:"details,omitempty"`
}
func (s *Error) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"code": "code", "details": "details", "message": "message", "status": "status"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
return
}
}
}
func (s *Error) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"code": "code", "details": "details", "message": "message", "status": "status"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
return
}
}
}
func (s Error) 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)
}
-44
View File
@@ -1,52 +1,8 @@
// Code generated by "make api"; DO NOT EDIT.
package api
import (
"encoding/json"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/watchtower/api/internal/strutil"
)
type ErrorDetails struct {
defaultFields []string
TraceId string `json:"TraceId,omitempty"`
RequestId string `json:"request_id,omitempty"`
RequestFields []*FieldError `json:"request_fields,omitempty"`
}
func (s *ErrorDetails) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"requestfields": "request_fields", "requestid": "request_id", "traceid": "TraceId"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
return
}
}
}
func (s *ErrorDetails) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"requestfields": "request_fields", "requestid": "request_id", "traceid": "TraceId"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
return
}
}
}
func (s ErrorDetails) 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)
}
-44
View File
@@ -1,51 +1,7 @@
// Code generated by "make api"; DO NOT EDIT.
package api
import (
"encoding/json"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/watchtower/api/internal/strutil"
)
type FieldError struct {
defaultFields []string
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
func (s *FieldError) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"description": "description", "name": "name"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
return
}
}
}
func (s *FieldError) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"description": "description", "name": "name"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
return
}
}
}
func (s FieldError) 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)
}
+41 -20
View File
@@ -11,31 +11,35 @@ type structInfo struct {
structFields string
parentName string
detailName string
outputOnly bool
templateType templateType
nameJsonMap map[string]string
}
var inputStructs = []*structInfo{
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/error.pb.go",
inName: "Error",
outFile: os.Getenv("GEN_BASEPATH") + "/api/error.gen.go",
outName: "Error",
outPkg: "api",
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/error.pb.go",
inName: "Error",
outFile: os.Getenv("GEN_BASEPATH") + "/api/error.gen.go",
outName: "Error",
outPkg: "api",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/error.pb.go",
inName: "ErrorDetails",
outFile: os.Getenv("GEN_BASEPATH") + "/api/error_details.gen.go",
outName: "ErrorDetails",
outPkg: "api",
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/error.pb.go",
inName: "ErrorDetails",
outFile: os.Getenv("GEN_BASEPATH") + "/api/error_details.gen.go",
outName: "ErrorDetails",
outPkg: "api",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/error.pb.go",
inName: "FieldError",
outFile: os.Getenv("GEN_BASEPATH") + "/api/field_error.gen.go",
outName: "FieldError",
outPkg: "api",
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/error.pb.go",
inName: "FieldError",
outFile: os.Getenv("GEN_BASEPATH") + "/api/field_error.gen.go",
outName: "FieldError",
outPkg: "api",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host.pb.go",
@@ -101,11 +105,28 @@ var inputStructs = []*structInfo{
outPkg: "roles",
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/roles/role.pb.go",
inName: "Principal",
outFile: os.Getenv("GEN_BASEPATH") + "/api/roles/principal.gen.go",
outName: "Principal",
outPkg: "roles",
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/roles/role.pb.go",
inName: "Principal",
outFile: os.Getenv("GEN_BASEPATH") + "/api/roles/principal.gen.go",
outName: "Principal",
outPkg: "roles",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/roles/role.pb.go",
inName: "Grant",
outFile: os.Getenv("GEN_BASEPATH") + "/api/roles/grant.gen.go",
outName: "Grant",
outPkg: "roles",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/roles/role.pb.go",
inName: "GrantJson",
outFile: os.Getenv("GEN_BASEPATH") + "/api/roles/grant_json.gen.go",
outName: "GrantJson",
outPkg: "roles",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_catalog.pb.go",
+1 -1
View File
@@ -78,7 +78,7 @@ func parsePBs() {
}
// Add default fields if a base resource
if inputStruct.templateType == templateTypeResource {
if inputStruct.templateType == templateTypeResource && !inputStruct.outputOnly {
st.Fields.List = append([]*ast.Field{{
Names: []*ast.Ident{
{
+4 -1
View File
@@ -26,11 +26,13 @@ func writeStructTemplates() {
Package string
StructFields string
NameJsonMap map[string]string
OutputOnly bool
}{
Name: inputStruct.outName,
Package: inputStruct.outPkg,
StructFields: inputStruct.structFields,
NameJsonMap: inputStruct.nameJsonMap,
OutputOnly: inputStruct.outputOnly,
})
case templateTypeDetail:
@@ -45,7 +47,6 @@ func writeStructTemplates() {
ParentName: inputStruct.parentName,
DetailName: inputStruct.detailName,
})
}
outFile, err := filepath.Abs(inputStruct.outFile)
@@ -84,6 +85,7 @@ type {{ .Name }} struct {
{{ .StructFields }}
}
{{ if (not .OutputOnly) }}
func (s *{{ .Name }}) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := {{ printf "%#v" .NameJsonMap }}
@@ -116,6 +118,7 @@ func (s {{ .Name }}) MarshalJSON() ([]byte, error) {
}
return json.Marshal(m)
}
{{ end }}
`))
var detailTemplate = template.Must(template.New("").Parse(
+20
View File
@@ -0,0 +1,20 @@
// Code generated by "make api"; DO NOT EDIT.
package roles
import (
"github.com/hashicorp/watchtower/api"
)
type Grant struct {
Client *api.Client `json:"-"`
// The original user-supplied string.
// Output only.
Raw string `json:"raw,omitempty"`
// The canonically formatted string.
// Output only.
Canonical string `json:"canonical,omitempty"`
// The JSON representation of the grant.
// Output only.
Json *GrantJson `json:"json,omitempty"`
}
+20
View File
@@ -0,0 +1,20 @@
// Code generated by "make api"; DO NOT EDIT.
package roles
import (
"github.com/hashicorp/watchtower/api"
)
type GrantJson struct {
Client *api.Client `json:"-"`
// The ID, if set.
// Output only.
Id string `json:"id,omitempty"`
// The type, if set.
// Output only.
Type string `json:"type,omitempty"`
// The actions.
// Output only.
Actions []string `json:"actions,omitempty"`
}
-41
View File
@@ -2,20 +2,12 @@
package roles
import (
"encoding/json"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/internal/strutil"
)
type Principal struct {
Client *api.Client `json:"-"`
defaultFields []string
// The ID of the principal.
// Output only.
Id string `json:"id,omitempty"`
@@ -26,36 +18,3 @@ type Principal struct {
// Output only.
ScopeId string `json:"scope_id,omitempty"`
}
func (s *Principal) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"id": "id", "scopeid": "scope_id", "type": "type"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
return
}
}
}
func (s *Principal) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"id": "id", "scopeid": "scope_id", "type": "type"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
return
}
}
}
func (s Principal) 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)
}
+5 -8
View File
@@ -50,18 +50,15 @@ type Role struct {
Principals []*Principal `json:"principals,omitempty"`
// The grants that this role provides for its principals.
// Output only.
Grants []string `json:"grants,omitempty"`
// The canonical version of the grants in the grants field with the same index.
GrantStrings []string `json:"grant_strings,omitempty"`
// The parsed grant information.
// Output only.
GrantsCanonical []string `json:"grants_canonical,omitempty"`
// The JSON version of the grants in the grants field with the same index.
// Output only.
GrantsJson []string `json:"grants_json,omitempty"`
Grants []*Grant `json:"grants,omitempty"`
}
func (s *Role) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "grants": "grants", "grantscanonical": "grants_canonical", "grantscopeid": "grant_scope_id", "grantsjson": "grants_json", "id": "id", "name": "name", "principalids": "principal_ids", "principals": "principals", "updatedtime": "updated_time", "version": "version"}
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "grants": "grants", "grantscopeid": "grant_scope_id", "grantstrings": "grant_strings", "id": "id", "name": "name", "principalids": "principal_ids", "principals": "principals", "updatedtime": "updated_time", "version": "version"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
@@ -72,7 +69,7 @@ func (s *Role) SetDefault(key string) {
func (s *Role) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "grants": "grants", "grantscanonical": "grants_canonical", "grantscopeid": "grant_scope_id", "grantsjson": "grants_json", "id": "id", "name": "name", "principalids": "principal_ids", "principals": "principals", "updatedtime": "updated_time", "version": "version"}
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "grants": "grants", "grantscopeid": "grant_scope_id", "grantstrings": "grant_strings", "id": "id", "name": "name", "principalids": "principal_ids", "principals": "principals", "updatedtime": "updated_time", "version": "version"}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)