From dd3f0ab9e828e8ba9ec28e5a30a02b04749f3770 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Wed, 7 Aug 2019 16:00:29 +0200 Subject: [PATCH] Add customvar_flat --- .../customvar/customvarflat/customvarflat.go | 151 ++++++++++++++++++ etc/schema/mysql/customvar.sql | 6 +- main.go | 2 + 3 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 configobject/objecttypes/customvar/customvarflat/customvarflat.go diff --git a/configobject/objecttypes/customvar/customvarflat/customvarflat.go b/configobject/objecttypes/customvar/customvarflat/customvarflat.go new file mode 100644 index 00000000..8f5aacfe --- /dev/null +++ b/configobject/objecttypes/customvar/customvarflat/customvarflat.go @@ -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), + } +} \ No newline at end of file diff --git a/etc/schema/mysql/customvar.sql b/etc/schema/mysql/customvar.sql index 29d0997d..85074fcb 100644 --- a/etc/schema/mysql/customvar.sql +++ b/etc/schema/mysql/customvar.sql @@ -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; \ No newline at end of file diff --git a/main.go b/main.go index 33a693df..e1e53da7 100644 --- a/main.go +++ b/main.go @@ -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) { ¬ificationusergroup.ObjectInformation, &customvar.ObjectInformation, + &customvarflat.ObjectInformation, &zone.ObjectInformation,