1   /*
2    * Copyright 2024 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.api.sysadmin;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.List;
22  import java.util.concurrent.CompletableFuture;
23  
24  import com.linecorp.armeria.server.annotation.ConsumesJson;
25  import com.linecorp.armeria.server.annotation.Delete;
26  import com.linecorp.armeria.server.annotation.Get;
27  import com.linecorp.armeria.server.annotation.Param;
28  import com.linecorp.armeria.server.annotation.Post;
29  import com.linecorp.armeria.server.annotation.ProducesJson;
30  import com.linecorp.armeria.server.annotation.Put;
31  import com.linecorp.armeria.server.annotation.StatusCode;
32  import com.linecorp.centraldogma.common.Author;
33  import com.linecorp.centraldogma.server.command.CommandExecutor;
34  import com.linecorp.centraldogma.server.internal.api.AbstractService;
35  import com.linecorp.centraldogma.server.internal.api.auth.RequiresSystemAdministrator;
36  import com.linecorp.centraldogma.server.internal.mirror.DefaultMirrorAccessController;
37  import com.linecorp.centraldogma.server.internal.mirror.MirrorAccessControl;
38  
39  /**
40   * A service which provides the API for managing the mirror access control.
41   */
42  @ProducesJson
43  @ConsumesJson
44  @RequiresSystemAdministrator
45  public final class MirrorAccessControlService extends AbstractService {
46  
47      public static final String MIRROR_ACCESS_CONTROL_PATH = "/mirror-access-control/";
48  
49      private final DefaultMirrorAccessController accessController;
50  
51      public MirrorAccessControlService(CommandExecutor executor,
52                                        DefaultMirrorAccessController accessController) {
53          super(executor);
54          this.accessController = requireNonNull(accessController, "accessController");
55      }
56  
57      /**
58       * GET /mirror/access
59       *
60       * <p>Returns the list of mirror access control.
61       */
62      @Get("/mirror/access")
63      public CompletableFuture<List<MirrorAccessControl>> list() {
64          return accessController.list();
65      }
66  
67      /**
68       * POST /mirror/access
69       *
70       * <p>Creates a new mirror access control.
71       */
72      @StatusCode(201)
73      @Post("/mirror/access")
74      public CompletableFuture<MirrorAccessControl> create(MirrorAccessControlRequest request, Author author) {
75          return accessController.add(request, author);
76      }
77  
78      /**
79       * PUT /mirror/access
80       *
81       * <p>Updates the mirror access control.
82       */
83      @Put("/mirror/access")
84      public CompletableFuture<MirrorAccessControl> update(MirrorAccessControlRequest request, Author author) {
85          return accessController.update(request, author);
86      }
87  
88      /**
89       * GET /mirror/access/{id}
90       *
91       * <p>Returns the mirror access control.
92       */
93      @Get("/mirror/access/{id}")
94      public CompletableFuture<MirrorAccessControl> get(@Param String id) {
95          return accessController.get(id);
96      }
97  
98      /**
99       * DELETE /mirror/access/{id}
100      *
101      * <p>Deletes the mirror access control.
102      */
103     @Delete("/mirror/access/{id}")
104     public CompletableFuture<Void> delete(@Param String id, Author author) {
105         return accessController.delete(id, author);
106     }
107 }