|
8 | 8 | "math/rand"
|
9 | 9 | "os"
|
10 | 10 | "strconv"
|
11 |
| - |
12 |
| - "github.com/linode/linodego" |
13 | 11 | )
|
14 | 12 |
|
15 | 13 | var spendMoney = false
|
@@ -69,255 +67,6 @@ func ExampleClient_ListUsers() {
|
69 | 67 | // User exists: true
|
70 | 68 | }
|
71 | 69 |
|
72 |
| -func Example() { |
73 |
| - // Example readers, Ignore this bit of setup code needed to record test fixtures |
74 |
| - linodeClient, teardown := createTestClient(nil, "fixtures/Example") |
75 |
| - defer teardown() |
76 |
| - |
77 |
| - var linode *linodego.Instance |
78 |
| - linode, err := linodeClient.GetInstance(context.Background(), 1231) |
79 |
| - fmt.Println("## Instance request with Invalid ID") |
80 |
| - fmt.Println("### Linode:", linode) |
81 |
| - fmt.Println("### Error:", err) |
82 |
| - |
83 |
| - if spendMoney { |
84 |
| - linode, err = linodeClient.CreateInstance(context.Background(), linodego.InstanceCreateOptions{Region: "us-southeast", Type: "g6-nanode-1", FirewallID: GetFirewallID()}) |
85 |
| - if err != nil { |
86 |
| - log.Fatalln("* While creating instance: ", err) |
87 |
| - } |
88 |
| - linode, err = linodeClient.UpdateInstance(context.Background(), linode.ID, linodego.InstanceUpdateOptions{Label: linode.Label + "-renamed"}) |
89 |
| - if err != nil { |
90 |
| - log.Fatalln("* While renaming instance: ", err) |
91 |
| - } |
92 |
| - fmt.Println("## Created Instance") |
93 |
| - event, errEvent := linodeClient.WaitForEventFinished(context.Background(), linode.ID, linodego.EntityLinode, linodego.ActionLinodeCreate, *linode.Created, 240) |
94 |
| - if errEvent != nil { |
95 |
| - log.Fatalf("* Failed to wait for Linode %d to finish creation: %s", linode.ID, errEvent) |
96 |
| - } |
97 |
| - if errEvent = linodeClient.MarkEventRead(context.Background(), event); errEvent != nil { |
98 |
| - log.Fatalln("* Failed to mark Linode create event seen", errEvent) |
99 |
| - } |
100 |
| - |
101 |
| - diskSwap, errSwap := linodeClient.CreateInstanceDisk(context.Background(), linode.ID, linodego.InstanceDiskCreateOptions{Size: 50, Filesystem: "swap", Label: "linodego_swap"}) |
102 |
| - if errSwap != nil { |
103 |
| - log.Fatalln("* While creating swap disk:", errSwap) |
104 |
| - } |
105 |
| - eventSwap, errSwapEvent := linodeClient.WaitForEventFinished(context.Background(), linode.ID, linodego.EntityLinode, linodego.ActionDiskCreate, *diskSwap.Created, 240) |
106 |
| - // @TODO it is not sufficient that a disk was created. Which disk was it? |
107 |
| - // Sounds like we'll need a WaitForEntityStatus function. |
108 |
| - if errSwapEvent != nil { |
109 |
| - log.Fatalf("* Failed to wait for swap disk %d to finish creation: %s", diskSwap.ID, errSwapEvent) |
110 |
| - } |
111 |
| - if errSwapEvent = linodeClient.MarkEventRead(context.Background(), eventSwap); errSwapEvent != nil { |
112 |
| - log.Fatalln("* Failed to mark swap disk create event seen", errSwapEvent) |
113 |
| - } |
114 |
| - |
115 |
| - diskRaw, errRaw := linodeClient.CreateInstanceDisk(context.Background(), linode.ID, linodego.InstanceDiskCreateOptions{Size: 50, Filesystem: "raw", Label: "linodego_raw"}) |
116 |
| - if errRaw != nil { |
117 |
| - log.Fatalln("* While creating raw disk:", errRaw) |
118 |
| - } |
119 |
| - eventRaw, errRawEvent := linodeClient.WaitForEventFinished(context.Background(), linode.ID, linodego.EntityLinode, linodego.ActionDiskCreate, *diskRaw.Created, 240) |
120 |
| - // @TODO it is not sufficient that a disk was created. Which disk was it? |
121 |
| - // Sounds like we'll need a WaitForEntityStatus function. |
122 |
| - if errRawEvent != nil { |
123 |
| - log.Fatalf("* Failed to wait for raw disk %d to finish creation: %s", diskRaw.ID, errRawEvent) |
124 |
| - } |
125 |
| - if errRawEvent = linodeClient.MarkEventRead(context.Background(), eventRaw); errRawEvent != nil { |
126 |
| - log.Fatalln("* Failed to mark raw disk create event seen", errRawEvent) |
127 |
| - } |
128 |
| - |
129 |
| - diskDebian, errDebian := linodeClient.CreateInstanceDisk( |
130 |
| - context.Background(), |
131 |
| - linode.ID, |
132 |
| - linodego.InstanceDiskCreateOptions{ |
133 |
| - Size: 1500, |
134 |
| - Filesystem: "ext4", |
135 |
| - Image: "linode/debian9", |
136 |
| - Label: "linodego_debian", |
137 |
| - RootPass: randPassword(), |
138 |
| - }, |
139 |
| - ) |
140 |
| - if errDebian != nil { |
141 |
| - log.Fatalln("* While creating Debian disk:", errDebian) |
142 |
| - } |
143 |
| - eventDebian, errDebianEvent := linodeClient.WaitForEventFinished(context.Background(), linode.ID, linodego.EntityLinode, linodego.ActionDiskCreate, *diskDebian.Created, 240) |
144 |
| - // @TODO it is not sufficient that a disk was created. Which disk was it? |
145 |
| - // Sounds like we'll need a WaitForEntityStatus function. |
146 |
| - if errDebianEvent != nil { |
147 |
| - log.Fatalf("* Failed to wait for Debian disk %d to finish creation: %s", diskDebian.ID, errDebianEvent) |
148 |
| - } |
149 |
| - if errDebianEvent = linodeClient.MarkEventRead(context.Background(), eventDebian); errDebianEvent != nil { |
150 |
| - log.Fatalln("* Failed to mark Debian disk create event seen", errDebianEvent) |
151 |
| - } |
152 |
| - fmt.Println("### Created Disks") |
153 |
| - |
154 |
| - createOpts := linodego.InstanceConfigCreateOptions{ |
155 |
| - Devices: linodego.InstanceConfigDeviceMap{ |
156 |
| - SDA: &linodego.InstanceConfigDevice{DiskID: diskDebian.ID}, |
157 |
| - SDB: &linodego.InstanceConfigDevice{DiskID: diskRaw.ID}, |
158 |
| - SDC: &linodego.InstanceConfigDevice{DiskID: diskSwap.ID}, |
159 |
| - }, |
160 |
| - Kernel: "linode/direct-disk", |
161 |
| - Label: "example config label", |
162 |
| - // RunLevel: "default", |
163 |
| - // VirtMode: "paravirt", |
164 |
| - Comments: "example config comment", |
165 |
| - // RootDevice: "/dev/sda", |
166 |
| - Helpers: &linodego.InstanceConfigHelpers{ |
167 |
| - Network: true, |
168 |
| - ModulesDep: false, |
169 |
| - }, |
170 |
| - } |
171 |
| - config, errConfig := linodeClient.CreateInstanceConfig(context.Background(), linode.ID, createOpts) |
172 |
| - if errConfig != nil { |
173 |
| - log.Fatalln("* Failed to create Config", errConfig) |
174 |
| - } |
175 |
| - fmt.Println("### Created Config:") |
176 |
| - updateOpts := linodego.InstanceConfigUpdateOptions{ |
177 |
| - Comments: "updated example config comment", |
178 |
| - } |
179 |
| - config, errConfig = linodeClient.UpdateInstanceConfig(context.Background(), linode.ID, config.ID, updateOpts) |
180 |
| - if errConfig != nil { |
181 |
| - log.Fatalln("* Failed to update Config", errConfig) |
182 |
| - } |
183 |
| - fmt.Println("### Updated Config:") |
184 |
| - |
185 |
| - errBoot := linodeClient.BootInstance(context.Background(), linode.ID, config.ID) |
186 |
| - if errBoot != nil { |
187 |
| - log.Fatalln("* Failed to boot Instance", errBoot) |
188 |
| - } |
189 |
| - fmt.Println("### Booted Instance") |
190 |
| - |
191 |
| - eventBooted, errBootEvent := linodeClient.WaitForEventFinished(context.Background(), linode.ID, linodego.EntityLinode, linodego.ActionLinodeBoot, *config.Updated, 240) |
192 |
| - if errBootEvent != nil { |
193 |
| - fmt.Println("### Boot Instance failed as expected:", errBootEvent) |
194 |
| - } else { |
195 |
| - log.Fatalln("* Expected boot Instance to fail") |
196 |
| - } |
197 |
| - |
198 |
| - if errBootEvent = linodeClient.MarkEventRead(context.Background(), eventBooted); errBootEvent != nil { |
199 |
| - log.Fatalln("* Failed to mark boot event seen", errBootEvent) |
200 |
| - } |
201 |
| - |
202 |
| - err = linodeClient.DeleteInstanceConfig(context.Background(), linode.ID, config.ID) |
203 |
| - if err != nil { |
204 |
| - log.Fatalln("* Failed to delete Config", err) |
205 |
| - } |
206 |
| - fmt.Println("### Deleted Config") |
207 |
| - |
208 |
| - err = linodeClient.DeleteInstanceDisk(context.Background(), linode.ID, diskSwap.ID) |
209 |
| - if err != nil { |
210 |
| - log.Fatalln("* Failed to delete Disk", err) |
211 |
| - } |
212 |
| - fmt.Println("### Deleted Disk") |
213 |
| - |
214 |
| - err = linodeClient.DeleteInstance(context.Background(), linode.ID) |
215 |
| - if err != nil { |
216 |
| - log.Fatalln("* Failed to delete Instance", err) |
217 |
| - } |
218 |
| - fmt.Println("### Deleted Instance") |
219 |
| - } |
220 |
| - |
221 |
| - linodes, err := linodeClient.ListInstances(context.Background(), nil) |
222 |
| - if err != nil { |
223 |
| - log.Fatal(err) |
224 |
| - } |
225 |
| - fmt.Println("## List Instances") |
226 |
| - |
227 |
| - if len(linodes) == 0 { |
228 |
| - log.Println("No Linodes to inspect.") |
229 |
| - } else { |
230 |
| - // This is redundantly used for illustrative purposes |
231 |
| - linode, err = linodeClient.GetInstance(context.Background(), linodes[0].ID) |
232 |
| - if err != nil { |
233 |
| - log.Fatal(err) |
234 |
| - } |
235 |
| - |
236 |
| - fmt.Println("## First Linode") |
237 |
| - |
238 |
| - configs, err := linodeClient.ListInstanceConfigs(context.Background(), linode.ID, nil) |
239 |
| - if err != nil { |
240 |
| - log.Fatal(err) |
241 |
| - } else if len(configs) > 0 { |
242 |
| - config, err := linodeClient.GetInstanceConfig(context.Background(), linode.ID, configs[0].ID) |
243 |
| - if err != nil { |
244 |
| - log.Fatal(err) |
245 |
| - } |
246 |
| - fmt.Println("### First Config:", config.ID > 0) |
247 |
| - } else { |
248 |
| - fmt.Println("### No Configs") |
249 |
| - } |
250 |
| - |
251 |
| - disks, err := linodeClient.ListInstanceDisks(context.Background(), linode.ID, nil) |
252 |
| - if err != nil { |
253 |
| - log.Fatal(err) |
254 |
| - } else if len(disks) > 0 { |
255 |
| - disk, err := linodeClient.GetInstanceDisk(context.Background(), linode.ID, disks[0].ID) |
256 |
| - if err != nil { |
257 |
| - log.Fatal(err) |
258 |
| - } |
259 |
| - fmt.Println("### First Disk:", disk.ID > 0) |
260 |
| - } else { |
261 |
| - fmt.Println("### No Disks") |
262 |
| - } |
263 |
| - |
264 |
| - backups, err := linodeClient.GetInstanceBackups(context.Background(), linode.ID) |
265 |
| - if err != nil { |
266 |
| - log.Fatal(err) |
267 |
| - } |
268 |
| - if len(backups.Automatic) > 0 { |
269 |
| - fmt.Println("### First Auto Backup") |
270 |
| - } else { |
271 |
| - fmt.Println("### No Auto Backups") |
272 |
| - } |
273 |
| - fmt.Println("### Snapshots") |
274 |
| - if backups.Snapshot.Current != nil { |
275 |
| - // snapshot fetched will be exactly the same as backups.Snapshot.Current |
276 |
| - // just being redundant for illustrative purposes |
277 |
| - if snapshot, err := linodeClient.GetInstanceSnapshot(context.Background(), linode.ID, backups.Snapshot.Current.ID); err == nil { |
278 |
| - fmt.Println("#### Current:", snapshot.ID > 0) |
279 |
| - } else { |
280 |
| - fmt.Println("#### No Current Snapshot:", err) |
281 |
| - } |
282 |
| - } else { |
283 |
| - fmt.Println("### No Current Snapshot") |
284 |
| - } |
285 |
| - |
286 |
| - volumes, err := linodeClient.ListInstanceVolumes(context.Background(), linode.ID, nil) |
287 |
| - if err != nil { |
288 |
| - log.Fatal(err) |
289 |
| - } else if len(volumes) > 0 { |
290 |
| - volume, err := linodeClient.GetVolume(context.Background(), volumes[0].ID) |
291 |
| - if err != nil { |
292 |
| - log.Fatal(err) |
293 |
| - } |
294 |
| - fmt.Println("### First Volume:", volume.ID > 0) |
295 |
| - } else { |
296 |
| - fmt.Println("### No Volumes") |
297 |
| - } |
298 |
| - |
299 |
| - stackscripts, err := linodeClient.ListStackscripts(context.Background(), &linodego.ListOptions{Filter: "{\"mine\":true}"}) |
300 |
| - if err != nil { |
301 |
| - log.Fatal(err) |
302 |
| - } |
303 |
| - fmt.Println("## Your Stackscripts:", len(stackscripts) > 0) |
304 |
| - } |
305 |
| - |
306 |
| - // Output: |
307 |
| - // ## Instance request with Invalid ID |
308 |
| - // ### Linode: <nil> |
309 |
| - // ### Error: [404] Not found |
310 |
| - // ## List Instances |
311 |
| - // ## First Linode |
312 |
| - // ### First Config: true |
313 |
| - // ### First Disk: true |
314 |
| - // ### First Auto Backup |
315 |
| - // ### Snapshots |
316 |
| - // ### No Current Snapshot |
317 |
| - // ### No Volumes |
318 |
| - // ## Your Stackscripts: true |
319 |
| -} |
320 |
| - |
321 | 70 | const (
|
322 | 71 | lowerBytes = "abcdefghijklmnopqrstuvwxyz"
|
323 | 72 | upperBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
0 commit comments