/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.watcher.transport.actions.delete;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.watcher.support.init.proxy.WatcherClientProxy;
import org.elasticsearch.xpack.watcher.transport.actions.WatcherTransportAction;
import org.elasticsearch.xpack.watcher.trigger.TriggerService;
import org.elasticsearch.xpack.watcher.watch.Watch;

/**
 * Performs the delete operation.
 */
public class TransportDeleteWatchAction extends WatcherTransportAction<DeleteWatchRequest, DeleteWatchResponse> {

    private final WatcherClientProxy client;
    private final TriggerService triggerService;

    @Inject
    public TransportDeleteWatchAction(Settings settings, TransportService transportService, ClusterService clusterService,
                                      ThreadPool threadPool, ActionFilters actionFilters,
                                      IndexNameExpressionResolver indexNameExpressionResolver, WatcherClientProxy client,
                                      XPackLicenseState licenseState, TriggerService triggerService) {
        super(settings, DeleteWatchAction.NAME, transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver,
                licenseState, DeleteWatchRequest::new);
        this.client = client;
        this.triggerService = triggerService;
    }

    @Override
    protected String executor() {
        return ThreadPool.Names.MANAGEMENT;
    }

    @Override
    protected DeleteWatchResponse newResponse() {
        return new DeleteWatchResponse();
    }

    @Override
    protected void masterOperation(DeleteWatchRequest request, ClusterState state, ActionListener<DeleteWatchResponse> listener) throws
            ElasticsearchException {
        client.deleteWatch(request.getId(), ActionListener.wrap(deleteResponse -> {
                    boolean deleted = deleteResponse.getResult() == DocWriteResponse.Result.DELETED;
                    DeleteWatchResponse response = new DeleteWatchResponse(deleteResponse.getId(), deleteResponse.getVersion(), deleted);
                    if (deleted) {
                        triggerService.remove(request.getId());
                    }
                    listener.onResponse(response);
                },
                listener::onFailure));
    }

    @Override
    protected ClusterBlockException checkBlock(DeleteWatchRequest request, ClusterState state) {
        return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, Watch.INDEX);
    }
}

@@ -504,16 +504,90 @@
 ilters;%0A
+import org.elasticsearch.action.support.master.TransportMasterNodeAction;%0A
 import o
@@ -975,60 +975,8 @@
 gs;%0A
-import org.elasticsearch.license.XPackLicenseState;%0A
 impo
@@ -1154,89 +1154,8 @@
 xy;%0A
-import org.elasticsearch.xpack.watcher.transport.actions.WatcherTransportAction;%0A
 impo
@@ -1303,16 +1303,174 @@
 eration.
+ This inherits directly from TransportMasterNodeAction, because deletion should always work%0A * independently from the license check in WatcherTransportAction!
 %0A */%0Apub
@@ -1514,23 +1514,16 @@
 extends 
-Watcher
 Transpor
@@ -1519,24 +1519,34 @@
 ds Transport
+MasterNode
 Action%3CDelet
@@ -2067,40 +2067,8 @@
     
- XPackLicenseState licenseState,
  Tri
@@ -2254,22 +2254,8 @@
     
- licenseState,
  Del

Allow deletion of watches regardless of license (elastic/elasticsearch#4414)