1   /*
2    * Copyright 2019 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.metadata;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  
24  import javax.annotation.Nullable;
25  
26  import com.fasterxml.jackson.annotation.JsonCreator;
27  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
28  import com.fasterxml.jackson.annotation.JsonInclude;
29  import com.fasterxml.jackson.annotation.JsonInclude.Include;
30  import com.fasterxml.jackson.annotation.JsonProperty;
31  import com.google.common.base.MoreObjects;
32  import com.google.common.collect.ImmutableMap;
33  
34  import com.linecorp.centraldogma.server.QuotaConfig;
35  import com.linecorp.centraldogma.server.storage.repository.Repository;
36  
37  /**
38   * Specifies details of a {@link Repository}.
39   */
40  @JsonIgnoreProperties(ignoreUnknown = true)
41  @JsonInclude(Include.NON_NULL)
42  public class RepositoryMetadata implements Identifiable {
43  
44      /**
45       * A name of this repository.
46       */
47      private final String name;
48  
49      /**
50       * A default permission of this repository which is based on a {@link ProjectRole} of a user.
51       */
52      private final PerRolePermissions perRolePermissions;
53  
54      /**
55       * A map of username and {@link Permission}s who has permission specified by a owner.
56       */
57      private final Map<String, Collection<Permission>> perUserPermissions;
58  
59      /**
60       * A map of token ID and {@link Permission}s who has permission specified by a owner.
61       */
62      private final Map<String, Collection<Permission>> perTokenPermissions;
63  
64      /**
65       * Specifies when this repository is created by whom.
66       */
67      private final UserAndTimestamp creation;
68  
69      /**
70       * Specifies when this repository is removed by whom.
71       */
72      @Nullable
73      private final UserAndTimestamp removal;
74  
75      /**
76       * A write quota of this repository.
77       */
78      @Nullable
79      private final QuotaConfig writeQuota;
80  
81      /**
82       * Creates a new instance with default properties.
83       */
84      public RepositoryMetadata(String name, UserAndTimestamp creation, PerRolePermissions perRolePermissions) {
85          this(name, perRolePermissions, ImmutableMap.of(), ImmutableMap.of(),
86               creation, /* removal */ null, /* writeQuota */ null);
87      }
88  
89      /**
90       * Creates a new instance.
91       */
92      @JsonCreator
93      public RepositoryMetadata(@JsonProperty("name") String name,
94                                @JsonProperty("perRolePermissions") PerRolePermissions perRolePermissions,
95                                @JsonProperty("perUserPermissions")
96                                        Map<String, Collection<Permission>> perUserPermissions,
97                                @JsonProperty("perTokenPermissions")
98                                        Map<String, Collection<Permission>> perTokenPermissions,
99                                @JsonProperty("creation") UserAndTimestamp creation,
100                               @JsonProperty("removal") @Nullable UserAndTimestamp removal,
101                               @JsonProperty("writeQuota") @Nullable QuotaConfig writeQuota) {
102         this.name = requireNonNull(name, "name");
103         this.perRolePermissions = requireNonNull(perRolePermissions, "perRolePermissions");
104         this.perUserPermissions = ImmutableMap.copyOf(requireNonNull(perUserPermissions,
105                                                                      "perUserPermissions"));
106         this.perTokenPermissions = ImmutableMap.copyOf(requireNonNull(perTokenPermissions,
107                                                                       "perTokenPermissions"));
108         this.creation = requireNonNull(creation, "creation");
109         this.removal = removal;
110         this.writeQuota = writeQuota;
111     }
112 
113     @Override
114     public String id() {
115         return name;
116     }
117 
118     /**
119      * Returns the repository name.
120      */
121     @JsonProperty
122     public String name() {
123         return name;
124     }
125 
126     /**
127      * Returns the {@link PerRolePermissions} of this repository.
128      */
129     @JsonProperty
130     public PerRolePermissions perRolePermissions() {
131         return perRolePermissions;
132     }
133 
134     /**
135      * Returns the per-user {@link Permission}s of this repository.
136      */
137     @JsonProperty
138     public Map<String, Collection<Permission>> perUserPermissions() {
139         return perUserPermissions;
140     }
141 
142     /**
143      * Returns the per-token {@link Permission}s of this repository.
144      */
145     @JsonProperty
146     public Map<String, Collection<Permission>> perTokenPermissions() {
147         return perTokenPermissions;
148     }
149 
150     /**
151      * Returns who created this repository when.
152      */
153     @JsonProperty
154     public UserAndTimestamp creation() {
155         return creation;
156     }
157 
158     /**
159      * Returns who removed this repository when.
160      */
161     @Nullable
162     @JsonProperty
163     public UserAndTimestamp removal() {
164         return removal;
165     }
166 
167     /**
168      * Returns the maximum allowed write quota.
169      */
170     @Nullable
171     @JsonProperty("writeQuota")
172     public QuotaConfig writeQuota() {
173         return writeQuota;
174     }
175 
176     @Override
177     public String toString() {
178         return MoreObjects.toStringHelper(this)
179                           .omitNullValues()
180                           .add("name", name())
181                           .add("perRolePermissions", perRolePermissions())
182                           .add("perUserPermissions", perUserPermissions())
183                           .add("perTokenPermissions", perTokenPermissions())
184                           .add("creation", creation())
185                           .add("removal", removal())
186                           .add("writeQuota", writeQuota())
187                           .toString();
188     }
189 }