Skip to content

Commit

Permalink
Extract GCPMetadataConfig into support library
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Dec 12, 2023
1 parent 63bb79f commit b0a0878
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 43 deletions.
16 changes: 5 additions & 11 deletions detectors/resources-support/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id 'java'
}

group 'org.example'

repositories {
mavenCentral()
}
description = 'Support library for Google Cloud Resource Detector'

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
useJUnitPlatform()
afterEvaluate {
tasks.named("compileJava"){
options.release = 8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ public GCPMetadataConfig(String url) {
this.url = url;
}

boolean isRunningOnGcp() {
public boolean isRunningOnGcp() {
return getProjectId() != null && !getProjectId().isEmpty();
}

// Returns null on failure to retrieve from metadata server
String getProjectId() {
public String getProjectId() {
return getAttribute("project/project-id");
}

// Example response: projects/640212054955/zones/australia-southeast1-a
// Returns null on failure to retrieve from metadata server
String getZone() {
public String getZone() {
String zone = getAttribute("instance/zone");
if (zone != null && zone.contains("/")) {
zone = zone.substring(zone.lastIndexOf('/') + 1);
Expand All @@ -66,7 +66,7 @@ String getZone() {
// method involve detecting region in GAE standard environment
// Example response: projects/5689182099321/regions/us-central1
// Returns null on failure to retrieve from metadata server
String getRegion() {
public String getRegion() {
String region = getAttribute("instance/region");
if (region != null && region.contains("/")) {
region = region.substring(region.lastIndexOf('/') + 1);
Expand All @@ -75,7 +75,7 @@ String getRegion() {
}

// Example response: projects/640212054955/machineTypes/e2-medium
String getMachineType() {
public String getMachineType() {
String machineType = getAttribute("instance/machine-type");
if (machineType != null && machineType.contains("/")) {
machineType = machineType.substring(machineType.lastIndexOf('/') + 1);
Expand All @@ -84,27 +84,27 @@ String getMachineType() {
}

// Returns null on failure to retrieve from metadata server
String getInstanceId() {
public String getInstanceId() {
return getAttribute("instance/id");
}

// Returns null on failure to retrieve from metadata server
String getClusterName() {
public String getClusterName() {
return getAttribute("instance/attributes/cluster-name");
}

// Returns null on failure to retrieve from metadata server
String getClusterLocation() {
public String getClusterLocation() {
return getAttribute("instance/attributes/cluster-location");
}

// Returns null on failure to retrieve from metadata server
String getInstanceHostName() {
public String getInstanceHostName() {
return getAttribute("instance/hostname");
}

// Returns null on failure to retrieve from metadata server
String getInstanceName() {
public String getInstanceName() {
return getAttribute("instance/name");
}

Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions detectors/resources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation(libraries.opentelemetry_sdk_autoconf)
implementation(libraries.opentelemetry_semconv)
implementation platform(libraries.opentelemetry_bom)
implementation project(':detector-resources-support')
testImplementation(testLibraries.assertj)
testImplementation(testLibraries.junit)
testImplementation(testLibraries.wiremock)
Expand Down

0 comments on commit b0a0878

Please sign in to comment.