summaryrefslogtreecommitdiff
path: root/digitalocean/listKeys.go
diff options
context:
space:
mode:
Diffstat (limited to 'digitalocean/listKeys.go')
-rw-r--r--digitalocean/listKeys.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/digitalocean/listKeys.go b/digitalocean/listKeys.go
new file mode 100644
index 0000000..000a66d
--- /dev/null
+++ b/digitalocean/listKeys.go
@@ -0,0 +1,31 @@
+package digitalocean
+
+import (
+ "context"
+ "fmt"
+
+ "golang.org/x/oauth2"
+
+ "github.com/digitalocean/godo"
+)
+
+func GetSSHKeyID(token, name string) (string, error) {
+ tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
+ oauthClient := oauth2.NewClient(context.Background(), tokenSource)
+ client := godo.NewClient(oauthClient)
+
+ // List all keys.
+ keys, _, err := client.Keys.List(context.Background(), &godo.ListOptions{})
+ if err != nil {
+ return "", err
+ }
+
+ // Find the key by name.
+ for _, key := range keys {
+ if key.Name == name {
+ return key.Fingerprint, nil
+ }
+ }
+
+ return "", fmt.Errorf("SSH Key not found")
+}