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
04bcf884
Commit
04bcf884
authored
7 months ago
by
Karel van Klink
Committed by
Neda Moeini
7 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Re-use session in LibreNMS client
parent
0e348068
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!253
LibreNMS retries
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gso/services/librenms_client.py
+18
-7
18 additions, 7 deletions
gso/services/librenms_client.py
with
18 additions
and
7 deletions
gso/services/librenms_client.py
+
18
−
7
View file @
04bcf884
...
...
@@ -3,10 +3,10 @@
import
logging
from
http
import
HTTPStatus
from
importlib
import
metadata
from
typing
import
Any
from
typing
import
Any
,
Literal
import
requests
from
requests
import
HTTPError
from
requests
import
HTTPError
,
Response
from
gso.settings
import
load_oss_params
from
gso.utils.helpers
import
SNMPVersion
...
...
@@ -25,12 +25,23 @@ class LibreNMSClient:
self
.
base_url
=
config
.
LIBRENMS
.
base_url
self
.
snmp_config
=
config
.
SNMP
self
.
headers
=
{
self
.
session
=
requests
.
Session
()
self
.
session
.
headers
.
update
({
"
User-Agent
"
:
f
"
geant-service-orchestrator/
{
metadata
.
version
(
"
geant-service-orchestrator
"
)
}
"
,
"
Accept
"
:
"
application/json
"
,
"
Content-Type
"
:
"
application/json
"
,
"
X-Auth-Token
"
:
token
,
}
})
def
_send_request
(
self
,
method
:
Literal
[
"
GET
"
,
"
POST
"
,
"
PUT
"
,
"
DELETE
"
],
endpoint
:
str
,
data
:
dict
[
str
,
Any
]
|
None
=
None
)
->
Response
:
url
=
self
.
base_url
+
endpoint
logger
.
debug
(
"
LibreNMS - Sending request
"
,
extra
=
{
"
method
"
:
method
,
"
endpoint
"
:
url
,
"
form_data
"
:
data
})
result
=
self
.
session
.
request
(
method
,
url
,
json
=
data
)
logger
.
debug
(
"
LibreNMS - Received response
"
,
extra
=
result
.
__dict__
)
return
result
def
get_device
(
self
,
fqdn
:
str
)
->
dict
[
str
,
Any
]:
"""
Get an existing device from LibreNMS.
...
...
@@ -39,7 +50,7 @@ class LibreNMSClient:
:return dict[str, Any]: A :term:`JSON` formatted list of devices that match the queried :term:`FQDN`.
:raises HTTPError: Raises an HTTP error 404 when the device is not found
"""
response
=
requests
.
get
(
f
"
{
self
.
base_url
}
/devices/
{
fqdn
}
"
,
headers
=
self
.
headers
,
timeout
=
(
0.5
,
75
)
)
response
=
self
.
_send_request
(
"
GET
"
,
f
"
/devices/
{
fqdn
}
"
)
response
.
raise_for_status
()
return
response
.
json
()
...
...
@@ -74,7 +85,7 @@ class LibreNMSClient:
}
device_data
.
update
(
getattr
(
self
.
snmp_config
,
snmp_version
))
device
=
requests
.
post
(
f
"
{
self
.
base_url
}
/devices
"
,
headers
=
self
.
headers
,
json
=
device_data
,
timeout
=
(
0.5
,
75
)
)
device
=
self
.
_send_request
(
"
POST
"
,
"
/devices
"
,
device_data
)
device
.
raise_for_status
()
return
device
.
json
()
...
...
@@ -86,7 +97,7 @@ class LibreNMSClient:
:return dict[str, Any]: A JSON representation of the device that got removed.
:raises HTTPError: Raises an exception if the request did not succeed.
"""
device
=
requests
.
delete
(
f
"
{
self
.
base_url
}
/devices/
{
fqdn
}
"
,
headers
=
self
.
headers
,
timeout
=
(
0.5
,
75
)
)
device
=
self
.
_send_request
(
"
DELETE
"
,
f
"
/devices/
{
fqdn
}
"
)
device
.
raise_for_status
()
return
device
.
json
()
...
...
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