// 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 network interfaces with Unipede and FE for network related actions. package network import ( "fmt" "net/http" "strings" dcmTools "github.rackspace.com/DCM/tools" "github.rackspace.com/DCM/migration/pkg/collection/dp" "github.rackspace.com/DCM/migration/pkg/collection/mongo" "github.rackspace.com/gorax/clients/raxNewrelic" "github.rackspace.com/gorax/clients/unipede" ) // routines to use for importing of data. Each call will spawn x number of routines // to run the calls to populate the database. const routines = 10 var r *strings.Replacer type ( // Client type represents client references to third-party resources required // for the methods within this package. Client struct { u *unipede.Client db *mongo.Client nr *raxNewrelic.Client dp *dp.Client Collection string MigrationType string } // Opt defines the different types of options passed into the New function Opt func(c *Options) // Options defines data you can optionally set during client creation Options struct { collection string migrationType string } // PublicZoneInfo type represents the data that is collected on // within the migration database. PublicZoneInfo struct { Aggr string `json:"aggr"` Vlan int `json:"vlan"` Zone string `json:"zone"` ZoneCount int `json:"zone_count"` AccountZones []string `json:"account_zones"` } ) // SetCollection sets the collection name // nolint:unused func SetCollection(collection string) Opt { return func(o *Options) { o.collection = collection } } // SetMigrationType sets the migration type // nolint:unused func SetMigrationType(migType string) Opt { return func(o *Options) { if migType == "" { return } o.migrationType = migType } } // NewContext function will take a pointer to a Request, and pull the client references // from the Context() offered within the default net/http package. func NewContext(r *http.Request, opts ...Opt) *Client { // Apply default options and generate a new options type. opt := &Options{migrationType: dcmTools.MigTypeRackspace} // Apply all options for _, o := range opts { o(opt) } return &Client{ u: unipede.FromContext(r.Context()), db: mongo.NewContext(r), nr: raxNewrelic.FromContext(r.Context()), dp: dp.NewContext(r), Collection: opt.collection, MigrationType: opt.migrationType, } } func aggrReplacer(dc string) *strings.Replacer { r := []string{ "a-", "-", "b-", "-", "dist1-1", "dist1", "dist1-2", "dist1", "dist2-1", "dist2", "dist2-2", "dist2", "dist3-1", "dist3", "dist3-2", "dist3", "dist4-1", "dist4", "dist4-2", "dist4", "aggrit1", "aggrit", "aggrit2", "aggrit", "agg-r1", "agg-r", "agg-r2", "agg-r", "colo1", "colo", "colo2", "colo", } r = append(r, fmt.Sprintf("a.%s", dc), "") r = append(r, fmt.Sprintf("b.%s", dc), "") r = append(r, fmt.Sprintf(".%s", dc), "") return strings.NewReplacer(r...) }