This is a implementation of dim, which is built in python. But this time in go.
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.
 
 

52 lines
1.3 KiB

package main
import (
"fmt"
"dim/query"
"dim/types"
)
type (
Layer3DomainListOptions struct {
Attributes types.FieldList `json:"attributes"`
}
)
var (
layer3DomainListMap = map[string]string{
"name": "l.name",
"modified_by": "l.modified_by",
"modified_at": "l.modified_at",
"created_by": "l.created_by",
"created_at": "l.created_at",
}
)
// Layer3DomainList lists all registered layer3domains.
func layer3DomainList(c *Context, req Request, res *Response) error {
options := Layer3DomainListOptions{
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("l", options.Attributes, layer3DomainListMap)
from := "layer3domains l"
queryStr := fmt.Sprintf(`select %s from %s`, selClause, from)
rows, err := c.tx.Query(queryStr)
if err != nil {
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not get layer3domain list: %s - query %s", err, queryStr)
}
defer rows.Close()
res.Result, err = query.RowsToMap(rows)
if err != nil {
res.Result = nil
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not parse layer3domain list: %#v", err)
}
return nil
}