All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.hazelcast.azure.AzureComputeApi Maven / Gradle / Ivy
/*
* Copyright 2020 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.hazelcast.azure;
import com.hazelcast.internal.json.Json;
import com.hazelcast.internal.json.JsonArray;
import com.hazelcast.internal.json.JsonObject;
import com.hazelcast.internal.json.JsonValue;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import static com.hazelcast.azure.Utils.isEmpty;
/**
* Responsible for connecting to the Azure Cloud Compute API.
*
* @see Azure Compute API
*/
class AzureComputeApi {
static final String API_VERSION = "2018-08-01";
static final String API_VERSION_SCALE_SET = "2018-06-01";
private static final String AZURE_API_ENDPOINT = "https://management.azure.com";
private final String endpoint;
AzureComputeApi() {
this.endpoint = AZURE_API_ENDPOINT;
}
/**
* For test purposes only.
*/
AzureComputeApi(String endpoint) {
this.endpoint = endpoint;
}
Collection instances(String subscriptionId, String resourceGroup, String scaleSet,
Tag tag, String accessToken) {
String privateIpResponse = RestClient
.create(urlForPrivateIpList(subscriptionId, resourceGroup, scaleSet))
.withHeader("Authorization", String.format("Bearer %s", accessToken))
.get();
Map networkInterfaces = parsePrivateIpResponse(privateIpResponse);
String publicIpResponse = RestClient
.create(urlForPublicIpList(subscriptionId, resourceGroup, scaleSet))
.withHeader("Authorization", String.format("Bearer %s", accessToken))
.get();
Map publicIpMap = parsePublicIpResponse(publicIpResponse);
Set addresses = new LinkedHashSet(networkInterfaces.size());
for (AzureNetworkInterface anInterface : networkInterfaces.values()) {
if (tag == null || anInterface.hasTag(tag)) {
addresses.add(new AzureAddress(anInterface.getPrivateIp(), publicIpMap.get(anInterface.getPublicIpId())));
}
}
return addresses;
}
private String urlForPrivateIpList(String subscriptionId, String resourceGroup, String scaleSet) {
if (isEmpty(scaleSet)) {
return String.format("%s/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network"
+ "/networkInterfaces?api-version=%s", endpoint, subscriptionId, resourceGroup, API_VERSION);
} else {
return String.format("%s/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute"
+ "/virtualMachineScaleSets/%s/networkInterfaces?api-version=%s",
endpoint, subscriptionId, resourceGroup, scaleSet, API_VERSION_SCALE_SET);
}
}
private Map parsePrivateIpResponse(String response) {
Map interfaces = new HashMap();
for (JsonValue item : toJsonArray(Json.parse(response).asObject().get("value"))) {
Set tagList = new HashSet<>();
JsonObject tags = toJsonObject(item.asObject().get("tags"));
for (String key : tags.asObject().names()) {
tagList.add(new Tag(key, tags.asObject().getString(key, null)));
}
JsonObject properties = item.asObject().get("properties").asObject();
if (properties.get("virtualMachine") != null) {
for (JsonValue ipConfiguration : toJsonArray(properties.get("ipConfigurations"))) {
JsonObject ipProps = ipConfiguration.asObject().get("properties").asObject();
String privateIp = ipProps.getString("privateIPAddress", null);
String publicIpId = toJsonObject(ipProps.get("publicIPAddress")).getString("id", null);
if (!isEmpty(publicIpId)) {
interfaces.put(publicIpId, new AzureNetworkInterface(privateIp, publicIpId, tagList));
}
}
}
}
return interfaces;
}
private String urlForPublicIpList(String subscriptionId, String resourceGroup, String scaleSet) {
if (isEmpty(scaleSet)) {
return String.format("%s/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network"
+ "/publicIPAddresses?api-version=%s", endpoint, subscriptionId, resourceGroup, API_VERSION);
} else {
return String.format("%s/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute"
+ "/virtualMachineScaleSets/%s/publicIPAddresses?api-version=%s",
endpoint, subscriptionId, resourceGroup, scaleSet, API_VERSION_SCALE_SET);
}
}
private Map parsePublicIpResponse(String response) {
Map publicIps = new HashMap();
for (JsonValue item : toJsonArray(Json.parse(response).asObject().get("value"))) {
String id = item.asObject().getString("id", null);
String ip = toJsonObject(item.asObject().get("properties")).getString("ipAddress", null);
if (!isEmpty(ip)) {
publicIps.put(id, ip);
}
}
return publicIps;
}
private static JsonArray toJsonArray(JsonValue jsonValue) {
if (jsonValue == null || jsonValue.isNull()) {
return new JsonArray();
} else {
return jsonValue.asArray();
}
}
private static JsonObject toJsonObject(JsonValue jsonValue) {
if (jsonValue == null || jsonValue.isNull()) {
return new JsonObject();
} else {
return jsonValue.asObject();
}
}
}