1package integration 2 3/** 4 * The tests in the examples directory demontrate use and test the library 5 * in a real-use setting 6 */ 7 8import ( 9 "context" 10 "fmt" 11 "log" 12 "strings" 13 14 "github.com/linode/linodego" 15) 16 17func ExampleClient_ListTypes_all() { 18 // Example readers, Ignore this bit of setup code needed to record test fixtures 19 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListTypes_all") 20 defer teardown() 21 22 types, err := linodeClient.ListTypes(context.Background(), nil) 23 if err != nil { 24 log.Fatal(err) 25 } 26 27 fmt.Println("ID contains class:", strings.Contains(types[0].ID, string(types[0].Class))) 28 fmt.Println("Plan has Ram:", types[0].Memory > 0) 29 30 // Output: 31 // ID contains class: true 32 // Plan has Ram: true 33} 34 35// ExampleGetType_missing demonstrates the Error type, which allows inspecting 36// the request and response. Error codes will be the HTTP status code, 37// or sub-100 for errors before the request was issued. 38func ExampleClient_GetType_missing() { 39 // Example readers, Ignore this bit of setup code needed to record test fixtures 40 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetType_missing") 41 defer teardown() 42 43 _, err := linodeClient.GetType(context.Background(), "missing-type") 44 if err != nil { 45 if v, ok := err.(*linodego.Error); ok { 46 fmt.Println("Request was:", v.Response.Request.URL) 47 fmt.Println("Response was:", v.Response.Status) 48 fmt.Println("Error was:", v) 49 } 50 } 51 52 // Output: 53 // Request was: https://api.linode.com/v4beta/linode/types/missing-type 54 // Response was: 404 NOT FOUND 55 // Error was: [404] Not found 56} 57 58// ExampleListKernels_all Demonstrates how to list all Linode Kernels. Paginated 59// responses are automatically traversed and concatenated when the ListOptions are nil 60func ExampleClient_ListKernels_all() { 61 // Example readers, Ignore this bit of setup code needed to record test fixtures 62 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_all") 63 defer teardown() 64 65 kernels, err := linodeClient.ListKernels(context.Background(), nil) 66 if err != nil { 67 log.Fatal(err) 68 } 69 70 // The Linode API default pagination size is 100. 71 fmt.Println("Fetched > 100:", len(kernels) > 100) 72 73 // Output: 74 // Fetched > 100: true 75} 76 77func ExampleClient_ListKernels_allWithOpts() { 78 // Example readers, Ignore this bit of setup code needed to record test fixtures 79 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_allWithOpts") 80 defer teardown() 81 82 filterOpt := linodego.NewListOptions(0, "") 83 kernels, err := linodeClient.ListKernels(context.Background(), filterOpt) 84 if err != nil { 85 log.Fatal(err) 86 } 87 88 // The Linode API default pagination size is 100. 89 fmt.Println("Fetched > 100:", len(kernels) > 100) 90 fmt.Println("Fetched Results/100 pages:", filterOpt.Pages > filterOpt.Results/100) 91 fmt.Println("Fetched all results:", filterOpt.Results == len(kernels)) 92 93 // Output: 94 // Fetched > 100: true 95 // Fetched Results/100 pages: true 96 // Fetched all results: true 97} 98 99func ExampleClient_ListKernels_filtered() { 100 // Example readers, Ignore this bit of setup code needed to record test fixtures 101 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_filtered") 102 defer teardown() 103 104 filterOpt := linodego.ListOptions{Filter: "{\"label\":\"Recovery - Finnix (kernel)\"}"} 105 kernels, err := linodeClient.ListKernels(context.Background(), &filterOpt) 106 if err != nil { 107 log.Fatal(err) 108 } 109 for _, kern := range kernels { 110 fmt.Println(kern.ID, kern.Label) 111 } 112 113 // Unordered output: 114 // linode/finnix Recovery - Finnix (kernel) 115 // linode/finnix-legacy Recovery - Finnix (kernel) 116} 117 118func ExampleClient_ListKernels_page1() { 119 // Example readers, Ignore this bit of setup code needed to record test fixtures 120 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListKernels_page1") 121 defer teardown() 122 123 filterOpt := linodego.NewListOptions(1, "") 124 kernels, err := linodeClient.ListKernels(context.Background(), filterOpt) 125 if err != nil { 126 log.Fatal(err) 127 } 128 // The Linode API default pagination size is 100. 129 fmt.Println("Fetched == 100:", len(kernels) == 100) 130 fmt.Println("Results > 100:", filterOpt.Results > 100) 131 fmt.Println("Pages > 1:", filterOpt.Pages > 1) 132 k := kernels[len(kernels)-1] 133 fmt.Println("Kernel Version in ID:", strings.Contains(k.ID, k.Label)) 134 135 // Output: 136 // Fetched == 100: true 137 // Results > 100: true 138 // Pages > 1: true 139 // Kernel Version in ID: true 140} 141 142func ExampleClient_GetKernel_specific() { 143 // Example readers, Ignore this bit of setup code needed to record test fixtures 144 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetKernel_specific") 145 defer teardown() 146 147 l32, err := linodeClient.GetKernel(context.Background(), "linode/latest-32bit") 148 if err == nil { 149 fmt.Println("Label starts:", l32.Label[0:9]) 150 } else { 151 log.Fatalln(err) 152 } 153 154 l64, err := linodeClient.GetKernel(context.Background(), "linode/latest-64bit") 155 if err == nil { 156 fmt.Println("Label starts:", l64.Label[0:9]) 157 } else { 158 log.Fatalln(err) 159 } 160 // Interference check 161 fmt.Println("First Label still starts:", l32.Label[0:9]) 162 163 // Output: 164 // Label starts: Latest 32 165 // Label starts: Latest 64 166 // First Label still starts: Latest 32 167} 168 169func ExampleClient_GetImage_missing() { 170 // Example readers, Ignore this bit of setup code needed to record test fixtures 171 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetImage_missing") 172 defer teardown() 173 174 _, err := linodeClient.GetImage(context.Background(), "not-found") 175 if err != nil { 176 if v, ok := err.(*linodego.Error); ok { 177 fmt.Println("Request was:", v.Response.Request.URL) 178 fmt.Println("Response was:", v.Response.Status) 179 fmt.Println("Error was:", v) 180 } 181 } 182 183 // Output: 184 // Request was: https://api.linode.com/v4beta/images/not-found 185 // Response was: 404 NOT FOUND 186 // Error was: [404] Not found 187} 188 189func ExampleClient_ListImages_all() { 190 // Example readers, Ignore this bit of setup code needed to record test fixtures 191 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListImages_all") 192 defer teardown() 193 194 filterOpt := linodego.NewListOptions(0, "") 195 images, err := linodeClient.ListImages(context.Background(), filterOpt) 196 if err != nil { 197 log.Fatal(err) 198 } 199 fmt.Println("Fetched Results/100 pages:", filterOpt.Pages > filterOpt.Results/100) 200 fmt.Println("Fetched all results:", filterOpt.Results == len(images)) 201 202 // Output: 203 // Fetched Results/100 pages: true 204 // Fetched all results: true 205} 206 207// ExampleListImages_notfound demonstrates that an empty slice is returned, 208// not an error, when a filter matches no results. 209func ExampleClient_ListImages_notfound() { 210 // Example readers, Ignore this bit of setup code needed to record test fixtures 211 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListImages_notfound") 212 defer teardown() 213 214 filterOpt := linodego.ListOptions{Filter: "{\"label\":\"not-found\"}"} 215 images, err := linodeClient.ListImages(context.Background(), &filterOpt) 216 if err != nil { 217 log.Fatal(err) 218 } 219 fmt.Println("Images with Label 'not-found':", len(images)) 220 221 // Output: 222 // Images with Label 'not-found': 0 223} 224 225// ExampleListImages_notfound demonstrates that an error is returned by 226// the API and linodego when an invalid filter is provided 227func ExampleClient_ListImages_badfilter() { 228 // Example readers, Ignore this bit of setup code needed to record test fixtures 229 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListImages_badfilter") 230 defer teardown() 231 232 filterOpt := linodego.ListOptions{Filter: "{\"foo\":\"bar\"}"} 233 images, err := linodeClient.ListImages(context.Background(), &filterOpt) 234 if err == nil { 235 log.Fatal(err) 236 } 237 fmt.Println("Error given on bad filter:", err) 238 fmt.Println("Images on bad filter:", images) // TODO: nil would be better here 239 240 // Output: 241 // Error given on bad filter: [400] [X-Filter] Cannot filter on foo 242 // Images on bad filter: [] 243} 244 245func ExampleClient_ListLongviewSubscriptions_page1() { 246 // Example readers, Ignore this bit of setup code needed to record test fixtures 247 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListLongviewSubscriptions_page1") 248 defer teardown() 249 250 pageOpt := linodego.ListOptions{PageOptions: &linodego.PageOptions{Page: 1}} 251 subscriptions, err := linodeClient.ListLongviewSubscriptions(context.Background(), &pageOpt) 252 if err != nil { 253 log.Fatal(err) 254 } 255 fmt.Println("Longview Subscription Types:", len(subscriptions)) 256 257 // Output: 258 // Longview Subscription Types: 4 259} 260 261func ExampleClient_ListStackscripts_page1() { 262 // Example readers, Ignore this bit of setup code needed to record test fixtures 263 linodeClient, teardown := createTestClient(nil, "fixtures/ExampleListStackscripts_page1") 264 defer teardown() 265 266 filterOpt := linodego.NewListOptions(1, "") 267 scripts, err := linodeClient.ListStackscripts(context.Background(), filterOpt) 268 if err != nil { 269 log.Fatal(err) 270 } 271 // The Linode API default pagination size is 100. 272 fmt.Println("Fetched == 100:", len(scripts) == 100) 273 fmt.Println("Results > 100:", filterOpt.Results > 100) 274 fmt.Println("Pages > 1:", filterOpt.Pages > 1) 275 s := scripts[len(scripts)-1] 276 fmt.Println("StackScript Script has shebang:", strings.Contains(s.Script, "#!/")) 277 fmt.Println("Created is parsed:", s.Created != nil) 278 279 // Output: 280 // Fetched == 100: true 281 // Results > 100: true 282 // Pages > 1: true 283 // StackScript Script has shebang: true 284 // Created is parsed: true 285} 286