jacek-lewandowski commented on code in PR #2462: URL: https://github.com/apache/cassandra/pull/2462#discussion_r1265235739
########## src/java/org/apache/cassandra/locator/AzureSnitch.java: ########## @@ -0,0 +1,128 @@ +/* + * 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 com.google.common.collect.ImmutableMap; + +import com.fasterxml.jackson.databind.JsonNode; +import org.apache.cassandra.utils.JsonUtils; +import org.apache.cassandra.utils.Pair; + +import static java.lang.String.format; + +/** + * AzureSnitch will resolve datacenter and rack by calling {@code /metadata/instance/compute} endpoint returning + * the response in JSON format for API version {@code 2021-12-13}. The version of API is configurable via property + * {@code azure_api_version} in cassandra-rackdc.properties. + * + * A datacenter is resolved from {@code location} field and a rack is resolved by looking into {@code zone} field first. + * When zone is not set, or it is empty string, it will look into {@code platformFaultDomain} field. Such resolved + * value is prepended by {@code rack-} string. + */ +public class AzureSnitch extends AbstractCloudMetadataServiceSnitch +{ + public AzureSnitch() throws IOException + { + this(new SnitchProperties()); + } + + public AzureSnitch(SnitchProperties properties) throws IOException + { + this(new AzureCloudConnector(properties), properties); + } + + public AzureSnitch(AzureCloudConnector connector, SnitchProperties properties) throws IOException + { + super(connector, properties, resolveDcAndRack(connector, properties)); + } + + private static Pair<String, String> resolveDcAndRack(AzureCloudConnector connector, SnitchProperties properties) throws IOException + { + String response = connector.getResponse(); + JsonNode jsonNode = JsonUtils.JSON_OBJECT_MAPPER.readTree(response); + + JsonNode location = jsonNode.get("location"); + JsonNode zone = jsonNode.get("zone"); + JsonNode platformFaultDomain = jsonNode.get("platformFaultDomain"); + + String datacenter; + String rack; + + if (location == null || location.isNull() || location.asText().isEmpty()) + datacenter = DEFAULT_DC; + else + { Review Comment: nit: braces seems redundant -- 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]

