Make Type & SubscriptionType strings instead of enums

Original commit: elastic/x-pack-elasticsearch@e48ebc447d
This commit is contained in:
Areek Zillur 2014-10-27 21:58:00 -04:00
parent a82a0a4e6a
commit 021974fb22
9 changed files with 36 additions and 168 deletions

View File

@ -21,15 +21,15 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
private final String issuer;
private final String issuedTo;
private final long issueDate;
private final Type type;
private final SubscriptionType subscriptionType;
private final String type;
private final String subscriptionType;
private final String feature;
private final String signature;
private final long expiryDate;
private final int maxNodes;
private ESLicense(String uid, String issuer, String issuedTo, long issueDate, Type type,
SubscriptionType subscriptionType, String feature, String signature, long expiryDate, int maxNodes) {
private ESLicense(String uid, String issuer, String issuedTo, long issueDate, String type,
String subscriptionType, String feature, String signature, long expiryDate, int maxNodes) {
this.uid = uid;
this.issuer = issuer;
this.issuedTo = issuedTo;
@ -53,14 +53,14 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
/**
* @return type of the license [trial, subscription, internal]
*/
public Type type() {
public String type() {
return type;
}
/**
* @return subscription type of the license [none, silver, gold, platinum]
*/
public SubscriptionType subscriptionType() {
public String subscriptionType() {
return subscriptionType;
}
@ -123,8 +123,8 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
in.readVInt(); // Version for future extensibility
Builder builder = builder();
builder.uid(in.readString());
builder.type(Type.fromString(in.readString()));
builder.subscriptionType(SubscriptionType.fromString(in.readString()));
builder.type(in.readString());
builder.subscriptionType(in.readString());
builder.issueDate(in.readLong());
builder.feature(in.readString());
builder.expiryDate(in.readLong());
@ -138,8 +138,8 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(VERSION);
out.writeString(uid);
out.writeString(type.string());
out.writeString(subscriptionType.string());
out.writeString(type);
out.writeString(subscriptionType);
out.writeLong(issueDate);
out.writeString(feature);
out.writeLong(expiryDate);
@ -153,8 +153,8 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(XFields.UID, uid);
builder.field(XFields.TYPE, type.string());
builder.field(XFields.SUBSCRIPTION_TYPE, subscriptionType.string());
builder.field(XFields.TYPE, type);
builder.field(XFields.SUBSCRIPTION_TYPE, subscriptionType);
builder.field(XFields.ISSUE_DATE, issueDate);
builder.field(XFields.FEATURE, feature);
builder.field(XFields.EXPIRY_DATE, expiryDate);
@ -208,9 +208,9 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
if (Fields.UID.equals(currentFieldName)) {
builder.uid(parser.text());
} else if (Fields.TYPE.equals(currentFieldName)) {
builder.type(Type.fromString(parser.text()));
builder.type(parser.text());
} else if (Fields.SUBSCRIPTION_TYPE.equals(currentFieldName)) {
builder.subscriptionType(SubscriptionType.fromString(parser.text()));
builder.subscriptionType(parser.text());
} else if (Fields.ISSUE_DATE.equals(currentFieldName)) {
builder.issueDate(parser.longValue());
} else if (Fields.FEATURE.equals(currentFieldName)) {
@ -242,77 +242,6 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
return builder.verifyAndBuild();
}
/**
* Enum for License Type
*/
public enum Type {
TRIAL("trial"),
SUBSCRIPTION("subscription"),
INTERNAL("internal");
private final String name;
private Type(String name) {
this.name = name;
}
public String string() {
return name;
}
public static Type fromString(String type) {
if (type.equalsIgnoreCase(TRIAL.string())) {
return TRIAL;
} else if (type.equalsIgnoreCase(SUBSCRIPTION.string())) {
return SUBSCRIPTION;
} else if (type.equalsIgnoreCase(INTERNAL.string())) {
return INTERNAL;
} else {
throw new IllegalArgumentException("Invalid Type=" + type);
}
}
}
/**
* Enum for License Subscription Type
*/
public enum SubscriptionType {
NONE("none"),
DEVELOPMENT("development"),
SILVER("silver"),
GOLD("gold"),
PLATINUM("platinum");
public static SubscriptionType DEFAULT = NONE;
private final String name;
private SubscriptionType(String name) {
this.name = name;
}
public String string() {
return name;
}
public static SubscriptionType fromString(String subscriptionType) {
if (subscriptionType.equalsIgnoreCase(NONE.string())) {
return NONE;
} else if (subscriptionType.equalsIgnoreCase(DEVELOPMENT.string())) {
return DEVELOPMENT;
} else if (subscriptionType.equalsIgnoreCase(SILVER.string())) {
return SILVER;
} else if (subscriptionType.equalsIgnoreCase(GOLD.string())) {
return GOLD;
} else if (subscriptionType.equalsIgnoreCase(PLATINUM.string())) {
return PLATINUM;
} else {
throw new IllegalArgumentException("Invalid SubscriptionType=" + subscriptionType);
}
}
}
public static Builder builder() {
return new Builder();
}
@ -322,8 +251,8 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
private String issuer;
private String issuedTo;
private long issueDate = -1;
private Type type;
private SubscriptionType subscriptionType = SubscriptionType.DEFAULT;
private String type;
private String subscriptionType = "none";
private String feature;
private String signature;
private long expiryDate = -1;
@ -350,12 +279,12 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
return this;
}
public Builder type(Type type) {
public Builder type(String type) {
this.type = type;
return this;
}
public Builder subscriptionType(SubscriptionType subscriptionType) {
public Builder subscriptionType(String subscriptionType) {
this.subscriptionType = subscriptionType;
return this;
}

View File

@ -127,8 +127,8 @@ public class ESLicenseSigner {
private void featureToXContent(ESLicense license, XContentBuilder builder) throws IOException {
builder.startObject();
builder.field("feature", license.feature());
builder.field("type", license.type().string());
builder.field("subscription_type", license.subscriptionType().string());
builder.field("type", license.type());
builder.field("subscription_type", license.subscriptionType());
builder.field("max_nodes", license.maxNodes());
builder.endObject();
}

View File

@ -14,15 +14,12 @@ import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import static org.elasticsearch.license.core.ESLicense.SubscriptionType;
import static org.elasticsearch.license.core.ESLicense.Type;
public class LicenseSpecs {
private static ESLicense fromXContent(Map<String, Object> map) throws IOException, ParseException {
ESLicense.Builder builder = new ESLicense.Builder()
.type(Type.fromString((String) map.get("type")))
.subscriptionType(SubscriptionType.fromString((String) map.get("subscription_type")))
.type((String) map.get("type"))
.subscriptionType((String) map.get("subscription_type"))
.feature((String) map.get("feature"))
.maxNodes((int) map.get("max_nodes"))
.issuedTo((String) map.get("issued_to"))

View File

@ -22,9 +22,6 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import static org.elasticsearch.license.core.ESLicense.SubscriptionType;
import static org.elasticsearch.license.core.ESLicense.Type;
/**
* Class responsible for reading signed licenses, maintaining an effective esLicenses instance, verification of licenses
* and querying against licenses on a feature basis
@ -111,8 +108,8 @@ public class ESLicenseManager {
&& licenseFeatures.subscriptionType != null) {
builder.maxNodes(licenseFeatures.maxNodes)
.feature(licenseFeatures.feature)
.type(Type.fromString(licenseFeatures.type))
.subscriptionType(SubscriptionType.fromString(licenseFeatures.subscriptionType));
.type(licenseFeatures.type)
.subscriptionType(licenseFeatures.subscriptionType);
break;
}
} catch (IOException ignored) {}
@ -142,8 +139,8 @@ public class ESLicenseManager {
String featureName = feature.getName();
LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName);
maxNodesValid = eslicense.maxNodes() == licenseFeatures.maxNodes;
typeValid = eslicense.type().string().equals(licenseFeatures.type);
subscriptionTypeValid = eslicense.subscriptionType().string().equals(licenseFeatures.subscriptionType);
typeValid = eslicense.type().equals(licenseFeatures.type);
subscriptionTypeValid = eslicense.subscriptionType().equals(licenseFeatures.subscriptionType);
featureValid = eslicense.feature().equals(licenseFeatures.feature);
if (maxNodesValid && typeValid && subscriptionTypeValid && featureValid) {

View File

@ -16,9 +16,6 @@ import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import static org.elasticsearch.license.core.ESLicense.SubscriptionType;
import static org.elasticsearch.license.core.ESLicense.Type;
public class TrialLicenseUtils {
public static TrialLicenseBuilder builder() {
@ -27,8 +24,8 @@ public class TrialLicenseUtils {
public static class TrialLicenseBuilder {
private static final String DEFAULT_ISSUER = "elasticsearch";
private static final Type DEFAULT_TYPE = Type.TRIAL;
private static final SubscriptionType DEFAULT_SUBSCRIPTION_TYPE = SubscriptionType.NONE;
private static final String DEFAULT_TYPE = "trial";
private static final String DEFAULT_SUBSCRIPTION_TYPE = "none";
private String feature;
private long expiryDate = -1;

View File

@ -71,8 +71,8 @@ public class RestGetLicenseAction extends BaseRestHandler {
builder.startObject();
builder.field("uid", license.uid());
builder.field("type", license.type().string());
builder.field("subscription_type", license.subscriptionType().string());
builder.field("type", license.type());
builder.field("subscription_type", license.subscriptionType());
builder.field("issued_to", license.issuedTo());
builder.field("issue_date", DateUtils.dateStringFromLongDate(license.issueDate()));
builder.field("expiry_date", DateUtils.dateStringFromLongDate(license.expiryDate()));

View File

@ -88,8 +88,8 @@ public class TestUtils {
final ESLicense esLicense = esLicenses.get(featureType);
assertTrue("license for " + featureType + " should be present", esLicense != null);
assertTrue("expected value for issuedTo was: " + attributes.issuedTo + " but got: " + esLicense.issuedTo(), esLicense.issuedTo().equals(attributes.issuedTo));
assertTrue("expected value for type was: " + attributes.type + " but got: " + esLicense.type().string(), esLicense.type().string().equals(attributes.type));
assertTrue("expected value for subscriptionType was: " + attributes.subscriptionType + " but got: " + esLicense.subscriptionType().string(), esLicense.subscriptionType().string().equals(attributes.subscriptionType));
assertTrue("expected value for type was: " + attributes.type + " but got: " + esLicense.type(), esLicense.type().equals(attributes.type));
assertTrue("expected value for subscriptionType was: " + attributes.subscriptionType + " but got: " + esLicense.subscriptionType(), esLicense.subscriptionType().equals(attributes.subscriptionType));
assertTrue("expected value for feature was: " + attributes.featureType + " but got: " + esLicense.feature(), esLicense.feature().equals(attributes.featureType));
assertTrue("expected value for issueDate was: " + DateUtils.longFromDateString(attributes.issueDate) + " but got: " + esLicense.issueDate(), esLicense.issueDate() == DateUtils.longFromDateString(attributes.issueDate));
assertTrue("expected value for expiryDate: " + DateUtils.longExpiryDateFromString(attributes.expiryDate) + " but got: " + esLicense.expiryDate(), esLicense.expiryDate() == DateUtils.longExpiryDateFromString(attributes.expiryDate));
@ -128,8 +128,8 @@ public class TestUtils {
assertTrue("Should have same uid; got: " + license1.uid() + " and " + license2.uid(), license1.uid().equals(license2.uid()));
assertTrue("Should have same feature; got: " + license1.feature() + " and " + license2.feature(), license1.feature().equals(license2.feature()));
assertTrue("Should have same subscriptType; got: " + license1.subscriptionType().string() + " and " + license2.subscriptionType().string(), license1.subscriptionType().string().equals(license2.subscriptionType().string()));
assertTrue("Should have same type; got: " + license1.type().string() + " and " + license2.type().string(), license1.type().string().equals(license2.type().string()));
assertTrue("Should have same subscriptType; got: " + license1.subscriptionType() + " and " + license2.subscriptionType(), license1.subscriptionType().equals(license2.subscriptionType()));
assertTrue("Should have same type; got: " + license1.type() + " and " + license2.type(), license1.type().equals(license2.type()));
assertTrue("Should have same issuedTo; got: " + license1.issuedTo() + " and " + license2.issuedTo(), license1.issuedTo().equals(license2.issuedTo()));
assertTrue("Should have same signature; got: " + license1.signature() + " and " + license2.signature(), license1.signature().equals(license2.signature()));
assertTrue("Should have same expiryDate; got: " + license1.expiryDate() + " and " + license2.expiryDate(), license1.expiryDate() == license2.expiryDate());

View File

@ -77,56 +77,4 @@ public class LicenseGenerationTests extends AbstractLicensingTestBase {
assertTrue("Exception should indicate mandatory param --license, got: " + e.getMessage(), e.getMessage().contains("license"));
}
}
@Test
public void testInvalidSubscriptionType() throws ParseException, IOException {
Map<String, TestUtils.FeatureAttributes> map = new HashMap<>();
TestUtils.FeatureAttributes featureAttributes =
new TestUtils.FeatureAttributes("shield", "trial", "nodavne", "foo bar Inc.", "elasticsearch", 25, "2014-12-13", "2015-12-13");
map.put(TestUtils.SHIELD, featureAttributes);
String licenseString = TestUtils.generateESLicenses(map);
String[] args = new String[6];
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
try {
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
fail();
} catch (IllegalArgumentException e) {
assertTrue("Exception should indicate invalid SubscriptionType, got: " + e.getMessage(), e.getMessage().contains("Invalid SubscriptionType"));
}
}
@Test
public void testInvalidType() throws ParseException, IOException {
Map<String, TestUtils.FeatureAttributes> map = new HashMap<>();
TestUtils.FeatureAttributes featureAttributes =
new TestUtils.FeatureAttributes("shield", "inininternal", "gold", "foo bar Inc.", "elasticsearch", 12, "2014-12-13", "2015-12-13");
map.put(TestUtils.SHIELD, featureAttributes);
String licenseString = TestUtils.generateESLicenses(map);
String[] args = new String[6];
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
try {
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
fail();
} catch (IllegalArgumentException e) {
assertTrue("Exception should indicate invalid Type, got: " + e.getMessage(), e.getMessage().contains("Invalid Type"));
}
}
}

View File

@ -163,8 +163,8 @@ public class LicenseVerificationTests extends AbstractLicensingTestBase {
assertTrue("License should have issuer of " + featureAttributes.issuer, license.issuer().equals(featureAttributes.issuer));
assertTrue("License should have issue date of " + DateUtils.longFromDateString(featureAttributes.issueDate), license.issueDate() == DateUtils.longFromDateString(featureAttributes.issueDate));
assertTrue("License should have expiry date of " + DateUtils.longExpiryDateFromString(featureAttributes.expiryDate) + " got: " + license.expiryDate(), license.expiryDate() == DateUtils.longExpiryDateFromString(featureAttributes.expiryDate));
assertTrue("License should have type of " + featureAttributes.type + " got: " + license.type().string(), license.type() == ESLicense.Type.fromString(featureAttributes.type));
assertTrue("License should have subscription type of " + featureAttributes.subscriptionType, license.subscriptionType() == ESLicense.SubscriptionType.fromString(featureAttributes.subscriptionType));
assertTrue("License should have type of " + featureAttributes.type + " got: " + license.type(), license.type().equals(featureAttributes.type));
assertTrue("License should have subscription type of " + featureAttributes.subscriptionType, license.subscriptionType().equals(featureAttributes.subscriptionType));
assertTrue("License should be valid for maxNodes = " + (featureAttributes.maxNodes), license.maxNodes() == featureAttributes.maxNodes);