Skip to content

Commit 3a35072

Browse files
committed
Add test for discussion 202
1 parent f5e2e15 commit 3a35072

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/disc_202_test.go

Lines changed: 116 additions & 0 deletions
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)