Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csentry
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
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
Code review analytics
Issue 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
Anders Harrisson
csentry
Commits
b5e4d2c1
Commit
b5e4d2c1
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Add interfaces filtering by domain
parent
226f186c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/api/network.py
+9
-1
9 additions, 1 deletion
app/api/network.py
app/api/utils.py
+8
-5
8 additions, 5 deletions
app/api/utils.py
tests/functional/test_api.py
+22
-0
22 additions, 0 deletions
tests/functional/test_api.py
with
39 additions
and
6 deletions
app/api/network.py
+
9
−
1
View file @
b5e4d2c1
...
@@ -9,7 +9,7 @@ This module implements the network API.
...
@@ -9,7 +9,7 @@ This module implements the network API.
:license: BSD 2-Clause, see LICENSE for more details.
:license: BSD 2-Clause, see LICENSE for more details.
"""
"""
from
flask
import
Blueprint
from
flask
import
Blueprint
,
request
from
flask_jwt_extended
import
jwt_required
from
flask_jwt_extended
import
jwt_required
from
..
import
models
from
..
import
models
from
..decorators
import
jwt_groups_accepted
from
..decorators
import
jwt_groups_accepted
...
@@ -89,6 +89,14 @@ def get_interfaces():
...
@@ -89,6 +89,14 @@ def get_interfaces():
.. :quickref: Network; Get interfaces
.. :quickref: Network; Get interfaces
"""
"""
domain
=
request
.
args
.
get
(
'
domain
'
,
None
)
if
domain
is
not
None
:
query
=
models
.
Interface
.
query
query
=
query
.
join
(
models
.
Interface
.
network
).
join
(
models
.
Network
.
domain
).
filter
(
models
.
Domain
.
name
==
domain
)
query
=
query
.
order_by
(
models
.
Interface
.
ip
)
return
get_generic_model
(
model
=
None
,
query
=
query
)
return
get_generic_model
(
models
.
Interface
,
return
get_generic_model
(
models
.
Interface
,
order_by
=
models
.
Interface
.
ip
)
order_by
=
models
.
Interface
.
ip
)
...
...
This diff is collapsed.
Click to expand it.
app/api/utils.py
+
8
−
5
View file @
b5e4d2c1
...
@@ -59,20 +59,23 @@ def build_pagination_header(pagination, base_url, **kwargs):
...
@@ -59,20 +59,23 @@ def build_pagination_header(pagination, base_url, **kwargs):
return
header
return
header
def
get_generic_model
(
model
,
order_by
=
None
):
def
get_generic_model
(
model
,
order_by
=
None
,
query
=
None
):
"""
Return data from model as json
"""
Return data from model as json
:param model: model class
:param model: model class
:param order_by: column to order the result by
:param order_by: column to order the result by
:param query: optional query to use (for more complex queries)
:returns: data from model as json
:returns: data from model as json
"""
"""
kwargs
=
request
.
args
.
to_dict
()
kwargs
=
request
.
args
.
to_dict
()
page
=
int
(
kwargs
.
pop
(
'
page
'
,
1
))
page
=
int
(
kwargs
.
pop
(
'
page
'
,
1
))
per_page
=
int
(
kwargs
.
pop
(
'
per_page
'
,
20
))
per_page
=
int
(
kwargs
.
pop
(
'
per_page
'
,
20
))
query
=
utils
.
get_query
(
model
.
query
,
**
kwargs
)
if
query
is
None
:
if
order_by
is
None
:
query
=
utils
.
get_query
(
model
.
query
,
**
kwargs
)
order_by
=
getattr
(
model
,
'
name
'
)
if
order_by
is
None
:
pagination
=
query
.
order_by
(
order_by
).
paginate
(
page
,
per_page
)
order_by
=
getattr
(
model
,
'
name
'
)
query
=
query
.
order_by
(
order_by
)
pagination
=
query
.
paginate
(
page
,
per_page
)
data
=
[
item
.
to_dict
()
for
item
in
pagination
.
items
]
data
=
[
item
.
to_dict
()
for
item
in
pagination
.
items
]
header
=
build_pagination_header
(
pagination
,
request
.
base_url
,
**
kwargs
)
header
=
build_pagination_header
(
pagination
,
request
.
base_url
,
**
kwargs
)
return
jsonify
(
data
),
200
,
header
return
jsonify
(
data
),
200
,
header
...
...
This diff is collapsed.
Click to expand it.
tests/functional/test_api.py
+
22
−
0
View file @
b5e4d2c1
...
@@ -598,6 +598,28 @@ def test_get_interfaces(client, network_factory, interface_factory, readonly_tok
...
@@ -598,6 +598,28 @@ def test_get_interfaces(client, network_factory, interface_factory, readonly_tok
check_input_is_subset_of_response
(
response
,
(
interface3
.
to_dict
(),))
check_input_is_subset_of_response
(
response
,
(
interface3
.
to_dict
(),))
def
test_get_interfaces_by_domain
(
client
,
domain_factory
,
network_factory
,
interface_factory
,
readonly_token
):
# Create some interfaces
domain1
=
domain_factory
(
name
=
'
tn.esss.lu.se
'
)
domain2
=
domain_factory
(
name
=
'
ics.esss.lu.se
'
)
network1
=
network_factory
(
address
=
'
192.168.1.0/24
'
,
first_ip
=
'
192.168.1.10
'
,
last_ip
=
'
192.168.1.250
'
,
domain
=
domain1
)
network2
=
network_factory
(
address
=
'
192.168.2.0/24
'
,
first_ip
=
'
192.168.2.10
'
,
last_ip
=
'
192.168.2.250
'
,
domain
=
domain2
)
interface1
=
interface_factory
(
network
=
network1
,
ip
=
'
192.168.1.10
'
)
interface2
=
interface_factory
(
network
=
network1
,
ip
=
'
192.168.1.11
'
,
name
=
'
interface2
'
)
interface3
=
interface_factory
(
network
=
network2
,
ip
=
'
192.168.2.10
'
)
# test filtering by domain
response
=
get
(
client
,
f
'
{
API_URL
}
/network/interfaces?domain=tn.esss.lu.se
'
,
token
=
readonly_token
)
assert
response
.
status_code
==
200
assert
len
(
response
.
json
)
==
2
check_input_is_subset_of_response
(
response
,
(
interface1
.
to_dict
(),
interface2
.
to_dict
()))
response
=
get
(
client
,
f
'
{
API_URL
}
/network/interfaces?domain=ics.esss.lu.se
'
,
token
=
readonly_token
)
assert
response
.
status_code
==
200
assert
len
(
response
.
json
)
==
1
check_input_is_subset_of_response
(
response
,
(
interface3
.
to_dict
(),))
def
test_create_interface
(
client
,
network_factory
,
user_token
):
def
test_create_interface
(
client
,
network_factory
,
user_token
):
network
=
network_factory
(
address
=
'
192.168.1.0/24
'
,
first_ip
=
'
192.168.1.10
'
,
last_ip
=
'
192.168.1.250
'
)
network
=
network_factory
(
address
=
'
192.168.1.0/24
'
,
first_ip
=
'
192.168.1.10
'
,
last_ip
=
'
192.168.1.250
'
)
# check that network_id and ip are mandatory
# check that network_id and ip are mandatory
...
...
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