def _add_host(host):
    """
    Add or update a host

    Required parameters:
     - at least one of the canonical facts fields is required
     - account number
    """
    validated_input_host_dict = HostSchema(strict=True).load(host)

    input_host = Host.from_json(validated_input_host_dict.data)

    if not current_identity.is_trusted_system and current_identity.account_number != input_host.account:
        raise InventoryException(
            title="Invalid request",
            detail=
            "The account number associated with the user does not match the account number associated with the "
            "host",
        )

    existing_host = find_existing_host(input_host.account,
                                       input_host.canonical_facts)

    if existing_host:
        return update_existing_host(existing_host, input_host)
    else:
        return create_new_host(input_host)
Example #2
0
def test_host_schema_valid_tags(tags):
    host = {
        "fqdn": "fred.flintstone.com",
        "display_name": "display_name",
        "account": "00102",
        "tags": tags
    }
    validated_host = HostSchema(strict=True).load(host)

    assert validated_host.data["tags"] == tags
Example #3
0
def test_host_schema_invalid_tags(tags):
    host = {
        "fqdn": "fred.flintstone.com",
        "display_name": "display_name",
        "account": "00102",
        "tags": tags
    }
    with pytest.raises(ValidationError) as excinfo:
        _ = HostSchema(strict=True).load(host)

    assert "Missing data for required field" in str(excinfo.value)
Example #4
0
def test_host_schema_timezone_enforced():
    host = {
        "fqdn": "scooby.doo.com",
        "display_name": "display_name",
        "account": "00102",
        "stale_timestamp": "2020-03-31T10:10:06.754201",
        "reporter": "test",
    }
    with pytest.raises(ValidationError) as excinfo:
        _ = HostSchema(strict=True).load(host)

    assert "Timestamp must contain timezone info" in str(excinfo.value)
Example #5
0
def test_host_schema_valid_tags(tags):
    host = {
        "fqdn": "fred.flintstone.com",
        "display_name": "display_name",
        "account": "00102",
        "tags": tags,
        "stale_timestamp": "2019-12-16T10:10:06.754201+00:00",
        "reporter": "test",
    }
    validated_host = HostSchema(strict=True).load(host)

    assert validated_host.data["tags"] == tags
Example #6
0
def test_host_schema_timezone_enforced():
    host = {
        "fqdn": "scooby.doo.com",
        "display_name": "display_name",
        "account": USER_IDENTITY["account_number"],
        "stale_timestamp": now().replace(tzinfo=None).isoformat(),
        "reporter": "test",
    }
    with pytest.raises(MarshmallowValidationError) as exception:
        HostSchema().load(host)

    assert "Timestamp must contain timezone info" in str(exception.value)
Example #7
0
def test_host_schema_valid_tags(tags):
    host = {
        "fqdn": "fred.flintstone.com",
        "display_name": "display_name",
        "account": USER_IDENTITY["account_number"],
        "tags": tags,
        "stale_timestamp": now().isoformat(),
        "reporter": "test",
    }
    validated_host = HostSchema().load(host)

    assert validated_host["tags"] == tags
Example #8
0
def deserialize_host(raw_data):
    try:
        validated_data = HostSchema(strict=True).load(raw_data).data
    except ValidationError as e:
        raise ValidationException(str(e.messages)) from None

    canonical_facts = _deserialize_canonical_facts(validated_data)
    facts = _deserialize_facts(validated_data.get("facts"))
    tags = _deserialize_tags(validated_data.get("tags"))
    return Host(
        canonical_facts,
        validated_data.get("display_name"),
        validated_data.get("ansible_host"),
        validated_data.get("account"),
        facts,
        tags,
        validated_data.get("system_profile", {}),
        validated_data.get("stale_timestamp"),
        validated_data.get("reporter"),
    )
Example #9
0
def test_host_schema_invalid_tags(tags):
    host = {
        "fqdn": "fred.flintstone.com",
        "display_name": "display_name",
        "account": USER_IDENTITY["account_number"],
        "tags": tags,
        "stale_timestamp": now().isoformat(),
        "reporter": "test",
    }
    with pytest.raises(MarshmallowValidationError) as exception:
        HostSchema().load(host)

    error_messages = exception.value.normalized_messages()
    assert "tags" in error_messages
    assert error_messages["tags"] == {0: {"key": ["Missing data for required field."]}}
Example #10
0
from flask import abort, g, jsonify, request, url_for
from flask_restful import Api, Resource
import json
from app import db
from app.utils import bad_request, normal_request, query_request
from app.models import SubappModel, SubappSchema, InstanceModel, InstanceSchema, HostModel, HostSchema

subapps_schema = SubappSchema(many=True)
subapp_schema = SubappSchema()
host_schema = HostSchema()
instance_schema = InstanceSchema()


class Agents(Resource):
    def get(self):
        # 返回所有数据
        pass

    def post(self):
        data = request.get_json()
        host_info = data['host']
        subapp_list = data['app']
        hdbapp_list = data['hdb'] if "hdb" in data else []
        """
        {
            "host": {
                "cpu": 16,
                "mem": 135204020224,
                "swap": 68728909824,
                "hostname": "s4ides1",
                "ip": [
Example #11
0
from flask import abort, g, jsonify, request, url_for
from flask_restful import Api, Resource
from flask_jwt_extended import jwt_required

from app import db
from app.utils import bad_request, normal_request, query_request
from app.models import HostModel, HostSchema
from app.models import InstanceModel, InstanceSchema

host_schema = HostSchema()
hosts_schema = HostSchema(many=True)
instance_schema = InstanceSchema()
instances_schema = InstanceSchema(many=True)


class Hosts(Resource):
    # @jwt_required
    def get(self):
        # 返回所有数据
        page = request.args.get("page", 1, type=int)
        pagesize = min(request.args.get("limit", 50, type=int), 100)
        data = HostModel.query.paginate(page, pagesize)
        datacount = HostModel.query.count()
        hosts_result = hosts_schema.dump(data.items)
        return query_request({'rows': hosts_result, 'count': datacount})

    def post(self):
        # 新增数据
        data = request.get_json()
        hostschema = host_schema.load(data)
        host = HostModel(**hostschema)