package asa import ( "strconv" "github.com/gofrs/uuid" "github.com/sirupsen/logrus" "github.rackspace.com/gorax/clients/asahttp" "github.rackspace.com/gorax/clients/coreAPI" "github.rackspace.com/gorax/clients/mydp" "github.rackspace.com/gorax/clients/sshAPI" ) type ( // Opt defines optional parameters that can be passed into an ASA // initialisation function to generalize client inits Opt func(*ASA) ) // WithCoreAPI will set the coreapi elem in the ASA type with the passed in variable func WithCoreAPI(c *coreAPI.Client) Opt { return func(asa *ASA) { asa.coreapi = c } } // WithDeviceInfo will set the coreapi deviceinfo in the ASA type with the passed in variable func WithDeviceInfo(d *coreAPI.DeviceInfo) Opt { return func(asa *ASA) { asa.Info = d } } // WithMyDPInfo will set the mydp.Device elem in the ASA type with the passed in variable func WithMyDPInfo(c *mydp.Device) Opt { return func(asa *ASA) { asa.dpInfo = *c } } // WithLogger will set the log elem in the ASA type with the passed in variable func WithLogger(logger logrus.Ext1FieldLogger) Opt { return func(asa *ASA) { asa.log = logger } } // WithSSHAPI will set the sshClient elem in the ASA type with the passed in variable func WithSSHAPI(ssh *sshAPI.SSHClient) Opt { return func(asa *ASA) { asa.sshClient = ssh } } // UseDRAC will set the useDRAC variable func UseDRAC() Opt { return func(asa *ASA) { asa.useDRAC = true } } // NewOptASA is a general initialization of the ASA client that takes optional parameters to // initialize the ASA type in different ways. func NewOptASA(id int, user, pass string, opts ...Opt) (a *ASA, err error) { a = &ASA{Device: id} // Apply the optional arguments from the initialization for _, opt := range opts { opt(a) } // If the UUID was not set from the options, create a new UUID // type and set it on the ASA client. if a.uuid == nil { uid, err := uuid.NewV4() if err != nil { return nil, err } a.uuid = &uid } if a.log == nil { newLog := logrus.New() newLog.SetLevel(defaultLogLevel) a.log = newLog } // If we are going to use coreapi, then this will be treated as a CORE device if a.coreapi != nil { if a.Info == nil { if a.Info, err = a.coreapi.DeviceInfo("asa", id, false); err != nil { return nil, err } if a.Info.PrimaryIP == "" && a.Info.DracIP == "" { if a.Info, err = a.coreapi.DeviceInfo("asa", id, true); err != nil { return nil, err } } } a.Failover = func() *int { if a.Info.FailoverDevice == nil { return nil } if i, err := strconv.Atoi(a.Info.FailoverDevice.FailoverID); err == nil { return &i } return nil }() if a.sshClient == nil { if a.sshClient, err = sshAPI.NewSSHAPIClient(a.coreapi, a.Info, "asa", !a.useDRAC, *a.uuid); err != nil { return nil, err } } } // If we've initialized with Datapipe device information, then we will initialize this as // a Datapipe client if a.dpInfo.DeviceID != "" { a.Info = &coreAPI.DeviceInfo{ PrimaryIP: a.dpInfo.PrimaryIP, Name: a.dpInfo.Name, DataCenter: a.dpInfo.DatacenterTitle, Status: a.dpInfo.Disabled, PlatformModel: a.dpInfo.SpecificDeviceType, AccountInfo: coreAPI.AccountInfo{ Name: a.dpInfo.CustomerName, Number: a.dpInfo.CustomerID, }, } if a.sshClient == nil { core := &coreAPI.Client{SessionInfo: coreAPI.SessionInfo{Username: user}} if a.sshClient, err = sshAPI.NewDPSSHAPIClient(a.dpInfo, core, "asa", !a.useDRAC, *a.uuid); err != nil { return nil, err } } } // In either scenario, we will initialize the http client in the same manner httpClient, err := asahttp.NewASA(a.Info, user, pass, !a.useDRAC) if err != nil { return nil, err } a.httpClient = *httpClient return a, nil }