-
Notifications
You must be signed in to change notification settings - Fork 18
/
ApiVIdeo03-ObjectFilters.md.html
469 lines (383 loc) · 10.4 KB
/
ApiVIdeo03-ObjectFilters.md.html
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE html>
<html>
<head>
<title>The SoftLayer API</title>
<meta charset="utf-8">
<style>
body {
font-family: monospace;
font-size: 2em;
}
h1 {
font-family: monospace;
color: #F2B632;
}
h2 {
font-family: monospace;
color: #8834A5;
}
h3 {
color: #A57712;
font-weight: normal;
}
h4 {
color: #A57712;
font-weight: normal;
}
.remark-code, .remark-inline-code {
font-family: 'Courier New';
font-size: 16px;
}
/*.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }*/
.remark-slide-content {
background-color: #252839;
color: #E1F4EC;
font-size: 25px;
padding: 0em 1em 1em 1em;
}
.minicode {
font-size: 10px;
}
img {background-color: #E1F4EC ;}
.columnA {float:left; width: 49%;}
.columnB {float:right; width: 49%; }
.columnD {float:right; width: 40%; }
.columnC {float:right; width: 60%; }
a {color:#A57712; }
hr {border-color: #8834A5; background-color:#8834A5; border-width: 2px; border-style: solid; }
li {margin-bottom: .5em; margin-top: .5em;}
blockquote {
color: #F2B632;
font-family: Palatino;
font-weight: normal;
font-size: 35px;
}
.MathJax {
font-size: 8em !important;
color: #D69ECA !important;
}
th {
color:#A57712 ;
}
.brightGold {
color: #F2B632
}
.lightPurple {
color: #8834A5 ;
}
.bigO {
height: 475px;
}
.darkGold {
color: #A57712 ;
}
.warning {
color: #FC2003;
}
</style>
<!-- <link rel="stylesheet" href="/remark/default.min.css"> -->
</head>
<body>
<!--#A57712 Dark Gold-->
<!--#E1F4EC White Gold -->
<!--#F2B632 Bright Gold -->
<!--#27182C Dark Purple -->
<!--#8834A5 Light Purple -->
<!--#D69ECA White Purple -->
<textarea id="source">
class: center, middle
# .center[SoftLayer API]
### .center[Object Filters]
Christopher Gallo, Senior API Developer
---
# Object Filters
>A JSON Object used to restrict and order the API results
```json
{
"property1": {
"subProperty": {
"operation": "value_to_check"
}
}
}
```
.warning[__*WARNING*__] Bad object filters are often ignored silently, make sure your filter is actually filtering before assuming it is.
.darkGold[__*REQUIREMENT*__] The method should have a ObjectFilter listed in the .lightPurple[Required Headers] section
---
# Concepts
### Datatype
While objectMasks start at the datatype returned by a method, objectFilters start at the Service of that method.
For example if you were making an API call to .darkGold[SoftLayer_Account::getVirtualGuests()]
- ObjectMask: SoftLayer_Virtual_Guest datatype (.darkGold[mask[id,hostname,domain]])
- ObjectFilter: SoftLayer_Account datatype (.darkGold[{"virtualGuests":{"hostname":{"operation":"testHostname"}}}])
---
# Concepts
### Unfilterable Properties
- Count properties can't be filtered by
- Relational property that returns a "basic" value (like string, int).
- Some "computed" properties (that don't exist as a literal column in a database table) Such as "fullyQualifiedDomainName" for Hardware and Virtual Guests.
Any property in your objectFilter should also be in your objectMask
---
# Concepts
ObjectFilters contol which properties are added to the SQL `WHERE` and `ORDER BY` clauses
```bash
$ slcli --format=json call-api SoftLayer_Account getVirtualGuests --id=113821290 \
--mask="mask[id,hostname]" \
--json-filter='{"virtualGuests":{"hostname":{"operation":"testHostname"}}}'
```
```SQL
SELECT virtual_guest.id, virtual_guest.hostname FROM virtual_guest
WHERE virtual_guest.account_id = active_user.account_id
AND virtual_guest.hostname = 'testHostname'
```
---
# Step 1: Find The Datastructure
The starting point for your filter will usually be the Service for the Method you are calling.
If you are calling something like "SoftLayer_Service::getProperty", your objectFilter will start with `property`
- SoftLayer_Account::getVirtualGuests -> {"virtualGuests"}
+ https://sldn.softlayer.com/reference/datatypes/SoftLayer_Account/#virtualGuests
- SoftLayer_Hardware_Server::getActiveTickets -> {"activeTickets"}
+ https://sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware_Server/#activeTickets
- SoftLayer_User_Customer::getHardware -> {"hardware"}
+ https://sldn.softlayer.com/reference/datatypes/SoftLayer_User_Customer/#hardware
---
# Step 2: Find Properties
Keep adding child objects to your filter until you reach a local property you want to filter by.
For Example, if we want to find all the Hardware on our account that is connected to a specific backendRouter, we would build our filter this way.
```json
{
"hardware": { // sldn.softlayer.com/reference/datatypes/SoftLayer_Account/#hardware
"backendRouters": { // sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware/#backendRouters
"hostname": { // sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware/#hostname
"operation": "bcr03a.dal10 "
}
}
}
}
```
---
# Step 3: Create the filter
Bookmark: https://sldn.softlayer.com/article/object-filters/
.columnA[<img src="./Pics/objectFilters.png" height="400px">]
.columnB[
```json
{
"property1": {
"operation": "^= someString"
},
"property2": {
"subProperty": {
"childProperty": {
"operation": "!= somethingElse"
}
}
}
}
```
Property1 start with 'someString' AND childProperty does not equal 'somethingElse'
]
---
# Complex Filters
.columnA[
### IN
```json
{"virtualGuests": {
"hostname": {
"operation": "in",
"options": [{
"name":"data",
"value":["hostname1", "hostname2", "hostname3"]}]}}}
// does not support operators, faster than 'OR'
```
### OR
```json
{"virtualGuests": {
"hostname": {
"operation": "or",
"options": [{
"name":"data",
"value":["^= host", "$= name"]}]}}}
// supports operators like ^= and $=
```
]
.columnB[
### NOT IN
```json
{"virtualGuests": {
"hostname": {
"operation": "not in",
"options": [{
"name":"data",
"value":["hostname1", "hostname2", "hostname3"]}]}}}
// opposite of IN
```
### AND
```json
{"virtualGuests": {
"hostname": {
"operation": "and",
"options": [{
"name":"data",
"value":["^= host", "$= name"]}]}}}
// hostname starts with 'host' and ends with 'name'
```
]
---
# Date Operations
>Date format is .lightPurple[MM/DD/YY HH24:MI:SS] (Oracle Notation). .lightPurple[sysdate] is also an option
.columnA[
```json
{
"virtualGuests": {
"provisionDate": {
"operation": { "> sysdate - 30"}
}
}
}
// Greater than NOW/sysdate minus 30 days
```
```json
{ "virtualGuests": {
"provisionDate": {
"operation": "betweenDate",
"options": [
{ "name": "date",
"value": ["02/01/06"]}, // Fab 01, 2006
{ "name": "date",
"value": ["02/29/06"]} // Feb 29, 2006
]}
}
}
```
]
.columnB[
```json
{
"virtualGuests": {
"provisionDate": {
"operation": "greaterThanDate",
"options": [
{
"name": "date",
"value": ["02/01/06"] // Feb 1, 2006
}
]
}
}
}
```
]
---
# All Date Operations
- lessThanDate
- greaterThanDate
- betweenDate : Required 2 options, start and end
- isDate
---
# Sorting
.columnA[
- Not all queries have a default order, so subsequent queries might have different ordering
- Dont have to sort on the client isde
- Can not filter and sort by the same property
]
.columnB[
```json
{
"virtualGuests": {
"id": {
"operation": "orderBy",
"options": [
{
"name": "sort",
"value": ["DESC"] // ASC only other option
}
]
}
}
}
```
]
---
# Sort Order
It is possible to sort by multiple properties, you just need to specify their priority with the .lightPurple[sortOrder] option.
```json
{
"virtualGuests": {
"provisionDate": {
"operation": "orderBy",
"options": [
{"name": "sort", "value": ["DESC"]},
* {"name": "sortOrder", "value": [0]}
]
},
"maxMemory": {
"operation": "orderBy",
"options": [
{"name": "sort", "value": ["ASC"]},
* {"name": "sortOrder","value": [1]}
]
}
}
}
```
---
# Result Limits
A result limit allows you set to the maximum number of results from an API request, along with the offset, allowing for pagination of large queries.
.warning[__*WARNING*__] ALWAYS use a orderBy filter, usually on an id column, when using result limits. Not all queries have an order imposed on them internally and the database might choose a different index between API calls, giving you results out of order.
Just add `resultLimit=<OFFSET>,<LIMIT>` as a URL parameter for REST calls
```bash
curl -u $SL_USER:$SL_APIKEY \
'https://api.softlayer.com/rest/v3.1/SoftLayer_Account/getHardware.json?resultLimit=0%2C2'
```
---
# Result Limit XML/SOAP
.columnA[
```xml
<xml>
<struct>
<member>
<name>resultLimit</name>
<value><struct>
<member>
<name>limit</name>
<value><int>10</int></value>
</member>
<member>
<name>offset</name>
<value><int>5</int></value>
</member>
</struct></value>
</member>
</struct>
</xml>
```
]
.columnB[
```xml
<xml>
<resultLimit xsi:type="slt:resultLimit" xmlns:slt="http://api.service.softlayer.com/soap/v3/SLTypes/">
<limit xsi:type="xsd:int">10</limit>
<offset xsi:type="xsd:int">5</offset>
</resultLimit>
</xml>
```
]
---
# Conclusion
- https://sldn.softlayer.com/article/object-filters/ has all this written down for reference
- Filters that are incorrect will usually be silently ignored
- Require orderBy filters when using a resultLimit
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<!-- <script src="/remark/jquery-2.2.4.min.js"></script> -->
<!-- <script src="/remark/highlight.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript"></script>
<script type="text/javascript">
var slideshow = remark.create({
highlightStyle: 'default',
ratio: '16:9',
highlightLines: true
});
</script>
</body>
</html>