package mongo import ( "context" "fmt" "time" "github.rackspace.com/DCM/tools" "go.mongodb.org/mongo-driver/bson" ) const ( nagiosMGCollection = "nagios_move_groups" nagiosSnapshotCollection = "nagiosSnapshots" nagiosSnapsArchiveCollection = "nagiosSnapshotsArchive" ) // NagiosMovegroup defines the structure for the document in the DB type NagiosMovegroup struct { Name string `bson:"_id"` Collection string `bson:"collection"` Devices []tools.Device `bson:"devices,omitempty"` SourceDC string `bson:"source_dc"` DestinationDC string `bson:"migration_dc"` NagiosHost string `bson:"nagios_host"` // e.g., "ord-nagios" NagiosSSHIP string `bson:"nagios_ssh_ip"` RCFilter string `bson:"rc_filter"` NMGroups bool `bson:"has_nm_groups"` // has Not Moving (NM) groups } // ArchiveNagiosSnapshots method will move items from the nagiosSnapshots collection to the // nagiosSnapshotsArchive collection func (c *Client) ArchiveNagiosSnapshots() (string, error) { ret := "" ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() cursor, err := c.db3.Database(database).Collection(nagiosSnapshotCollection).Find( ctx, bson.M{}, ) if err != nil { return ret, err } defer cursor.Close(ctx) snapshots := make([]interface{}, 0) for cursor.Next(ctx) { payload := new(NagiosSnapshot) if err = cursor.Decode(payload); err != nil { return ret, err } snapshots = append(snapshots, payload) } if len(snapshots) == 0 { return ret, nil } result, err := c.db3.Database(database).Collection(nagiosSnapsArchiveCollection).InsertMany( ctx, snapshots, ) if err != nil { return ret, err } ret = fmt.Sprintf("%d documents inserted", len(result.InsertedIDs)) _, err = c.db3.Database(database).Collection(nagiosSnapshotCollection).DeleteMany( ctx, bson.M{}, ) return ret, err } // GetNagiosMoveGroups() returns the current Nagios move groups in the db /* func (c *Client) GetNagiosMoveGroups(host string) ([]NagiosMovegroup, error) { ret := make([]NagiosMovegroup, 0) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() cursor, err := c.db3.Database(database).Collection(nagiosMGCollection).Find( ctx, bson.M{"nagios_host": host}, ) if err != nil { return ret, err } defer cursor.Close(ctx) for cursor.Next(ctx) { payload := new(NagiosMovegroup) if err = cursor.Decode(payload); err != nil { return ret, err } ret = append(ret, *payload) } return ret, nil } */ // UpdateNagiosMoveGroups updates the current Nagios move groups in the db; // called when a nagios config is generated func (c *Client) UpdateNagiosMoveGroups(movegroups []NagiosMovegroup) error { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() _, err := c.db3.Database(database).Collection(nagiosMGCollection).DeleteMany( ctx, bson.M{"nagios_host": movegroups[0].NagiosHost}, ) if err != nil { return err } payload := make([]interface{}, len(movegroups)) for i := range movegroups { payload[i] = movegroups[i] } _, err = c.db3.Database(database).Collection(nagiosMGCollection).InsertMany( ctx, payload, ) return err }