Skip to content

Add tests for discussions #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions tests/disc_183_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*

Issue Body:

```
Sup guyz! I'm tryna to get a devicelist like that
```
res1, httpRes, err := c.DcimAPI.DcimDevicesList(ctx).Status([]string{"active"}).Limit(10).Execute()

if err != nil {
log.Printf("Error get devices list: %v", err)
}

log.Printf("%v", res1.Results)
log.Printf("Response: %v\n", res1)
log.Printf("HTTP Response: %+v", httpRes)
```
but all i got is
```
2024/09/06 17:13:41 Error get devices list: no value given for required property device_count
2024/09/06 17:13:41 []
2024/09/06 17:13:41 Response: &{0 {<nil> false} {<nil> false} [] map[]}
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
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
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,
"results":[{ ___HERE ALL MY DEVICES___ }]}} ContentLength:5577 TransferEncoding:[] Close:false Uncompressed:false Trailer:map[] Request:0xc0001712c0 TLS:<nil>}
```

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

P.S. I use the latest netbox 4.1.0
````


*/

package main

import (
"context"
"testing"

"github.com/netbox-community/go-netbox/v4"
)

type Seed183Disc struct {
Devices []*netbox.DeviceWithConfigContext
}

func (s *Seed183Disc) Cleanup(t *testing.T, client *netbox.APIClient) {
t.Helper()

for _, device := range s.Devices {
res, err := client.DcimAPI.DcimDevicesDestroy(context.Background(), device.Id).Execute()
if err != nil {
fatalHttp(t, "failed to delete device", err, res)
}
}
}

const disc183DeviceCount = 10

var disc183DeviceDescription = "test-disc-183"

func HSeed183Disc(t *testing.T, client *netbox.APIClient, defaults *Defaults) *Seed183Disc {
t.Helper()
seed := &Seed183Disc{}

for i := 0; i < disc183DeviceCount; i++ {
name := randString(10)
device := netbox.WritableDeviceWithConfigContextRequest{
Name: *netbox.NewNullableString(&name),
Description: &disc183DeviceDescription,
Site: *netbox.NewBriefSiteRequest(defaults.Site.Name, defaults.Site.Slug),
DeviceType: *netbox.NewBriefDeviceTypeRequest(*netbox.NewBriefManufacturerRequest(defaults.Manufacturer.Name, defaults.Manufacturer.Slug), defaults.DeviceType.Model, defaults.DeviceType.Slug),
Role: *netbox.NewBriefDeviceRoleRequest(defaults.DeviceRole.Name, defaults.DeviceRole.Slug),
}

d, res, err := client.DcimAPI.DcimDevicesCreate(context.Background()).WritableDeviceWithConfigContextRequest(device).Execute()
if err != nil {
fatalHttp(t, "failed to create device", err, res)
}
seed.Devices = append(seed.Devices, d)
}

return seed
}

func Test183Disc(t *testing.T) {
harness := GetHarness(t)
defer harness.Cleanup(t)
client := harness.client

seed := HSeed183Disc(t, client, harness.defaults)
harness.AddCleanup(seed)

devicelist, res, err := client.DcimAPI.DcimDevicesList(context.Background()).Description([]string{disc183DeviceDescription}).Execute()
if err != nil {
fatalHttp(t, "failed to get device list", err, res)
}

if len(devicelist.Results) != disc183DeviceCount {
t.Fatalf("expected %d devices, got %d", disc183DeviceCount, len(devicelist.Results))
}
}
116 changes: 116 additions & 0 deletions tests/disc_202_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**

Issue Body:

```
Hi,

Below is my sample code.

// Set up NetBox API client
targetPrefix := "192.168.1.0/24" // Change to your desired prefix

client := netbox.NewNetboxWithAPIKey(netboxURL, apiToken)

// Step 1: Find the Prefix ID dynamically
prefixListReq := ipam.NewIpamPrefixesListParams().WithPrefix(&targetPrefix)
prefixListResp, err := client.Ipam.IpamPrefixesList(prefixListReq, nil)
if err != nil || prefixListResp.Payload == nil || len(prefixListResp.Payload.Results) == 0 {
fmt.Println(err)
fmt.Println(prefixListResp.Payload)
fmt.Println(prefixListResp.Payload.Results)
log.Fatalf("Prefix %s not found in NetBox", targetPrefix)
}
prefixID := prefixListResp.Payload.Results[0].ID

// Step 2: Allocate an Available IP from the Prefix
ipReq := ipam.NewIpamPrefixesAvailableIpsCreateParams().WithID(prefixID)
ipResp, err := client.Ipam.IpamPrefixesAvailableIpsCreate(ipReq, nil)
if err != nil || ipResp.Payload == nil || len(prefixListResp.Payload.Results) == 0 {
log.Fatalf("Failed to allocate IP: %v", err)
}

Though the IP address has been created in the netbox, verified via GUI, this API is throwing an error as below.
"Failed to allocate IP: json: cannot unmarshal object into Go value of type []*models.IPAddress"

Is there something wrong in the code above?
````
**/

package main

import (
"context"
"testing"

"github.com/netbox-community/go-netbox/v4"
)

type Seed202Disc struct {
Prefix *netbox.Prefix
IP *netbox.IPAddress
}

func (s *Seed202Disc) Cleanup(t *testing.T, client *netbox.APIClient) {
t.Helper()

res, err := client.IpamAPI.IpamPrefixesDestroy(context.Background(), s.Prefix.GetId()).Execute()
if err != nil {
fatalHttp(t, "failed to delete prefix", err, res)
}

res, err = client.IpamAPI.IpamIpAddressesDestroy(context.Background(), s.IP.GetId()).Execute()
if err != nil {
fatalHttp(t, "failed to delete ip address", err, res)
}
}

func HSeed202Disc(t *testing.T, client *netbox.APIClient) *Seed202Disc {
t.Helper()

prefixReq := netbox.WritablePrefixRequest{
Prefix: "192.168.1.0/24",
}

prefix, res, err := client.IpamAPI.IpamPrefixesCreate(context.Background()).WritablePrefixRequest(prefixReq).Execute()
if err != nil {
fatalHttp(t, "failed to create prefix", err, res)
}

ipReq := netbox.WritableIPAddressRequest{
Address: "192.168.1.1/24",
}

ip, res, err := client.IpamAPI.IpamIpAddressesCreate(context.Background()).WritableIPAddressRequest(ipReq).Execute()
if err != nil {
fatalHttp(t, "failed to create ip address", err, res)
}

return &Seed202Disc{
Prefix: prefix,
IP: ip,
}
}

func Test202Disc(t *testing.T) {
harness := GetHarness(t)
defer harness.Cleanup(t)
client := harness.client

seed := HSeed202Disc(t, harness.client)
harness.AddCleanup(seed)

_, res, err := client.IpamAPI.IpamPrefixesList(context.Background()).Execute()
if err != nil {
fatalHttp(t, "failed to list prefixes", err, res)
}

ipAddressRequest := netbox.IPAddressRequest{
Address: "192.168.1.1/24",
}

_, res, err = client.IpamAPI.IpamPrefixesAvailableIpsCreate(context.Background(), seed.Prefix.Id).IPAddressRequest([]netbox.IPAddressRequest{ipAddressRequest}).Execute()
if err != nil {
fatalHttp(t, "failed to create ip address", err, res)
}
}