You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boundary/api/internal/genapi/create.go

122 lines
3.1 KiB

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{
"hosts": {
{
"HostCatalog",
"Host",
`fmt.Sprintf("host-catalogs/%s/hosts", s.Id)`,
},
{
"HostCatalog",
"HostSet",
`fmt.Sprintf("host-catalogs/%s/host-sets", s.Id)`,
},
},
"scopes": {
{
"Organization",
"Project",
`"projects"`,
},
},
}
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
LowerTargetType string
Path string
}{
BaseType: createInfo.baseType,
TargetType: createInfo.targetType,
LowerTargetType: strings.ToLower(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{{ .TargetType }}(ctx context.Context, {{ .LowerTargetType }} *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetType }} request")
}
if s.Id == "" {
{{ if (eq .BaseType "Organization") }}
// Assume the client has been configured with organization already and
// move on
{{ else if (eq .BaseType "Project") }}
// Assume the client has been configured with project already and move
// on
{{ else }}
return nil, nil, fmt.Errorf("missing {{ .BaseType}} ID in Create{{ .TargetType }} request")
{{ end }}
} else {
// If it's explicitly set here, override anything that might be in the
// client
{{ if (eq .BaseType "Organization") }}
ctx = context.WithValue(ctx, "org", s.Id)
{{ else if (eq .BaseType "Project") }}
ctx = context.WithValue(ctx, "project", s.Id)
{{ end }}
}
req, err := s.Client.NewRequest(ctx, "POST", {{ .Path }}, {{ .LowerTargetType }})
if err != nil {
return nil, nil, fmt.Errorf("error creating Create{{ .TargetType }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Create{{ .TargetType }} call: %w", err)
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Create{{ .TargetType }} repsonse: %w", err)
}
{{ if (eq .TargetType "Organization") }}
target.Client = s.Client.Clone()
target.Client.SetOrgnization(target.Id)
{{ else if (eq .TargetType "Project") }}
target.Client = s.Client.Clone()
target.Client.SetProject(target.Id)
{{ else }}
target.Client = s.Client
{{ end }}
return target, apiErr, nil
}
`))