|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.license;
|
7 | 7 |
|
8 |
| -import java.io.IOException; |
9 |
| -import java.io.InputStream; |
10 |
| -import java.nio.ByteBuffer; |
11 |
| -import java.util.ArrayList; |
12 |
| -import java.util.Base64; |
13 |
| -import java.util.Comparator; |
14 |
| -import java.util.List; |
15 |
| -import java.util.Locale; |
16 |
| - |
17 | 8 | import org.apache.lucene.util.CollectionUtil;
|
18 | 9 | import org.elasticsearch.ElasticsearchException;
|
19 | 10 | import org.elasticsearch.ElasticsearchParseException;
|
|
31 | 22 | import org.elasticsearch.common.xcontent.XContentType;
|
32 | 23 | import org.elasticsearch.protocol.xpack.license.LicenseStatus;
|
33 | 24 |
|
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.nio.ByteBuffer; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Base64; |
| 30 | +import java.util.Comparator; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import java.util.stream.Stream; |
| 35 | + |
34 | 36 | /**
|
35 | 37 | * Data structure for license. Use {@link Builder} to build a license.
|
36 | 38 | * Provides serialization/deserialization & validation methods for license object
|
37 | 39 | */
|
38 | 40 | public class License implements ToXContentObject {
|
| 41 | + |
| 42 | + public enum LicenseType { |
| 43 | + BASIC, |
| 44 | + STANDARD, |
| 45 | + GOLD, |
| 46 | + PLATINUM, |
| 47 | + ENTERPRISE, |
| 48 | + TRIAL; |
| 49 | + |
| 50 | + public String getTypeName() { |
| 51 | + return name().toLowerCase(Locale.ROOT); |
| 52 | + } |
| 53 | + |
| 54 | + public static LicenseType parse(String type) throws IllegalArgumentException { |
| 55 | + try { |
| 56 | + return LicenseType.valueOf(type.toUpperCase(Locale.ROOT)); |
| 57 | + } catch (IllegalArgumentException e) { |
| 58 | + throw new IllegalArgumentException("unrecognised license type [ " + type + "], supported license types are [" |
| 59 | + + Stream.of(values()).map(LicenseType::getTypeName).collect(Collectors.joining(",")) + "]"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Backward compatible license type parsing for older license models |
| 65 | + */ |
| 66 | + public static LicenseType resolve(String name) { |
| 67 | + switch (name.toLowerCase(Locale.ROOT)) { |
| 68 | + case "missing": |
| 69 | + return null; |
| 70 | + case "trial": |
| 71 | + case "none": // bwc for 1.x subscription_type field |
| 72 | + case "dev": // bwc for 1.x subscription_type field |
| 73 | + case "development": // bwc for 1.x subscription_type field |
| 74 | + return TRIAL; |
| 75 | + case "basic": |
| 76 | + return BASIC; |
| 77 | + case "standard": |
| 78 | + return STANDARD; |
| 79 | + case "silver": |
| 80 | + case "gold": |
| 81 | + return GOLD; |
| 82 | + case "platinum": |
| 83 | + case "cloud_internal": |
| 84 | + case "internal": // bwc for 1.x subscription_type field |
| 85 | + return PLATINUM; |
| 86 | + case "enterprise": |
| 87 | + return ENTERPRISE; |
| 88 | + default: |
| 89 | + throw new IllegalArgumentException("unknown license type [" + name + "]"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static boolean isBasic(String typeName) { |
| 94 | + return BASIC.getTypeName().equals(typeName); |
| 95 | + } |
| 96 | + |
| 97 | + static boolean isTrial(String typeName) { |
| 98 | + return TRIAL.getTypeName().equals(typeName); |
| 99 | + } |
| 100 | + } |
| 101 | + |
39 | 102 | public static final int VERSION_START = 1;
|
40 | 103 | public static final int VERSION_NO_FEATURE_TYPE = 2;
|
41 | 104 | public static final int VERSION_START_DATE = 3;
|
@@ -102,28 +165,25 @@ public static int compare(OperationMode opMode1, OperationMode opMode2) {
|
102 | 165 | return Integer.compare(opMode1.id, opMode2.id);
|
103 | 166 | }
|
104 | 167 |
|
105 |
| - public static OperationMode resolve(String type) { |
106 |
| - switch (type.toLowerCase(Locale.ROOT)) { |
107 |
| - case "missing": |
108 |
| - return MISSING; |
109 |
| - case "trial": |
110 |
| - case "none": // bwc for 1.x subscription_type field |
111 |
| - case "dev": // bwc for 1.x subscription_type field |
112 |
| - case "development": // bwc for 1.x subscription_type field |
113 |
| - return TRIAL; |
114 |
| - case "basic": |
| 168 | + public static OperationMode resolve(String typeName) { |
| 169 | + LicenseType type = LicenseType.resolve(typeName); |
| 170 | + if (type == null) { |
| 171 | + return MISSING; |
| 172 | + } |
| 173 | + switch (type) { |
| 174 | + case BASIC: |
115 | 175 | return BASIC;
|
116 |
| - case "standard": |
| 176 | + case STANDARD: |
117 | 177 | return STANDARD;
|
118 |
| - case "silver": |
119 |
| - case "gold": |
| 178 | + case GOLD: |
120 | 179 | return GOLD;
|
121 |
| - case "platinum": |
122 |
| - case "cloud_internal": |
123 |
| - case "internal": // bwc for 1.x subscription_type field |
| 180 | + case PLATINUM: |
| 181 | + case ENTERPRISE: // TODO Add an explicit enterprise operating mode |
124 | 182 | return PLATINUM;
|
| 183 | + case TRIAL: |
| 184 | + return TRIAL; |
125 | 185 | default:
|
126 |
| - throw new IllegalArgumentException("unknown type [" + type + "]"); |
| 186 | + throw new IllegalArgumentException("unsupported license type [" + type.getTypeName() + "]"); |
127 | 187 | }
|
128 | 188 | }
|
129 | 189 |
|
@@ -301,7 +361,7 @@ private void validate() {
|
301 | 361 | throw new IllegalStateException("maxNodes has to be set");
|
302 | 362 | } else if (expiryDate == -1) {
|
303 | 363 | throw new IllegalStateException("expiryDate has to be set");
|
304 |
| - } else if (expiryDate == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && "basic".equals(type) == false) { |
| 364 | + } else if (expiryDate == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && LicenseType.isBasic(type) == false) { |
305 | 365 | throw new IllegalStateException("only basic licenses are allowed to have no expiration");
|
306 | 366 | }
|
307 | 367 | }
|
@@ -689,6 +749,10 @@ public Builder issueDate(long issueDate) {
|
689 | 749 | return this;
|
690 | 750 | }
|
691 | 751 |
|
| 752 | + public Builder type(LicenseType type) { |
| 753 | + return type(type.getTypeName()); |
| 754 | + } |
| 755 | + |
692 | 756 | public Builder type(String type) {
|
693 | 757 | this.type = type;
|
694 | 758 | return this;
|
@@ -778,6 +842,7 @@ public Builder validate() {
|
778 | 842 | }
|
779 | 843 | return this;
|
780 | 844 | }
|
| 845 | + |
781 | 846 | }
|
782 | 847 |
|
783 | 848 | }
|
0 commit comments