Skip to content

Commit

Permalink
Remove CloudLocationUtil class
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Jan 4, 2024
1 parent cbaaa31 commit d9c39eb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 106 deletions.
2 changes: 1 addition & 1 deletion detectors/resources-support/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ afterEvaluate {
tasks.named("compileJava"){
options.release = 8
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ String getProjectId() {
return getAttribute("project/project-id");
}

// Example response: projects/640212054955/zones/australia-southeast1-a
// Returns null on failure to retrieve from metadata server
/**
* Method to extract cloud availability zone from the metadata server.
*
* <p>Example response: projects/640212054955/zones/australia-southeast1-a
*
* <p>Example zone: australia-southeast1-a
*
* @return the extracted zone from the metadata server response or null in case of failure to
* retrieve from metadata server.
*/
String getZone() {
String zone = getAttribute("instance/zone");
if (zone != null && zone.contains("/")) {
Expand All @@ -62,10 +70,14 @@ String getZone() {
return zone;
}

// Use this method only when the region cannot be parsed from the zone. Known use-cases of this
// method involve detecting region in GAE standard environment
// Example response: projects/5689182099321/regions/us-central1
// Returns null on failure to retrieve from metadata server
/**
* Use this method only when the region cannot be parsed from the zone. Known use-cases of this
* method involve detecting region in GAE standard environment.
*
* <p>Example response: projects/5689182099321/regions/us-central1.
*
* @return the retrieved region or null in case of failure to retrieve from metadata server
*/
String getRegion() {
String region = getAttribute("instance/region");
if (region != null && region.contains("/")) {
Expand All @@ -74,6 +86,27 @@ String getRegion() {
return region;
}

/**
* Use this method to parse region from zone.
*
* <p>Example region: australia-southeast1
*
* @return parsed region from the zone, if zone is not found or is invalid, this method returns
* null.
*/
String getRegionFromZone() {
String region = null;
String zone = getZone();
if (zone != null && !zone.isEmpty()) {
// Parsing required to scope up to a region
String[] splitArr = zone.split("-");
if (splitArr.length > 2) {
region = String.join("-", splitArr[0], splitArr[1]);
}
}
return region;
}

// Example response: projects/640212054955/machineTypes/e2-medium
String getMachineType() {
String machineType = getAttribute("instance/machine-type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ private Map<String, Optional<String>> prepareAttributes() {
map.put(GAE_MODULE_NAME, Optional.ofNullable(this.environmentVariables.get("GAE_SERVICE")));
map.put(GAE_APP_VERSION, Optional.ofNullable(this.environmentVariables.get("GAE_VERSION")));
map.put(GAE_INSTANCE_ID, Optional.ofNullable(this.environmentVariables.get("GAE_INSTANCE")));
map.put(
GAE_AVAILABILITY_ZONE,
CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig));
map.put(GAE_AVAILABILITY_ZONE, Optional.ofNullable(this.metadataConfig.getZone()));
map.put(GAE_CLOUD_REGION, getCloudRegion());
return Collections.unmodifiableMap(map);
}

private Optional<String> getCloudRegion() {
if (this.environmentVariables.get("GAE_ENV") != null
&& this.environmentVariables.get("GAE_ENV").equals("standard")) {
return CloudLocationUtil.getCloudRegionFromMetadataUsingRegion(this.metadataConfig);
return Optional.ofNullable(this.metadataConfig.getRegion());
} else {
return CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig);
return Optional.ofNullable(this.metadataConfig.getRegionFromZone());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ final class GoogleComputeEngine implements DetectedPlatform {
private Map<String, Optional<String>> prepareAttributes() {
Map<String, Optional<String>> map = new HashMap<>();
map.put(GCE_PROJECT_ID, Optional.ofNullable(this.metadataConfig.getProjectId()));
map.put(
GCE_AVAILABILITY_ZONE,
CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig));
map.put(
GCE_CLOUD_REGION,
CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig));
map.put(GCE_AVAILABILITY_ZONE, Optional.ofNullable(this.metadataConfig.getRegionFromZone()));
map.put(GCE_CLOUD_REGION, Optional.ofNullable(this.metadataConfig.getRegionFromZone()));
map.put(GCE_INSTANCE_ID, Optional.ofNullable(this.metadataConfig.getInstanceId()));
map.put(GCE_INSTANCE_NAME, Optional.ofNullable(this.metadataConfig.getInstanceName()));
map.put(GCE_MACHINE_TYPE, Optional.ofNullable(this.metadataConfig.getMachineType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ private Map<String, Optional<String>> prepareAttributes() {
Optional.ofNullable(this.environmentVariables.get("K_REVISION")));
map.put(
AttributeKeys.SERVERLESS_COMPUTE_AVAILABILITY_ZONE,
CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig));
Optional.ofNullable(this.metadataConfig.getZone()));
map.put(
AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION,
CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig));
Optional.ofNullable(this.metadataConfig.getRegionFromZone()));
map.put(
AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID,
Optional.ofNullable(this.metadataConfig.getInstanceId()));
Expand Down

0 comments on commit d9c39eb

Please sign in to comment.