Browse Source

provide initial versions of zone endpoints

These are only examples for now and nothing serious yet but showcase the
innards of query and types for now.

It is a beginning.
master
Gibheer 1 year ago
parent
commit
feb4569547
  1. 26
      zone_create.go
  2. 50
      zone_list.go

26
zone_create.go

@ -0,0 +1,26 @@
package main
import (
"fmt"
"dim/types"
)
// Creates a new DNS zone.
func zoneCreate(c *Context, req Request, res *Response) error {
zoneName := new(types.ZoneName)
if err := req.Parse(&zoneName); err != nil {
res.AddMessage(LevelError, "expected zone name, but got error: %s", err)
return nil
}
id := 0
err := c.tx.
QueryRow(`insert into zones(name, created_by, modified_by)
values ($1, $2, $2) returning id`, zoneName, c.username).
Scan(&id)
if err != nil {
res.AddMessage(LevelError, "could not create zone")
return fmt.Errorf("could not create zone '%s': %s", zoneName, err)
}
return nil
}

50
zone_list.go

@ -0,0 +1,50 @@
package main
import (
"fmt"
"dim/query"
"dim/types"
)
type (
ZoneListOptions struct {
Attributes types.FieldList `json:"attributes"`
}
)
var (
zoneListMap = map[string]string{
"name": "z.name",
"modified_by": "z.modified_by",
"modified_at": "z.modified_at",
"created_by": "z.modified_by",
"created_at": "z.modified_at",
}
)
func zoneList(c *Context, req Request, res *Response) error {
options := ZoneListOptions{
Attributes: types.NewFieldList("name"),
}
if err := req.ParseAtLeast(0, &options); err != nil {
res.AddMessage(LevelError, "could not parse options: %s", err)
return nil
}
selClause := query.FieldListToSelect(options.Attributes, zoneListMap)
rows, err := c.tx.Query(fmt.Sprintf(`select %s from zones z`, selClause))
if err != nil {
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not get zone list: %s", err)
}
defer rows.Close()
res.Result["zones"], err = query.RowsToMap(rows)
if err != nil {
delete(res.Result, "zones")
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not parse zone list: %v", err)
}
return nil
}
Loading…
Cancel
Save