lemur/lemur/destinations/views.py

451 lines
15 KiB
Python
Raw Normal View History

2015-06-22 22:47:27 +02:00
"""
.. module: lemur.destinations.views
2015-06-22 22:47:27 +02:00
:platform: Unix
:synopsis: This module contains all of the accounts view code.
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more
2015-06-22 22:47:27 +02:00
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
from flask import Blueprint
2016-11-23 06:11:20 +01:00
from flask_restful import Api, reqparse
from lemur.destinations import service
2015-06-22 22:47:27 +02:00
from lemur.auth.service import AuthenticatedResource
from lemur.auth.permissions import admin_permission
2016-05-10 22:43:26 +02:00
from lemur.common.utils import paginated_parser
from lemur.common.schema import validate_schema
2019-05-16 16:57:02 +02:00
from lemur.destinations.schemas import (
destinations_output_schema,
destination_input_schema,
destination_output_schema,
)
2015-06-22 22:47:27 +02:00
2019-05-16 16:57:02 +02:00
mod = Blueprint("destinations", __name__)
2015-06-22 22:47:27 +02:00
api = Api(mod)
class DestinationsList(AuthenticatedResource):
""" Defines the 'destinations' endpoint """
2019-05-16 16:57:02 +02:00
2015-06-22 22:47:27 +02:00
def __init__(self):
self.reqparse = reqparse.RequestParser()
super(DestinationsList, self).__init__()
2015-06-22 22:47:27 +02:00
2016-05-10 22:43:26 +02:00
@validate_schema(None, destinations_output_schema)
2015-06-22 22:47:27 +02:00
def get(self):
"""
.. http:get:: /destinations
2015-06-22 22:47:27 +02:00
The current account list
**Example request**:
.. sourcecode:: http
GET /destinations HTTP/1.1
2015-06-22 22:47:27 +02:00
Host: example.com
Accept: application/json, text/javascript
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Vary: Accept
Content-Type: text/javascript
{
"items": [{
"description": "test",
"options": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
}
"total": 1
2015-06-22 22:47:27 +02:00
}
:query sortBy: field to sort on
:query sortDir: asc or desc
2015-06-22 22:47:27 +02:00
:query page: int. default is 1
2016-01-29 20:47:16 +01:00
:query filter: key value pair format is k;v
:query count: count number default is 10
2015-06-22 22:47:27 +02:00
:reqheader Authorization: OAuth token to authenticate
:statuscode 200: no error
"""
parser = paginated_parser.copy()
args = parser.parse_args()
return service.render(args)
@admin_permission.require(http_exception=403)
2016-05-10 22:43:26 +02:00
@validate_schema(destination_input_schema, destination_output_schema)
def post(self, data=None):
2015-06-22 22:47:27 +02:00
"""
.. http:post:: /destinations
2015-06-22 22:47:27 +02:00
Creates a new account
**Example request**:
.. sourcecode:: http
POST /destinations HTTP/1.1
2015-06-22 22:47:27 +02:00
Host: example.com
Accept: application/json, text/javascript
Content-Type: application/json;charset=UTF-8
2015-06-22 22:47:27 +02:00
{
"description": "test33",
"options": [{
"name": "accountNumber",
"required": true,
"value": "34324324",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "34324324",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
2015-06-22 22:47:27 +02:00
}
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Vary: Accept
Content-Type: text/javascript
{
"description": "test33",
"options": [{
"name": "accountNumber",
"required": true,
"value": "34324324",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
2015-06-22 22:47:27 +02:00
}
:arg label: human readable account label
:arg description: some description about the account
2015-06-22 22:47:27 +02:00
:reqheader Authorization: OAuth token to authenticate
:statuscode 200: no error
"""
2019-05-16 16:57:02 +02:00
return service.create(
data["label"],
data["plugin"]["slug"],
data["plugin"]["plugin_options"],
data["description"],
)
2015-06-22 22:47:27 +02:00
class Destinations(AuthenticatedResource):
2015-06-22 22:47:27 +02:00
def __init__(self):
self.reqparse = reqparse.RequestParser()
super(Destinations, self).__init__()
2015-06-22 22:47:27 +02:00
2016-05-10 22:43:26 +02:00
@validate_schema(None, destination_output_schema)
def get(self, destination_id):
2015-06-22 22:47:27 +02:00
"""
.. http:get:: /destinations/1
2015-06-22 22:47:27 +02:00
Get a specific account
**Example request**:
.. sourcecode:: http
GET /destinations/1 HTTP/1.1
2015-06-22 22:47:27 +02:00
Host: example.com
Accept: application/json, text/javascript
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Vary: Accept
Content-Type: text/javascript
{
"description": "test",
"options": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
2015-06-22 22:47:27 +02:00
}
:reqheader Authorization: OAuth token to authenticate
:statuscode 200: no error
"""
return service.get(destination_id)
2015-06-22 22:47:27 +02:00
@admin_permission.require(http_exception=403)
2016-05-10 22:43:26 +02:00
@validate_schema(destination_input_schema, destination_output_schema)
def put(self, destination_id, data=None):
2015-06-22 22:47:27 +02:00
"""
.. http:put:: /destinations/1
2015-06-22 22:47:27 +02:00
Updates an account
**Example request**:
.. sourcecode:: http
POST /destinations/1 HTTP/1.1
2015-06-22 22:47:27 +02:00
Host: example.com
Accept: application/json, text/javascript
Content-Type: application/json;charset=UTF-8
2015-06-22 22:47:27 +02:00
{
"description": "test33",
"options": [{
"name": "accountNumber",
"required": true,
"value": "34324324",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "34324324",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
}
2015-06-22 22:47:27 +02:00
2015-06-22 22:47:27 +02:00
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Vary: Accept
Content-Type: text/javascript
{
"description": "test",
"options": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
2015-06-22 22:47:27 +02:00
}
:arg accountNumber: aws account number
:arg label: human readable account label
:arg description: some description about the account
2015-06-22 22:47:27 +02:00
:reqheader Authorization: OAuth token to authenticate
:statuscode 200: no error
"""
2019-05-16 16:57:02 +02:00
return service.update(
destination_id,
data["label"],
data["plugin"]["slug"],
2019-05-16 16:57:02 +02:00
data["plugin"]["plugin_options"],
data["description"],
)
2015-06-22 22:47:27 +02:00
@admin_permission.require(http_exception=403)
def delete(self, destination_id):
service.delete(destination_id)
2019-05-16 16:57:02 +02:00
return {"result": True}
2015-06-22 22:47:27 +02:00
class CertificateDestinations(AuthenticatedResource):
""" Defines the 'certificate/<int:certificate_id/destinations'' endpoint """
2019-05-16 16:57:02 +02:00
2015-06-22 22:47:27 +02:00
def __init__(self):
super(CertificateDestinations, self).__init__()
2015-06-22 22:47:27 +02:00
2016-05-10 22:43:26 +02:00
@validate_schema(None, destination_output_schema)
2015-06-22 22:47:27 +02:00
def get(self, certificate_id):
"""
.. http:get:: /certificates/1/destinations
2015-06-22 22:47:27 +02:00
The current account list for a given certificates
**Example request**:
.. sourcecode:: http
GET /certificates/1/destinations HTTP/1.1
2015-06-22 22:47:27 +02:00
Host: example.com
Accept: application/json, text/javascript
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Vary: Accept
Content-Type: text/javascript
{
"items": [{
"description": "test",
"options": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"id": 4,
"plugin": {
"pluginOptions": [{
"name": "accountNumber",
"required": true,
"value": "111111111111111",
"helpMessage": "Must be a valid AWS account number!",
"validation": "/^[0-9]{12,12}$/",
"type": "str"
}],
"description": "Allow the uploading of certificates to AWS IAM",
"slug": "aws-destination",
"title": "AWS"
},
"label": "test546"
}
"total": 1
}
2015-06-22 22:47:27 +02:00
:query sortBy: field to sort on
:query sortDir: asc or desc
2016-01-29 20:47:16 +01:00
:query page: int default is 1
:query filter: key value pair format is k;v
:query count: count number default is 10
2015-06-22 22:47:27 +02:00
:reqheader Authorization: OAuth token to authenticate
:statuscode 200: no error
"""
parser = paginated_parser.copy()
args = parser.parse_args()
2019-05-16 16:57:02 +02:00
args["certificate_id"] = certificate_id
2015-06-22 22:47:27 +02:00
return service.render(args)
class DestinationsStats(AuthenticatedResource):
2021-02-22 23:56:34 +01:00
""" Defines the 'destinations' stats endpoint """
2019-05-16 16:57:02 +02:00
def __init__(self):
self.reqparse = reqparse.RequestParser()
super(DestinationsStats, self).__init__()
def get(self):
2019-05-16 16:57:02 +02:00
self.reqparse.add_argument("metric", type=str, location="args")
args = self.reqparse.parse_args()
items = service.stats(**args)
return dict(items=items, total=len(items))
2019-05-16 16:57:02 +02:00
api.add_resource(DestinationsList, "/destinations", endpoint="destinations")
api.add_resource(
Destinations, "/destinations/<int:destination_id>", endpoint="destination"
)
api.add_resource(
CertificateDestinations,
"/certificates/<int:certificate_id>/destinations",
endpoint="certificateDestinations",
)
api.add_resource(DestinationsStats, "/destinations/stats", endpoint="destinationStats")