1   /*
2    * Copyright 2023 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.mirror;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.net.URI;
22  
23  import javax.annotation.Nullable;
24  
25  import com.cronutils.model.Cron;
26  import com.google.common.base.MoreObjects;
27  
28  import com.linecorp.centraldogma.server.credential.Credential;
29  import com.linecorp.centraldogma.server.storage.repository.Repository;
30  
31  /**
32   * A context of a mirror.
33   */
34  public final class MirrorContext {
35  
36      private final String id;
37      private final boolean enabled;
38      @Nullable
39      private final Cron schedule;
40      private final MirrorDirection direction;
41      private final Credential credential;
42      private final Repository localRepo;
43      private final String localPath;
44      private final URI remoteUri;
45      @Nullable
46      private final String gitignore;
47      @Nullable
48      private final String zone;
49  
50      /**
51       * Creates a new instance.
52       */
53      public MirrorContext(String id, boolean enabled, @Nullable Cron schedule, MirrorDirection direction,
54                           Credential credential, Repository localRepo,
55                           String localPath, URI remoteUri, @Nullable String gitignore, @Nullable String zone) {
56          this.id = requireNonNull(id, "id");
57          this.enabled = enabled;
58          this.schedule = schedule;
59          this.direction = requireNonNull(direction, "direction");
60          this.credential = requireNonNull(credential, "credential");
61          this.localRepo = requireNonNull(localRepo, "localRepo");
62          this.localPath = requireNonNull(localPath, "localPath");
63          this.remoteUri = requireNonNull(remoteUri, "remoteUri");
64          this.gitignore = gitignore;
65          this.zone = zone;
66      }
67  
68      /**
69       * Returns the ID of this mirror.
70       */
71      public String id() {
72          return id;
73      }
74  
75      /**
76       * Returns whether this mirror is enabled or not.
77       */
78      public boolean enabled() {
79          return enabled;
80      }
81  
82      /**
83       * Returns the cron schedule of this mirror.
84       * {@code null} if this mirror is not scheduled automatically.
85       */
86      @Nullable
87      public Cron schedule() {
88          return schedule;
89      }
90  
91      /**
92       * Returns the direction of this mirror.
93       */
94      public MirrorDirection direction() {
95          return direction;
96      }
97  
98      /**
99       * Returns the credential of this mirror.
100      */
101     public Credential credential() {
102         return credential;
103     }
104 
105     /**
106      * Returns the local repository of this mirror.
107      */
108     public Repository localRepo() {
109         return localRepo;
110     }
111 
112     /**
113      * Returns the local path of this mirror.
114      */
115     public String localPath() {
116         return localPath;
117     }
118 
119     /**
120      * Returns the remote URI of this mirror.
121      */
122     public URI remoteUri() {
123         return remoteUri;
124     }
125 
126     /**
127      * Returns the gitignore of this mirror.
128      */
129     @Nullable
130     public String gitignore() {
131         return gitignore;
132     }
133 
134     /**
135      * Returns the zone where this mirror is pinned.
136      */
137     @Nullable
138     public String zone() {
139         return zone;
140     }
141 
142     @Override
143     public String toString() {
144         return MoreObjects.toStringHelper(this).omitNullValues()
145                           .add("id", id)
146                           .add("enabled", enabled)
147                           .add("schedule", schedule)
148                           .add("direction", direction)
149                           .add("credential", credential)
150                           .add("localRepo", localRepo)
151                           .add("localPath", localPath)
152                           .add("remoteUri", remoteUri)
153                           .add("gitignore", gitignore)
154                           .add("zone", zone)
155                           .toString();
156     }
157 }