forked from cloudfoundry/bosh-softlayer-cpi-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softlayer_network_storage.go
173 lines (138 loc) · 4.9 KB
/
softlayer_network_storage.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
165
166
167
168
169
170
171
172
173
package services
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
datatypes "github.com/maximilien/softlayer-go/data_types"
softlayer "github.com/maximilien/softlayer-go/softlayer"
)
const (
NETWORK_STORAGE_PACKAGE_ID = 0
CREATE_ISCSI_VOLUME_MAX_RETRY_TIME = 3
CREATE_ISCSI_VOLUME_CHECK_INTERVAL = 20 // seconds
)
type softLayer_Network_Storage_Service struct {
client softlayer.Client
}
func NewSoftLayer_Network_Storage_Service(client softlayer.Client) *softLayer_Network_Storage_Service {
return &softLayer_Network_Storage_Service{
client: client,
}
}
func (slns *softLayer_Network_Storage_Service) GetName() string {
return "SoftLayer_Network_Storage"
}
func (slns *softLayer_Network_Storage_Service) CreateIscsiVolume(size int, location string) (datatypes.SoftLayer_Network_Storage, error) {
iscsiVolumeItemId, err := slns.getIscsiVolumeItemIdBasedOnSize(size)
if err != nil {
return datatypes.SoftLayer_Network_Storage{}, err
}
order := datatypes.SoftLayer_Product_Order{
Location: location,
ComplexType: "SoftLayer_Container_Product_Order",
Prices: []datatypes.SoftLayer_Item_Price{
datatypes.SoftLayer_Item_Price{
Id: iscsiVolumeItemId,
},
},
PackageId: NETWORK_STORAGE_PACKAGE_ID,
}
productOrderService, _ := slns.client.GetSoftLayer_Product_Order_Service()
receipt, err := productOrderService.PlaceOrder(order)
if err != nil {
return datatypes.SoftLayer_Network_Storage{}, err
}
var iscsiStorage datatypes.SoftLayer_Network_Storage
for i := 0; i < CREATE_ISCSI_VOLUME_MAX_RETRY_TIME; i++ {
iscsiStorage, err = slns.findIscsiVolumeIdByOrderId(receipt.OrderId)
if err == nil {
break
} else if i == CREATE_ISCSI_VOLUME_MAX_RETRY_TIME-1 {
return datatypes.SoftLayer_Network_Storage{}, err
}
time.Sleep(CREATE_ISCSI_VOLUME_CHECK_INTERVAL * time.Second)
}
return iscsiStorage, nil
}
func (slns *softLayer_Network_Storage_Service) DeleteIscsiVolume(volumeId int, immediateCancellationFlag bool) error {
accountService, _ := slns.client.GetSoftLayer_Account_Service()
iscsiStorages, _ := accountService.GetIscsiNetworkStorage()
var accountId, billingItemId int
for i := 0; i < len(iscsiStorages); i++ {
if iscsiStorages[i].Id == volumeId {
accountId = iscsiStorages[i].AccountId
billingItemId = iscsiStorages[i].BillingItem.Id
break
}
}
billingItemCancellationRequest := datatypes.SoftLayer_Billing_Item_Cancellation_Request{
ComplexType: "SoftLayer_Billing_Item_Cancellation_Request",
AccountId: accountId,
Items: []datatypes.SoftLayer_Billing_Item_Cancellation_Request_Item{
{
BillingItemId: billingItemId,
ImmediateCancellationFlag: immediateCancellationFlag,
},
},
}
billingItemCancellationRequestService, _ := slns.client.GetSoftLayer_Billing_Item_Cancellation_Request_Service()
_, err := billingItemCancellationRequestService.CreateObject(billingItemCancellationRequest)
if err != nil {
return err
}
return nil
}
func (slns *softLayer_Network_Storage_Service) GetIscsiVolume(volumeId int) (datatypes.SoftLayer_Network_Storage, error) {
response, err := slns.client.DoRawHttpRequest(fmt.Sprintf("%s/%d/getObject.json", slns.GetName(), volumeId), "GET", new(bytes.Buffer))
if err != nil {
return datatypes.SoftLayer_Network_Storage{}, err
}
volume := datatypes.SoftLayer_Network_Storage{}
err = json.Unmarshal(response, &volume)
if err != nil {
return datatypes.SoftLayer_Network_Storage{}, err
}
return volume, nil
}
// Private methods
func (slns *softLayer_Network_Storage_Service) findIscsiVolumeIdByOrderId(orderId int) (datatypes.SoftLayer_Network_Storage, error) {
accountService, _ := slns.client.GetSoftLayer_Account_Service()
iscsiStorages, _ := accountService.GetIscsiNetworkStorage()
for i := 0; i < len(iscsiStorages); i++ {
storage := iscsiStorages[i]
if storage.BillingItem != nil && storage.BillingItem.OrderItem.Order.Id == orderId {
return storage, nil
}
}
return datatypes.SoftLayer_Network_Storage{}, errors.New(fmt.Sprintf("Can not find an iSCSI volume with order id %d", orderId))
}
func (slns *softLayer_Network_Storage_Service) getIscsiVolumeItemIdBasedOnSize(size int) (int, error) {
productPackageService, err := slns.client.GetSoftLayer_Product_Package_Service()
if err != nil {
return 0, err
}
itemPrices, err := productPackageService.GetItemPrices(NETWORK_STORAGE_PACKAGE_ID)
if err != nil {
return 0, err
}
var currentItemId, currentVolumeCapacity int
for _, itemPrice := range itemPrices {
if strings.Contains(itemPrice.Item.Description, "iSCSI SAN Storage") {
capacity, _ := strconv.Atoi(itemPrice.Item.Capacity)
if capacity >= size {
if currentItemId == 0 || currentVolumeCapacity >= capacity {
currentItemId = itemPrice.Id
currentVolumeCapacity = capacity
}
}
}
}
if currentItemId == 0 {
return 0, errors.New(fmt.Sprintf("No proper iSCSI volume for size %d", size))
}
return currentItemId, nil
}