Skip to content

Commit e36f15f

Browse files
authored
Merge pull request #203 from netbox-community/test-discussion-202
Add tests for discussions
2 parents f5e2e15 + df90449 commit e36f15f

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

tests/disc_183_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

tests/disc_202_test.go

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
3+
Issue Body:
4+
5+
```
6+
Hi,
7+
8+
Below is my sample code.
9+
10+
// Set up NetBox API client
11+
targetPrefix := "192.168.1.0/24" // Change to your desired prefix
12+
13+
client := netbox.NewNetboxWithAPIKey(netboxURL, apiToken)
14+
15+
// Step 1: Find the Prefix ID dynamically
16+
prefixListReq := ipam.NewIpamPrefixesListParams().WithPrefix(&targetPrefix)
17+
prefixListResp, err := client.Ipam.IpamPrefixesList(prefixListReq, nil)
18+
if err != nil || prefixListResp.Payload == nil || len(prefixListResp.Payload.Results) == 0 {
19+
fmt.Println(err)
20+
fmt.Println(prefixListResp.Payload)
21+
fmt.Println(prefixListResp.Payload.Results)
22+
log.Fatalf("Prefix %s not found in NetBox", targetPrefix)
23+
}
24+
prefixID := prefixListResp.Payload.Results[0].ID
25+
26+
// Step 2: Allocate an Available IP from the Prefix
27+
ipReq := ipam.NewIpamPrefixesAvailableIpsCreateParams().WithID(prefixID)
28+
ipResp, err := client.Ipam.IpamPrefixesAvailableIpsCreate(ipReq, nil)
29+
if err != nil || ipResp.Payload == nil || len(prefixListResp.Payload.Results) == 0 {
30+
log.Fatalf("Failed to allocate IP: %v", err)
31+
}
32+
33+
Though the IP address has been created in the netbox, verified via GUI, this API is throwing an error as below.
34+
"Failed to allocate IP: json: cannot unmarshal object into Go value of type []*models.IPAddress"
35+
36+
Is there something wrong in the code above?
37+
````
38+
**/
39+
40+
package main
41+
42+
import (
43+
"context"
44+
"testing"
45+
46+
"github.com/netbox-community/go-netbox/v4"
47+
)
48+
49+
type Seed202Disc struct {
50+
Prefix *netbox.Prefix
51+
IP *netbox.IPAddress
52+
}
53+
54+
func (s *Seed202Disc) Cleanup(t *testing.T, client *netbox.APIClient) {
55+
t.Helper()
56+
57+
res, err := client.IpamAPI.IpamPrefixesDestroy(context.Background(), s.Prefix.GetId()).Execute()
58+
if err != nil {
59+
fatalHttp(t, "failed to delete prefix", err, res)
60+
}
61+
62+
res, err = client.IpamAPI.IpamIpAddressesDestroy(context.Background(), s.IP.GetId()).Execute()
63+
if err != nil {
64+
fatalHttp(t, "failed to delete ip address", err, res)
65+
}
66+
}
67+
68+
func HSeed202Disc(t *testing.T, client *netbox.APIClient) *Seed202Disc {
69+
t.Helper()
70+
71+
prefixReq := netbox.WritablePrefixRequest{
72+
Prefix: "192.168.1.0/24",
73+
}
74+
75+
prefix, res, err := client.IpamAPI.IpamPrefixesCreate(context.Background()).WritablePrefixRequest(prefixReq).Execute()
76+
if err != nil {
77+
fatalHttp(t, "failed to create prefix", err, res)
78+
}
79+
80+
ipReq := netbox.WritableIPAddressRequest{
81+
Address: "192.168.1.1/24",
82+
}
83+
84+
ip, res, err := client.IpamAPI.IpamIpAddressesCreate(context.Background()).WritableIPAddressRequest(ipReq).Execute()
85+
if err != nil {
86+
fatalHttp(t, "failed to create ip address", err, res)
87+
}
88+
89+
return &Seed202Disc{
90+
Prefix: prefix,
91+
IP: ip,
92+
}
93+
}
94+
95+
func Test202Disc(t *testing.T) {
96+
harness := GetHarness(t)
97+
defer harness.Cleanup(t)
98+
client := harness.client
99+
100+
seed := HSeed202Disc(t, harness.client)
101+
harness.AddCleanup(seed)
102+
103+
_, res, err := client.IpamAPI.IpamPrefixesList(context.Background()).Execute()
104+
if err != nil {
105+
fatalHttp(t, "failed to list prefixes", err, res)
106+
}
107+
108+
ipAddressRequest := netbox.IPAddressRequest{
109+
Address: "192.168.1.1/24",
110+
}
111+
112+
_, res, err = client.IpamAPI.IpamPrefixesAvailableIpsCreate(context.Background(), seed.Prefix.Id).IPAddressRequest([]netbox.IPAddressRequest{ipAddressRequest}).Execute()
113+
if err != nil {
114+
fatalHttp(t, "failed to create ip address", err, res)
115+
}
116+
}

0 commit comments

Comments
 (0)