// 2021 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 tools import ( "context" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type ( // Platform will define all the attributes we can store about a specific device platform. Platform struct { ID primitive.ObjectID `json:"_id" bson:"_id"` ModelName string `json:"model_name" bson:"model_name"` ModelNumber string `json:"model_number" bson:"model_number"` Vendor string `json:"vendor" bson:"vendor"` PlatformMatch []string `json:"platform_match" bson:"platform_match"` Interfaces []Interface `json:"interfaces" bson:"interfaces"` Power []Power `json:"power_adapters" bson:"power_adapters"` ImageURL string `json:"image_url" bson:"image_url"` } // Interface is a ethernet or console connection that exist on the specific platform. Interface struct { Name string `json:"name" bson:"name"` Label string `json:"label" bson:"label"` FormFactor string `json:"form_factor" bson:"form_factor"` Slot string `json:"slot" bson:"slot"` XPos float64 `json:"x_pos" bson:"x_pos"` YPos float64 `json:"y_pos" bson:"y_pos"` ManagementOnly bool `json:"management_only" bson:"management_only"` } // Power is a more simplistic interface that represents a power adapter that // is supported by the platform. Power struct { Name string `json:"name" bson:"name"` Label string `json:"label" bson:"label"` Slot string `json:"slot" bson:"slot"` } ) // PlatformGetByID is a helper method to fetch a single platform record. func (c *Client) PlatformGetByID(id string) (platform Platform, err error) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() objID, err := primitive.ObjectIDFromHex(id) if err != nil { return } cursor := c.db.Database(MigrationDB).Collection(PlatformCollection).FindOne(ctx, bson.M{"_id": objID}) err = cursor.Decode(&platform) return } // PlatformGetByMapping is a helper method to fetch records that are mapped with a given CORE sku. func (c *Client) PlatformGetByMapping(sku string) (platforms []Platform, err error) { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() query := bson.M{ "platform_match": sku, } cursor, err := c.db.Database(MigrationDB).Collection(PlatformCollection).Find(ctx, query) if err != nil { return } platforms = make([]Platform, 0) err = cursor.All(ctx, &platforms) return } // PlatformGet is a helper method to fetch platform records with a custom query. func (c *Client) PlatformGet(query bson.M, projection []string) (platforms []Platform, err error) { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() opts := options.Find() if len(projection) != 0 { proj := bson.M{} for i := range projection { proj[projection[i]] = 1 } opts = options.Find().SetProjection(proj) } cursor, err := c.db.Database(MigrationDB).Collection(PlatformCollection).Find(ctx, query, opts) if err != nil { return } platforms = make([]Platform, 0) err = cursor.All(ctx, &platforms) return } // PlatformCreateUpdate is a helper method to update or insert a platform into the platforms collection. func (c *Client) PlatformCreateUpdate(platform Platform) (*mongo.UpdateResult, error) { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() if platform.ID == primitive.NilObjectID { platform.ID = primitive.NewObjectID() } query := bson.M{ "_id": platform.ID, } update := bson.M{ "$set": bson.M{ "model_name": platform.ModelName, "model_number": platform.ModelNumber, "vendor": platform.Vendor, "platform_match": platform.PlatformMatch, "interfaces": platform.Interfaces, "power_adapters": platform.Power, "image_url": platform.ImageURL, }, } opts := options.Update().SetUpsert(true) result, err := c.db.Database(MigrationDB).Collection(PlatformCollection).UpdateOne(ctx, query, update, opts) if err != nil { return nil, err } return result, nil } // PlatformDelete is a helper method to remove a single platform record. func (c *Client) PlatformDelete(id string) (*mongo.DeleteResult, error) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() objID, err := primitive.ObjectIDFromHex(id) if err != nil { return nil, err } return c.db.Database(MigrationDB).Collection(PlatformCollection).DeleteOne(ctx, bson.M{"_id": objID}) }