// Copyright 2016 The Domain Registry Authors. 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.

package com.google.domain.registry.export;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.html.HtmlEscapers.htmlEscaper;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK;

import com.google.common.net.MediaType;
import com.google.domain.registry.config.RegistryEnvironment;
import com.google.domain.registry.util.Clock;
import com.google.domain.registry.util.FormattingLogger;
import com.google.domain.registry.util.NonFinalForTesting;
import com.google.domain.registry.util.SystemClock;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Trigger a backup-as-a-service job that writes a snapshot to Google Cloud Storage. */
public class ExportSnapshotServlet extends HttpServlet {

  private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();

  /** Queue to use for enqueuing the task that will actually launch the backup. */
  static final String QUEUE = "export-snapshot";  // See queue.xml.

  /** Prefix to use for naming all snapshots that are started by this servlet. */
  static final String SNAPSHOT_PREFIX = "auto_snapshot_";

  private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();

  @NonFinalForTesting
  private static Clock clock = new SystemClock();

  @NonFinalForTesting
  private static DatastoreBackupService backupService = DatastoreBackupService.get();

  @NonFinalForTesting
  private static CheckSnapshotServlet checkSnapshotServlet = new CheckSnapshotServlet();

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    try {
      // Use a unique name for the snapshot so we can explicitly check its completion later.
      String snapshotName = SNAPSHOT_PREFIX + clock.nowUtc().toString("YYYYMMdd_HHmmss");
      backupService.launchNewBackup(
          QUEUE,
          snapshotName,
          ENVIRONMENT.config().getSnapshotsBucket(),
          ExportConstants.getBackupKinds());
      // Enqueue a poll task to monitor the backup and load reporting-related kinds into bigquery.
      checkSnapshotServlet.enqueuePollTask(snapshotName, ExportConstants.getReportingKinds());
      String message = "Datastore backup started with name: " + snapshotName;
      logger.info(message);
      rsp.setStatus(SC_OK);
      rsp.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
      rsp.getWriter().write("OK\n\n" + message);
    } catch (Throwable e) {
      logger.severe(e, e.toString());
      rsp.sendError(
          e instanceof IllegalArgumentException ? SC_BAD_REQUEST : SC_INTERNAL_SERVER_ERROR,
          htmlEscaper().escape(firstNonNull(e.getMessage(), e.toString())));
    }
  }
}

@@ -1481,16 +1481,19 @@
 se;%0A%0A/**
+%0A *
  Trigger
@@ -1566,16 +1566,489 @@
 Storage.
+%0A *%0A * %3Cp%3EThis is the first step of a four step workflow for exporting snapshots, with each step calling%0A * the next upon successful completion:%3Col%3E%0A *   %3Cli%3EThe snapshot is exported to Google Cloud Storage (this servlet).%0A *   %3Cli%3EThe %7B@link CheckSnapshotServlet%7D polls until the export is completed.%0A *   %3Cli%3EThe %7B@link LoadSnapshotAction%7D imports the data from GCS to BigQuery.%0A *   %3Cli%3EThe %7B@link UpdateSnapshotViewAction%7D updates the view in latest_snapshot.%0A * %3C/ol%3E%0A
  */%0Apubl

Add code documentation on how the snapshot actions interact ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=120707865