// 2019 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 postmigration will perform audits against devices after they are brought online in the destination package postmigration import ( "net/http" "github.rackspace.com/DCM/tools" "go.mongodb.org/mongo-driver/mongo" "github.com/sirupsen/logrus" migrationMongo "github.rackspace.com/DCM/migration/pkg/collection/mongo" "github.rackspace.com/gorax/clients/coreAPI" "github.rackspace.com/gorax/clients/dcxAutohost" "github.rackspace.com/gorax/clients/unipede" "github.rackspace.com/gorax/passkey" ) type ( // Client type represents the client connectivity required for the // migration/spanning-tree package. Client struct { MigrationType string u *unipede.Client d *dcxAutohost.Client c *coreAPI.Client m *mongo.Client pk *passkey.Client ip string age *int } // Option defines the signature that will // allow different options in the New function. Option func(*Client) // Audit interface defines the requirements for a platform to fulfill // in it's methods to satisfy a valid post-migration audit. Audit interface { Run() (Result, error) init() error ports() boot() failover() other() } // Result represents the return values passed back to the caller of // the New audit function. // // swagger:model Result struct { Audits map[ErrType][]string `json:"audits"` Error map[ErrType][]string `json:"errors"` } ) // WithHTTPRequest will create a new spanning-tree audit using // an *http.Request as the initializer. // nolint:unused func WithHTTPRequest(r *http.Request) Option { return func(c *Client) { c.u = unipede.FromContext(r.Context()) c.d = dcxAutohost.FromContext(r.Context()) c.c = coreAPI.FromContext(r.Context()) c.m = tools.MongoFromContext(r.Context()) c.pk = passkey.FromContext(r.Context()) } } // WithClient will create a new spanning-tree audit using // an *unipede.Client as the initializer. // nolint:unused func WithClient(u *unipede.Client, d *dcxAutohost.Client, core *coreAPI.Client, mgo *mongo.Client, pk *passkey.Client) Option { return func(c *Client) { c.u = u c.d = d c.c = core c.m = mgo c.pk = pk } } // WithAge will set the unipede age to use for queries // nolint:unused func WithAge(i int) Option { logrus.Debugf("setting unipede age to %d seconds", i) return func(c *Client) { c.age = &i } } // WithMgmtIP will set an IP address to use to connect to the device in place of // the management-ip defined in CORE. // nolint:unused func WithMgmtIP(ip string) Option { return func(c *Client) { c.ip = ip } } // New method will take a device within the mongo collection, and determine the amount of VLANs // in the source with their related switches in the destination to determine if we are going to // exceed the amount of spanning-tree instances on a migration switch. func New(d tools.Device, migType string, opts ...Option) (Result, error) { // Initialize an Audit client to use within // the specific platform type. client := new(Client) for _, f := range opts { f(client) } client.MigrationType = migType switch migrationMongo.Platform(d) { case "asa": logrus.Infof("Running %d as type Cisco ASA", d.ID) return PerformFor(ASA(client, d)) case "f5": logrus.Infof("Running %d as type F5 Big-IP", d.ID) return PerformFor(BigIP(client, d)) case "waf": logrus.Infof("Running %d as type Imperva WAF", d.ID) return PerformFor(WAF(client, d)) case "adx": logrus.Infof("Running %d as type Brocade ADX", d.ID) return PerformFor(ADX(client, d)) case "netscaler": logrus.Infof("Running %d as type Citrix Netscaler", d.ID) return PerformFor(Default(client, d)) case "ids": logrus.Infof("Running %d as type Alert Logic IDS", d.ID) return PerformFor(Default(client, d)) default: logrus.Infof("Running %d as Default", d.ID) return PerformFor(Default(client, d)) } } // PerformFor will take an Audit interface and preform the platform specific audit // based on the passed in type. func PerformFor(a Audit) (Result, error) { return a.Run() }