|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "math/rand" |
| 10 | + |
| 11 | + "github.com/netbox-community/go-netbox/v4" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + manufacturerName = "test-manufacturer" |
| 16 | + deviceTypeName = "test-device-type" |
| 17 | + siteName = "test-site" |
| 18 | + deviceName = "test-device" |
| 19 | + deviceRoleName = "test-device-role" |
| 20 | + interfaceName = "test-interface" |
| 21 | +) |
| 22 | + |
| 23 | +type Defaults struct { |
| 24 | + Site netbox.Site |
| 25 | + DeviceRole netbox.DeviceRole |
| 26 | + Manufacturer netbox.Manufacturer |
| 27 | + DeviceType netbox.DeviceType |
| 28 | + Device netbox.DeviceWithConfigContext |
| 29 | +} |
| 30 | + |
| 31 | +func (d *Defaults) Cleanup(t *testing.T, client *netbox.APIClient) { |
| 32 | + |
| 33 | + _, err := client.DcimAPI.DcimDevicesDestroy(context.Background(), d.Device.Id).Execute() |
| 34 | + if err != nil { |
| 35 | + t.Errorf("Failed to delete device: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + _, err = client.DcimAPI.DcimDeviceTypesDestroy(context.Background(), d.DeviceType.Id).Execute() |
| 39 | + if err != nil { |
| 40 | + t.Errorf("Failed to delete device type: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + _, err = client.DcimAPI.DcimManufacturersDestroy(context.Background(), d.Manufacturer.Id).Execute() |
| 44 | + if err != nil { |
| 45 | + t.Errorf("Failed to delete manufacturer: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + _, err = client.DcimAPI.DcimDeviceRolesDestroy(context.Background(), d.DeviceRole.Id).Execute() |
| 49 | + if err != nil { |
| 50 | + t.Errorf("Failed to delete device role: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + _, err = client.DcimAPI.DcimSitesDestroy(context.Background(), d.Site.Id).Execute() |
| 54 | + if err != nil { |
| 55 | + t.Errorf("Failed to delete site: %v", err) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func randString(n int) string { |
| 60 | + letters := []rune("abcdefghijklmnopqrstuvwxyz") |
| 61 | + s := make([]rune, n) |
| 62 | + for i := range s { |
| 63 | + s[i] = letters[rand.Intn(len(letters))] |
| 64 | + } |
| 65 | + return string(s) |
| 66 | +} |
| 67 | + |
| 68 | +func fatalHttp(t *testing.T, msg string, err error, res *http.Response) { |
| 69 | + body, _ := io.ReadAll(res.Body) |
| 70 | + t.Fatalf("%s: %v\n response: %v", msg, err, string(body)) |
| 71 | +} |
| 72 | + |
| 73 | +func HGetDefaults(t *testing.T, client *netbox.APIClient) *Defaults { |
| 74 | + site := netbox.WritableSiteRequest{ |
| 75 | + Name: siteName + randString(4), |
| 76 | + Slug: siteName + randString(4), |
| 77 | + } |
| 78 | + |
| 79 | + newSite, res, err := client.DcimAPI.DcimSitesCreate(context.Background()).WritableSiteRequest(site).Execute() |
| 80 | + if err != nil { |
| 81 | + fatalHttp(t, "Failed to create site", err, res) |
| 82 | + } |
| 83 | + |
| 84 | + deviceRole := netbox.DeviceRoleRequest{ |
| 85 | + Name: deviceRoleName + randString(4), |
| 86 | + Slug: deviceRoleName + randString(4), |
| 87 | + } |
| 88 | + |
| 89 | + newDeviceRole, res, err := client.DcimAPI.DcimDeviceRolesCreate(context.Background()).DeviceRoleRequest(deviceRole).Execute() |
| 90 | + if err != nil { |
| 91 | + fatalHttp(t, "Failed to create device role", err, res) |
| 92 | + } |
| 93 | + |
| 94 | + manufacturer := netbox.ManufacturerRequest{ |
| 95 | + Name: manufacturerName + randString(4), |
| 96 | + Slug: manufacturerName + randString(4), |
| 97 | + } |
| 98 | + |
| 99 | + newManufacturer, res, err := client.DcimAPI.DcimManufacturersCreate(context.Background()).ManufacturerRequest(manufacturer).Execute() |
| 100 | + if err != nil { |
| 101 | + fatalHttp(t, "Failed to create manufacturer", err, res) |
| 102 | + } |
| 103 | + |
| 104 | + deviceType := netbox.WritableDeviceTypeRequest{ |
| 105 | + Model: deviceTypeName + randString(4), |
| 106 | + Slug: deviceTypeName + randString(4), |
| 107 | + Manufacturer: *netbox.NewBriefManufacturerRequest(newManufacturer.Name, newManufacturer.Slug), |
| 108 | + } |
| 109 | + |
| 110 | + newDeviceType, res, err := client.DcimAPI.DcimDeviceTypesCreate(context.Background()).WritableDeviceTypeRequest(deviceType).Execute() |
| 111 | + if err != nil { |
| 112 | + fatalHttp(t, "Failed to create device type", err, res) |
| 113 | + } |
| 114 | + |
| 115 | + name := deviceName + randString(4) |
| 116 | + |
| 117 | + device := netbox.WritableDeviceWithConfigContextRequest{ |
| 118 | + Name: *netbox.NewNullableString(&name), |
| 119 | + Role: *netbox.NewBriefDeviceRoleRequest(newDeviceRole.Name, newDeviceRole.Slug), |
| 120 | + Site: *netbox.NewBriefSiteRequest(newSite.Name, newSite.Slug), |
| 121 | + DeviceType: *netbox.NewBriefDeviceTypeRequest(*netbox.NewBriefManufacturerRequest(newManufacturer.Name, newManufacturer.Slug), newDeviceType.Model, newDeviceType.Slug), |
| 122 | + } |
| 123 | + |
| 124 | + newDevice, res, err := client.DcimAPI.DcimDevicesCreate(context.Background()).WritableDeviceWithConfigContextRequest(device).Execute() |
| 125 | + if err != nil { |
| 126 | + fatalHttp(t, "Failed to create device", err, res) |
| 127 | + } |
| 128 | + |
| 129 | + return &Defaults{ |
| 130 | + Site: *newSite, |
| 131 | + DeviceRole: *newDeviceRole, |
| 132 | + Device: *newDevice, |
| 133 | + Manufacturer: *newManufacturer, |
| 134 | + DeviceType: *newDeviceType, |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestDefaults(t *testing.T) { |
| 139 | + |
| 140 | + client := HGetClient(t) |
| 141 | + |
| 142 | + defaults := HGetDefaults(t, client) |
| 143 | + |
| 144 | + defaults.Cleanup(t, client) |
| 145 | +} |
0 commit comments