From da84af2d0f27be0994299464b2addc0af8c4009d Mon Sep 17 00:00:00 2001 From: Scott Hussey Date: Wed, 18 Oct 2017 13:56:16 -0500 Subject: [PATCH] Add node enquiry API Provide an API endpoint in Drydock to query the state of nodes in the downstream MAAS service. Change-Id: I7cf4f3cf81e51ef5dc604c778d6a03d7f1208ffa --- drydock_provisioner/control/api.py | 6 +++- drydock_provisioner/control/nodes.py | 48 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 drydock_provisioner/control/nodes.py diff --git a/drydock_provisioner/control/api.py b/drydock_provisioner/control/api.py index 28e14252..e31007eb 100644 --- a/drydock_provisioner/control/api.py +++ b/drydock_provisioner/control/api.py @@ -16,6 +16,7 @@ import falcon from .designs import * from .tasks import * from .bootdata import * +from .nodes import NodesResource from .base import DrydockRequest from .middleware import AuthMiddleware, ContextMiddleware, LoggingMiddleware @@ -59,7 +60,10 @@ def start_api(state_manager=None, ingester=None, orchestrator=None): # API for nodes to discover their bootdata during curtin install ('/bootdata/{hostname}/{data_key}', BootdataResource( - state_manager=state_manager, orchestrator=orchestrator)) + state_manager=state_manager, orchestrator=orchestrator)), + + # API to list current MaaS nodes + ('/nodes', NodesResource()), ] for path, res in v1_0_routes: diff --git a/drydock_provisioner/control/nodes.py b/drydock_provisioner/control/nodes.py new file mode 100644 index 00000000..b8785ad4 --- /dev/null +++ b/drydock_provisioner/control/nodes.py @@ -0,0 +1,48 @@ +# Copyright 2017 AT&T Intellectual Property. All other rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import falcon +import json + +from drydock_provisioner import policy +from drydock_provisioner import config + +from drydock_provisioner.drivers.node.maasdriver.api_client import MaasRequestFactory +from drydock_provisioner.drivers.node.maasdriver.models.machine import Machines + +from .base import BaseResource + + +class NodesResource(BaseResource): + def __init__(self): + super().__init__() + + @policy.ApiEnforcer('physical_provisioner:read_data') + def on_get(self, req, resp): + try: + maas_client = MaasRequestFactory(config.config_mgr.conf.maasdriver.maas_api_url, + config.config_mgr.conf.maasdriver.maas_api_key) + + machine_list = Machines(maas_client) + machine_list.refresh() + + node_view = list() + for m in machine_list: + node_view.append(dict(hostname=m.hostname, memory=m.memory, cpu_count=m.cpu_count, status_name=m.status_name, boot_mac=m.boot_mac)) + + resp.body = json.dumps(node_view) + resp.status = falcon.HTTP_200 + except Exception as ex: + self.error(req.context, "Unknown error: %s" % str(ex), exc_info=ex) + self.return_error( + resp, falcon.HTTP_500, message="Unknown error", retry=False)