Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
GÉANT Service Orchestrator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GÉANT Orchestration and Automation Team
GAP
GÉANT Service Orchestrator
Commits
2ce67726
Commit
2ce67726
authored
1 year ago
by
Ubuntu
Committed by
Neda Moeini
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
NAT-243: added support custom fields to listing of interfaces
parent
94aa97d5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!77
Netbox integration including intial CLI for populating base data and ...
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/netboxcli.py
+161
-157
161 additions, 157 deletions
utils/netboxcli.py
with
161 additions
and
157 deletions
utils/netboxcli.py
+
161
−
157
View file @
2ce67726
"""
Command line tool to communicate with
the NetBox API
"""
import
click
import
pandas
as
pd
from
gso.services
import
netbox_client
as
nc
# Define url and token for netbox API
# TODO move to config file
token
=
"
<Token>
"
api
=
'
http://127.0.0.1:8001
'
nbclient
=
nc
.
connect
(
api
,
token
)
def
convert_to_table
(
data
,
fields
):
df
=
pd
.
DataFrame
(
data
)
df
=
df
[
fields
]
# Selecting specific fields to display
return
df
@click.group
()
def
cli
():
pass
@cli.group
()
def
create
():
pass
@create.command
()
@click.option
(
'
--fqdn
'
,
prompt
=
'
Enter device name
'
,
help
=
'
Device name
'
)
@click.option
(
'
--model
'
,
default
=
"
vmx
"
,
help
=
'
Device model
'
)
@click.option
(
'
--role
'
,
default
=
"
router
"
,
help
=
'
Device role
'
)
@click.option
(
'
--site
'
,
default
=
"
Amsterdam
"
,
help
=
'
Device role
'
)
def
device
(
fqdn
:
str
,
model
:
str
,
role
:
str
,
site
:
str
):
click
.
echo
(
f
"
Creating device: fqdn=
{
fqdn
}
, model=
{
model
}
, role=
{
role
}
"
)
new_device
=
nc
.
create_device
(
nbclient
,
fqdn
,
model
,
role
,
site
)
print
(
new_device
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Interfacename
'
)
@click.option
(
'
--type
'
,
default
=
'
10gbase-t
'
,
help
=
'
Interface type, default is 10GBASE-T
'
)
@click.option
(
'
--fqdn
'
,
help
=
'
Device where to create interface
'
)
def
interface
(
name
:
str
,
type
:
str
,
fqdn
:
str
):
click
.
echo
(
f
"
Creating interface: name=
{
name
}
, type=
{
type
}
, fqdn=
{
fqdn
}
"
)
new_interface
=
nc
.
create_interface
(
nbclient
,
name
,
type
,
fqdn
)
print
(
new_interface
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Manufacturer name
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for manufacturer
'
)
def
manufacturer
(
name
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating manufacturer: name=
{
name
}
"
)
manufacturer
=
nc
.
create_device_manufacturer
(
nbclient
,
name
,
slug
)
print
(
manufacturer
)
@create.command
()
@click.option
(
'
--manufacturer
'
,
help
=
'
Manufacturer for device
'
)
@click.option
(
'
--model
'
,
help
=
'
Model for device
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for manufacturer
'
)
def
device_type
(
manufacturer
:
str
,
model
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating device type: manufacturer=
{
manufacturer
}
model =
{
model
}
"
)
device_type
=
nc
.
create_device_type
(
nbclient
,
manufacturer
,
model
,
slug
)
print
(
device_type
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Name for device role
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for device role
'
)
def
device_role
(
name
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating device role: name=
{
name
}
"
)
device_role
=
nc
.
create_device_role
(
nbclient
,
name
,
slug
)
print
(
device_role
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Name for device site
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for device site
'
)
def
device_site
(
name
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating device site: name=
{
name
}
"
)
device_site
=
nc
.
create_device_site
(
nbclient
,
name
,
slug
)
print
(
device_site
)
create
.
add_command
(
device
)
create
.
add_command
(
interface
)
create
.
add_command
(
manufacturer
)
create
.
add_command
(
device_type
)
create
.
add_command
(
device_role
)
create
.
add_command
(
device_site
)
# Define list commands here
@cli.group
()
def
list
():
pass
@list.command
()
@click.option
(
'
--fqdn
'
,
help
=
'
Device name to list interfaces
'
)
@click.option
(
'
--type
'
,
default
=
"
10gbase-t
"
,
help
=
'
Interface type to list interfaces
'
)
def
interfaces
(
fqdn
:
str
,
type
:
str
):
click
.
echo
(
f
"
Listing all interfaces for: device with fqdn=
{
fqdn
}
, type=
{
type
}
"
)
interface_list
=
nc
.
get_interfaces_by_device
(
nbclient
,
fqdn
)
display_fields
=
[
"
name
"
,
"
enabled
"
,
"
type
"
,
"
parent
"
,
"
lag
"
,
"
speed
"
]
iface_list
=
[]
for
iface
in
interface_list
:
iface_list
.
append
(
dict
(
iface
))
table
=
convert_to_table
(
iface_list
,
display_fields
)
print
(
table
)
@list.command
()
def
devices
():
click
.
echo
(
"
Listing all devices:
"
)
device_list
=
nc
.
get_all_devices
(
nbclient
)
display_fields
=
[
"
name
"
,
"
device_type
"
]
devices
=
[]
for
device
in
device_list
:
devices
.
append
(
dict
(
device
))
table
=
convert_to_table
(
devices
,
display_fields
)
print
(
table
)
list
.
add_command
(
interfaces
)
list
.
add_command
(
devices
)
# Define here attach command
@cli.group
()
def
attach
():
pass
@attach.command
()
@click.option
(
'
--fqdn
'
,
help
=
'
Device name where to attach interface to lag
'
)
@click.option
(
'
--iface
'
,
help
=
'
Interface name to attach to lag
'
)
@click.option
(
'
--lag
'
,
help
=
'
LAG name to attach interface
'
)
def
interface
(
fqdn
:
str
,
iface
:
str
,
lag
:
str
):
click
.
echo
(
f
"
Attaching interface to lag: device =
{
fqdn
}
, interface name=
{
iface
}
to lag=
{
lag
}
"
)
new_iface
=
nc
.
attach_interface_to_lag
(
nbclient
,
fqdn
,
lag
,
iface
)
print
(
new_iface
)
attach
.
add_command
(
interface
)
if
__name__
==
'
__main__
'
:
cli
()
"""
Command line tool to communicate with
the NetBox API
"""
import
click
import
pandas
as
pd
from
gso.services
import
netbox_client
as
nc
# Define url and token for netbox API
# TODO move to config file
token
=
"
428b68e7cb7afc6e7441879f21c3de6583081e40
"
api
=
'
http://127.0.0.1:8000
'
nbclient
=
nc
.
connect
(
api
,
token
)
def
convert_to_table
(
data
,
fields
):
if
len
(
data
)
==
0
:
print
(
"
No data is available for your request
"
)
exit
()
df
=
pd
.
DataFrame
(
data
)
df
=
df
[
fields
]
# Selecting specific fields to display
return
df
@click.group
()
def
cli
():
pass
@cli.group
()
def
create
():
pass
@create.command
()
@click.option
(
'
--fqdn
'
,
prompt
=
'
Enter device name
'
,
help
=
'
Device name
'
)
@click.option
(
'
--model
'
,
default
=
"
vmx
"
,
help
=
'
Device model
'
)
@click.option
(
'
--role
'
,
default
=
"
router
"
,
help
=
'
Device role
'
)
@click.option
(
'
--site
'
,
default
=
"
Amsterdam
"
,
help
=
'
Device role
'
)
def
device
(
fqdn
:
str
,
model
:
str
,
role
:
str
,
site
:
str
):
click
.
echo
(
f
"
Creating device: fqdn=
{
fqdn
}
, model=
{
model
}
, role=
{
role
}
"
)
new_device
=
nc
.
create_device
(
nbclient
,
fqdn
,
model
,
role
,
site
)
print
(
new_device
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Interfacename
'
)
@click.option
(
'
--type
'
,
default
=
'
10gbase-t
'
,
help
=
'
Interface type, default is 10GBASE-T
'
)
@click.option
(
'
--fqdn
'
,
help
=
'
Device where to create interface
'
)
def
interface
(
name
:
str
,
type
:
str
,
fqdn
:
str
):
click
.
echo
(
f
"
Creating interface: name=
{
name
}
, type=
{
type
}
, fqdn=
{
fqdn
}
"
)
new_interface
=
nc
.
create_interface
(
nbclient
,
name
,
type
,
fqdn
)
print
(
new_interface
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Manufacturer name
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for manufacturer
'
)
def
manufacturer
(
name
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating manufacturer: name=
{
name
}
"
)
manufacturer
=
nc
.
create_device_manufacturer
(
nbclient
,
name
,
slug
)
print
(
manufacturer
)
@create.command
()
@click.option
(
'
--manufacturer
'
,
help
=
'
Manufacturer for device
'
)
@click.option
(
'
--model
'
,
help
=
'
Model for device
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for manufacturer
'
)
def
device_type
(
manufacturer
:
str
,
model
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating device type: manufacturer=
{
manufacturer
}
model =
{
model
}
"
)
device_type
=
nc
.
create_device_type
(
nbclient
,
manufacturer
,
model
,
slug
)
print
(
device_type
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Name for device role
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for device role
'
)
def
device_role
(
name
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating device role: name=
{
name
}
"
)
device_role
=
nc
.
create_device_role
(
nbclient
,
name
,
slug
)
print
(
device_role
)
@create.command
()
@click.option
(
'
--name
'
,
help
=
'
Name for device site
'
)
@click.option
(
'
--slug
'
,
help
=
'
Short name for device site
'
)
def
device_site
(
name
:
str
,
slug
:
str
):
click
.
echo
(
f
"
Creating device site: name=
{
name
}
"
)
device_site
=
nc
.
create_device_site
(
nbclient
,
name
,
slug
)
print
(
device_site
)
create
.
add_command
(
device
)
create
.
add_command
(
interface
)
create
.
add_command
(
manufacturer
)
create
.
add_command
(
device_type
)
create
.
add_command
(
device_role
)
create
.
add_command
(
device_site
)
# Define list commands here
@cli.group
()
def
list
():
pass
@list.command
()
@click.option
(
'
--fqdn
'
,
help
=
'
Device name to list interfaces
'
)
@click.option
(
'
--type
'
,
default
=
"
10gbase-t
"
,
help
=
'
Interface type to list interfaces
'
)
def
interfaces
(
fqdn
:
str
,
type
:
str
):
click
.
echo
(
f
"
Listing all interfaces for: device with fqdn=
{
fqdn
}
, type=
{
type
}
"
)
interface_list
=
nc
.
get_interfaces_by_device
(
nbclient
,
fqdn
)
display_fields
=
[
"
name
"
,
"
enabled
"
,
"
mark_connected
"
,
"
custom_fields
"
,
"
lag
"
,
"
speed
"
]
iface_list
=
[]
for
iface
in
interface_list
:
iface_list
.
append
(
dict
(
iface
))
table
=
convert_to_table
(
iface_list
,
display_fields
)
print
(
table
)
@list.command
()
def
devices
():
click
.
echo
(
"
Listing all devices:
"
)
device_list
=
nc
.
get_all_devices
(
nbclient
)
display_fields
=
[
"
name
"
,
"
device_type
"
]
devices
=
[]
for
device
in
device_list
:
devices
.
append
(
dict
(
device
))
table
=
convert_to_table
(
devices
,
display_fields
)
print
(
table
)
list
.
add_command
(
interfaces
)
list
.
add_command
(
devices
)
# Define here attach command
@cli.group
()
def
attach
():
pass
@attach.command
()
@click.option
(
'
--fqdn
'
,
help
=
'
Device name where to attach interface to lag
'
)
@click.option
(
'
--iface
'
,
help
=
'
Interface name to attach to lag
'
)
@click.option
(
'
--lag
'
,
help
=
'
LAG name to attach interface
'
)
def
interface
(
fqdn
:
str
,
iface
:
str
,
lag
:
str
):
click
.
echo
(
f
"
Attaching interface to lag: device =
{
fqdn
}
, interface name=
{
iface
}
to lag=
{
lag
}
"
)
new_iface
=
nc
.
attach_interface_to_lag
(
nbclient
,
fqdn
,
lag
,
iface
)
print
(
new_iface
)
attach
.
add_command
(
interface
)
if
__name__
==
'
__main__
'
:
cli
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment