package unipede import ( "errors" "net/http" "net/url" ) type ( UpConfigText struct { Count int `json:"count" bson:"count"` Items []*ConfigItems `json:"items" bson:"items"` Links *map[string]Links `json:"links" bson:"links"` } ConfigItems struct { Config *string `json:"config" bson:"config"` Created string `json:"created" bson:"created"` Id int `json:"id" bson:"id"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` Updated string `json:"updated" bson:"updated"` } AllowedVlanRange struct { Count int `json:"count" bson:"count"` Items []VlanRange `json:"items" bson:"items"` } VlanRange struct { Created string `json:"created" bson:"created"` High int `json:"high" bson:"high"` ID int `json:"id" bson:"id"` Interface string `json:"interface_name" bson:"interface_name"` Low int `json:"low" bson:"low"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` Updated string `json:"updated" bson:"updated"` } cfgLineResp struct { Count int `json:"count" bson:"count"` Items []ConfigLineItem `json:"items" bson:"items"` Links map[string]Links `json:"links" bson:"links"` } // ConfigLineItem is the usable data returned from the devices/config_line request ConfigLineItem struct { Succeeded string `json:"succeeded" bson:"succeeded"` ID int `json:"id" bson:"id"` UpDeviceID int `json:"up_device_id" bson:"up_device_id"` Created string `json:"created" bson:"created"` UpConfigContentID int `json:"up_config_line_content_id" bson:"up_config_line_content_id"` Context string `json:"context" bson:"context"` Command string `json:"command" bson:"command"` Updated string `json:"updated" bson:"updated"` } ) func (u *Client) UpConfigText(switchName string, refresh *int, options ...*UnipedeOptions) (*UpConfigText, error) { o := processOptions(refresh, nil, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/UpConfigText::Model", http.MethodGet, o, nil) if err != nil { return nil, err } swconf := new(UpConfigText) _, err = u.do(req, o, swconf) return swconf, err } // ConfigLine method will retrieve a list of configuration lines for a device. Can be filtered by context // See: https://unipede.rackspace.net/docs/api/#/deviceProperties/ConfigLineForDevice func (u *Client) ConfigLine(switchName string, refresh *int, options ...*UnipedeOptions) ([]ConfigLineItem, error) { o := processOptions(refresh, nil, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/config_line", http.MethodGet, o, nil) if err != nil { return nil, err } response := new(cfgLineResp) if _, err := u.do(req, o, response); err != nil { return nil, err } return response.Items, nil } func (u *Client) ConfigAllowedVlanRange(params url.Values, switchName string, refresh *int, options ...*UnipedeOptions) (AllowedVlanRange, error) { ret := new(AllowedVlanRange) o := processOptions(refresh, ¶ms, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/config_allowed_vlan_range", http.MethodGet, o, nil) if err != nil { return *ret, err } _, err = u.do(req, o, ret) return *ret, err } func (u *Client) GetSwitchConfig(switchName string, refresh *int, options ...*UnipedeOptions) (*string, error) { upconf, err := u.UpConfigText(switchName, refresh, options...) if err != nil { return nil, err } return upconf.GetConfig() } // returns first config in items list func (u *UpConfigText) GetConfig() (*string, error) { for _, i := range u.Items { if i.Config != nil { if *i.Config == "Command authorization failed." { return nil, errors.New("Unipede returned Command authorization failed.") } return i.Config, nil } } return nil, errors.New("Unable to find config text.") } func (u *UpConfigText) Updated() *string { for _, i := range u.Items { return &i.Updated } return nil }