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
1e684911
Commit
1e684911
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Add hosts API endpoint
parent
8e687e30
No related branches found
Branches containing commit
Tags
v0.5.2
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/api/network.py
+15
-0
15 additions, 0 deletions
app/api/network.py
tests/functional/test_api.py
+41
-2
41 additions, 2 deletions
tests/functional/test_api.py
with
56 additions
and
2 deletions
app/api/network.py
+
15
−
0
View file @
1e684911
...
...
@@ -65,6 +65,21 @@ def create_interface():
return
create_generic_model
(
models
.
Interface
,
mandatory_fields
=
(
'
network
'
,
'
ip
'
,
'
name
'
))
@bp.route
(
'
/hosts
'
)
@jwt_required
def
get_hosts
():
return
get_generic_model
(
models
.
Host
,
order_by
=
models
.
Host
.
name
)
@bp.route
(
'
/hosts
'
,
methods
=
[
'
POST
'
])
@jwt_required
@jwt_groups_accepted
(
'
admin
'
,
'
create
'
)
def
create_host
():
"""
Create a new host
"""
return
create_generic_model
(
models
.
Host
,
mandatory_fields
=
(
'
name
'
,))
@bp.route
(
'
/macs
'
)
@jwt_required
def
get_macs
():
...
...
This diff is collapsed.
Click to expand it.
tests/functional/test_api.py
+
41
−
2
View file @
1e684911
...
...
@@ -25,12 +25,13 @@ ENDPOINT_MODEL = {
'
inventory/items
'
:
models
.
Item
,
'
network/networks
'
:
models
.
Network
,
'
network/interfaces
'
:
models
.
Interface
,
'
network/hosts
'
:
models
.
Host
,
'
network/macs
'
:
models
.
Mac
,
}
GENERIC_GET_ENDPOINTS
=
[
key
for
key
in
ENDPOINT_MODEL
.
keys
()
if
key
not
in
(
'
inventory/items
'
,
'
network/macs
'
,
'
network/networks
'
,
'
netw
or
k
/i
n
te
rface
s
'
)
]
if
key
.
startswith
(
'
inventory
'
)
and
key
!=
'
invent
or
y
/ite
m
s
'
]
GENERIC_CREATE_ENDPOINTS
=
[
key
for
key
in
ENDPOINT_MODEL
.
keys
()
if
key
not
in
(
'
inventory/items
'
,
'
inventory/action
s
'
,
'
network/macs
'
,
'
network/networks
'
,
'
network/interface
s
'
)]
if
key
.
startswith
(
'
inventory
'
)
and
key
not
in
(
'
inventory/items
'
,
'
inventory/actions
'
)]
CREATE_AUTH_ENDPOINTS
=
[
key
for
key
in
ENDPOINT_MODEL
.
keys
()
if
key
!=
'
inventory/actions
'
]
...
...
@@ -695,3 +696,41 @@ def test_create_mac_invalid_address(address, client, user_token):
data
=
{
'
address
'
:
address
}
response
=
post
(
client
,
f
'
{
API_URL
}
/network/macs
'
,
data
=
data
,
token
=
user_token
)
check_response_message
(
response
,
f
"'
{
address
}
'
does not appear to be a MAC address
"
,
422
)
def
test_get_hosts
(
client
,
host_factory
,
readonly_token
):
# Create some hosts
host1
=
host_factory
()
host2
=
host_factory
()
response
=
get
(
client
,
f
'
{
API_URL
}
/network/hosts
'
,
token
=
readonly_token
)
assert
response
.
status_code
==
200
assert
len
(
response
.
json
)
==
2
check_input_is_subset_of_response
(
response
,
(
host1
.
to_dict
(),
host2
.
to_dict
()))
def
test_create_host
(
client
,
item_factory
,
user_token
):
item
=
item_factory
()
# check that name is mandatory
response
=
post
(
client
,
f
'
{
API_URL
}
/network/hosts
'
,
data
=
{},
token
=
user_token
)
check_response_message
(
response
,
"
Missing mandatory field
'
name
'"
,
422
)
data
=
{
'
name
'
:
'
my-hostname
'
}
response
=
post
(
client
,
f
'
{
API_URL
}
/network/hosts
'
,
data
=
data
,
token
=
user_token
)
assert
response
.
status_code
==
201
assert
{
'
id
'
,
'
name
'
,
'
type
'
,
'
description
'
,
'
item
'
,
'
interfaces
'
,
'
created_at
'
,
'
updated_at
'
,
'
user
'
}
==
set
(
response
.
json
.
keys
())
assert
response
.
json
[
'
name
'
]
==
data
[
'
name
'
]
# Check that name shall be unique
response
=
post
(
client
,
f
'
{
API_URL
}
/network/hosts
'
,
data
=
data
,
token
=
user_token
)
check_response_message
(
response
,
'
(psycopg2.IntegrityError) duplicate key value violates unique constraint
'
,
422
)
# Check that we can pass an item_id
data2
=
{
'
name
'
:
'
another-hostname
'
,
'
item_id
'
:
item
.
id
}
response
=
post
(
client
,
f
'
{
API_URL
}
/network/hosts
'
,
data
=
data2
,
token
=
user_token
)
assert
response
.
status_code
==
201
# check that all items were created
assert
models
.
Host
.
query
.
count
()
==
2
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