• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H03-Mar-2021-10693

config/H03-Mar-2021-14695

datatypes/H03-Mar-2021-34,05912,516

examples/H03-Mar-2021-539350

filter/H03-Mar-2021-392259

helpers/H03-Mar-2021-816502

services/H03-Mar-2021-58,30138,704

session/H03-Mar-2021-1,4891,055

sl/H03-Mar-2021-445229

tests/H03-Mar-2021-12173

tools/H03-Mar-2021-803541

.gitignoreH A D03-Mar-2021357 3527

.travis.ymlH A D03-Mar-202172 87

LICENSEH A D03-Mar-202111.1 KiB203169

MakefileH A D03-Mar-20212 KiB8262

README.mdH A D03-Mar-202110.4 KiB360265

go.modH A D03-Mar-2021218 107

go.sumH A D03-Mar-20212.5 KiB2625

README.md

1# softlayer-go
2
3[![Build Status](https://travis-ci.org/softlayer/softlayer-go.svg?branch=master)](https://travis-ci.org/softlayer/softlayer-go)
4[![GoDoc](https://godoc.org/github.com/softlayer/softlayer-go?status.svg)](https://godoc.org/github.com/softlayer/softlayer-go)
5[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)
6![Tests](https://github.com/softlayer/softlayer-go/workflows/Tests/badge.svg)
7[![Coverage Status](https://coveralls.io/repos/github/softlayer/softlayer-go/badge.svg?branch=master)](https://coveralls.io/github/softlayer/softlayer-go?branch=master)
8
9The Official and Complete SoftLayer API Client for Golang (the Go programming language).
10
11## Introduction
12
13This library contains a complete implementation of the SoftLayer API for client application development in the Go programming language. Code for each API data type and service method is pre-generated, using the SoftLayer API metadata endpoint as input, thus ensuring 100% coverage of the API right out of the gate.
14
15It was designed to feel as natural as possible for programmers familiar with other popular SoftLayer SDKs, and attempts to minimize unnecessary boilerplate and type assertions where possible.
16
17## Usage
18
19### Basic example:
20
21Three easy steps:
22
23```go
24// 1. Create a session
25sess := session.New(username, apikey)
26
27// 2. Get a service
28accountService := services.GetAccountService(sess)
29
30// 3. Invoke a method:
31account, err := accountService.GetObject()
32```
33
34[More examples](https://github.com/softlayer/softlayer-go/tree/master/examples)
35
36### Sessions
37
38In addition to the example above, sessions can also be created using values
39set in the environment, or from the local configuration file (i.e. ~/.softlayer):
40
41```go
42sess := session.New()
43```
44
45In this usage, the username, API key, and endpoint are read from specific environment
46variables, then the local configuration file (i.e. ~/.softlayer).  First match ends
47the search:
48
49* _Username_
50	1. environment variable `SL_USERNAME`
51	1. environment variable `SOFTLAYER_USERNAME`
52	1. local config `username`.
53* _API Key_
54	1. environment variable `SL_API_KEY`
55	1. environment variable `SOFTLAYER_API_KEY`
56	1. local config `api_key`.
57* _Endpoint_
58	1. environment variable `SL_ENDPOINT_URL`
59	1. environment variable `SOFTLAYER_ENDPOINT_URL`
60	1. local config `endpoint_url`.
61* _Timeout_
62	1. environment variable `SL_TIMEOUT`
63	1. environment variable `SOFTLAYER_TIMEOUT`
64	1. local config `timeout`.
65
66*Note:* Endpoint defaults to `https://api.softlayer.com/rest/v3` if not configured through any of the above methods. Timeout defaults to 120 seconds.
67
68Example of the **~/.softlayer** local configuration file:
69```
70[softlayer]
71username = <your username>
72api_key = <your api key>
73endpoint_url = <optional>
74timeout = <optional>
75```
76
77### Instance methods
78
79To call a method on a specific instance, set the instance ID before making the call:
80
81```go
82service := services.GetUserCustomerService(sess)
83
84service.Id(6786566).GetObject()
85```
86
87### Passing Parameters
88
89All non-slice method parameters are passed as pointers. This is to allow for optional values to be omitted (by passing `nil`)
90
91```go
92guestId := 123456
93userCustomerService.RemoveVirtualGuestAccess(&guestId)
94```
95
96For convenience, a set of helper functions exists that allocate storage for a literal and return a pointer. The above can be refactored to:
97
98```go
99userCustomerService.RemoveVirtualGuestAccess(sl.Int(123456))
100```
101
102### Using datatypes
103
104A complete library of SoftLayer API data type structs exists in the `datatypes` package. Like method parameters, all non-slice members are declared as pointers. This has the advantage of permitting updates without re-sending the complete data structure (since `nil` values are omitted from the resulting JSON). Use the same set of helper functions to assist in populating individual members.
105
106```go
107package main
108
109import (
110	"fmt"
111	"log"
112
113	"github.com/softlayer/softlayer-go/datatypes"
114	"github.com/softlayer/softlayer-go/services"
115	"github.com/softlayer/softlayer-go/session"
116	"github.com/softlayer/softlayer-go/sl"
117)
118
119func main() {
120	sess := session.New() // See above for details about creating a new session
121
122	// Get the Virtual_Guest service
123	service := services.GetVirtualGuestService(sess)
124
125	// Create a Virtual_Guest struct as a template
126	vGuestTemplate := datatypes.Virtual_Guest{
127		// Set Creation values - use helpers from the sl package to set pointer values.
128		// Unset (nil) values are not sent
129		Hostname:                     sl.String("sample"),
130		Domain:                       sl.String("example.com"),
131		MaxMemory:                    sl.Int(4096),
132		StartCpus:                    sl.Int(1),
133		Datacenter:                   &datatypes.Location{Name: sl.String("wdc01")},
134		OperatingSystemReferenceCode: sl.String("UBUNTU_LATEST"),
135		LocalDiskFlag:                sl.Bool(true),
136	}
137
138	// Tell the API to create the virtual guest
139	newGuest, err := service.CreateObject(&vGuestTemplate)
140	// optional error checking...
141	if err != nil {
142		log.Fatal(err)
143	}
144
145	// Print the ID of the new guest.  Don't forget to dereference
146	fmt.Printf("New guest %d created", *newGuest.Id)
147}
148```
149
150### Object Masks, Filters, Result Limits
151
152Object masks, object filters, and pagination (limit and offset) can be set
153by calling the `Mask()`, `Filter()`, `Limit()` and `Offset()` service methods
154prior to invoking an API method.
155
156For example, to set an object mask and filter that will be applied to
157the results of the Account.GetObject() method:
158
159```go
160accountService := services.GetAccountService(sess)
161
162accountService.
163	Mask("id;hostname").
164	Filter(`{"virtualGuests":{"domain":{"operation":"example.com"}}}`).
165	GetObject()
166```
167
168The mask and filter are applied to the current request only, and reset after the
169method returns. To preserve these options for future requests, save the return value:
170
171```go
172accountServiceWithMaskAndFilter = accountService.Mask("id;hostname").
173	Filter(`{"virtualGuests":{"domain":{"operation":"example.com"}}}`)
174```
175
176Result limits are specified as separate `Limit` and `Offset` values:
177
178```go
179accountService.
180	Offset(100).      // start at the 100th element in the list
181	Limit(25).        // limit to 25 results
182	GetVirtualGuests()
183```
184
185#### Filter Builder
186
187There is also a **filter builder** you can use to create a _Filter_ instead of writing out the raw string:
188
189```go
190// requires importing the filter package
191accountServiceWithMaskAndFilter = accountService.
192    Mask("id;hostname").
193    Filter(filter.Path("virtualGuests.domain").Eq("example.com").Build())
194```
195
196You can also create a filter incrementally:
197
198```go
199// Create initial filters
200filters := filter.New(
201    filter.Path("virtualGuests.hostname").StartsWith("KM078"),
202    filter.Path("virtualGuests.id").NotEq(12345),
203)
204
205// ....
206// Later, append another filter
207filters = append(filters, filter.Path("virtualGuests.domain").Eq("example.com"))
208
209accountServiceWithMaskAndFilter = accountService.
210    Mask("id;hostname").
211    Filter(filters.Build())
212```
213
214Or you can build all those filters in one step:
215
216```go
217// Create initial filters
218filters := filter.Build(
219    filter.Path("virtualGuests.hostname").StartsWith("KM078"),
220    filter.Path("virtualGuests.id").NotEq(12345),
221    filter.Path("virtualGuests.domain").Eq("example.com"),
222)
223
224accountServiceWithMaskAndFilter = accountService.
225    Mask("id;hostname").Filter(filters)
226```
227
228See _filter/filters.go_ for the full range of operations supported.
229The file at _examples/filters.go_ will show additional examples.
230Also, [this is a good article](https://sldn.softlayer.com/article/object-filters) that describes SoftLayer filters at length.
231
232### Handling Errors
233
234For any error that occurs within one of the SoftLayer API services, a custom
235error type is returned, with individual fields that can be parsed separately.
236
237```go
238_, err := service.Id(0).      // invalid object ID
239	GetObject()
240
241if err != nil {
242	// Note: type assertion is only necessary for inspecting individual fields
243	apiErr := err.(sl.Error)
244	fmt.Printf("API Error:")
245	fmt.Printf("HTTP Status Code: %d\n", apiErr.StatusCode)
246	fmt.Printf("API Code: %s\n", apiErr.Exception)
247	fmt.Printf("API Error: %s\n", apiErr.Message)
248}
249```
250
251Note that `sl.Error` implements the standard `error` interface, so it can
252be handled like any other error, if the above granularity is not needed:
253
254```go
255_, err := service.GetObject()
256if err != nil {
257	fmt.Println("Error during processing: ", err)
258}
259```
260
261### Session Options
262
263To set a different endpoint (e.g., the backend network endpoint):
264
265```go
266session.Endpoint = "https://api.service.softlayer.com/rest/v3"
267```
268
269To enable debug output:
270
271```go
272session.Debug = true
273```
274
275By default, the debug output is sent to standard output. You can customize this by setting up your own logger:
276
277```go
278import "github.com/softlayer/softlayer-go/session"
279
280session.Logger = log.New(os.Stderr, "[CUSTOMIZED] ", log.LstdFlags)
281```
282
283You can also tell the session to retry the api requests if there is a timeout error:
284
285```go
286// Specify how many times to retry the request, the request timeout, and how much time to wait
287// between retries.
288services.GetVirtualGuestService(
289	sess.SetTimeout(900).SetRetries(2).SetRetryWait(3)
290).GetObject(...)
291```
292
293### Password-based authentication
294
295Password-based authentication (via requesting a token from the API) is
296only supported when talking to the API using the XML-RPC transport protocol.
297
298To use the XML-RPC protocol, simply specify an XML-RPC endpoint url:
299
300```go
301func main() {
302    // Create a session specifying an XML-RPC endpoint url.
303    sess := &session.Session{
304        Endpoint: "https://api.softlayer.com/xmlrpc/v3",
305    }
306
307    // Get a token from the api using your username and password
308    userService := services.GetUserCustomerService(sess)
309    token, err := userService.GetPortalLoginToken(username, password, nil, nil)
310    if err != nil {
311        log.Fatal(err)
312    }
313
314    // Add user id and token to the session.
315    sess.UserId = *token.UserId
316    sess.AuthToken = *token.Hash
317
318    // You have a complete authenticated session now.
319    // Call any api from this point on as normal...
320    keys, err := userService.Id(sess.UserId).GetApiAuthenticationKeys()
321    if err != nil {
322        log.Fatal(err)
323    }
324
325    log.Println("API Key:", *keys[0].AuthenticationKey)
326}
327```
328
329## Development
330
331### Setup
332
333To get _softlayer-go_:
334
335```
336go get github.com/softlayer/softlayer-go/...
337```
338
339### Build
340
341```
342make
343```
344
345### Test
346
347```
348make test
349```
350
351### Updating dependencies
352
353```
354make update_deps
355```
356
357## Copyright
358
359This software is Copyright (c) 2016 IBM Corp. See the bundled LICENSE file for more information.
360