diff --git a/interfaces.go b/dcim_interfaces.go similarity index 89% rename from interfaces.go rename to dcim_interfaces.go index 629cadc..0b4e459 100644 --- a/interfaces.go +++ b/dcim_interfaces.go @@ -12,10 +12,10 @@ import ( "github.com/gorilla/schema" ) -type InterfacesService service +type DCIMInterfacesService service -// NewInterface is used for creating a new interface -type NewInterface struct { +// NewDCIMInterface is used for creating a new interface +type NewDCIMInterface struct { CustomFields interface{} `json:"custom_fields"` WWN string `json:"wwn"` Label string `json:"label"` @@ -51,16 +51,16 @@ type NewInterface struct { MGMTOnly bool `json:"mgmt_only"` } -// Interfaces is a list of interfaces -type Interfaces struct { - Next interface{} `json:"next,omitempty"` - Previous interface{} `json:"previous,omitempty"` - Results []Interface `json:"results,omitempty"` - Count int `json:"count,omitempty"` +// DCIMInterfaces is a list of interfaces +type DCIMInterfaces struct { + Next interface{} `json:"next,omitempty"` + Previous interface{} `json:"previous,omitempty"` + Results []DCIMInterface `json:"results,omitempty"` + Count int `json:"count,omitempty"` } -// Interface is one interface -type Interface struct { +// DCIMInterface is one interface +type DCIMInterface struct { CustomFields struct{} `json:"custom_fields"` LastUpdated time.Time `json:"last_updated"` LinkPeer interface{} `json:"link_peer"` @@ -193,8 +193,8 @@ type Interface struct { Occupied bool `json:"_occupied"` } -// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list -type InterfaceFilter struct { +// DCIMInterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list +type DCIMInterfaceFilter struct { // User specific filters ID string `schema:"id,omitempty"` Name string `schema:"name,omitempty"` @@ -222,8 +222,8 @@ type InterfaceFilter struct { const interfacesPath = dcimPath + "/interfaces" // List devices. DeviceFilter is used to list based on filter queries. -func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Interfaces, error) { - var interfaces Interfaces +func (s *DCIMInterfacesService) List(ctx context.Context, f *DCIMInterfaceFilter) (*DCIMInterfaces, error) { + var interfaces DCIMInterfaces var query string var req *http.Request var err error @@ -251,10 +251,10 @@ func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Inte } // Create a interfaces -func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Interface, error) { +func (s *DCIMInterfacesService) Create(ctx context.Context, c *NewDCIMInterface) (*DCIMInterface, error) { var err error var req *http.Request - var nic Interface + var nic DCIMInterface req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c) if err != nil { @@ -270,7 +270,7 @@ func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Inter } // Delete a interface -func (s *InterfacesService) Delete(ctx context.Context, i string) error { +func (s *DCIMInterfacesService) Delete(ctx context.Context, i string) error { var err error var req *http.Request diff --git a/netbox.go b/netbox.go index 2fb47f4..66dcf4d 100644 --- a/netbox.go +++ b/netbox.go @@ -15,29 +15,24 @@ import ( // Client struct is used to create a new NetBox endpoint type Client struct { - // DCIM *DCIM - // Tenancy *Tenancy - // IPAM *IPAM - // Virtualization *Virtualization - // Secret *Secret - - Clusters *ClustersService - ClusterTypes *ClusterTypesService - Devices *DevicesService - DeviceRoles *DeviceRolesService - DeviceTypes *DeviceTypesService - Interfaces *InterfacesService - InventoryItems *InventoryItemsService - Secrets *SecretsService - Sites *SitesService - SiteGroups *SiteGroupsService - Platforms *PlatformsService - Prefixes *PrefixesService - RearPorts *RearPortsService - Tenants *TenantsService - VirtualMachines *VirtualMachinesService - VRFs *VRFsService - VLANs *VLANsService + Clusters *ClustersService + ClusterTypes *ClusterTypesService + Devices *DevicesService + DeviceRoles *DeviceRolesService + DeviceTypes *DeviceTypesService + DCIMInterfaces *DCIMInterfacesService + InventoryItems *InventoryItemsService + Secrets *SecretsService + Sites *SitesService + SiteGroups *SiteGroupsService + Platforms *PlatformsService + Prefixes *PrefixesService + RearPorts *RearPortsService + Tenants *TenantsService + VirtualMachines *VirtualMachinesService + VirtualizationInterfaces *VirtualizationInterfacesService + VRFs *VRFsService + VLANs *VLANsService // baseURL is the URL used for the base URL of the API baseURL *url.URL @@ -132,7 +127,8 @@ func NewClient(apiurl string, httpClient *http.Client) (*Client, error) { c.Devices = (*DevicesService)(&c.common) c.DeviceRoles = (*DeviceRolesService)(&c.common) c.DeviceTypes = (*DeviceTypesService)(&c.common) - c.Interfaces = (*InterfacesService)(&c.common) + c.DCIMInterfaces = (*DCIMInterfacesService)(&c.common) + c.VirtualizationInterfaces = (*VirtualizationInterfacesService)(&c.common) c.InventoryItems = (*InventoryItemsService)(&c.common) c.Prefixes = (*PrefixesService)(&c.common) c.Platforms = (*PlatformsService)(&c.common) diff --git a/virtualization_interfaces.go b/virtualization_interfaces.go new file mode 100644 index 0000000..c0848ad --- /dev/null +++ b/virtualization_interfaces.go @@ -0,0 +1,212 @@ +package netboxgo + +import ( + "context" + "fmt" + "net/http" + "net/url" + "time" + + "github.com/gorilla/schema" +) + +type VirtualizationInterfacesService service + +// NewVirtualizationInterface is used for creating a new virtualization interface +type NewVirtualizationInterface struct { + VirtualMachine int `json:"virtual_machine"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + Parent int `json:"parent"` + Bridge int `json:"bridge"` + MTU int `json:"mtu"` + MACAddress string `json:"mac_address"` + Description string `json:"description"` + Mode string `json:"mode"` + UntaggedVLAN int `json:"untagged_vlan"` + TaggedVLANs []int `json:"tagged_vlans"` + Tags []struct { + Name string `json:"name"` + Slug string `json:"slug"` + Color string `json:"color"` + } `json:"tags"` + CustomFields interface{} `json:"custom_fields"` +} + +// VirtualizationInterfaces is a list of interfaces +type VirtualizationInterfaces struct { + Next string `json:"next,omitempty"` + Previous string `json:"previous,omitempty"` + Results []VirtualizationInterface `json:"results,omitempty"` + Count int `json:"count,omitempty"` +} + +// VirtualizationInterface is one virtualization interface +type VirtualizationInterface struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + VirtualMachine struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + } `json:"virtual_machine"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + Parent struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + VirtualMachine struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + } `json:"virtual_machine"` + Name string `json:"name"` + } `json:"parent"` + Bridge struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + VirtualMachine struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + } `json:"virtual_machine"` + Name string `json:"name"` + } `json:"bridge"` + MTU int `json:"mtu"` + MACAddress string `json:"mac_address"` + Description string `json:"description"` + Mode struct { + Label string `json:"label"` + Value string `json:"value"` + } `json:"mode"` + UntaggedVLAN struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Vid int `json:"vid"` + Name string `json:"name"` + } `json:"untagged_vlan"` + TaggedVLANs []struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Vid int `json:"vid"` + Name string `json:"name"` + } `json:"tagged_vlans"` + Tags []struct { + ID int `json:"id"` + URL string `json:"url"` + Display string `json:"display"` + Name string `json:"name"` + Slug string `json:"slug"` + Color string `json:"color"` + } `json:"tags"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + CountIPAddresses int `json:"count_ipaddresses"` + CountFHRPGroups int `json:"count_fhrp_groups"` +} + +// VirtualizationInterfaceFilter is used to filter out virtualization interfaces +type VirtualizationInterfaceFilter struct { + // User specific filters + ID string `schema:"id,omitempty"` + Name string `schema:"name,omitempty"` + ConnectionStatus string `schema:"connection_status,omitempty"` + Type string `schema:"type,omitempty"` + MTU string `schema:"mtu,omitempty"` + MGMTOnly string `schema:"mgmt_only,omitempty"` + Mode string `schema:"mode,omitempty"` + Description string `schema:"description,omitempty"` + Q string `schema:"q,omitempty"` + VirtualMachine string `schema:"device,omitempty"` + ClusterID string `schema:"cluster_id,omitempty"` + VirtualMachineID string `schema:"virtual_machine_id,omitempty"` + Cabled string `schema:"cabled,omitempty"` + Kind string `schema:"kind,omitempty"` + LagID string `schema:"lag_id,omitempty"` + MACAddress string `schema:"mac_address,omitempty"` + Tag string `schema:"tag,omitempty"` + VLANID string `schema:"vlan_id,omitempty"` + VLAN string `schema:"vlan,omitempty"` + ParentID string `schema:"parent_id,omitempty"` + BridgeID string `schema:"bridge_id,omitempty"` + + Offset int64 `schema:"offset,omitempty"` + Limit int64 `schema:"limit,omitempty"` +} + +const virtualizationInterfacesPath = virtualizationPath + "/interfaces" + +// List devices. DeviceFilter is used to list based on filter queries. +func (s *VirtualizationInterfacesService) List(ctx context.Context, f *VirtualizationInterfaceFilter) (*VirtualizationInterfaces, error) { + var interfaces VirtualizationInterfaces + 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 &interfaces, err + } + query = form.Encode() + + req, err = s.client.newRequest(ctx, "GET", interfacesPath, query, nil) + if err != nil { + return &interfaces, err + } + + _, err = s.client.do(req, &interfaces) + if err != nil { + return &interfaces, err + } + + return &interfaces, nil +} + +// Create a interfaces +func (s *VirtualizationInterfacesService) Create(ctx context.Context, c *NewVirtualizationInterface) (*VirtualizationInterface, error) { + var err error + var req *http.Request + var nic VirtualizationInterface + + req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c) + if err != nil { + return &nic, fmt.Errorf("unable to create request: %w", err) + } + + _, err = s.client.do(req, &nic) + if err != nil { + return &nic, fmt.Errorf("unable to do request: %w", err) + } + + return &nic, nil +} + +// Delete a interface +func (s *VirtualizationInterfacesService) Delete(ctx context.Context, i string) error { + var err error + var req *http.Request + + req, err = s.client.newRequest(ctx, "DELETE", virtualizationInterfacesPath+"/"+i+"/", "", nil) + 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 +}