• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

softlayer / softlayer-go / 13824212013

13 Mar 2025 12:26AM UTC coverage: 95.953% (-2.2%) from 98.143%
13824212013

Pull #201

github

web-flow
Merge 0268bb9e4 into 80a745b76
Pull Request #201: Bump golang.org/x/net from 0.23.0 to 0.36.0

36825 of 38378 relevant lines covered (95.95%)

7.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/helpers/product/product.go
1
/**
2
 * Copyright 2016 IBM Corp.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package product
18

19
import (
20
        "fmt"
21

22
        "strings"
23

24
        "github.com/softlayer/softlayer-go/datatypes"
25
        "github.com/softlayer/softlayer-go/filter"
26
        "github.com/softlayer/softlayer-go/services"
27
        "github.com/softlayer/softlayer-go/session"
28
        "github.com/softlayer/softlayer-go/sl"
29
)
30

31
// CPUCategoryCode Category code for cpus
32
const CPUCategoryCode = "guest_core"
33

34
// MemoryCategoryCode Category code for Memory
35
const MemoryCategoryCode = "ram"
36

37
// NICSpeedCategoryCode Category code for NIC speed
38
const NICSpeedCategoryCode = "port_speed"
39

40
// DedicatedLoadBalancerCategoryCode Category code for Dedicated Load Balancer
41
const DedicatedLoadBalancerCategoryCode = "dedicated_load_balancer"
42

43
// ProxyLoadBalancerCategoryCode Category code for Shared local load balancer (proxy load balancer)
44
const ProxyLoadBalancerCategoryCode = "proxy_load_balancer"
45

46
// GetPackageByType Get the Product_Package which matches the specified
47
// package type
48
func GetPackageByType(
49
        sess *session.Session,
50
        packageType string,
51
        mask ...string,
52
) (datatypes.Product_Package, error) {
×
53

×
54
        objectMask := "id,name,description,isActive,type[keyName]"
×
55
        if len(mask) > 0 {
×
56
                objectMask = mask[0]
×
57
        }
×
58

59
        service := services.GetProductPackageService(sess)
×
60

×
61
        // Get package id
×
62
        packages, err := service.
×
63
                Mask(objectMask).
×
64
                Filter(
×
65
                        filter.Build(
×
66
                                filter.Path("type.keyName").Eq(packageType),
×
67
                        ),
×
68
                ).
×
69
                Limit(1).
×
70
                GetAllObjects()
×
71
        if err != nil {
×
72
                return datatypes.Product_Package{}, err
×
73
        }
×
74

75
        packages = rejectOutletPackages(packages)
×
76

×
77
        if len(packages) == 0 {
×
78
                return datatypes.Product_Package{}, fmt.Errorf("No product packages found for %s", packageType)
×
79
        }
×
80

81
        return packages[0], nil
×
82
}
83

84
// rejectOutletPackages removes packages whose description or name contains the
85
// string "OUTLET".
86
func rejectOutletPackages(packages []datatypes.Product_Package) []datatypes.Product_Package {
×
87
        selected := []datatypes.Product_Package{}
×
88

×
89
        for _, pkg := range packages {
×
90
                if (pkg.Name == nil || !strings.Contains(*pkg.Name, "OUTLET")) &&
×
91
                        (pkg.Description == nil || !strings.Contains(*pkg.Description, "OUTLET")) {
×
92

×
93
                        selected = append(selected, pkg)
×
94
                }
×
95
        }
96

97
        return selected
×
98
}
99

100
// GetPackageProducts Get a list of product items for a specific product
101
// package ID
102
func GetPackageProducts(
103
        sess *session.Session,
104
        packageId int,
105
        mask ...string,
106
) ([]datatypes.Product_Item, error) {
×
107

×
108
        objectMask := "id,capacity,description,units,keyName,prices[id,locationGroupId,categories[id,name,categoryCode]]"
×
109
        if len(mask) > 0 {
×
110
                objectMask = mask[0]
×
111
        }
×
112

113
        service := services.GetProductPackageService(sess)
×
114

×
115
        // Get product items for package id
×
116
        return service.
×
117
                Id(packageId).
×
118
                Mask(objectMask).
×
119
                GetItems()
×
120
}
121

122
// SelectProductPricesByCategory Get a list of Product_Item_Prices that
123
// match a specific set of price category code / product item
124
// capacity combinations.
125
// These combinations are passed as a map of strings (category code) mapped
126
// to float64 (capacity)
127
// For example, these are the options to specify an upgrade to 8 cpus and 32
128
// GB or memory:
129
// {"guest_core": 8.0, "ram": 32.0}
130
// public[0] checks type of network.
131
// public[1] checks type of cores.
132
func SelectProductPricesByCategory(
133
        productItems []datatypes.Product_Item,
134
        options map[string]float64,
135
        public ...bool,
136
) []datatypes.Product_Item_Price {
×
137

×
138
        forPublicNetwork := true
×
139
        if len(public) > 0 {
×
140
                forPublicNetwork = public[0]
×
141
        }
×
142

143
        // Check type of cores
144
        forPublicCores := true
×
145
        if len(public) > 1 {
×
146
                forPublicCores = public[1]
×
147
        }
×
148

149
        forDedicatedHost := false
×
150
        if len(public) > 2 {
×
151
                forDedicatedHost = public[2]
×
152
        }
×
153

154
        // Filter product items based on sets of category codes and capacity numbers
155
        prices := []datatypes.Product_Item_Price{}
×
156
        priceCheck := map[string]bool{}
×
157
        for _, productItem := range productItems {
×
158
                isPrivate := strings.Contains(sl.Get(productItem.KeyName, "").(string), "PRIVATE")
×
159
                isPublic := strings.Contains(sl.Get(productItem.Description, "Public").(string), "Public")
×
160
                isDedicated := strings.Contains(sl.Get(productItem.KeyName, "").(string), "DEDICATED")
×
161
                //Logic taken from softlayer-python @ https://bit.ly/2DV2bBi
×
162
                for _, p := range productItem.Prices {
×
163
                        if p.LocationGroupId != nil {
×
164
                                continue
×
165
                        }
166

167
                        for _, category := range p.Categories {
×
168
                                for categoryCode, capacity := range options {
×
169
                                        if _, ok := priceCheck[categoryCode]; ok {
×
170
                                                continue
×
171
                                        }
172

173
                                        if productItem.Capacity == nil {
×
174
                                                continue
×
175
                                        }
176

177
                                        if *category.CategoryCode != categoryCode {
×
178
                                                continue
×
179
                                        }
180

181
                                        if *productItem.Capacity != datatypes.Float64(capacity) {
×
182
                                                continue
×
183
                                        }
184

185
                                        // Logic taken from softlayer-python @ http://bit.ly/2bN9Gbu
186
                                        switch categoryCode {
×
187
                                        case CPUCategoryCode:
×
188
                                                if forPublicCores == isPrivate || forDedicatedHost != isDedicated {
×
189
                                                        continue
×
190
                                                }
191
                                        case NICSpeedCategoryCode:
×
192
                                                if forPublicNetwork != isPublic || forDedicatedHost != isDedicated {
×
193
                                                        continue
×
194
                                                }
195
                                        case MemoryCategoryCode:
×
196
                                                if forDedicatedHost != isDedicated {
×
197
                                                        continue
×
198
                                                }
199
                                        }
200

201
                                        if strings.HasPrefix(categoryCode, "guest_disk") {
×
202
                                                categories := p.Categories
×
203
                                                var deviceCategories []datatypes.Product_Item_Category
×
204
                                                for _, reqCategory := range categories {
×
205
                                                        if *reqCategory.CategoryCode == categoryCode {
×
206
                                                                deviceCategories = append(deviceCategories, reqCategory)
×
207
                                                        }
×
208
                                                }
209
                                                p.Categories = deviceCategories
×
210
                                        }
211

212
                                        prices = append(prices, p)
×
213
                                        priceCheck[categoryCode] = true
×
214
                                }
215
                        }
216
                }
217
        }
218

219
        return prices
×
220
}
221

222
// GetPresetByKeyName Get the Product_Package_Preset which matches the specified
223
// preset key name
224
func GetPresetByKeyName(
225
        sess *session.Session,
226
        pkgID int,
227
        presetKeyName string,
228
        mask ...string,
229
) (datatypes.Product_Package_Preset, error) {
×
230

×
231
        objectMask := "id, name, keyName, description"
×
232
        if len(mask) > 0 {
×
233
                objectMask = mask[0]
×
234
        }
×
235

236
        service := services.GetProductPackageService(sess)
×
237

×
238
        // Get preset id
×
239
        preset, err := service.
×
240
                Id(pkgID).
×
241
                Mask(objectMask).
×
242
                Filter(
×
243
                        filter.Build(
×
244
                                filter.Path("activePresets.keyName").Eq(presetKeyName),
×
245
                        ),
×
246
                ).
×
247
                Limit(1).
×
248
                GetActivePresets()
×
249
        if err != nil {
×
250
                return datatypes.Product_Package_Preset{}, err
×
251
        }
×
252

253
        if len(preset) == 0 {
×
254
                return datatypes.Product_Package_Preset{}, fmt.Errorf("No product preset found for %s", presetKeyName)
×
255
        }
×
256

257
        return preset[0], nil
×
258
}
259

260
// GetPackageByKeyName Get the Product_Package which matches the specified
261
// package key name
262
func GetPackageByKeyName(
263
        sess *session.Session,
264
        packageKeyName string,
265
        mask ...string,
266
) (datatypes.Product_Package, error) {
×
267

×
268
        objectMask := "id,name,description,isActive,keyName"
×
269
        if len(mask) > 0 {
×
270
                objectMask = mask[0]
×
271
        }
×
272

273
        service := services.GetProductPackageService(sess)
×
274

×
275
        // Get package id
×
276
        packages, err := service.
×
277
                Mask(objectMask).
×
278
                Filter(
×
279
                        filter.Build(
×
280
                                filter.Path("keyName").Eq(packageKeyName),
×
281
                        ),
×
282
                ).
×
283
                Limit(1).
×
284
                GetAllObjects()
×
285
        if err != nil {
×
286
                return datatypes.Product_Package{}, err
×
287
        }
×
288

289
        packages = rejectOutletPackages(packages)
×
290

×
291
        if len(packages) == 0 {
×
292
                return datatypes.Product_Package{}, fmt.Errorf("No product packages found for %s", packageKeyName)
×
293
        }
×
294

295
        return packages[0], nil
×
296
}
297

298
// GetPriceIDByPackageIdandLocationGroups return the priceId of the item according to the location group ids of the datacenter you specify
299
// addon is the description of the item for which you want to get its priceId
300
func GetPriceIDByPackageIdandLocationGroups(sess *session.Session, locationGroupIds []int, packageid int, addon string) (int, error) {
×
301
        productpackageservice := services.GetProductPackageService(sess)
×
302
        productpackageservicefilter := strings.Replace(`{"items":{"description":{"operation":"appliance"}}}`, "appliance", addon, -1)
×
303
        productpackageservicemask := "description,prices.locationGroupId,prices.id"
×
304
        resp, err := productpackageservice.Mask(productpackageservicemask).Filter(productpackageservicefilter).Id(packageid).GetItems()
×
305
        if err != nil {
×
306
                return 0, err
×
307
        }
×
308
        m := make(map[int]int)
×
309
        if len(locationGroupIds) == 0 {
×
310
                locationGroupIds = append(locationGroupIds, 1)
×
311
        }
×
312
        for _, item := range locationGroupIds {
×
313
                for _, items := range resp {
×
314
                        for _, temp := range items.Prices {
×
315
                                if temp.LocationGroupId == nil {
×
316
                                        m[item] = *temp.Id
×
317
                                } else if item == *temp.LocationGroupId {
×
318
                                        m[*temp.LocationGroupId] = *temp.Id
×
319
                                }
×
320
                        }
321
                }
322
                if val, ok := m[item]; ok {
×
323
                        return val, nil
×
324
                }
×
325
        }
326
        return 0, nil
×
327
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc