Add webhook api
Add a new webhook api and its microversion is 1.4 Partially Implements: blueprint event-driven-optimization-based Change-Id: I50f7c824e52f3c5fc775d5064898ed422e375a99
This commit is contained in:
@@ -38,6 +38,7 @@ app = {
|
||||
'enable_acl': True,
|
||||
'acl_public_routes': [
|
||||
'/',
|
||||
'/v1/webhooks/.*',
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ from watcher.api.controllers.v1 import service
|
||||
from watcher.api.controllers.v1 import strategy
|
||||
from watcher.api.controllers.v1 import utils
|
||||
from watcher.api.controllers.v1 import versions
|
||||
from watcher.api.controllers.v1 import webhooks
|
||||
|
||||
|
||||
def min_version():
|
||||
@@ -131,6 +132,9 @@ class V1(APIBase):
|
||||
services = [link.Link]
|
||||
"""Links to the services resource"""
|
||||
|
||||
webhooks = [link.Link]
|
||||
"""Links to the webhooks resource"""
|
||||
|
||||
links = [link.Link]
|
||||
"""Links that point to a specific URL for this version and documentation"""
|
||||
|
||||
@@ -202,6 +206,14 @@ class V1(APIBase):
|
||||
'services', '',
|
||||
bookmark=True)
|
||||
]
|
||||
if utils.allow_webhook_api():
|
||||
v1.webhooks = [link.Link.make_link(
|
||||
'self', base_url, 'webhooks', ''),
|
||||
link.Link.make_link('bookmark',
|
||||
base_url,
|
||||
'webhooks', '',
|
||||
bookmark=True)
|
||||
]
|
||||
return v1
|
||||
|
||||
|
||||
@@ -217,6 +229,7 @@ class Controller(rest.RestController):
|
||||
services = service.ServicesController()
|
||||
strategies = strategy.StrategiesController()
|
||||
data_model = data_model.DataModelController()
|
||||
webhooks = webhooks.WebhookController()
|
||||
|
||||
@wsme_pecan.wsexpose(V1)
|
||||
def get(self):
|
||||
|
||||
@@ -185,3 +185,12 @@ def allow_list_datamodel():
|
||||
"""
|
||||
return pecan.request.version.minor >= (
|
||||
versions.VERSIONS.MINOR_3_DATAMODEL.value)
|
||||
|
||||
|
||||
def allow_webhook_api():
|
||||
"""Check if we should support webhook API.
|
||||
|
||||
Version 1.4 of the API added support to trigger webhook.
|
||||
"""
|
||||
return pecan.request.version.minor >= (
|
||||
versions.VERSIONS.MINOR_4_WEBHOOK_API.value)
|
||||
|
||||
@@ -22,7 +22,8 @@ class VERSIONS(enum.Enum):
|
||||
MINOR_1_START_END_TIMING = 1 # v1.1: Add start/end timei for audit
|
||||
MINOR_2_FORCE = 2 # v1.2: Add force field to audit
|
||||
MINOR_3_DATAMODEL = 3 # v1.3: Add list datamodel API
|
||||
MINOR_MAX_VERSION = 3
|
||||
MINOR_4_WEBHOOK_API = 4 # v1.4: Add webhook trigger API
|
||||
MINOR_MAX_VERSION = 4
|
||||
|
||||
# This is the version 1 API
|
||||
BASE_VERSION = 1
|
||||
|
||||
62
watcher/api/controllers/v1/webhooks.py
Normal file
62
watcher/api/controllers/v1/webhooks.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# 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.
|
||||
|
||||
"""
|
||||
Webhook endpoint for Watcher v1 REST API.
|
||||
"""
|
||||
|
||||
from oslo_log import log
|
||||
import pecan
|
||||
from pecan import rest
|
||||
from wsme import types as wtypes
|
||||
import wsmeext.pecan as wsme_pecan
|
||||
|
||||
from watcher.api.controllers.v1 import types
|
||||
from watcher.api.controllers.v1 import utils
|
||||
from watcher.common import exception
|
||||
from watcher.decision_engine import rpcapi
|
||||
from watcher import objects
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
class WebhookController(rest.RestController):
|
||||
"""REST controller for webhooks resource."""
|
||||
def __init__(self):
|
||||
super(WebhookController, self).__init__()
|
||||
self.dc_client = rpcapi.DecisionEngineAPI()
|
||||
|
||||
@wsme_pecan.wsexpose(None, wtypes.text, body=types.jsontype,
|
||||
status_code=202)
|
||||
def post(self, audit_ident, body):
|
||||
"""Trigger the given audit.
|
||||
|
||||
:param audit_ident: UUID or name of an audit.
|
||||
"""
|
||||
|
||||
LOG.debug("Webhook trigger Audit: %s.", audit_ident)
|
||||
|
||||
context = pecan.request.context
|
||||
audit = utils.get_resource('Audit', audit_ident)
|
||||
if audit is None:
|
||||
raise exception.AuditNotFound(audit=audit_ident)
|
||||
if audit.audit_type != objects.audit.AuditType.EVENT.value:
|
||||
raise exception.AuditTypeNotAllowed(audit_type=audit.audit_type)
|
||||
allowed_state = (
|
||||
objects.audit.State.PENDING,
|
||||
objects.audit.State.SUCCEEDED,
|
||||
)
|
||||
if audit.state not in allowed_state:
|
||||
raise exception.AuditStateNotAllowed(state=audit.state)
|
||||
|
||||
# trigger decision-engine to run the audit
|
||||
self.dc_client.trigger_audit(context, audit.uuid)
|
||||
Reference in New Issue
Block a user