File: client.rst

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (296 lines) | stat: -rw-r--r-- 9,514 bytes parent folder | download
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
.. _client:


API Documentation
=================
This is the primary API client to make API calls. It deals with constructing
and executing XML-RPC calls against the SoftLayer API. Below are some links
that will help to use the SoftLayer API.


* `SoftLayer API Documentation <https://softlayer.github.io/reference/softlayerapi/>`_
* `Source on GitHub <https://github.com/softlayer/softlayer-python>`_

::

	>>> import SoftLayer
	>>> client = SoftLayer.create_client_from_env(username="username", api_key="api_key")
	>>> resp = client.call('Account', 'getObject')
	>>> resp['companyName']
	'Your Company'


Getting Started
---------------
You can pass in your username and api_key when creating a SoftLayer client
instance. However, you can also set these in the environmental variables
'SL_USERNAME' and 'SL_API_KEY'.

Creating a client instance by passing in the username/api_key:
::

    import SoftLayer
    client = SoftLayer.create_client_from_env(username='YOUR_USERNAME', api_key='YOUR_API_KEY')

Creating a client instance with environmental variables set:
::

    $ export SL_USERNAME=YOUR_USERNAME
    $ export SL_API_KEY=YOUR_API_KEY
    $ python
    >>> import SoftLayer
    >>> client = SoftLayer.create_client_from_env()

Below is an example of creating a client instance with more options. This will
create a client with the private API endpoint (only accessible from the
SoftLayer private network) and a timeout of 4 minutes.
::

    client = SoftLayer.create_client_from_env(username='YOUR_USERNAME',
                                              api_key='YOUR_API_KEY'
                                              endpoint_url=SoftLayer.API_PRIVATE_ENDPOINT,
                                              timeout=240)

Managers
--------
For day-to-day operation, most users will find the managers to be the most
convenient means for interacting with the API. Managers abstract a lot of the
complexities of using the API into classes that provide a simpler interface to
various services. These are higher-level interfaces to the SoftLayer API.
::

	from SoftLayer import VSManager, Client
	client = Client(...)
	vs = VSManager(client)
	vs.list_instances()
	[...]

**Available managers**:

.. currentmodule:: SoftLayer.managers

.. autosummary::
    :toctree: managers
    :recursive:
    :template: manager_template.rst

    AccountManager
    BandwidthManager
    BlockStorageManager
    CapacityManager
    CDNManager
    DedicatedHostManager
    DNSManager
    EventLogManager
    FileStorageManager
    FirewallManager
    HardwareManager
    ImageManager
    IPSECManager
    LicensesManager
    LoadBalancerManager
    MetadataManager
    NetworkManager
    ObjectStorageManager
    OrderingManager
    PlacementManager
    SearchManager
    SshKeyManager
    SSLManager
    TagManager
    TicketManager
    UserManager
    VSManager

    





If you need more power or functionality than the managers provide, you can
make direct API calls as well.


Making API Calls
----------------
For full control over your account and services, you can directly call the
SoftLayer API. The SoftLayer API client for python leverages SoftLayer's
XML-RPC API. It supports authentication, object masks, object filters, limits,
offsets, and retrieving objects by id. The following section assumes you have
an initialized client named 'client'.

The best way to test our setup is to call the
`getObject <https://sldn.softlayer.com/reference/services/SoftLayer_Account/getObject>`_
method on the
`SoftLayer_Account <https://sldn.softlayer.com/reference/services/SoftLayer_Account>`_
service.
::

    client.call('Account', 'getObject')

For a more complex example we'll retrieve a support ticket with id 123456 along
with the ticket's updates, the user it's assigned to, the servers attached to
it, and the datacenter those servers are in. To retrieve our extra information
using an `object mask <https://sldn.softlayer.com/article/object-masks/>`_.

Retrieve a ticket using object masks.
::

    ticket = client.call('Ticket', 'getObject',
        id=123456, mask="updates, assignedUser, attachedHardware.datacenter")


Now add an update to the ticket with `Ticket.addUpdate <https://sldn.softlayer.com/reference/services/SoftLayer_Ticket/addUpdate>`_.
This uses a parameter, which translate to positional arguments in the order
that they appear in the API docs.


::

    update = client.call('Ticket', 'addUpdate', {'entry' : 'Hello!'}, id=123456)

Let's get a listing of virtual guests using the domain example.com


::

    client.call('Account', 'getVirtualGuests',
        filter={'virtualGuests': {'domain': {'operation': 'example.com'}}})

This call gets tickets created between the beginning of March 1, 2013 and March 15, 2013.
More information on `Object Filters <https://sldn.softlayer.com/article/object-filters/>`_.

:NOTE: The `value` field for startDate and endDate is in `[]`, if you do not put the date in brackets the filter will not work.

::

    client.call('Account', 'getTickets',
        filter={
            'tickets': {
                'createDate': {
                    'operation': 'betweenDate',
                    'options': [
                        {'name': 'startDate', 'value': ['03/01/2013 0:0:0']},
                        {'name': 'endDate', 'value': ['03/15/2013 23:59:59']}
                    ]
                }
            }
        }
    )

SoftLayer's XML-RPC API also allows for pagination.
::

    from pprint import pprint

    page1 = client.call('Account', 'getVirtualGuests', limit=10, offset=0)  # Page 1
    page2 = client.call('Account', 'getVirtualGuests', limit=10, offset=10)  # Page 2

    #Automatic Pagination (v5.5.3+), default limit is 100
    result = client.call('Account', 'getVirtualGuests', iter=True, limit=10)
    pprint(result)

    # Using a python generator, default limit is 100
    results = client.iter_call('Account', 'getVirtualGuests', limit=10)
    for result in results:
        pprint(result)

:NOTE: `client.call(iter=True)` will pull all results, then return. `client.iter_call()` will return a generator, and only make API calls as you iterate over the results. 

Here's how to create a new Cloud Compute Instance using
`SoftLayer_Virtual_Guest.createObject <https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/createObject>`_.
Be warned, this call actually creates an hourly virtual server so this will
have billing implications.
::

    client.call('Virtual_Guest', 'createObject', {
            'hostname': 'myhostname',
            'domain': 'example.com',
            'startCpus': 1,
            'maxMemory': 1024,
            'hourlyBillingFlag': 'true',
            'operatingSystemReferenceCode': 'UBUNTU_LATEST',
            'localDiskFlag': 'false'
        })


Debugging
-------------
If you ever need to figure out what exact API call the client is making, you can do the following:

*NOTE* the `print_reproduceable` method produces different output for REST and XML-RPC endpoints. If you are using REST, this will produce a CURL call. IF you are using XML-RPC, it will produce some pure python code you can use outside of the SoftLayer library. 

::

    # Setup the client as usual
    client = SoftLayer.Client()
    # Create an instance of the DebugTransport, which logs API calls
    debugger = SoftLayer.DebugTransport(client.transport)
    # Set that as the default client transport
    client.transport = debugger
    # Make your API call
    client.call('Account', 'getObject')

    # Print out the reproduceable call
    for call in client.transport.get_last_calls():
        print(client.transport.print_reproduceable(call))


Dealing with KeyError Exceptions
--------------------------------

One of the pain points in dealing with the SoftLayer API can be handling issues where you expected a property to be returned, but none was.

The hostname property of a `SoftLayer_Billing_Item <https://sldn.softlayer.com/reference/datatypes/SoftLayer_Billing_Item/#hostname>`_ is a good example of this.

For example.

::
    
    # Uses default username and apikey from ~/.softlayer
    client = SoftLayer.create_client_from_env()
    # iter_call returns a python generator, and only makes another API call when the loop runs out of items.
    result = client.iter_call('Account', 'getAllBillingItems', iter=True, mask="mask[id,hostName]")
    print("Id, hostname")
    for item in result:
        # will throw a KeyError: 'hostName' exception on certain billing items that do not have a hostName
        print(f"{item['id']}, {item['hostName']}")

The Solution
^^^^^^^^^^^^

Using the python dictionary's `.get() <https://docs.python.org/3/library/stdtypes.html#dict.get>`_ is great for non-nested items.

::

    print(f"{item.get('id')}, {item.get('hostName')}")

Otherwise, this SDK provides a util function to do something similar. Each additional argument passed into `utils.lookup` will go one level deeper into the nested dictionary to find the item requested, returning `None` if a KeyError shows up.

::

    itemId = SoftLayer.utils.lookup(item, 'id')
    itemHostname = SoftLayer.utils.lookup(item, 'hostName')
    print(f"{itemId}, {itemHostname}")


API Reference
-------------

.. automodule:: SoftLayer
    :members:
    :ignore-module-all:

.. automodule:: SoftLayer.API
    :members:
    :ignore-module-all:

.. automodule:: SoftLayer.exceptions
    :members:     

.. automodule:: SoftLayer.decoration
    :members:

.. automodule:: SoftLayer.utils
    :members: