Skip to content

Commit eb5f2cf

Browse files
Merge pull request #613 from BrianSantivanez/issue612
Update `order_endurance_storage` example
2 parents fa4a27f + 8976774 commit eb5f2cf

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

content/go/order_endurance_storage.go.md

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "order_endurance_storage.go"
33
description: "order_endurance_storage.go"
4-
date: "2018-04-25"
4+
date: "2023-03-10"
55
classes:
66
- "SoftLayer_Product_Item_Price"
77
- "SoftLayer_Product_Package"
@@ -46,45 +46,100 @@ func main() {
4646
username := ""
4747
apikey := "apikey_goes_here"
4848

49+
// Create a session.
50+
sess := session.New(username, apikey)
51+
52+
// To get item prices and regions and order an endurance storage package ID is required
53+
enduranceStoragePackageID := 240
54+
55+
// This method will return all regions where the package is available
56+
getRegions(sess, enduranceStoragePackageID)
57+
// This method will return all item prices that endurance storage package has available
58+
getItemPrices(sess, enduranceStoragePackageID)
59+
// This method will build a template to order an endurance storage and verify it
60+
VerifyOrder(sess, enduranceStoragePackageID)
61+
62+
}
63+
64+
func getRegions(sess *session.Session, enduranceStoragePackageID int) {
65+
// Get SoftLayer_Product_Package service.
66+
service := services.GetProductPackageService(sess)
67+
68+
// Use GetRegions() method to return all regions where the package is available
69+
regions, err := service.Id(enduranceStoragePackageID).GetRegions()
70+
if err != nil {
71+
fmt.Printf("\n Unable to get regions:\n - %s\n", err)
72+
return
73+
}
74+
75+
// Following helps to print the result in json format.
76+
jsonFormat, jsonErr := json.MarshalIndent(regions, "", " ")
77+
if jsonErr != nil {
78+
fmt.Println(jsonErr)
79+
return
80+
}
81+
82+
fmt.Println(string(jsonFormat))
83+
}
84+
85+
func getItemPrices(sess *session.Session, enduranceStoragePackageID int) {
86+
// Get SoftLayer_Product_Package service.
87+
service := services.GetProductPackageService(sess)
88+
89+
// Use GetItemPrices() method to return all item prices that endurance storage package has available
90+
itemPrices, err := service.Id(enduranceStoragePackageID).GetItemPrices()
91+
if err != nil {
92+
fmt.Printf("\n Unable to get item prices:\n - %s\n", err)
93+
return
94+
}
95+
96+
// Following helps to print the result in json format.
97+
jsonFormat, jsonErr := json.MarshalIndent(itemPrices, "", " ")
98+
if jsonErr != nil {
99+
fmt.Println(jsonErr)
100+
return
101+
}
102+
103+
fmt.Println(string(jsonFormat))
104+
}
105+
106+
func VerifyOrder(sess *session.Session, enduranceStoragePackageID int) {
107+
// Get SoftLayer_Product_Order service.
108+
service := services.GetProductOrderService(sess)
109+
49110
// Declare the location, packageId and quantity for the storage you wish to order
111+
// Use getRegions method to see other locations
50112
quantity := 1
51-
location := "MEXICO"
113+
location := "TORONTO"
52114
packageId := 240
53115
osKeyName := "LINUX"
54116

55117
// Build a skeleton SoftLayer_Product_Item_Price objects. To get the list of valid
56118
// prices for the package use the SoftLayer_Product_Package:getItemPrices method
57-
prices := []datatypes.Product_Item_Price {
58-
{ Id: sl.Int(45098) }, // Block Storage
59-
{ Id: sl.Int(45058) }, // Endurance Storage
60-
{ Id: sl.Int(45148) }, // 40 GB Storage Space
61-
{ Id: sl.Int(45068) }, // 0.25 IOPS per GB
119+
prices := []datatypes.Product_Item_Price{
120+
{Id: sl.Int(45098)}, // Block Storage
121+
{Id: sl.Int(45058)}, // Endurance Storage
122+
{Id: sl.Int(45148)}, // 40 GB Storage Space
123+
{Id: sl.Int(45068)}, // 0.25 IOPS per GB
62124
}
63125

64126
// This should match the OS type of the host(s) that will connect to the volume.
65127
// The only required property is the keyName.
66-
osFormatType := &datatypes.Network_Storage_Iscsi_OS_Type {
128+
osFormatType := &datatypes.Network_Storage_Iscsi_OS_Type{
67129
KeyName: sl.String(osKeyName),
68-
69130
}
70131

71132
// Build a Container_Product_Order_Network_PerformanceStorage object
72-
templateOrder := datatypes.Container_Product_Order_Network_Storage_Enterprise {
73-
Container_Product_Order : datatypes.Container_Product_Order {
74-
Quantity : sl.Int(quantity),
75-
Location : sl.String(location),
76-
PackageId : sl.Int(packageId),
77-
Prices : prices,
133+
templateOrder := datatypes.Container_Product_Order_Network_Storage_Enterprise{
134+
Container_Product_Order: datatypes.Container_Product_Order{
135+
Quantity: sl.Int(quantity),
136+
Location: sl.String(location),
137+
PackageId: sl.Int(packageId),
138+
Prices: prices,
78139
},
79140
OsFormatType: osFormatType,
80141
}
81142

82-
// Create a session.
83-
sess := session.New(username, apikey)
84-
85-
// Get SoftLayer_Product_Order service.
86-
service := services.GetProductOrderService(sess)
87-
88143
// Use verifyOrder() method to check for errors. Replace this with placeOrder() when
89144
// you are ready to order.
90145
receipt, err := service.VerifyOrder(&templateOrder)
@@ -94,7 +149,7 @@ func main() {
94149
}
95150

96151
// Following helps to print the result in json format.
97-
jsonFormat, jsonErr := json.MarshalIndent(receipt,""," ")
152+
jsonFormat, jsonErr := json.MarshalIndent(receipt, "", " ")
98153
if jsonErr != nil {
99154
fmt.Println(jsonErr)
100155
return

0 commit comments

Comments
 (0)