jynx/backend/hetzner/sshKeys.go

39 lines
699 B
Go

package hetzner
import (
"context"
"github.com/hetznercloud/hcloud-go/hcloud"
)
func getSSHKeys(names ...string) ([]*hcloud.SSHKey, error) {
// if no names were provided return nil object
if len(names) == 0 {
return nil, nil
}
// connect to hcloud client
client := connect()
// get all ssh keys from hcloud
keys, err := client.SSHKey.All(context.Background())
if err != nil {
return nil, err
}
// variable to hold found keys
var foundKeys []*hcloud.SSHKey
// check if name is found in keys
for i := range keys {
for j := range names {
if keys[i].Name == names[j] {
foundKeys = append(foundKeys, keys[i])
}
}
}
// return found keys
return foundKeys, nil
}