Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 104 lines (91 sloc) 4.2 kb
fb1335c Added interpreter and encoding lines to the example script. (Thanks, …
SoftLayer authored
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
9c84bea Initial commit.
SoftLayer authored
4 # Copyright (c) 2010, SoftLayer Technologies, Inc. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation
13 # and/or other materials provided with the distribution.
14 # * Neither SoftLayer Technologies, Inc. nor the names of its contributors may
15 # be used to endorse or promote products derived from this software without
16 # specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 # The SoftLayer API client package is here:
31 import SoftLayer.API
32
33 # Used for pretty printing output.
34 import pprint
35
36 # It's possible to define your SoftLayer API username and key diredtly in the
37 # package, but it's far easier to define them before creating your API client.
38 api_username = 'set me'
39 api_key = 'set me too'
40
41 # Usage:
42 # SoftLayer.API.Client([API Service], <object id>, [username], [API key])
43 #
44 # API Service: The name of the API service you wish to connect to.
45 # id: An optional id to initialize your API service with, if you're
46 # interacting with a specific object. If you don't need to specify
47 # an id then pass None to the client.
48 # username: Your SoftLayer API username.
49 # API key: Your SoftLayer API key,
50 client = SoftLayer.API.Client('SoftLayer_Account', None, api_username, api_key)
51
52 # Once your client object is created you can call API methods for that service
53 # directly against your client object. A call may throw an exception on error,
54 # so it's best to try your call and catch exceptions.
55 #
56 # This example calls the getObject() method in the SoftLayer_Account API
57 # service. <http://sldn.softlayer.com/wiki/index.php/SoftLayer_Account::getObject>
651044a Documentation readability fixes.
SoftLayer authored
58 # It retrieves basic account information, and is a great way to test your API
9c84bea Initial commit.
SoftLayer authored
59 # account and connectivity.
60 #
61 # If you're using Python 3.x then you'll need to change these except and
62 # print() statements accordingly.
63 try:
64 pprint.pprint(client.getObject())
65 except Exception, e:
66 print e
74ed05a Added another example.
SoftLayer authored
67
68
69 # For a more complex example we’ll retrieve a support ticket with id 123456
70 # along with the ticket’s updates, the user it’s assigned to, the servers
71 # attached to it, and the datacenter those servers are in. We’ll retrieve our
72 # extra information using a nested object mask. After we have the ticket we’ll
73 # update it with the text ‘Hello!’.
74
75 # Declare an API client to connect to the SoftLayer_Ticket API service.
f2de722 Corrected sample ticket number.
SoftLayer authored
76 client = SoftLayer.API.Client('SoftLayer_Ticket', 123456, api_username, api_key)
74ed05a Added another example.
SoftLayer authored
77
78 # Assign an object mask to our API client:
79 client.set_object_mask({
80 'updates' : {},
81 'assignedUser' : {},
82 'attachedHardware' : {
83 'datacenter' : {}
84 },
85 })
86
87 # Retrieve the ticket record.
88 ticket = None
89 try:
90 ticket = client.getObject()
91 except Exception, e:
fcf29ed Fixed print usage in the example script. (Thanks, mahmoudimus!)
SoftLayer authored
92 print "Unable to retrieve ticket record: ", e
74ed05a Added another example.
SoftLayer authored
93
94 # Now update the ticket.
95 update = {
96 'entry' : 'Hello!',
97 }
98
99 try:
100 update = client.addUpdate(update)
42f80d7 SoftLayer_Ticket::addUpdate now returns an array.
SoftLayer authored
101 print "Update ticket 123456. The new update's id is %s." % update[0]['id']
74ed05a Added another example.
SoftLayer authored
102 except Exception, e:
fcf29ed Fixed print usage in the example script. (Thanks, mahmoudimus!)
SoftLayer authored
103 print "Unable to update ticket: ", e
Something went wrong with that request. Please try again.