|
| 1 | +/* |
| 2 | +
|
| 3 | +Issue Body: |
| 4 | +
|
| 5 | +``` |
| 6 | +Sup guyz! I'm tryna to get a devicelist like that |
| 7 | +``` |
| 8 | +res1, httpRes, err := c.DcimAPI.DcimDevicesList(ctx).Status([]string{"active"}).Limit(10).Execute() |
| 9 | +
|
| 10 | +if err != nil { |
| 11 | + log.Printf("Error get devices list: %v", err) |
| 12 | +} |
| 13 | +
|
| 14 | +log.Printf("%v", res1.Results) |
| 15 | +log.Printf("Response: %v\n", res1) |
| 16 | +log.Printf("HTTP Response: %+v", httpRes) |
| 17 | +``` |
| 18 | +but all i got is |
| 19 | +``` |
| 20 | +2024/09/06 17:13:41 Error get devices list: no value given for required property device_count |
| 21 | +2024/09/06 17:13:41 [] |
| 22 | +2024/09/06 17:13:41 Response: &{0 {<nil> false} {<nil> false} [] map[]} |
| 23 | +2024/09/06 17:13:41 HTTP Response: &{Status:200 OK StatusCode:200 Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Allow:[GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS] Connection:[keep-alive] Content-L |
| 24 | +anguage:[en] Content-Length:[5577] Content-Type:[application/json] Cross-Origin-Opener-Policy:[same-origin] Date:[Fri, 06 Sep 2024 17:13:41 GMT] Referrer-Policy:[same-origin] Server:[nginx] Vary:[HX-Reque |
| 25 | +st, Accept-Language, Cookie, origin] X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN] X-Request-Id:[7fed8b0e-c509-450d-9d5f-23ef54cf2c2b]] Body:{Reader:{"count":3,"next":null,"previous":null, |
| 26 | +"results":[{ ___HERE ALL MY DEVICES___ }]}} ContentLength:5577 TransferEncoding:[] Close:false Uncompressed:false Trailer:map[] Request:0xc0001712c0 TLS:<nil>} |
| 27 | +``` |
| 28 | +
|
| 29 | +Why the list is empty? Am i doing something wrong or it's just because of bugs: https://github.com/netbox-community/netbox/issues/16670 https://github.com/netbox-community/go-netbox/issues/165 |
| 30 | +
|
| 31 | +P.S. I use the latest netbox 4.1.0 |
| 32 | +```` |
| 33 | +
|
| 34 | +
|
| 35 | +*/ |
| 36 | + |
| 37 | +package main |
| 38 | + |
| 39 | +import ( |
| 40 | + "context" |
| 41 | + "testing" |
| 42 | + |
| 43 | + "github.com/netbox-community/go-netbox/v4" |
| 44 | +) |
| 45 | + |
| 46 | +type Seed183Disc struct { |
| 47 | + Devices []*netbox.DeviceWithConfigContext |
| 48 | +} |
| 49 | + |
| 50 | +func (s *Seed183Disc) Cleanup(t *testing.T, client *netbox.APIClient) { |
| 51 | + t.Helper() |
| 52 | + |
| 53 | + for _, device := range s.Devices { |
| 54 | + res, err := client.DcimAPI.DcimDevicesDestroy(context.Background(), device.Id).Execute() |
| 55 | + if err != nil { |
| 56 | + fatalHttp(t, "failed to delete device", err, res) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const disc183DeviceCount = 10 |
| 62 | + |
| 63 | +var disc183DeviceDescription = "test-disc-183" |
| 64 | + |
| 65 | +func HSeed183Disc(t *testing.T, client *netbox.APIClient, defaults *Defaults) *Seed183Disc { |
| 66 | + t.Helper() |
| 67 | + seed := &Seed183Disc{} |
| 68 | + |
| 69 | + for i := 0; i < disc183DeviceCount; i++ { |
| 70 | + name := randString(10) |
| 71 | + device := netbox.WritableDeviceWithConfigContextRequest{ |
| 72 | + Name: *netbox.NewNullableString(&name), |
| 73 | + Description: &disc183DeviceDescription, |
| 74 | + Site: *netbox.NewBriefSiteRequest(defaults.Site.Name, defaults.Site.Slug), |
| 75 | + DeviceType: *netbox.NewBriefDeviceTypeRequest(*netbox.NewBriefManufacturerRequest(defaults.Manufacturer.Name, defaults.Manufacturer.Slug), defaults.DeviceType.Model, defaults.DeviceType.Slug), |
| 76 | + Role: *netbox.NewBriefDeviceRoleRequest(defaults.DeviceRole.Name, defaults.DeviceRole.Slug), |
| 77 | + } |
| 78 | + |
| 79 | + d, res, err := client.DcimAPI.DcimDevicesCreate(context.Background()).WritableDeviceWithConfigContextRequest(device).Execute() |
| 80 | + if err != nil { |
| 81 | + fatalHttp(t, "failed to create device", err, res) |
| 82 | + } |
| 83 | + seed.Devices = append(seed.Devices, d) |
| 84 | + } |
| 85 | + |
| 86 | + return seed |
| 87 | +} |
| 88 | + |
| 89 | +func Test183Disc(t *testing.T) { |
| 90 | + harness := GetHarness(t) |
| 91 | + defer harness.Cleanup(t) |
| 92 | + client := harness.client |
| 93 | + |
| 94 | + seed := HSeed183Disc(t, client, harness.defaults) |
| 95 | + harness.AddCleanup(seed) |
| 96 | + |
| 97 | + devicelist, res, err := client.DcimAPI.DcimDevicesList(context.Background()).Description([]string{disc183DeviceDescription}).Execute() |
| 98 | + if err != nil { |
| 99 | + fatalHttp(t, "failed to get device list", err, res) |
| 100 | + } |
| 101 | + |
| 102 | + if len(devicelist.Results) != disc183DeviceCount { |
| 103 | + t.Fatalf("expected %d devices, got %d", disc183DeviceCount, len(devicelist.Results)) |
| 104 | + } |
| 105 | +} |
0 commit comments