forked from onecityuni/docker-volume-plugin-dostorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_facade.go
164 lines (134 loc) · 4.3 KB
/
do_facade.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"time"
"github.com/Sirupsen/logrus"
"github.com/digitalocean/go-metadata"
"github.com/digitalocean/godo"
)
const (
StorageActionRetryCount = 3
StorageActionRetryInterval = 1000 * time.Millisecond
StorageActionCompletionPollCount = 60
StorageActionCompletionPollInterval = 500 * time.Millisecond
MaxResultsPerPage = 200
)
type DoFacade struct {
metadataClient *metadata.Client
apiClient *godo.Client
}
func NewDoFacade(metadataClient *metadata.Client, apiClient *godo.Client) *DoFacade {
return &DoFacade{
metadataClient: metadataClient,
apiClient: apiClient,
}
}
func (s DoFacade) GetLocalRegion() (string, error) {
return s.metadataClient.Region()
}
func (s DoFacade) GetLocalDropletID() (int, error) {
return s.metadataClient.DropletID()
}
func (s DoFacade) GetVolume(volumeID string) (*godo.Volume, error) {
doVolume, _, err := s.apiClient.Storage.GetVolume(volumeID)
return doVolume, err
}
func (s DoFacade) GetVolumeByRegionAndName(region string, name string) *godo.Volume {
doVolumes, _, err := s.apiClient.Storage.ListVolumes(&godo.ListOptions{Page: 1, PerPage: MaxResultsPerPage})
if err != nil {
logrus.Errorf("failed to get the volume by region and name: %v", err)
return nil
}
for i := range doVolumes {
if doVolumes[i].Region.Slug == region && doVolumes[i].Name == name {
return &doVolumes[i]
}
}
return nil
}
func (s DoFacade) IsVolumeAttachedToDroplet(volumeID string, dropletID int) bool {
doVolume, _, err := s.apiClient.Storage.GetVolume(volumeID)
if err != nil {
logrus.Errorf("failed to get the volume: %v", err)
return false
}
for _, attachedDropletID := range doVolume.DropletIDs {
if attachedDropletID == dropletID {
return true
}
}
return false
}
func (s DoFacade) DetachVolumeFromAllDroplets(volumeID string) error {
logrus.Infof("detaching the volume '%v' from all droplets", volumeID)
attachedDropletIDs := s.getAttachedDroplets(volumeID)
for _, attachedDropletID := range attachedDropletIDs {
derr := s.DetachVolumeFromDroplet(volumeID, attachedDropletID)
if derr != nil {
return derr
}
}
return nil
}
func (s DoFacade) DetachVolumeFromDroplet(volumeID string, dropletID int) error {
logrus.Infof("detaching the volume from the droplet %v", dropletID)
var lastErr error
for i := 1; i <= StorageActionRetryCount; i++ {
action, _, derr := s.apiClient.StorageActions.DetachByDropletID(volumeID, dropletID)
if derr != nil {
logrus.Errorf("failed to detach the volume: %v", lastErr)
time.Sleep(StorageActionRetryInterval)
lastErr = derr
} else {
lastErr = s.waitForVolumeActionToComplete(volumeID, action.ID)
break
}
}
return lastErr
}
func (s DoFacade) AttachVolumeToDroplet(volumeID string, dropletID int) error {
logrus.Infof("detaching the volume '%v' from the droplet %v", dropletID)
var lastErr error
for i := 1; i <= StorageActionRetryCount; i++ {
action, _, aerr := s.apiClient.StorageActions.Attach(volumeID, dropletID)
if aerr != nil {
logrus.Errorf("failed to attach the volume: %v", aerr)
time.Sleep(StorageActionRetryInterval)
lastErr = aerr
} else {
lastErr = s.waitForVolumeActionToComplete(volumeID, action.ID)
break
}
}
return lastErr
}
func (s DoFacade) getAttachedDroplets(volumeID string) []int {
doVolume, _, err := s.apiClient.Storage.GetVolume(volumeID)
if err != nil {
logrus.Errorf("Error getting the volume: %v", err.Error())
return []int{}
}
return doVolume.DropletIDs
}
func (s DoFacade) waitForVolumeActionToComplete(volumeID string, actionID int) error {
logrus.Infof("waiting for the storage action %v to complete", actionID)
lastStatus := "n/a"
for i := 1; i <= StorageActionCompletionPollCount; i++ {
action, _, aerr := s.apiClient.StorageActions.Get(volumeID, actionID)
if aerr == nil {
lastStatus = action.Status
if action.Status == "completed" || action.Status == "errored" {
break
}
} else {
logrus.Errorf("failed to query the storage action: %v", aerr)
}
time.Sleep(StorageActionCompletionPollInterval)
}
if lastStatus == "completed" {
logrus.Info("the action completed")
return nil
}
logrus.Errorf("the action did not complete but ended with status '%v'", lastStatus)
return fmt.Errorf("the action did not complete but ended with status '%v'", lastStatus)
}