-
Notifications
You must be signed in to change notification settings - Fork 1
/
routers.go
103 lines (89 loc) · 2.5 KB
/
routers.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"strings"
"time"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/v2/pagination"
)
type Router struct {
resource *RouterParser
client *gophercloud.ServiceClient
}
func (s Router) CreatedAt() time.Time {
return s.resource.CreatedAt
}
func (s Router) Delete(ctx context.Context) error {
for _, subnet := range s.resource.subnets {
if _, err := routers.RemoveInterface(ctx, s.client, s.resource.ID, routers.RemoveInterfaceOpts{SubnetID: subnet}).Extract(); err != nil {
return err
}
}
return routers.Delete(ctx, s.client, s.resource.ID).ExtractErr()
}
func (s Router) Type() string {
return "router"
}
func (s Router) ID() string {
return s.resource.ID
}
func (s Router) Name() string {
return s.resource.Name
}
func (s Router) Tags() []string {
return s.resource.Tags
}
func (s Router) ClusterID() string {
for _, tag := range s.resource.Tags {
if value := strings.TrimPrefix(tag, "openshiftClusterID="); value != tag {
return value
}
}
return ""
}
type RouterParser struct {
routers.Router
CreatedAt time.Time `json:"created_at"`
subnets []string
}
func ListRouters(ctx context.Context, client *gophercloud.ServiceClient) <-chan Resource {
ch := make(chan Resource)
go func() {
defer close(ch)
if err := routers.List(client, routers.ListOpts{}).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
var routerPage struct {
Routers []RouterParser `json:"routers"`
}
err := (page.(routers.RouterPage)).ExtractInto(&routerPage)
if err != nil {
return true, err
}
for i := range routerPage.Routers {
if err := ports.List(client, ports.ListOpts{DeviceID: routerPage.Routers[i].ID, DeviceOwner: "network:router_interface"}).EachPage(ctx, func(_ context.Context, page pagination.Page) (bool, error) {
portList, err := ports.ExtractPorts(page)
if err != nil {
return false, err
}
for _, port := range portList {
for j := range port.FixedIPs {
routerPage.Routers[i].subnets = append(routerPage.Routers[i].subnets, port.FixedIPs[j].SubnetID)
}
}
return true, nil
}); err != nil {
return true, err
}
ch <- Router{
resource: &routerPage.Routers[i],
client: client,
}
}
return true, err
}); err != nil {
panic(err)
}
}()
return ch
}