dim/ipblock_create.go

44 lines
1.2 KiB
Go

package main
import (
"fmt"
"dim/types"
)
type (
IPBlockCreateOptions struct {
Attributes string `json:"attributes"`
Layer3Domain string `json:"layer3domain"`
AllowOverlap bool `json:"allow_overlap"`
}
)
func ipblockCreate(c *Context, req Request, res *Response) error {
block := new(types.Subnet)
options := IPBlockCreateOptions{
Attributes: "{}",
}
if err := req.ParseAtLeast(1, block, &options); err != nil {
res.AddMessage(LevelError, "could not parse parameters: %s", err)
return nil
}
l3Id := 0
err := c.tx.QueryRow(`select id from layer3domains where name = $1 for update`, options.Layer3Domain).Scan(&l3Id)
if err != nil {
res.AddMessage(LevelError, "could not get layer3domain")
return fmt.Errorf("could not resolve layer3domain '%s': %s", options.Layer3Domain, err)
}
_, err = c.tx.Exec(`insert into containers(layer3domain_id, network, created_by, modified_by, attributes)
values ($1, $2, $3, $3, $4::jsonb)`,
l3Id, block, c.username, options.Attributes)
if err != nil {
res.AddMessage(LevelError, "could not create ip block")
return fmt.Errorf("could not create container '%s': %s", block, err)
}
return nil
}