##
# Copyright (c) 2014-2017 Apple Inc. All 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.
##

"""
Control API resource.

This provides an HTTP API to allow an admin to trigger various "internal" actions on the server.
The intent of this is to primarily support automated testing tools that may need to alter
server behavior during tests via an HTTP-only API.
"""

__all__ = [
    "ControlAPIResource",
]

from calendarserver.tools.util import recordForPrincipalID

from twext.enterprise.jobs.jobitem import JobItem
from twext.enterprise.jobs.workitem import WORK_PRIORITY_HIGH, WORK_WEIGHT_1
from twext.python.log import Logger

from twisted.internet import reactor
from twisted.internet.defer import succeed, inlineCallbacks, returnValue

from twistedcaldav.config import config
from twistedcaldav.extensions import DAVResource, \
    DAVResourceWithoutChildrenMixin
from twistedcaldav.resource import ReadOnlyNoCopyResourceMixIn

from txdav.caldav.datastore.scheduling.work import ScheduleOrganizerWork, \
    ScheduleOrganizerSendWork, ScheduleReplyWork, ScheduleRefreshWork, \
    ScheduleAutoReplyWork
from txdav.common.datastore.work.load_work import TestWork
from txdav.who.groups import GroupCacherPollingWork, GroupRefreshWork, \
    GroupAttendeeReconciliationWork, GroupDelegateChangesWork, \
    GroupShareeReconciliationWork
from txdav.xml import element as davxml

from txweb2 import responsecode
from txweb2.dav.method.propfind import http_PROPFIND
from txweb2.dav.noneprops import NonePropertyStore