smiklosovic commented on code in PR #2403: URL: https://github.com/apache/cassandra/pull/2403#discussion_r1243300351
########## src/java/org/apache/cassandra/locator/Ec2MetadataServiceConnector.java: ########## @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.locator; + +import java.io.IOException; +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; + +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.cassandra.utils.Clock; +import org.apache.cassandra.utils.Pair; + +import static java.lang.String.format; +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.joining; +import static org.apache.cassandra.locator.Ec2MetadataServiceConnector.EC2MetadataType.v2; + +abstract class Ec2MetadataServiceConnector extends AbstractCloudMetadataServiceConnector +{ + static final String EC2_METADATA_TYPE_PROPERTY = "ec2_metadata_type"; + static final String EC2_METADATA_URL_PROPERTY = "ec2_metadata_url"; + static final String DEFAULT_EC2_METADATA_URL = "http://169.254.169.254"; + + Ec2MetadataServiceConnector(SnitchProperties properties) + { + metadataServiceUrl = properties.get(EC2_METADATA_URL_PROPERTY, DEFAULT_EC2_METADATA_URL); + } + + enum EC2MetadataType + { + v1(props -> Ec2MetadataServiceConnector.V1Connector.create(props)), + v2(props -> Ec2MetadataServiceConnector.V2Connector.create(props)); + + private final Function<SnitchProperties, Ec2MetadataServiceConnector> creator; + + EC2MetadataType(Function<SnitchProperties, Ec2MetadataServiceConnector> creator) + { + this.creator = creator; + } + + Ec2MetadataServiceConnector create(SnitchProperties properties) + { + return creator.apply(properties); + } + } + + static Ec2MetadataServiceConnector create(SnitchProperties props) + { + try + { + return EC2MetadataType.valueOf(props.get(EC2_METADATA_TYPE_PROPERTY, v2.name())).create(props); + } + catch (IllegalArgumentException ex) + { + throw new ConfigurationException(format("%s must be one of %s", EC2_METADATA_TYPE_PROPERTY, + stream(EC2MetadataType.values()).map(Enum::name).collect(joining(", ")))); + } + } + + static class V1Connector extends Ec2MetadataServiceConnector + { + static V1Connector create(SnitchProperties props) + { + return new V1Connector(props); + } + + V1Connector(SnitchProperties props) + { + super(props); + } + + @Override + public String toString() + { + return V1Connector.class.getName() + '{' + EC2_METADATA_URL_PROPERTY + '=' + metadataServiceUrl + '}'; + } + } + + static class V2Connector extends Ec2MetadataServiceConnector + { + @VisibleForTesting Review Comment: I will gladly get rid of them. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

