-
Notifications
You must be signed in to change notification settings - Fork 1
/
containers.go
133 lines (118 loc) · 3.83 KB
/
containers.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/objectstorage/v1/containers"
"github.com/gophercloud/gophercloud/v2/openstack/objectstorage/v1/objects"
"github.com/gophercloud/gophercloud/v2/pagination"
)
type Container struct {
resourceName string
client *gophercloud.ServiceClient
clusterID string
associatedResource Resource
}
func (s Container) CreatedAt() time.Time {
if s.associatedResource != nil {
return s.associatedResource.CreatedAt()
}
return time.Time{}
}
func (s Container) Delete(ctx context.Context) error {
if err := objects.List(s.client, s.ID(), &objects.ListOpts{Limit: 50}).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
objectsOnPage, err := objects.ExtractNames(page)
if err != nil {
return false, err
}
resp, err := objects.BulkDelete(ctx, s.client, s.ID(), objectsOnPage).Extract()
if err != nil {
return false, err
}
if len(resp.Errors) > 0 {
// Convert resp.Errors to golang errors.
// Each error is represented by a list of 2 strings, where the first one
// is the object name, and the second one contains an error message.
errs := make([]error, len(resp.Errors))
for i, objectError := range resp.Errors {
errs[i] = fmt.Errorf("cannot delete object %s: %s", objectError[0], objectError[1])
}
return false, fmt.Errorf("errors occurred during bulk deleting of container %s objects: %v", s.ID(), errs)
}
return true, nil
}); !gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
log.Printf("Bulk deleting of container %q objects failed: %v", s.ID(), err)
return err
}
log.Printf("Deleting container %q", s.ID())
if _, err := containers.Delete(ctx, s.client, s.ID()).Extract(); err != nil {
// Ignore the error if the container cannot be found and return with an appropriate message if it's another type of error
if gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
log.Printf("Cannot find container %q. It's probably already been deleted.", s.ID())
} else {
log.Printf("Deleting container %q failed: %v", s.ID(), err)
}
return err
}
return nil
}
func (s Container) Type() string {
return "container"
}
func (s Container) ID() string {
return s.resourceName
}
func (s Container) Name() string {
return s.resourceName
}
func (s Container) ClusterID() string {
return s.clusterID
}
type ContainerParser struct {
containers.Container
Properties map[string]string `json:"properties"`
}
func ListContainers(ctx context.Context, client *gophercloud.ServiceClient, networks <-chan Resource) <-chan Resource {
ch := make(chan Resource)
clusterNetworks := make(map[string]Resource)
for network := range networks {
if clusterNetwork, ok := network.(Clusterer); ok && clusterNetwork.ClusterID() != "" {
clusterNetworks[clusterNetwork.ClusterID()] = network
}
}
go func() {
defer close(ch)
if err := containers.List(client, nil).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
containerPage, err := containers.ExtractNames(page)
if err != nil {
return true, err
}
for _, containerName := range containerPage {
getResult := containers.Get(ctx, client, containerName, nil)
if getResult.Err != nil {
return true, err
}
c := Container{
resourceName: containerName,
client: client,
clusterID: getResult.Header.Get("X-Container-Meta-Openshiftclusterid"),
}
if n, ok := clusterNetworks[c.ClusterID()]; ok {
c.associatedResource = n
}
ch <- c
}
return true, nil
}); err != nil {
if gophercloud.ResponseCodeIs(err, http.StatusForbidden) {
log.Printf("Skipping containers deletion. User not authorized to perform the requested action")
} else {
panic(err)
}
}
}()
return ch
}