package netboxgo import ( "context" "fmt" "net/http" "net/url" "time" "github.com/gorilla/schema" ) type TenantsService service // NewTenant is used for the return values from Netbox API tenancy_tenants_create type NewTenant struct { CustomFields interface{} `json:"custom_fields"` Slug string `json:"slug"` Description string `json:"description"` Comments string `json:"comments"` Name string `json:"name"` Tags []string `json:"tags"` Group int `json:"group"` } // Tenant represents a tenant type Tenant struct { LastUpdated time.Time `json:"last_updated"` CustomFields map[string]interface{} `json:"custom_fields"` Created string `json:"created"` Name string `json:"name"` Slug string `json:"slug"` Description string `json:"description"` Comments string `json:"comments"` Group struct { URL string `json:"url"` Name string `json:"name"` Slug string `json:"slug"` ID int `json:"id"` } `json:"group"` Tags []interface{} `json:"tags"` VRFCount int `json:"vrf_count"` CircuitCount int `json:"circuit_count"` IPAddressCount int `json:"ipaddress_count"` VirtualmachineCount int `json:"virtualmachine_count"` RackCount int `json:"rack_count"` DeviceCount int `json:"device_count"` PrefixCount int `json:"prefix_count"` SiteCount int `json:"site_count"` VLANCount int `json:"vlan_count"` ID int `json:"id"` } // Tenants is represents a list of multiple tenants type Tenants struct { Previous interface{} `json:"previous"` Next string `json:"next"` Results []Tenant `json:"results"` Count int `json:"count"` } // TenantFilter is used to filter out returned object from Netbox API tenancy_tenants_list type TenantFilter struct { // User specific filters Name string `schema:"name,omitempty"` Slug string `schema:"slug,omitempty"` Group string `schema:"group,omitempty"` GroupID string `schema:"group_id,omitempty"` IDIn string `schema:"id__in,omitempty"` Q string `schema:"q,omitempty"` Tag string `schema:"tag,omitempty"` Offset int `schema:"offset,omitempty"` Limit int `schema:"limit,omitempty"` } const tenantsPath = tenancyPath + "/tenants" // List tenants. TenantFilter is used to list based on filter queries. func (s *TenantsService) List(ctx context.Context, f *TenantFilter) (*Tenants, error) { var tenants Tenants var query string var req *http.Request var err error encoder := schema.NewEncoder() form := url.Values{} err = encoder.Encode(f, form) if err != nil { return &tenants, err } query = form.Encode() req, err = s.client.newRequest(ctx, "GET", tenantsPath, query, nil) if err != nil { return &tenants, err } _, err = s.client.do(req, &tenants) if err != nil { return &tenants, err } return &tenants, nil } // ListAll tenants. TenantFilter is used to list all tenants based on filters func (s *TenantsService) ListAll(ctx context.Context, f *TenantFilter) (*Tenants, error) { var all Tenants var r *Tenants var num int var err error f.Limit = 1 r, err = s.List(ctx, f) if err != nil { return &all, fmt.Errorf("unable to count prefixes with filter %+v: %w", f, err) } num = r.Count for count := 0; count < num; count += 1000 { f.Limit = 1000 f.Offset = count r, err = s.List(ctx, f) if err != nil { return &all, fmt.Errorf("unable to list prefixes with filter %+v: %w", f, err) } all.Results = append(all.Results, r.Results...) } return &all, nil } // Create a tenant func (s *TenantsService) Create(ctx context.Context, c *NewTenant) error { var err error var req *http.Request req, err = s.client.newRequest(ctx, "POST", tenantsPath, "", c) if err != nil { return fmt.Errorf("unable to create request: %w", err) } _, err = s.client.do(req, nil) if err != nil { return fmt.Errorf("unable to do request: %w", err) } return nil } // // Update a device // func (s *TenantsService) Update(ctx context.Context, id string, c *UpdateDevice) error { // var req *http.Request // var err error // path := fmt.Sprintf("%s/%s", tenantsPath, id) // req, err = s.client.newRequest(ctx, "PATCH", path, "", c) // if err != nil { // return fmt.Errorf("unable to create request: %w", err) // } // _, err = s.client.do(req, nil) // if err != nil { // return fmt.Errorf("unable to do request: %w", err) // } // return nil // }