* Organizations -> orgs consistency * Fix up some tests and some mistaken substitutions
143 lines
3.5 KiB
Go
143 lines
3.5 KiB
Go
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: "Org",
|
|
targetType: "Project",
|
|
path: "projects",
|
|
},
|
|
{
|
|
baseType: "Org",
|
|
targetType: "groups.Group",
|
|
path: "groups",
|
|
},
|
|
{
|
|
baseType: "Org",
|
|
targetType: "roles.Role",
|
|
path: "roles",
|
|
},
|
|
{
|
|
baseType: "Org",
|
|
targetType: "users.User",
|
|
path: "users",
|
|
},
|
|
{
|
|
baseType: "Project",
|
|
targetType: "hosts.HostCatalog",
|
|
path: "host-catalogs",
|
|
},
|
|
{
|
|
baseType: "Project",
|
|
targetType: "groups.Group",
|
|
path: "groups",
|
|
},
|
|
{
|
|
baseType: "Project",
|
|
targetType: "roles.Role",
|
|
path: "roles",
|
|
},
|
|
},
|
|
}
|
|
|
|
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, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
|
|
if s.Client == nil {
|
|
return nil, nil, fmt.Errorf("nil client in Read{{ .TargetName }} request")
|
|
}
|
|
if s.Id == "" {
|
|
{{ if (eq .BaseType "Org") }}
|
|
// Assume the client has been configured with org 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 Read{{ .TargetType }} request")
|
|
{{ end }}
|
|
} else {
|
|
// If it's explicitly set here, override anything that might be in the
|
|
// client
|
|
{{ if (eq .BaseType "Org") }}
|
|
ctx = context.WithValue(ctx, "org", s.Id)
|
|
{{ else if (eq .BaseType "Project") }}
|
|
ctx = context.WithValue(ctx, "project", s.Id)
|
|
{{ end }}
|
|
}
|
|
if r.Id == "" {
|
|
return nil, nil, fmt.Errorf("empty {{ .TargetType }} ID field in Read{{ .TargetName }} request")
|
|
}
|
|
|
|
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "{{ .Path }}", r.Id), r)
|
|
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 "Org") }}
|
|
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
|
|
}
|
|
`))
|