示例#1
0
文件: api.py 项目: otto-de/gatekeeper
def api_update_gate(group, name, environment=None):
    try:
        data = util.data_from_request()
        entry = blueprint.mongo.get_gate(group, name)

        if environment:
            if "state" in data:
                blueprint.mongo.set_gate(group, name, environment, data["state"])
            if "message" in data:
                blueprint.mongo.set_message(group, name, environment, data["message"])
        else:
            if "group" in data:
                entry["group"] = data["group"]
                blueprint.mongo.update_gate(group, name, entry)
            if "name" in data:
                entry["name"] = data["name"]
                blueprint.mongo.update_gate(group, name, entry)
                name = data["name"]
            if "environments" in data:
                if type(data['environments']) is list:
                    entry["environments"] = blueprint.mongo.get_environment_structure(data["environments"])
                    blueprint.mongo.update_gate(group, name, entry)
                else:
                    for env in data["environments"]:
                        if "state" in data["environments"][env]:
                            blueprint.mongo.set_gate(group, name, env, data["environments"][env]["state"])
                        if "message" in data["environments"][env]:
                            blueprint.mongo.set_message(group, name, env, data["environments"][env]["message"])
        entry = blueprint.mongo.get_gate(group, name)
        return Response(json.dumps(entry), status=200, mimetype='application/json')
    except (NotMasterError, ServiceNameNotValid, NotFound, GateStateNotValid, EnvironmentNotFound,
            JsonValidationError,
            JsonStructureError) as error:
        return error_response(error)
示例#2
0
文件: api.py 项目: otto-de/gatekeeper
def api_new_gate(group, name):
    try:
        data = util.data_from_request()
        entry = blueprint.mongo.create_new_gate(group, name, data)
        return Response(json.dumps(entry), status=200, mimetype='application/json')
    except (
            NotMasterError, ServiceAlreadyExists, ServiceNameNotValid, JsonValidationError,
            JsonStructureError) as error:
        return error_response(error)
示例#3
0
文件: api.py 项目: otto-de/gatekeeper
def api_test_and_set():
    try:
        status = "ok"
        data = util.data_from_request()
        ticket_id = (data["ticket"] if "ticket" in data else None)
        ticket = (blueprint.mongo.get_ticket(ticket_id) if ticket_id else None)

        if ticket_id and not ticket:
            raise TicketNotFound

        if "gates" not in data:
            raise JsonStructureError("Could not find gates")  # TODO extract string into errors

        for group, services in data['gates'].iteritems():
            for service, environments in services.iteritems():
                entry = blueprint.mongo.get_gate(group, service)
                for env in as_list(environments):
                    if gate_is_closed(entry, env, ticket_id):
                        if not gate_is_manually_closed(entry, env) and request.args and request.args['queue']:
                            status = "queued"
                            break
                        return Response('{"status": "denied"}', status=200, mimetype='application/json')
        if status == "queued":
            expiration_date = blueprint.mongo.get_expiration_date(config.QUEUED_TICKET_LIFETIME)
        else:
            expiration_date = blueprint.mongo.get_expiration_date(
                config.CURRENT_TICKET_LIFETIME) if config.CURRENT_TICKET_LIFETIME != 0 else 0

        if not ticket:
            ticket_id = str(uuid.uuid4())
            ticket = {"id": ticket_id,
                      "updated": get_now_timestamp(),
                      "expiration_date": expiration_date,
                      "link": data["link"] if "link" in data else None}

            for group, services in data['gates'].iteritems():
                for service, environments in services.iteritems():
                    for env in as_list(environments):
                        blueprint.mongo.add_ticket_link(group, service, env, ticket_id)

            blueprint.mongo.add_ticket(ticket_id, ticket)

        else:
            ticket.update({"expiration_date": expiration_date})
            ticket.update({"updated": get_now_timestamp()})
            blueprint.mongo.update_ticket(ticket["id"], ticket)

        response = {
            "status": status,
            "ticket": ticket
        }
        return Response(json.dumps(response), status=200, mimetype='application/json')
    except (NotFound, NotMasterError, ServiceAlreadyExists, ServiceNameNotValid, JsonValidationError,
            JsonStructureError,
            TicketNotFound) as error:
        return error_response(error)
示例#4
0
def api_new_gate(group, name):
    try:
        data = util.data_from_request()
        entry = blueprint.mongo.create_new_gate(group, name, data)
        return Response(json.dumps(entry),
                        status=200,
                        mimetype='application/json')
    except (NotMasterError, ServiceAlreadyExists, ServiceNameNotValid,
            JsonValidationError, JsonStructureError) as error:
        return error_response(error)
示例#5
0
def api_replace_holidays():
    data = util.data_from_request()
    holidays = data['holidays']
    try:
        blueprint.mongo.clear_holidays()
        for holiday in holidays:
            blueprint.mongo.add_holiday(holiday)
        return Response('{"status": "ok"}',
                        status=200,
                        mimetype='application/json')
    except (NotMasterError, ServiceAlreadyExists, ServiceNameNotValid,
            JsonValidationError, JsonStructureError, TicketNotFound) as error:
        return error_response(error)
示例#6
0
def api_update_gate(group, name, environment=None):
    try:
        data = util.data_from_request()
        entry = blueprint.mongo.get_gate(group, name)

        if environment:
            if "state" in data:
                blueprint.mongo.set_gate(group, name, environment,
                                         data["state"])
            if "message" in data:
                blueprint.mongo.set_message(group, name, environment,
                                            data["message"])
        else:
            if "group" in data:
                entry["group"] = data["group"]
                blueprint.mongo.update_gate(group, name, entry)
            if "name" in data:
                entry["name"] = data["name"]
                blueprint.mongo.update_gate(group, name, entry)
                name = data["name"]
            if "environments" in data:
                if type(data['environments']) is list:
                    entry[
                        "environments"] = blueprint.mongo.get_environment_structure(
                            data["environments"])
                    blueprint.mongo.update_gate(group, name, entry)
                else:
                    for env in data["environments"]:
                        if "state" in data["environments"][env]:
                            blueprint.mongo.set_gate(
                                group, name, env,
                                data["environments"][env]["state"])
                        if "message" in data["environments"][env]:
                            blueprint.mongo.set_message(
                                group, name, env,
                                data["environments"][env]["message"])
        entry = blueprint.mongo.get_gate(group, name)
        return Response(json.dumps(entry),
                        status=200,
                        mimetype='application/json')
    except (NotMasterError, ServiceNameNotValid, NotFound, GateStateNotValid,
            EnvironmentNotFound, JsonValidationError,
            JsonStructureError) as error:
        return error_response(error)
示例#7
0
def api_test_and_set():
    try:
        status = "ok"
        data = util.data_from_request()
        ticket_id = (data["ticket"] if "ticket" in data else None)
        ticket = (blueprint.mongo.get_ticket(ticket_id) if ticket_id else None)

        if ticket_id and not ticket:
            raise TicketNotFound

        if "gates" not in data:
            raise JsonStructureError(
                "Could not find gates")  # TODO extract string into errors

        for group, services in data['gates'].iteritems():
            for service, environments in services.iteritems():
                entry = blueprint.mongo.get_gate(group, service)
                for env in as_list(environments):
                    blocked_queue = queue_is_blocked(
                        entry['environments'][env]['queue'], ticket_id)
                    want_to_queue = request.args and request.args['queue']

                    if env in entry['environments']:
                        if gates.gate_is_closed(
                                entry, blueprint.config, env, ticket_id
                        ) or blocked_queue and not want_to_queue:
                            return Response('{"status": "denied"}',
                                            status=200,
                                            mimetype='application/json')
                        if want_to_queue and blocked_queue:
                            status = "queued"
                    else:
                        raise EnvironmentNotFound(env)
        if status == "queued":
            expiration_date = blueprint.mongo.get_expiration_date(
                config.QUEUED_TICKET_LIFETIME)
        else:
            expiration_date = blueprint.mongo.get_expiration_date(
                config.CURRENT_TICKET_LIFETIME
            ) if config.CURRENT_TICKET_LIFETIME != 0 else 0

        if not ticket:
            ticket_id = str(uuid.uuid4())
            ticket = {
                "id": ticket_id,
                "updated": get_now_timestamp(),
                "expiration_date": expiration_date,
                "link": data["link"] if "link" in data else None
            }

            for group, services in data['gates'].iteritems():
                for service, environments in services.iteritems():
                    for env in as_list(environments):
                        blueprint.mongo.add_ticket_link(
                            group, service, env, ticket_id)

            blueprint.mongo.add_ticket(ticket_id, ticket)

        else:
            ticket.update({"expiration_date": expiration_date})
            ticket.update({"updated": get_now_timestamp()})
            blueprint.mongo.update_ticket(ticket["id"], ticket)

        response = {"status": status, "ticket": ticket}
        return Response(json.dumps(response),
                        status=200,
                        mimetype='application/json')
    except (NotFound, NotMasterError, ServiceAlreadyExists,
            ServiceNameNotValid, JsonValidationError, JsonStructureError,
            EnvironmentNotFound, TicketNotFound) as error:
        return error_response(error)