Merge "add bulk edit hosts api" into dev/experimental
This commit is contained in:
commit
2e3bb99460
@ -114,6 +114,18 @@ def _get_request_data():
|
||||
return {}
|
||||
|
||||
|
||||
def _get_request_data_as_list():
|
||||
if request.data:
|
||||
try:
|
||||
return json.loads(request.data)
|
||||
except Exception:
|
||||
raise exception_handler.BadRequest(
|
||||
'request data is not json formatted: %s' % request.data
|
||||
)
|
||||
else:
|
||||
return []
|
||||
|
||||
|
||||
def _get_request_args():
|
||||
return dict(request.args)
|
||||
|
||||
@ -1846,6 +1858,20 @@ def update_host(host_id):
|
||||
)
|
||||
|
||||
|
||||
@app.route("/hosts", methods=['PUT'])
|
||||
@log_user_action
|
||||
@login_required
|
||||
def update_hosts():
|
||||
"""update hosts."""
|
||||
data = _get_request_data_as_list()
|
||||
return utils.make_json_response(
|
||||
200,
|
||||
host_api.update_hosts(
|
||||
current_user, data
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@app.route("/hosts/<int:host_id>", methods=['DELETE'])
|
||||
@log_user_action
|
||||
@login_required
|
||||
@ -1937,7 +1963,7 @@ def list_host_networks(host_id):
|
||||
)
|
||||
|
||||
|
||||
@app.route("/host-networks", methods=['GET'])
|
||||
@app.route("/host/networks", methods=['GET'])
|
||||
@log_user_action
|
||||
@login_required
|
||||
def list_hostnetworks():
|
||||
@ -1965,7 +1991,7 @@ def show_host_network(host_id, host_network_id):
|
||||
)
|
||||
|
||||
|
||||
@app.route("/host-networks/<int:host_network_id>", methods=['GET'])
|
||||
@app.route("/host/networks/<int:host_network_id>", methods=['GET'])
|
||||
@log_user_action
|
||||
@login_required
|
||||
def show_hostnetwork(host_network_id):
|
||||
@ -1990,6 +2016,17 @@ def add_host_network(host_id):
|
||||
)
|
||||
|
||||
|
||||
@app.route("/hosts/networks", methods=['POST'])
|
||||
@log_user_action
|
||||
@login_required
|
||||
def add_host_networks():
|
||||
"""add host networks."""
|
||||
data = _get_request_data_as_list()
|
||||
return utils.make_json_response(
|
||||
200, host_api.add_host_networks(current_user, data)
|
||||
)
|
||||
|
||||
|
||||
@app.route(
|
||||
"/hosts/<int:host_id>/networks/<int:host_network_id>",
|
||||
methods=['PUT']
|
||||
|
@ -1144,7 +1144,7 @@ def review_cluster(session, reviewer, cluster_id, review={}, **kwargs):
|
||||
clusterhosts = []
|
||||
for clusterhost in cluster.clusterhosts:
|
||||
if (
|
||||
clusterhost.id in clusterhost_ids or
|
||||
clusterhost.clusterhost_id in clusterhost_ids or
|
||||
clusterhost.host_id in host_ids
|
||||
):
|
||||
clusterhosts.append(clusterhost)
|
||||
|
@ -15,6 +15,7 @@
|
||||
"""Host database operations."""
|
||||
import functools
|
||||
import logging
|
||||
import netaddr
|
||||
|
||||
from compass.db.api import database
|
||||
from compass.db.api import metadata_holder as metadata_api
|
||||
@ -60,7 +61,7 @@ RESP_DEPLOYED_CONFIG_FIELDS = [
|
||||
RESP_DEPLOY_FIELDS = [
|
||||
'status', 'host'
|
||||
]
|
||||
UPDATED_FIELDS = ['name', 'reinstall_os']
|
||||
UPDATED_FIELDS = ['host_id', 'name', 'reinstall_os']
|
||||
UPDATED_CONFIG_FIELDS = [
|
||||
'put_os_config'
|
||||
]
|
||||
@ -244,13 +245,9 @@ def validate_host(session, host):
|
||||
|
||||
|
||||
@utils.supported_filters(optional_support_keys=UPDATED_FIELDS)
|
||||
@database.run_in_session()
|
||||
@user_api.check_user_permission_in_session(
|
||||
permission.PERMISSION_UPDATE_HOST
|
||||
)
|
||||
@utils.wrap_to_dict(RESP_FIELDS)
|
||||
def update_host(session, updater, host_id, **kwargs):
|
||||
"""Update a host."""
|
||||
def _update_host(session, updater, host_id, **kwargs):
|
||||
"""Update a host internal."""
|
||||
host = utils.get_db_object(
|
||||
session, models.Host, id=host_id
|
||||
)
|
||||
@ -261,6 +258,26 @@ def update_host(session, updater, host_id, **kwargs):
|
||||
return utils.update_db_object(session, host, **kwargs)
|
||||
|
||||
|
||||
@database.run_in_session()
|
||||
@user_api.check_user_permission_in_session(
|
||||
permission.PERMISSION_UPDATE_HOST
|
||||
)
|
||||
def update_host(session, updater, host_id, **kwargs):
|
||||
"""Update a host."""
|
||||
return _update_host(session, updater, host_id=host_id, **kwargs)
|
||||
|
||||
|
||||
@database.run_in_session()
|
||||
@user_api.check_user_permission_in_session(
|
||||
permission.PERMISSION_UPDATE_HOST
|
||||
)
|
||||
def update_hosts(session, updater, data=[]):
|
||||
hosts = []
|
||||
for host_data in data:
|
||||
hosts.append(_update_host(session, updater, **host_data))
|
||||
return hosts
|
||||
|
||||
|
||||
@utils.supported_filters([])
|
||||
@database.run_in_session()
|
||||
@user_api.check_user_permission_in_session(
|
||||
@ -475,28 +492,71 @@ def get_hostnetwork(session, getter, host_network_id, **kwargs):
|
||||
@utils.input_validates(
|
||||
ip=utils.check_ip
|
||||
)
|
||||
@utils.wrap_to_dict(RESP_NETWORK_FIELDS)
|
||||
def _add_host_network(
|
||||
session, creator, host_id, exception_when_existing=True,
|
||||
interface=None, ip=None, **kwargs
|
||||
):
|
||||
host = utils.get_db_object(
|
||||
session, models.Host, id=host_id
|
||||
)
|
||||
is_host_editable(session, host, creator)
|
||||
ip_int = long(netaddr.IPAddress(ip))
|
||||
host_network = utils.get_db_object(
|
||||
session, models.HostNetwork, False,
|
||||
ip_int=ip_int
|
||||
)
|
||||
if host_network:
|
||||
raise exception.InvalidParameter(
|
||||
'ip %s exists in database' % ip
|
||||
)
|
||||
return utils.add_db_object(
|
||||
session, models.HostNetwork,
|
||||
exception_when_existing,
|
||||
host_id, interface, ip=ip, **kwargs
|
||||
)
|
||||
|
||||
|
||||
@database.run_in_session()
|
||||
@user_api.check_user_permission_in_session(
|
||||
permission.PERMISSION_ADD_HOST_NETWORK
|
||||
)
|
||||
@utils.wrap_to_dict(RESP_NETWORK_FIELDS)
|
||||
def add_host_network(
|
||||
session, creator, host_id,
|
||||
exception_when_existing=True,
|
||||
interface=None, **kwargs
|
||||
):
|
||||
"""Create a host network."""
|
||||
host = utils.get_db_object(
|
||||
session, models.Host, id=host_id
|
||||
)
|
||||
is_host_editable(session, host, creator)
|
||||
return utils.add_db_object(
|
||||
session, models.HostNetwork,
|
||||
exception_when_existing,
|
||||
host_id, interface, **kwargs
|
||||
return _add_host_network(
|
||||
session, creator, host_id, exception_when_existing,
|
||||
interface=interface, **kwargs
|
||||
)
|
||||
|
||||
|
||||
@database.run_in_session()
|
||||
@user_api.check_user_permission_in_session(
|
||||
permission.PERMISSION_ADD_HOST_NETWORK
|
||||
)
|
||||
def add_host_networks(
|
||||
session, creator,
|
||||
exception_when_existing=True,
|
||||
data=[]
|
||||
):
|
||||
"""Create host networks."""
|
||||
hosts = []
|
||||
for host_data in data:
|
||||
host_id = host_data['host_id']
|
||||
networks = host_data['networks']
|
||||
host_networks = []
|
||||
hosts.append({'host_id': host_id, 'networks': host_networks})
|
||||
for network in networks:
|
||||
host_networks.append(_add_host_network(
|
||||
session, creator, host_id, exception_when_existing,
|
||||
**network
|
||||
))
|
||||
return hosts
|
||||
|
||||
|
||||
@utils.supported_filters(
|
||||
optional_support_keys=UPDATED_NETWORK_FIELDS,
|
||||
ignore_support_keys=IGNORED_NETWORK_FIELDS
|
||||
|
Loading…
x
Reference in New Issue
Block a user