package sshAPI import ( "errors" "github.com/gofrs/uuid" "github.rackspace.com/gorax/clients/coreAPI" "github.rackspace.com/gorax/clients/mydp" ) const NDAPI = "http://sshapi.nsi.rackspace.com:26000/" var ( errNilClient = errors.New("device info cannot be nil") errNoHost = errors.New("host cannot be empty") errNoIP = errors.New("ip address cannot be empty") errNoDC = errors.New("dc cannot be empty") errBast = errors.New("bastion type should be cbast or netbast") ) type ( SSHClient struct { UsePublic bool `json:"use_public"` Context *string `json:"context"` core *coreAPI.Client device *coreAPI.DeviceInfo host string platform string xConnectID string deviceType string } SwitchClient struct { BastionType string `json:"bastion_type"` IPAddress string `json:"ip_address"` DC string `json:"dc"` Host string `json:"host"` } ) func NewSwitchClient(ip, host, dc, bastionType string) (*SwitchClient, error) { if host == "" { return nil, errNoHost } if ip == "" { return nil, errNoIP } if dc == "" { return nil, errNoDC } if bastionType != "cbast" && bastionType != "netbast" { return nil, errBast } return &SwitchClient{ Host: host, IPAddress: ip, DC: dc, BastionType: bastionType, }, nil } func NewSSHClient(c *coreAPI.Client, platform string, device int, public bool) (*SSHClient, error) { if c == nil { return nil, errNilClient } d, err := c.DeviceInfo(platform, device, false) if err != nil { return nil, err } if d.DracIP == "" && d.PrimaryIP == "" { return nil, errors.New("no drac or primary ip listed for device in core") } return &SSHClient{ core: c, device: d, UsePublic: public, platform: platform, }, nil } func NewSSHAPIClient(c *coreAPI.Client, d *coreAPI.DeviceInfo, platform string, public bool, cid uuid.UUID) (*SSHClient, error) { if c == nil { return nil, errNilClient } if d == nil { return nil, errNilClient } return &SSHClient{ core: c, device: d, UsePublic: public, platform: platform, xConnectID: cid.String(), }, nil } func NewDPSSHAPIClient(d mydp.Device, c *coreAPI.Client, platform string, public bool, cid uuid.UUID) (*SSHClient, error) { return &SSHClient{ device: &coreAPI.DeviceInfo{ PrimaryIP: d.PrimaryIP, Name: d.Name, DataCenter: d.DatacenterTitle, Status: d.Disabled, PlatformModel: d.SpecificDeviceType, AccountInfo: coreAPI.AccountInfo{ Name: d.CustomerName, Number: d.CustomerID, }, }, UsePublic: public, platform: platform, xConnectID: cid.String(), core: c, deviceType: "dp", }, nil } // UseAuth will override the initialized setting for the authentication method used // to send to sshapi. func (s *SSHClient) UseAuth(auth string) { s.deviceType = auth }