2015-06-20 18:25:21 +02:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
common "github.com/maximilien/softlayer-go/common"
|
2015-06-20 18:25:21 +02:00
|
|
|
datatypes "github.com/maximilien/softlayer-go/data_types"
|
|
|
|
softlayer "github.com/maximilien/softlayer-go/softlayer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type softLayer_Security_Ssh_Key_Service struct {
|
|
|
|
client softlayer.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSoftLayer_Security_Ssh_Key_Service(client softlayer.Client) *softLayer_Security_Ssh_Key_Service {
|
|
|
|
return &softLayer_Security_Ssh_Key_Service{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slssks *softLayer_Security_Ssh_Key_Service) GetName() string {
|
|
|
|
return "SoftLayer_Security_Ssh_Key"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slssks *softLayer_Security_Ssh_Key_Service) CreateObject(template datatypes.SoftLayer_Security_Ssh_Key) (datatypes.SoftLayer_Security_Ssh_Key, error) {
|
|
|
|
parameters := datatypes.SoftLayer_Shh_Key_Parameters{
|
|
|
|
Parameters: []datatypes.SoftLayer_Security_Ssh_Key{
|
|
|
|
template,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := json.Marshal(parameters)
|
|
|
|
if err != nil {
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
data, errorCode, err := slssks.client.GetHttpClient().DoRawHttpRequest(fmt.Sprintf("%s/createObject", slssks.GetName()), "POST", bytes.NewBuffer(requestBody))
|
2015-06-20 18:25:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if common.IsHttpErrorCode(errorCode) {
|
|
|
|
errorMessage := fmt.Sprintf("softlayer-go: could not SoftLayer_Security_Ssh_Key#createObject, HTTP error code: '%d'", errorCode)
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = slssks.client.GetHttpClient().CheckForHttpResponseErrors(data)
|
2015-06-20 18:25:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
softLayer_Ssh_Key := datatypes.SoftLayer_Security_Ssh_Key{}
|
|
|
|
err = json.Unmarshal(data, &softLayer_Ssh_Key)
|
|
|
|
if err != nil {
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return softLayer_Ssh_Key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slssks *softLayer_Security_Ssh_Key_Service) GetObject(sshKeyId int) (datatypes.SoftLayer_Security_Ssh_Key, error) {
|
|
|
|
objectMask := []string{
|
|
|
|
"createDate",
|
|
|
|
"fingerprint",
|
|
|
|
"id",
|
|
|
|
"key",
|
|
|
|
"label",
|
|
|
|
"modifyDate",
|
|
|
|
"notes",
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
response, errorCode, err := slssks.client.GetHttpClient().DoRawHttpRequestWithObjectMask(fmt.Sprintf("%s/%d/getObject.json", slssks.GetName(), sshKeyId), objectMask, "GET", new(bytes.Buffer))
|
2015-06-20 18:25:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if common.IsHttpErrorCode(errorCode) {
|
|
|
|
errorMessage := fmt.Sprintf("softlayer-go: could not SoftLayer_Security_Ssh_Key#getObject, HTTP error code: '%d'", errorCode)
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:25:21 +02:00
|
|
|
sshKey := datatypes.SoftLayer_Security_Ssh_Key{}
|
|
|
|
err = json.Unmarshal(response, &sshKey)
|
|
|
|
if err != nil {
|
|
|
|
return datatypes.SoftLayer_Security_Ssh_Key{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sshKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slssks *softLayer_Security_Ssh_Key_Service) EditObject(sshKeyId int, template datatypes.SoftLayer_Security_Ssh_Key) (bool, error) {
|
|
|
|
parameters := datatypes.SoftLayer_Shh_Key_Parameters{
|
|
|
|
Parameters: []datatypes.SoftLayer_Security_Ssh_Key{
|
|
|
|
template,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := json.Marshal(parameters)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
response, errorCode, err := slssks.client.GetHttpClient().DoRawHttpRequest(fmt.Sprintf("%s/%d/editObject.json", slssks.GetName(), sshKeyId), "POST", bytes.NewBuffer(requestBody))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-06-20 18:25:21 +02:00
|
|
|
|
|
|
|
if res := string(response[:]); res != "true" {
|
|
|
|
return false, errors.New(fmt.Sprintf("Failed to edit SSH key with id: %d, got '%s' as response from the API.", sshKeyId, res))
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if common.IsHttpErrorCode(errorCode) {
|
|
|
|
errorMessage := fmt.Sprintf("softlayer-go: could not SoftLayer_Security_Ssh_Key#editObject, HTTP error code: '%d'", errorCode)
|
|
|
|
return false, errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:25:21 +02:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slssks *softLayer_Security_Ssh_Key_Service) DeleteObject(sshKeyId int) (bool, error) {
|
2016-05-04 17:48:12 +02:00
|
|
|
response, errorCode, err := slssks.client.GetHttpClient().DoRawHttpRequest(fmt.Sprintf("%s/%d.json", slssks.GetName(), sshKeyId), "DELETE", new(bytes.Buffer))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-06-20 18:25:21 +02:00
|
|
|
|
|
|
|
if res := string(response[:]); res != "true" {
|
|
|
|
return false, errors.New(fmt.Sprintf("Failed to destroy ssh key with id '%d', got '%s' as response from the API.", sshKeyId, res))
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if common.IsHttpErrorCode(errorCode) {
|
|
|
|
errorMessage := fmt.Sprintf("softlayer-go: could not SoftLayer_Security_Ssh_Key#deleteObject, HTTP error code: '%d'", errorCode)
|
|
|
|
return false, errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:25:21 +02:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slssks *softLayer_Security_Ssh_Key_Service) GetSoftwarePasswords(sshKeyId int) ([]datatypes.SoftLayer_Software_Component_Password, error) {
|
2016-05-04 17:48:12 +02:00
|
|
|
response, errorCode, err := slssks.client.GetHttpClient().DoRawHttpRequest(fmt.Sprintf("%s/%d/getSoftwarePasswords.json", slssks.GetName(), sshKeyId), "GET", new(bytes.Buffer))
|
2015-06-20 18:25:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return []datatypes.SoftLayer_Software_Component_Password{}, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:48:12 +02:00
|
|
|
if common.IsHttpErrorCode(errorCode) {
|
|
|
|
errorMessage := fmt.Sprintf("softlayer-go: could not SoftLayer_Security_Ssh_Key#getSoftwarePasswords, HTTP error code: '%d'", errorCode)
|
|
|
|
return []datatypes.SoftLayer_Software_Component_Password{}, errors.New(errorMessage)
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:25:21 +02:00
|
|
|
passwords := []datatypes.SoftLayer_Software_Component_Password{}
|
|
|
|
err = json.Unmarshal(response, &passwords)
|
|
|
|
if err != nil {
|
|
|
|
return []datatypes.SoftLayer_Software_Component_Password{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return passwords, nil
|
|
|
|
}
|