Convert SDK to the new options API (#238)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-07 12:55:02 -04:00
1 parent b5acf6afdf
commit 908a299ea3
162 files changed
+6440 -5891

No files matched your search

+3 -1
View File
@@ -2,8 +2,10 @@
# Be sure to place this BEFORE `include` directives, if any.
THIS_FILE := $(lastword $(MAKEFILE_LIST))
export API_GEN_BASEPATH := ${GEN_BASEPATH}/api
api:
go run .
goimports -w ${GEN_BASEPATH}/api
goimports -w ${API_GEN_BASEPATH}
.PHONY: api
-127
View File
@@ -1,127 +0,0 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
type createInfo struct {
baseType string
targetType string
path string
}
var createFuncs = map[string][]*createInfo{
"scopes": {
{
baseType: "Scope",
targetType: "Scope",
path: `"scopes"`,
},
},
"groups": {
{
baseType: "Group",
targetType: "Group",
path: `"groups"`,
},
},
"users": {
{
baseType: "User",
targetType: "User",
path: `"users"`,
},
},
"roles": {
{
baseType: "Role",
targetType: "Role",
path: `"roles"`,
},
},
"hosts": {
{
baseType: "HostCatalog",
targetType: "Host",
path: `fmt.Sprintf("host-catalogs/%s/hosts", s.Id)`,
},
{
baseType: "HostCatalog",
targetType: "HostSet",
path: `fmt.Sprintf("host-catalogs/%s/host-sets", s.Id)`,
},
},
}
func writeCreateFuncs() {
for outPkg, funcs := range createFuncs {
outFile := os.Getenv("GEN_BASEPATH") + fmt.Sprintf("/api/%s/create.gen.go", outPkg)
outBuf := bytes.NewBuffer([]byte(fmt.Sprintf(
`// Code generated by "make api"; DO NOT EDIT.
package %s
`, outPkg)))
for _, createInfo := range funcs {
createFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: createInfo.baseType,
TargetType: createInfo.targetType,
TargetName: strings.Split(createInfo.targetType, ".")[strings.Count(createInfo.targetType, ".")],
Path: createInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
}
var createFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) Create{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetName }} request")
}
var opts []api.Option
if s.Scope.Id != "" {
// If it's explicitly set here, override anything that might be in the
// client
opts = append(opts, api.WithScopeId(s.Scope.Id))
}
req, err := s.Client.NewRequest(ctx, "POST", {{ .Path }}, r, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Create{{ .TargetName }} call: %w", err)
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Create{{ .TargetName }} repsonse: %w", err)
}
{{ if (eq .TargetType "Scope") }}
target.Client = s.Client.Clone()
target.Client.SetScopeId(target.Id)
{{ else }}
target.Client = s.Client
{{ end }}
return target, apiErr, nil
}
`))
-124
View File
@@ -1,124 +0,0 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
type deleteInfo struct {
baseType string
targetType string
path string
}
var deleteFuncs = map[string][]*deleteInfo{
"scopes": {
{
"Scope",
"Scope",
"scopes",
},
},
"authtokens": {
{
"AuthToken",
"AuthToken",
"auth-tokens",
},
},
"groups": {
{
"Group",
"Group",
"groups",
},
},
"users": {
{
"User",
"User",
"users",
},
},
"roles": {
{
"Role",
"Role",
"roles",
},
},
}
func writeDeleteFuncs() {
for outPkg, funcs := range deleteFuncs {
outFile := os.Getenv("GEN_BASEPATH") + fmt.Sprintf("/api/%s/delete.gen.go", outPkg)
outBuf := bytes.NewBuffer([]byte(fmt.Sprintf(
`// Code generated by "make api"; DO NOT EDIT.
package %s
`, outPkg)))
for _, deleteInfo := range funcs {
deleteFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: deleteInfo.baseType,
TargetType: deleteInfo.targetType,
TargetName: strings.Split(deleteInfo.targetType, ".")[strings.Count(deleteInfo.targetType, ".")],
Path: deleteInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
}
var deleteFuncTemplate = template.Must(template.New("").Parse(
`
// Delete{{ .TargetName }} returns true iff the {{ .TargetType }} existed when the delete attempt was made.
func (s {{ .BaseType }}) Delete{{ .TargetName }}(ctx context.Context, id string) (bool, *api.Error, error) {
if id == "" {
return false, nil, fmt.Errorf("empty id provided to Delete{{ .TargetName }} request")
}
if s.Client == nil {
return false, nil, fmt.Errorf("nil client in Delete{{ .TargetName }} request")
}
var opts []api.Option
if s.Scope.Id != "" {
// If it's explicitly set here, override anything that might be in the
// client
opts = append(opts, api.WithScopeId(s.Scope.Id))
}
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "{{ .Path }}", id), nil, opts...)
if err != nil {
return false, nil, fmt.Errorf("error creating Delete{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete{{ .TargetName }} call: %w", err)
}
type deleteResponse struct {
Existed bool
}
target := &deleteResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return false, nil, fmt.Errorf("error decoding Delete{{ .TargetName }} repsonse: %w", err)
}
return target.Existed, apiErr, nil
}
`))
+210 -120
View File
@@ -1,161 +1,251 @@
package main
import "os"
import (
"text/template"
"github.com/hashicorp/watchtower/internal/gen/controller/api"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/authmethods"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/authtokens"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/groups"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/hosts"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/roles"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/scopes"
"github.com/hashicorp/watchtower/internal/gen/controller/api/resources/users"
"google.golang.org/protobuf/proto"
)
type structureInfo struct {
pkg string
name string
fields []fieldInfo
}
type fieldInfo struct {
Name string
ProtoName string
FieldType string
GenerateSdkOption bool
SubtypeName string
}
type structInfo struct {
inFile string
inName string
outFile string
outName string
outPkg string
structFields string
parentName string
detailName string
outputOnly bool
templateType templateType
nameJsonMap map[string]string
inProto proto.Message
outFile string
generatedStructure structureInfo
templates []*template.Template
// Subtype name for types implementing an abstract resource type. This is
// used as text to insert into With/Default function calls to separate out
// implementations of the same abstract type. This way e.g. a WithUsername
// option turns into WithPasswordAccountUsername which is wordy but
// unambiguous. It also switches the behavior of the functions to work on
// the attributes map.
subtypeName string
// mappings of names of resources and param names for sub slice types, e.g.
// role principals and group members
sliceSubTypes map[string]string
// outputOnly indicates that we shouldn't create options for setting members
// for this struct
outputOnly bool
// versionEnabled indicates that we should build a Version handler in
// update. Eventually everything should support this and this param can go
// away.
versionEnabled bool
// The parameters passed into the path. These should be non-pluralized resource names.
// The templates will convert '-' to '_' and append an _id to them in the SDK param
// and append an 's' to it when building the path.
// The final value should be the path name of the resource since for single resource
// operations all values are used for the function argument.
// For collection based operations the last value is ignored for generating function argument.
pathArgs []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",
inProto: &api.Error{},
outFile: "error.gen.go",
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",
inProto: &api.ErrorDetails{},
outFile: "error_details.gen.go",
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",
inProto: &api.FieldError{},
outFile: "field_error.gen.go",
outputOnly: true,
},
// Scope related resources
{
inProto: &scopes.ScopeInfo{},
outFile: "scopes/scope_info.gen.go",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host.pb.go",
inName: "Host",
outFile: os.Getenv("GEN_BASEPATH") + "/api/hosts/host.gen.go",
outName: "Host",
outPkg: "hosts",
inProto: &scopes.Scope{},
outFile: "scopes/scope.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"scope"},
versionEnabled: true,
},
// User related resources
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_set.pb.go",
inName: "HostSet",
outFile: os.Getenv("GEN_BASEPATH") + "/api/hosts/host_set.gen.go",
outName: "HostSet",
outPkg: "hosts",
inProto: &users.User{},
outFile: "users/user.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"user"},
versionEnabled: true,
},
// Group related resources
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_catalog.pb.go",
inName: "HostCatalog",
outFile: os.Getenv("GEN_BASEPATH") + "/api/hosts/host_catalog.gen.go",
outName: "HostCatalog",
outPkg: "hosts",
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/authtokens/authtoken.pb.go",
inName: "AuthToken",
outFile: os.Getenv("GEN_BASEPATH") + "/api/authtokens/authtoken.gen.go",
outName: "AuthToken",
outPkg: "authtokens",
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/scopes/scope.pb.go",
inName: "Scope",
outFile: os.Getenv("GEN_BASEPATH") + "/api/scopes/scope.gen.go",
outName: "Scope",
outPkg: "scopes",
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/scopes/scope.pb.go",
inName: "ScopeInfo",
outFile: os.Getenv("GEN_BASEPATH") + "/api/info/scope.gen.go",
outName: "Scope",
outPkg: "info",
inProto: &groups.Member{},
outFile: "groups/member.gen.go",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/users/user.pb.go",
inName: "User",
outFile: os.Getenv("GEN_BASEPATH") + "/api/users/user.gen.go",
outName: "User",
outPkg: "users",
inProto: &groups.Group{},
outFile: "groups/group.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
sliceSubTypes: map[string]string{
"Members": "memberIds",
},
pathArgs: []string{"group"},
versionEnabled: true,
},
// Role related resources
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/groups/group.pb.go",
inName: "Group",
outFile: os.Getenv("GEN_BASEPATH") + "/api/groups/group.gen.go",
outName: "Group",
outPkg: "groups",
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/groups/group.pb.go",
inName: "Member",
outFile: os.Getenv("GEN_BASEPATH") + "/api/groups/member.gen.go",
outName: "Member",
outPkg: "groups",
inProto: &roles.Grant{},
outFile: "roles/grant.gen.go",
outputOnly: true,
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/roles/role.pb.go",
inName: "Role",
outFile: os.Getenv("GEN_BASEPATH") + "/api/roles/role.gen.go",
outName: "Role",
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",
inProto: &roles.Principal{},
outFile: "roles/principal.gen.go",
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",
inProto: &roles.GrantJson{},
outFile: "roles/grant_json.gen.go",
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,
inProto: &roles.Role{},
outFile: "roles/role.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
sliceSubTypes: map[string]string{
"Principals": "principalIds",
"Grants": "grantStrings",
},
pathArgs: []string{"role"},
versionEnabled: true,
},
// Auth Methods related resources
{
inProto: &authmethods.AuthMethod{},
outFile: "authmethods/authmethods.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"auth-method"},
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_catalog.pb.go",
inName: "StaticHostCatalogDetails",
outFile: os.Getenv("GEN_BASEPATH") + "/api/hosts/static_host_catalog.gen.go",
outName: "StaticHostCatalogDetails",
outPkg: "hosts",
parentName: "HostCatalog",
detailName: "StaticHostCatalog",
templateType: templateTypeDetail,
inProto: &authmethods.PasswordAuthMethodAttributes{},
outFile: "authmethods/password_auth_method_attributes.gen.go",
subtypeName: "PasswordAuthMethod",
},
{
inFile: os.Getenv("GEN_BASEPATH") + "/internal/gen/controller/api/resources/hosts/host_catalog.pb.go",
inName: "AwsEc2HostCatalogDetails",
outFile: os.Getenv("GEN_BASEPATH") + "/api/hosts/awsec2_host_catalog.gen.go",
outName: "AwsEc2HostCatalogDetails",
outPkg: "hosts",
parentName: "HostCatalog",
detailName: "AwsEc2HostCatalog",
templateType: templateTypeDetail,
inProto: &authmethods.Account{},
outFile: "authmethods/account.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"auth-method", "account"},
},
{
inProto: &authmethods.PasswordAccountAttributes{},
outFile: "authmethods/password_account_attributes.gen.go",
subtypeName: "PasswordAccount",
},
// Auth Tokens
{
inProto: &authtokens.AuthToken{},
outFile: "authtokens/authtokens.gen.go",
templates: []*template.Template{
clientTemplate,
readTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"auth-token"},
},
// Host related resources
{
inProto: &hosts.HostCatalog{},
outFile: "hosts/host_catalog.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"host-catalog"},
},
{
inProto: &hosts.Host{},
outFile: "hosts/host.gen.go",
templates: []*template.Template{
clientTemplate,
createTemplate,
readTemplate,
updateTemplate,
deleteTemplate,
listTemplate,
},
pathArgs: []string{"host-catalog", "host"},
},
}
-128
View File
@@ -1,128 +0,0 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
type listInfo struct {
baseType string
targetType string
path string
}
var listFuncs = map[string][]*listInfo{
"scopes": {
{
baseType: "Scope",
targetType: "Scope",
path: "scopes",
},
},
"authtokens": {
{
baseType: "AuthToken",
targetType: "AuthToken",
path: "auth-tokens",
},
},
"groups": {
{
baseType: "Group",
targetType: "Group",
path: "groups",
},
},
"users": {
{
baseType: "User",
targetType: "User",
path: "users",
},
},
"roles": {
{
baseType: "Role",
targetType: "Role",
path: "roles",
},
},
}
func writeListFuncs() {
for outPkg, funcs := range listFuncs {
outFile := os.Getenv("GEN_BASEPATH") + fmt.Sprintf("/api/%s/list.gen.go", outPkg)
outBuf := bytes.NewBuffer([]byte(fmt.Sprintf(
`// Code generated by "make api"; DO NOT EDIT.
package %s
`, outPkg)))
for _, listInfo := range funcs {
listFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: listInfo.baseType,
TargetType: listInfo.targetType,
TargetName: strings.Split(listInfo.targetType, ".")[strings.Count(listInfo.targetType, ".")],
Path: listInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
}
var listFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) List{{ .TargetName }}s(ctx context.Context) ([]*{{ .TargetType }}, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in List{{ .TargetName }} request")
}
var opts []api.Option
if s.Scope.Id != "" {
// If it's explicitly set here, override anything that might be in the
// client
opts = append(opts, api.WithScopeId(s.Scope.Id))
}
req, err := s.Client.NewRequest(ctx, "GET", "{{ .Path }}", nil, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List{{ .TargetName }}s request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during List{{ .TargetName }}s call: %w", err)
}
type listResponse struct {
Items []*{{ .TargetType }}
}
target := &listResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List{{ .TargetName }}s response: %w", err)
}
for _, t := range target.Items {
{{ if (eq .TargetType "Scope") }}
t.Client = s.Client.Clone()
t.Client.SetScopeId(t.Id)
{{ else }}
t.Client = s.Client
{{ end }}
}
return target.Items, apiErr, nil
}
`))
+1 -6
View File
@@ -2,10 +2,5 @@ package main
func main() {
parsePBs()
writeStructTemplates()
writeCreateFuncs()
writeReadFuncs()
writeUpdateFuncs()
writeListFuncs()
writeDeleteFuncs()
fillTemplates()
}
+92 -274
View File
@@ -1,286 +1,104 @@
package main
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
"strings"
_struct "github.com/golang/protobuf/ptypes/struct"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/hashicorp/watchtower/internal/gen/controller/protooptions"
"github.com/iancoleman/strcase"
)
var regex = regexp.MustCompile(`(json:".*")`)
type visitFn func(node ast.Node)
func (fn visitFn) Visit(node ast.Node) ast.Visitor {
fn(node)
return fn
func printDebug(desc protoreflect.MessageDescriptor) {
fmt.Printf("Evaluating: %q\n", desc.FullName())
fmt.Printf("Name: %q\n", strcase.ToCamel(string(desc.Name())))
fmt.Printf("ProtoName: %q\n", desc.Name())
fmt.Printf("Double Parent: %q\n", desc.FullName().Parent().Parent().Name())
fmt.Printf("Field Descriptors:\n")
for i := 0; i < desc.Fields().Len(); i++ {
fieldDesc := desc.Fields().Get(i)
fmt.Printf(" field %d: %#v\n", i, fieldDesc)
fieldDesc.Options()
}
fmt.Println()
}
func parsePBs() {
for _, inputStruct := range inputStructs {
inFile, err := filepath.Abs(inputStruct.inFile)
if err != nil {
fmt.Printf("error opening file %q: %v\n", inputStruct.inFile, err)
os.Exit(1)
for _, in := range inputStructs {
msg := in.inProto.ProtoReflect()
desc := msg.Descriptor()
//printDebug(desc)
// Evaluate above, populate below.
in.generatedStructure.pkg = packageFromFullName(desc.FullName())
in.generatedStructure.name = string(desc.Name())
for i := 0; i < desc.Fields().Len(); i++ {
fd := desc.Fields().Get(i)
fi := fieldInfo{
Name: strcase.ToCamel(string(fd.Name())),
ProtoName: string(fd.Name()),
}
// Adjust for slices
sliceText := ""
if fd.Cardinality() == protoreflect.Repeated {
sliceText = "[]"
}
// Add generate option info
opts := fd.Options().(*descriptorpb.FieldOptions)
if proto.GetExtension(opts, protooptions.E_GenerateSdkOption).(bool) {
fi.GenerateSdkOption = true
}
switch k := fd.Kind(); k {
case protoreflect.MessageKind:
ptr, pkg, name := messageKind(fd)
if pkg != "" && pkg != in.generatedStructure.pkg {
name = fmt.Sprintf("%s.%s", pkg, name)
}
fi.FieldType = sliceText + ptr + name
default:
fi.FieldType = sliceText + k.String()
}
in.generatedStructure.fields = append(in.generatedStructure.fields, fi)
}
fset := token.NewFileSet()
inAst, err := parser.ParseFile(fset, inFile, nil, parser.ParseComments)
if err != nil {
fmt.Printf("error parsing %s: %v\n", inFile, err)
os.Exit(1)
}
inputStruct.nameJsonMap = make(map[string]string)
ast.Walk(visitFn(func(n ast.Node) {
spec, ok := n.(*ast.TypeSpec)
if !ok {
return
}
if spec.Name == nil {
return
}
if spec.Name.Name != inputStruct.inName {
return
}
spec.Name.Name = inputStruct.outName
st, ok := spec.Type.(*ast.StructType)
if !ok {
fmt.Printf("expected struct type for identifier, got %t\n", spec.Type)
os.Exit(1)
return
}
if st.Fields.List == nil {
fmt.Printf("no fields found in %q\n", inputStruct.inName)
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++
}
// Add default fields if a base resource
if inputStruct.templateType == templateTypeResource && !inputStruct.outputOnly {
st.Fields.List = append([]*ast.Field{{
Names: []*ast.Ident{
{
Name: "defaultFields",
Obj: &ast.Object{
Kind: ast.Var,
Name: "defaultFields",
},
},
},
Type: &ast.ArrayType{
Elt: &ast.Ident{
Name: "string",
},
},
}}, st.Fields.List...)
}
}()
}
for i, field := range st.Fields.List {
if !field.Names[0].IsExported() {
elideList = append(elideList, i)
continue
}
// Anything that isn't a basic type we expect to be a star
// expression with a selector; that is, a wrapper value, timestamp
// value, etc.
//
// TODO: this isn't necessarily a good assumption, which means we
// might get failures with other types. This is an internal tools
// only; we can revisit as needed!
var selectorExpr *ast.SelectorExpr
switch typ := field.Type.(type) {
case *ast.Ident:
goto TAGMODIFY
case *ast.ArrayType:
goto TAGMODIFY
case *ast.StarExpr:
switch nextTyp := typ.X.(type) {
case *ast.Ident:
// Already a pointer, don't do anything
if nextTyp.Name == "ScopeInfo" {
st.Fields.List[i] = &ast.Field{
Names: field.Names,
Type: &ast.Ident{
Name: "info.Scope",
},
Tag: field.Tag,
}
}
goto TAGMODIFY
case *ast.SelectorExpr:
selectorExpr = nextTyp
}
case *ast.SelectorExpr:
selectorExpr = typ
}
switch {
case selectorExpr != nil:
xident, ok := selectorExpr.X.(*ast.Ident)
if !ok {
fmt.Printf("unexpected non-ident type in selector\n")
os.Exit(1)
}
switch xident.Name {
case "wrappers":
switch selectorExpr.Sel.Name {
case "StringValue":
st.Fields.List[i] = &ast.Field{
Names: field.Names,
Type: &ast.Ident{
Name: "*string",
},
Tag: field.Tag,
}
case "BoolValue":
st.Fields.List[i] = &ast.Field{
Names: field.Names,
Type: &ast.Ident{
Name: "*bool",
},
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)
}
case "timestamp":
switch selectorExpr.Sel.Name {
case "Timestamp":
st.Fields.List[i] = &ast.Field{
Names: field.Names,
Type: &ast.Ident{
Name: "time.Time",
},
Tag: field.Tag,
}
default:
fmt.Printf("unhandled timestamp selector sel name %q\n", selectorExpr.Sel.Name)
os.Exit(1)
}
case "scopes":
switch selectorExpr.Sel.Name {
case "ScopeInfo":
st.Fields.List[i] = &ast.Field{
Names: field.Names,
Type: &ast.Ident{
Name: "info.Scope",
},
Tag: field.Tag,
}
default:
fmt.Printf("unhandled scopes selector sel name %q\n", selectorExpr.Sel.Name)
os.Exit(1)
}
case "_struct":
switch selectorExpr.Sel.Name {
case "Struct":
st.Fields.List[i] = &ast.Field{
Names: field.Names,
Type: &ast.MapType{
Key: &ast.Ident{
Name: "string",
},
Value: &ast.InterfaceType{
Methods: &ast.FieldList{},
},
},
Tag: field.Tag,
}
default:
fmt.Printf("unhandled timestamp selector sel name %q\n", selectorExpr.Sel.Name)
os.Exit(1)
}
default:
fmt.Printf("unhandled xident name %q\n", xident.Name)
os.Exit(1)
}
default:
fmt.Println("unhandled non-ident, non-selector case")
os.Exit(1)
}
TAGMODIFY:
tagString := regex.FindString(st.Fields.List[i].Tag.Value)
st.Fields.List[i].Tag.Value = "`" + tagString + "`"
tagString = strings.TrimSuffix(strings.TrimPrefix(tagString, `json:"`), `,omitempty"`)
inputStruct.nameJsonMap[strings.ToLower(field.Names[0].Name)] = tagString
}
}), inAst)
buf := new(bytes.Buffer)
if err := format.Node(buf, fset, inAst); err != nil {
fmt.Printf("error formatting new code: %v\n", err)
os.Exit(1)
}
// We have to manually cut out the lines we don't want because comments
// won't be preserved otherwise. See the note about lossy comments in
// https://arslan.io/2017/09/14/the-ultimate-guide-to-writing-a-go-tool/
scanner := bufio.NewScanner(bytes.NewBufferString(buf.String()))
var inType bool
var outBuf []string
for scanner.Scan() {
if !inType {
if strings.HasPrefix(scanner.Text(), "type "+inputStruct.outName+" struct") {
inType = true
// Don't add this line, we'll do it in the template
continue
}
} else {
if scanner.Text() == "}" {
// We've reached the end of the type
break
}
}
if inType {
outBuf = append(outBuf, scanner.Text())
}
}
inputStruct.structFields = strings.Join(outBuf, "\n")
//fmt.Printf("Parsed: %#v\n", in.generatedStructure)
}
}
func packageFromFullName(fullName protoreflect.FullName) string {
// Example full name: controller.api.resources.groups.v1.Group
// Crawling up the parent twice jumps back past v1.Group placing us at "groups".
pkgName := fullName.Parent().Parent().Name()
if pkgName.IsValid() {
return string(pkgName)
}
return ""
}
var (
stringValueName = (&wrapperspb.StringValue{}).ProtoReflect().Descriptor().FullName()
boolValueName = (&wrapperspb.BoolValue{}).ProtoReflect().Descriptor().FullName()
structValueName = (&_struct.Struct{}).ProtoReflect().Descriptor().FullName()
timestampName = (&timestamppb.Timestamp{}).ProtoReflect().Descriptor().FullName()
)
func messageKind(fd protoreflect.FieldDescriptor) (ptr, pkg, name string) {
switch fd.Message().FullName() {
case stringValueName:
return "", "", "string"
case boolValueName:
return "", "", "bool"
case structValueName:
return "", "", "map[string]interface{}"
case timestampName:
return "", "time", "Time"
default:
return "*", packageFromFullName(fd.Message().FullName()), string(fd.Message().Name())
}
}
-133
View File
@@ -1,133 +0,0 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
type readInfo struct {
baseType string
targetType string
path string
}
var readFuncs = map[string][]*readInfo{
"scopes": {
{
baseType: "Scope",
targetType: "Scope",
path: "scopes",
},
},
"authtokens": {
{
baseType: "AuthToken",
targetType: "AuthToken",
path: "auth-tokens",
},
},
"groups": {
{
baseType: "Group",
targetType: "Group",
path: "groups",
},
},
"users": {
{
baseType: "User",
targetType: "User",
path: "users",
},
},
"roles": {
{
baseType: "Role",
targetType: "Role",
path: "roles",
},
},
"hosts": {
{
baseType: "HostCatalog",
targetType: "HostCatalog",
path: "host-catalogs",
},
},
}
func writeReadFuncs() {
for outPkg, funcs := range readFuncs {
outFile := os.Getenv("GEN_BASEPATH") + fmt.Sprintf("/api/%s/read.gen.go", outPkg)
outBuf := bytes.NewBuffer([]byte(fmt.Sprintf(
`// Code generated by "make api"; DO NOT EDIT.
package %s
`, outPkg)))
for _, readInfo := range funcs {
readFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: readInfo.baseType,
TargetType: readInfo.targetType,
TargetName: strings.Split(readInfo.targetType, ".")[strings.Count(readInfo.targetType, ".")],
Path: readInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
}
var readFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) Read{{ .TargetName }}(ctx context.Context, id string) (*{{ .TargetType }}, *api.Error, error) {
if id == "" {
return nil, nil, fmt.Errorf("empty ID value passed into Read{{ .TargetName }} request")
}
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Read{{ .TargetName }} request")
}
var opts []api.Option
if s.Scope.Id != "" {
// If it's explicitly set here, override anything that might be in the
// client
opts = append(opts, api.WithScopeId(s.Scope.Id))
}
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "{{ .Path }}", id), nil, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read{{ .TargetName }} call: %w", err)
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read{{ .TargetName }} repsonse: %w", err)
}
{{ if (eq .TargetType "Scope") }}
target.Client = s.Client.Clone()
target.Client.SetScopeId(target.Id)
{{ else }}
target.Client = s.Client
{{ end }}
return target, apiErr, nil
}
`))
+518 -111
View File
@@ -6,54 +6,162 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"github.com/iancoleman/strcase"
)
type templateType int
func toPath(segments []string, action string) string {
var printfString, printfArg []string
for i, s := range segments {
if i%2 == 0 {
// The first (zero index) is always the resource name, the next will be the id.
printfString = append(printfString, s)
} else {
printfString = append(printfString, "%s")
printfArg = append(printfArg, s)
}
}
if action != "" {
action = fmt.Sprintf(":%s", action)
}
return fmt.Sprintf("fmt.Sprintf(\"%s%s\", %s)", strings.Join(printfString, "/"), action, strings.Join(printfArg, ", "))
}
const (
templateTypeResource templateType = iota
templateTypeDetail
)
func getArgsAndPaths(in []string, action string) (colArgs, resArgs []string, colPath, resPath string) {
var argNames, pathSegment []string
for _, s := range in {
varName := fmt.Sprintf("%sId", strcase.ToLowerCamel(strings.ReplaceAll(s, "-", "_")))
collectionName := fmt.Sprintf("%ss", s)
func writeStructTemplates() {
for _, inputStruct := range inputStructs {
argNames = append(argNames, varName)
pathSegment = append(pathSegment, collectionName, varName)
}
return argNames[:len(argNames)-1], argNames, toPath(pathSegment[:len(pathSegment)-1], action), toPath(pathSegment, action)
}
type templateInput struct {
ClientName string
Name string
Package string
Fields []fieldInfo
PathArgs []string
CollectionFunctionArgs []string
ResourceFunctionArgs []string
CollectionPath string
ResourcePath string
SliceSubTypes map[string]string
VersionEnabled bool
}
func fillTemplates() {
optionsMap := map[string]map[string]fieldInfo{}
for _, in := range inputStructs {
outBuf := new(bytes.Buffer)
switch inputStruct.templateType {
case templateTypeResource:
structTemplate.Execute(outBuf, struct {
Name string
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:
detailTemplate.Execute(outBuf, struct {
Package string
StructFields string
ParentName string
DetailName string
}{
Package: inputStruct.outPkg,
StructFields: inputStruct.structFields,
ParentName: inputStruct.parentName,
DetailName: inputStruct.detailName,
})
input := templateInput{
ClientName: strings.ToLower(in.generatedStructure.name),
Name: in.generatedStructure.name,
Package: in.generatedStructure.pkg,
Fields: in.generatedStructure.fields,
PathArgs: in.pathArgs,
VersionEnabled: in.versionEnabled,
}
outFile, err := filepath.Abs(inputStruct.outFile)
if err != nil {
fmt.Printf("error opening file %q: %v\n", inputStruct.outFile, err)
if len(in.pathArgs) > 0 {
input.CollectionFunctionArgs, input.ResourceFunctionArgs, input.CollectionPath, input.ResourcePath = getArgsAndPaths(in.pathArgs, "")
}
if err := structTemplate.Execute(outBuf, input); err != nil {
fmt.Printf("error executing struct template for resource %s: %v\n", in.generatedStructure.name, err)
os.Exit(1)
}
if len(in.sliceSubTypes) > 0 {
input.SliceSubTypes = in.sliceSubTypes
in.templates = append(in.templates, sliceSubTypeTemplate)
}
for _, t := range in.templates {
if err := t.Execute(outBuf, input); err != nil {
fmt.Printf("error executing function template for resource %s: %v\n", in.generatedStructure.name, err)
os.Exit(1)
}
}
// We want to generate options per-package, not per-struct, so we
// collate them all here for writing later. The map argument of the
// package map is to prevent duplicates since we may have multiple e.g.
// Name or Description fields.
if !in.outputOnly {
pkgOptionMap := map[string]fieldInfo{}
for _, val := range input.Fields {
if val.GenerateSdkOption {
val.SubtypeName = in.subtypeName
pkgOptionMap[val.Name] = val
}
}
optionMap := optionsMap[input.Package]
if optionMap == nil {
optionMap = map[string]fieldInfo{}
}
for name, val := range pkgOptionMap {
optionMap[name] = val
}
optionsMap[input.Package] = optionMap
}
outFile, err := filepath.Abs(fmt.Sprintf("%s/%s", os.Getenv("API_GEN_BASEPATH"), in.outFile))
if err != nil {
fmt.Printf("error opening file %q: %v\n", in.outFile, err)
os.Exit(1)
}
outDir := filepath.Dir(outFile)
if _, err := os.Stat(outDir); os.IsNotExist(err) {
_ = os.Mkdir(outDir, os.ModePerm)
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
// Now reconstruct options per package and write them out
for pkg, options := range optionsMap {
outBuf := new(bytes.Buffer)
var fieldNames []string
for _, v := range options {
fieldNames = append(fieldNames, v.Name)
}
sort.Strings(fieldNames)
var fields []fieldInfo
for _, v := range fieldNames {
fields = append(fields, options[v])
}
input := templateInput{
Package: pkg,
Fields: fields,
}
if err := optionTemplate.Execute(outBuf, input); err != nil {
fmt.Printf("error executing option template for package %s: %v\n", pkg, err)
os.Exit(1)
}
outFile, err := filepath.Abs(fmt.Sprintf("%s/%s/%s", os.Getenv("API_GEN_BASEPATH"), pkg, "option.gen.go"))
if err != nil {
fmt.Printf("error opening file %q: %v\n", "option.gen.go", err)
os.Exit(1)
}
outDir := filepath.Dir(outFile)
if _, err := os.Stat(outDir); os.IsNotExist(err) {
_ = os.Mkdir(outDir, os.ModePerm)
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
@@ -61,99 +169,398 @@ func writeStructTemplates() {
}
}
// TODO: Add documentation around SetDefault and how it behaves when the field corresponding to the provided key is already set.
var listTemplate = template.Must(template.New("").Parse(`
func (c *{{ .ClientName }}Client) List(ctx context.Context, {{ range .CollectionFunctionArgs }} {{ . }} string, {{ end }}opt... Option) ([]*{{ .Name }}, *api.Error, error) { {{ range .CollectionFunctionArgs }}
if {{ . }} == "" {
return nil, nil, fmt.Errorf("empty {{ . }} value passed into List request")
}
{{ end }}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", {{ .CollectionPath }}, nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
type listResponse struct {
Items []*{{ .Name }}
}
target := &listResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
}
return target.Items, apiErr, nil
}
`))
var readTemplate = template.Must(template.New("").Parse(`
func (c *{{ .ClientName }}Client) Read(ctx context.Context, {{ range .ResourceFunctionArgs }} {{ . }} string, {{ end }} opt... Option) (*{{ .Name }}, *api.Error, error) { {{ range .ResourceFunctionArgs }}
if {{ . }} == "" {
return nil, nil, fmt.Errorf("empty {{ . }} value passed into Read request")
}
{{ end }}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", {{ .ResourcePath }}, nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new({{ .Name }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
}
return target, apiErr, nil
}
`))
var deleteTemplate = template.Must(template.New("").Parse(`
func (c *{{ .ClientName }}Client) Delete(ctx context.Context, {{ range .ResourceFunctionArgs }} {{ . }} string, {{ end }} opt... Option) (bool, *api.Error, error) { {{ range .ResourceFunctionArgs }}
if {{ . }} == "" {
return false, nil, fmt.Errorf("empty {{ . }} value passed into Delete request")
}
{{ end }}
if c.client == nil {
return false, nil, fmt.Errorf("nil client")
}
_, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", {{ .ResourcePath }}, nil, apiOpts...)
if err != nil {
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
}
type deleteResponse struct {
Existed bool
}
target := &deleteResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
return false, nil, fmt.Errorf("error decoding Delete response: %w", err)
}
return target.Existed, apiErr, nil
}
`))
var createTemplate = template.Must(template.New("").Parse(`
func (c *{{ .ClientName }}Client) Create(ctx context.Context, {{ range .CollectionFunctionArgs }} {{ . }} string, {{ end }} opt... Option) (*{{ .Name }}, *api.Error, error) { {{ range .CollectionFunctionArgs }}
if {{ . }} == "" {
return nil, nil, fmt.Errorf("empty {{ . }} value passed into Create request")
}
{{ end }}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "POST", {{ .CollectionPath }}, opts.valueMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
}
target := new({{ .Name }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
}
return target, apiErr, nil
}
`))
var updateTemplate = template.Must(template.New("").Parse(`
func (c *{{ .ClientName }}Client) Update(ctx context.Context, {{ range .ResourceFunctionArgs }} {{ . }} string, {{ end }}version uint32, opt... Option) (*{{ .Name }}, *api.Error, error) { {{ range .ResourceFunctionArgs }}
if {{ . }} == "" {
return nil, nil, fmt.Errorf("empty {{ . }} value passed into Update request")
}{{ end }}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
{{ if .VersionEnabled }}
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into Update request and automatic versioning not specified")
}
existingTarget, existingApiErr, existingErr := c.Read(ctx, {{ range .ResourceFunctionArgs }} {{ . }}, {{ end }} opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
{{ end }}
req, err := c.client.NewRequest(ctx, "PATCH", {{ .ResourcePath }}, opts.valueMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
}
target := new({{ .Name }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
}
return target, apiErr, nil
}
`))
var sliceSubTypeTemplate = template.Must(template.New("").Funcs(
template.FuncMap{
"makeSlice": makeSlice,
"snakeCase": snakeCase,
"kebabCase": kebabCase,
"getPathWithAction": getPathWithAction,
},
).Parse(`
{{ $input := . }}
{{ range $index, $op := makeSlice "Add" "Set" "Remove" }}
{{ range $key, $value := $input.SliceSubTypes }}
{{ $fullName := print $op $key }}
{{ $actionName := kebabCase $fullName }}
{{ $resPath := getPathWithAction $input.PathArgs $actionName }}
func (c *{{ $input.ClientName }}Client) {{ $fullName }}(ctx context.Context, {{ range $input.ResourceFunctionArgs }} {{ . }} string, {{ end }}version uint32, {{ $value }} []string, opt... Option) (*{{ $input.Name }}, *api.Error, error) { {{ range $input.ResourceFunctionArgs }}
if {{ . }} == "" {
return nil, nil, fmt.Errorf("empty {{ . }} value passed into {{ $fullName }} request")
}{{ end }}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
{{ if $input.VersionEnabled }}
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into {{ $fullName }} request")
}
existingTarget, existingApiErr, existingErr := c.Read(ctx, {{ range $input.ResourceFunctionArgs }} {{ . }}, {{ end }} opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
{{ end }}
if len({{ $value }}) > 0 {
opts.valueMap["{{ snakeCase $value }}"] = {{ $value }}
}{{ if ( eq $op "Set" ) }} else if {{ $value }} != nil {
// In this function, a non-nil but empty list means clear out
opts.valueMap["{{ snakeCase $value }}"] = nil
}
{{ end }}
req, err := c.client.NewRequest(ctx, "POST", {{ $resPath }}, opts.valueMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating {{ $fullName }} request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during {{ $fullName }} call: %w", err)
}
target := new({{ $input.Name }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding {{ $fullName }} response: %w", err)
}
return target, apiErr, nil
}
{{ end }}
{{ end }}
`))
var structTemplate = template.Must(template.New("").Parse(
`// Code generated by "make api"; DO NOT EDIT.
fmt.Sprint(`// Code generated by "make api"; DO NOT EDIT.
package {{ .Package }}
import (
"context"
"encoding/json"
"strings"
"fmt"
"time"
"github.com/fatih/structs"
"github.com/kr/pretty"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/internal/strutil"
"github.com/hashicorp/watchtower/api/scopes"
)
type {{ .Name }} struct {
{{ if (not .OutputOnly) }}
Client *api.Client ` + "`json:\"-\"`" + `
{{ end }}
{{ .StructFields }}
type {{ .Name }} struct { {{ range .Fields }}
{{ .Name }} {{ .FieldType }} `, "`json:\"{{ .ProtoName }},omitempty\"`", `{{ end }}
}
`)))
var clientTemplate = template.Must(template.New("").Parse(`
type {{ .ClientName }}Client struct {
client *api.Client
}
{{ if (not .OutputOnly) }}
func (s *{{ .Name }}) SetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := {{ printf "%#v" .NameJsonMap }}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
return
func New{{ .Name }}Client(c *api.Client) *{{ .ClientName }}Client {
return &{{ .ClientName }}Client{ client: c }
}
`))
var optionTemplate = template.Must(template.New("").Parse(`
package {{ .Package }}
import (
"context"
"fmt"
"time"
"github.com/hashicorp/watchtower/api"
)
// Option is a func that sets optional attributes for a call. This does not need
// to be used directly, but instead option arguments are built from the
// functions in this package. WithX options set a value to that given in the
// argument; DefaultX options indicate that the value should be set to its
// default. When an API call is made options are processed in ther order they
// appear in the function call, so for a given argument X, a succession of WithX
// or DefaultX calls will result in the last call taking effect.
type Option func(*options)
type options struct {
valueMap map[string]interface{}
withScopeId string
withAutomaticVersioning bool
}
func getDefaultOptions() options {
return options{
valueMap: make(map[string]interface{}),
}
}
func getOpts(opt ...Option) (options, []api.Option) {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
}
var apiOpts []api.Option
if opts.withScopeId != "" {
apiOpts = append(apiOpts, api.WithScopeId(opts.withScopeId))
}
return opts, apiOpts
}
func WithScopeId(id string) Option {
return func(o *options) {
o.withScopeId = id
}
}
// If set, and if the version is zero during an update, the API will perform a
// fetch to get the current version of the resource and populate it during the
// update call. This is convenient but opens up the possibility for subtle
// order-of-modification issues, so use carefully.
func WithAutomaticVersioning() Option {
return func(o *options) {
o.withAutomaticVersioning = true
}
}
{{ range .Fields }}
func With{{ .SubtypeName }}{{ .Name }}(in{{ .Name }} {{ .FieldType }}) Option {
return func(o *options) { {{ if ( not ( eq .SubtypeName "" ) ) }}
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
}
val := raw.(map[string]interface{})
val["{{ .ProtoName }}"] = in{{ .Name }}
o.valueMap["attributes"] = val
{{ else }}
o.valueMap["{{ .ProtoName }}"] = in{{ .Name }}
{{ end }} }
}
func (s *{{ .Name }}) UnsetDefault(key string) {
lowerKey := strings.ToLower(key)
validMap := {{ printf "%#v" .NameJsonMap }}
for k, v := range validMap {
if k == lowerKey || v == lowerKey {
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
return
func Default{{ .SubtypeName }}{{ .Name }}() Option {
return func(o *options) { {{ if ( not ( eq .SubtypeName "" ) ) }}
raw, ok := o.valueMap["attributes"]
if !ok {
raw = interface{}(map[string]interface{}{})
}
}
}
func (s {{ .Name }}) 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)
val := raw.(map[string]interface{})
val["{{ .ProtoName }}"] = nil
o.valueMap["attributes"] = val
{{ else }}
o.valueMap["{{ .ProtoName }}"] = nil
{{ end }} }
}
{{ end }}
`))
var detailTemplate = template.Must(template.New("").Parse(
`// Code generated by "make api"; DO NOT EDIT.
package {{ .Package }}
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/hashicorp/watchtower/api"
)
type {{ .DetailName }} struct {
*{{ .ParentName }}
{{ .StructFields }}
func makeSlice(strs ...string) []string {
return strs
}
func (s {{ .ParentName }}) As{{ .DetailName }}() (*{{ .DetailName }}, error) {
out := &{{ .DetailName }}{
{{ .ParentName }}: &s,
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: out,
TagName: "json",
})
if err != nil {
return nil, fmt.Errorf("error creating map decoder: %w", err)
}
if err := decoder.Decode(s.Attributes); err != nil {
return nil, fmt.Errorf("error decoding attributes map: %w", err)
}
return out, nil
func snakeCase(in string) string {
return strcase.ToSnake(in)
}
func kebabCase(in string) string {
return strcase.ToKebab(in)
}
func getPathWithAction(resArgs []string, action string) string {
_, _, _, resPath := getArgsAndPaths(resArgs, action)
return resPath
}
`))
-125
View File
@@ -1,125 +0,0 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
type updateInfo struct {
baseType string
targetType string
path string
}
var updateFuncs = map[string][]*updateInfo{
"scopes": {
{
baseType: "Scope",
targetType: "Scope",
path: "scopes",
},
},
"groups": {
{
baseType: "Group",
targetType: "Group",
path: "groups",
},
},
"users": {
{
baseType: "User",
targetType: "User",
path: "users",
},
},
"roles": {
{
baseType: "Role",
targetType: "Role",
path: "roles",
},
},
}
func writeUpdateFuncs() {
for outPkg, funcs := range updateFuncs {
outFile := os.Getenv("GEN_BASEPATH") + fmt.Sprintf("/api/%s/update.gen.go", outPkg)
outBuf := bytes.NewBuffer([]byte(fmt.Sprintf(
`// Code generated by "make api"; DO NOT EDIT.
package %s
`, outPkg)))
for _, updateInfo := range funcs {
updateFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: updateInfo.baseType,
TargetType: updateInfo.targetType,
TargetName: strings.Split(updateInfo.targetType, ".")[strings.Count(updateInfo.targetType, ".")],
Path: updateInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
}
var updateFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) Update{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
if r == nil {
}
if r.Id == "" {
return nil, nil, fmt.Errorf("empty {{ .TargetType }} ID field in Create{{ .TargetName }} request")
}
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetName }} request")
}
var opts []api.Option
if s.Scope.Id != "" {
// If it's explicitly set here, override anything that might be in the
// client
opts = append(opts, api.WithScopeId(s.Scope.Id))
}
id := r.Id
r.Id = ""
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "{{ .Path }}", id), r, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Update{{ .TargetName }} call: %w", err)
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update{{ .TargetName }} repsonse: %w", err)
}
{{ if (eq .TargetType "Scope") }}
target.Client = s.Client.Clone()
target.Client.SetScopeId(target.Id)
{{ else }}
target.Client = s.Client
{{ end }}
return target, apiErr, nil
}
`))