-
Notifications
You must be signed in to change notification settings - Fork 1
/
keypairs.go
71 lines (61 loc) · 1.5 KB
/
keypairs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"context"
"log"
"time"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/keypairs"
"github.com/gophercloud/gophercloud/v2/pagination"
)
type KeyPair struct {
resource *KeyPairParser
client *gophercloud.ServiceClient
}
func (s KeyPair) CreatedAt() time.Time {
return s.resource.UpdatedAt
}
func (s KeyPair) Delete(ctx context.Context) error {
return keypairs.Delete(ctx, s.client, s.ID(), keypairs.DeleteOpts{}).ExtractErr()
}
func (s KeyPair) Type() string {
return "key"
}
func (s KeyPair) ID() string {
return s.resource.Name
}
func (s KeyPair) Name() string {
return s.resource.Name
}
type KeyPairParser struct {
keypairs.KeyPair
UpdatedAt time.Time `json:"created_at"`
}
func ListKeyPairs(ctx context.Context, client *gophercloud.ServiceClient) <-chan Resource {
ch := make(chan Resource)
go func() {
defer close(ch)
if err := keypairs.List(client, nil).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
keypairPage, err := keypairs.ExtractKeyPairs(page)
if err != nil {
return true, err
}
for i := range keypairPage {
var k struct {
KeyPairParser `json:"keypair"`
}
if err := keypairs.Get(ctx, client, keypairPage[i].Name, nil).ExtractInto(&k); err != nil {
return true, err
}
log.Println(k)
ch <- KeyPair{
resource: &k.KeyPairParser,
client: client,
}
}
return true, err
}); err != nil {
panic(err)
}
}()
return ch
}