Files
threshold/api/internal/genapi/delete.go
T

124 lines
2.6 KiB
Go
Raw Normal View History

2020-05-20 08:04:23 -07:00
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": {
{
2020-07-22 00:14:28 -04:00
"Scope",
"Scope",
"scopes",
2020-06-05 13:02:58 -07:00
},
2020-07-22 00:14:28 -04:00
},
"authtokens": {
{
"AuthToken",
"AuthToken",
"auth-tokens",
},
},
2020-07-22 00:14:28 -04:00
"groups": {
2020-06-15 15:22:41 -07:00
{
2020-07-22 00:14:28 -04:00
"Group",
"Group",
2020-06-15 15:22:41 -07:00
"groups",
},
2020-07-22 00:14:28 -04:00
},
"users": {
2020-06-22 06:56:27 -07:00
{
2020-07-22 00:14:28 -04:00
"User",
"User",
2020-06-09 11:20:59 -07:00
"users",
},
2020-07-22 00:14:28 -04:00
},
"roles": {
2020-06-05 13:02:58 -07:00
{
2020-07-22 00:14:28 -04:00
"Role",
"Role",
"roles",
},
2020-05-20 08:04:23 -07:00
},
}
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 {
2020-06-05 13:02:58 -07:00
BaseType string
TargetType string
TargetName string
Path string
2020-05-20 08:04:23 -07:00
}{
2020-06-05 13:02:58 -07:00
BaseType: deleteInfo.baseType,
TargetType: deleteInfo.targetType,
TargetName: strings.Split(deleteInfo.targetType, ".")[strings.Count(deleteInfo.targetType, ".")],
Path: deleteInfo.path,
2020-05-20 08:04:23 -07:00
})
}
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(
`
2020-06-05 13:02:58 -07:00
// Delete{{ .TargetName }} returns true iff the {{ .TargetType }} existed when the delete attempt was made.
2020-07-22 00:14:28 -04:00
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")
}
2020-05-20 08:04:23 -07:00
if s.Client == nil {
2020-06-05 13:02:58 -07:00
return false, nil, fmt.Errorf("nil client in Delete{{ .TargetName }} request")
2020-05-20 08:04:23 -07:00
}
2020-07-22 00:14:28 -04:00
var opts []api.Option
if s.Scope.Id != "" {
2020-05-20 08:04:23 -07:00
// If it's explicitly set here, override anything that might be in the
// client
2020-07-22 00:14:28 -04:00
opts = append(opts, api.WithScopeId(s.Scope.Id))
2020-05-20 08:04:23 -07:00
}
2020-07-22 00:14:28 -04:00
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "{{ .Path }}", id), nil, opts...)
2020-05-20 08:04:23 -07:00
if err != nil {
2020-06-05 13:02:58 -07:00
return false, nil, fmt.Errorf("error creating Delete{{ .TargetName }} request: %w", err)
2020-05-20 08:04:23 -07:00
}
resp, err := s.Client.Do(req)
if err != nil {
2020-06-05 13:02:58 -07:00
return false, nil, fmt.Errorf("error performing client request during Delete{{ .TargetName }} call: %w", err)
2020-05-20 08:04:23 -07:00
}
type deleteResponse struct {
Existed bool
}
target := &deleteResponse{}
apiErr, err := resp.Decode(target)
if err != nil {
2020-06-05 13:02:58 -07:00
return false, nil, fmt.Errorf("error decoding Delete{{ .TargetName }} repsonse: %w", err)
2020-05-20 08:04:23 -07:00
}
return target.Existed, apiErr, nil
}
`))