Add customvar_flat

This commit is contained in:
Noah Hilverling 2019-08-07 16:00:29 +02:00
parent 618ea21571
commit dd3f0ab9e8
3 changed files with 156 additions and 3 deletions

View file

@ -0,0 +1,151 @@
package customvarflat
import (
"encoding/json"
"fmt"
"git.icinga.com/icingadb/icingadb-main/configobject"
"git.icinga.com/icingadb/icingadb-main/connection"
"git.icinga.com/icingadb/icingadb-main/utils"
)
var (
ObjectInformation configobject.ObjectInformation
Fields = []string{
"id",
"env_id",
"customvar_id",
"flatname_checksum",
"flatname",
"flatvalue",
}
)
type CustomvarFlat struct {
Id string `json:"id"`
EnvId string `json:"env_id"`
NameChecksum string `json:"name_checksum"`
Name string `json:"name"`
Value string `json:"value"`
}
func NewCustomvarFlat() connection.Row {
c := CustomvarFlat{}
return &c
}
func (c *CustomvarFlat) InsertValues() []interface{} {
return nil
}
func (c *CustomvarFlat) UpdateValues() []interface{} {
return nil
}
func (c *CustomvarFlat) GetId() string {
return c.Id
}
func (c *CustomvarFlat) SetId(id string) {
c.Id = id
}
func (c *CustomvarFlat) GetFinalRows() ([]connection.Row, error) {
var values interface{} = nil
if err := json.Unmarshal([]byte(c.Value), &values); err != nil {
return nil, err
}
return CollectScalarVars(c, values, c.Name, make([]string, 0)), nil
}
func CollectScalarVars(c *CustomvarFlat, value interface{}, name string, path []string) []connection.Row {
path = append(path, name)
switch v := value.(type) {
case map[string]interface{}:
var rows = []connection.Row{}
for flatName, flatValue := range v {
rows = append(rows, CollectScalarVars(c, flatValue, flatName, path)...)
}
return rows
case []interface{}:
var rows = []connection.Row{}
for i, flatValue := range v {
rows = append(rows, CollectScalarVars(c, flatValue, fmt.Sprintf("%d", i), path)...)
}
return rows
default:
flatName := fmt.Sprintf("%v", path)
flatValue := fmt.Sprintf("%v", v)
return []connection.Row{
&CustomvarFlatFinal{
Id: utils.StringToSha1String(c.EnvId + flatName + flatValue),
EnvId: c.EnvId,
CustomvarId: c.Id,
FlatNameChecksum: utils.StringToSha1String(flatName),
FlatName: flatName,
FlatValue: flatValue,
},
}
}
}
type CustomvarFlatFinal struct {
Id string
EnvId string
CustomvarId string
FlatNameChecksum string
FlatName string
FlatValue string
}
func (c *CustomvarFlatFinal) InsertValues() []interface{} {
v := c.UpdateValues()
return append([]interface{}{utils.Checksum(c.Id)}, v...)
}
func (c *CustomvarFlatFinal) UpdateValues() []interface{} {
v := make([]interface{}, 0)
v = append(
v,
utils.Checksum(c.EnvId),
utils.Checksum(c.CustomvarId),
utils.Checksum(c.FlatNameChecksum),
c.FlatName,
c.FlatValue,
)
return v
}
func (c *CustomvarFlatFinal) GetId() string {
return c.Id
}
func (c *CustomvarFlatFinal) SetId(id string) {
c.Id = id
}
func (c *CustomvarFlatFinal) GetFinalRows() ([]connection.Row, error) {
return []connection.Row{c}, nil
}
func init() {
name := "customvar_flat"
ObjectInformation = configobject.ObjectInformation{
ObjectType: name,
RedisKey: "customvar",
DeltaMySqlField: "customvar_id",
Factory: NewCustomvarFlat,
HasChecksum: false,
BulkInsertStmt: connection.NewBulkInsertStmt(name, Fields),
BulkDeleteStmt: &connection.BulkDeleteStmt{
Format: fmt.Sprintf("DELETE FROM %s WHERE customvar_id IN (%s)", name, "%s"),
},
BulkUpdateStmt: connection.NewBulkUpdateStmt(name, Fields),
}
}

View file

@ -13,13 +13,13 @@ CREATE TABLE customvar (
) ENGINE=InnoDb ROW_FORMAT=COMPRESSED DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;
CREATE TABLE customvar_flat (
customvar_id binary(20) NOT NULL COMMENT 'sha1(customvar.id)',
id binary(20) NOT NULL COMMENT 'sha1(environment.name + flatname + flatvalue)',
env_id binary(20) NOT NULL COMMENT 'sha1(environment.name)',
path_checksum binary(20) NOT NULL COMMENT 'sha1(flatname before conversion)',
customvar_id binary(20) NOT NULL COMMENT 'sha1(customvar.id)',
flatname_checksum binary(20) NOT NULL COMMENT 'sha1(flatname after conversion)',
flatname varchar(512) NOT NULL COLLATE utf8_bin COMMENT 'Path converted with `.` and `[ ]`',
flatvalue text NOT NULL,
PRIMARY KEY (customvar_id, path_checksum)
PRIMARY KEY (id)
) ENGINE=InnoDb ROW_FORMAT=COMPRESSED DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;

View file

@ -11,6 +11,7 @@ import (
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/checkcommand/checkcommandcustomvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/checkcommand/checkcommandenvvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/customvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/customvar/customvarflat"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/endpoint"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/eventcommand"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/eventcommand/eventcommandargument"
@ -152,6 +153,7 @@ func startConfigSyncOperators(super *supervisor.Supervisor, haInstance *ha.HA) {
&notificationusergroup.ObjectInformation,
&customvar.ObjectInformation,
&customvarflat.ObjectInformation,
&zone.ObjectInformation,