// 2017 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package mongo import ( "context" "errors" "time" "github.rackspace.com/DCM/tools" "go.mongodb.org/mongo-driver/bson" ) type ( // CreateNMGroupPayload is the create payload CreateNMGroupPayload struct { MovingGroupName string `json:"moving_group_name"` NotMovingGroupName string `json:"not_moving_group_name"` } // UpdateNMGroupPayload is the update payload UpdateNMGroupPayload struct { NotMovingGroupName string `json:"not_moving_group_name"` DeviceID int `json:"device_id"` } ) // NotMovingCreateGroup will create the NM group based on the payload func (c *Client) NotMovingCreateGroup(payload *CreateNMGroupPayload) error { m, err := c.CollectMetadataByMG([]string{payload.MovingGroupName}) if err != nil { return err } if len(m) != 1 { return errors.New("metadata not found for move group") } nmMetadata := m[0] nmMetadata.ID = payload.NotMovingGroupName ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() _, err = c.db3.Database(database).Collection(tools.MetaCollection).InsertOne(ctx, &nmMetadata) if err != nil { return err } return nil } // NotMovingUpdateGroup will update the move-group for a device to match the NM group func (c *Client) NotMovingUpdateGroup(payload *UpdateNMGroupPayload) error { m, err := c.CollectMetadataByMG([]string{payload.NotMovingGroupName}) if err != nil { return err } if len(m) != 1 { return errors.New("metadata not found for move group") } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() _, err = c.db3.Database(database).Collection(m[0].Collection).UpdateOne( ctx, bson.D{{"_id", payload.DeviceID}}, bson.D{{"$set", bson.D{{"move_group", payload.NotMovingGroupName}}}}, ) if err != nil { return err } return nil }