github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/sshkeys.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  type SSHKeyRequest struct {
     8  	Name        string `json:"name,omitempty"`
     9  	Description string `json:"description,omitempty"`
    10  	PublicKey   string `json:"public_key,omitempty"`
    11  }
    12  
    13  type SSHKey struct {
    14  	Identity
    15  	descField
    16  	State        string       `json:"state,omitempty"`
    17  	Servers      *[]SSHServer `json:"servers,omitempty"`
    18  	Md5          string       `json:"md5,omitempty"`
    19  	PublicKey    string       `json:"public_key,omitempty"`
    20  	CreationDate string       `json:"creation_date,omitempty"`
    21  	ApiPtr
    22  }
    23  
    24  type SSHServer struct {
    25  	Id   string `json:"id,omitempty"`
    26  	Name string `json:"name,omitempty"`
    27  }
    28  
    29  func (api *API) ListSSHKeys(args ...interface{}) ([]SSHKey, error) {
    30  	url, err := processQueryParams(createUrl(api, sshkeyPathSegment), args...)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	result := []SSHKey{}
    35  	err = api.Client.Get(url, &result, http.StatusOK)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	for index := range result {
    40  		result[index].api = api
    41  	}
    42  	return result, nil
    43  }
    44  
    45  func (api *API) GetSSHKey(id string) (*SSHKey, error) {
    46  	result := new(SSHKey)
    47  	url := createUrl(api, sshkeyPathSegment, id)
    48  	err := api.Client.Get(url, &result, http.StatusOK)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	result.api = api
    53  	return result, nil
    54  }
    55  
    56  func (api *API) CreateSSHKey(request *SSHKeyRequest) (string, *SSHKey, error) {
    57  	result := new(SSHKey)
    58  	url := createUrl(api, sshkeyPathSegment)
    59  	err := api.Client.Post(url, request, &result, http.StatusCreated)
    60  
    61  	if err != nil {
    62  		return "", nil, err
    63  	}
    64  	result.api = api
    65  	return result.Id, result, nil
    66  }
    67  
    68  func (api *API) DeleteSSHKey(id string) (*SSHKey, error) {
    69  	result := new(SSHKey)
    70  	url := createUrl(api, sshkeyPathSegment, id)
    71  	err := api.Client.Delete(url, nil, &result, http.StatusOK)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	result.api = api
    76  	return result, nil
    77  }
    78  
    79  func (api *API) RenameSSHKey(id string, new_name string, new_desc string) (*SSHKey, error) {
    80  	data := struct {
    81  		Name        string `json:"name,omitempty"`
    82  		Description string `json:"description,omitempty"`
    83  	}{Name: new_name, Description: new_desc}
    84  	result := new(SSHKey)
    85  	url := createUrl(api, sshkeyPathSegment, id)
    86  	err := api.Client.Put(url, &data, &result, http.StatusOK)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	result.api = api
    91  	return result, nil
    92  }