Skip to content

Commit

Permalink
Add Imds data retriever as a service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
klustria committed Jul 1, 2024
1 parent 2802c0f commit b4217a3
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 0 deletions.
18 changes: 18 additions & 0 deletions integrations/oci/oci/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common</artifactId>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
Expand All @@ -72,6 +80,16 @@
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
<artifactId>helidon-microprofile-server</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed 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 io.helidon.integrations.oci;

import io.helidon.builder.api.Option;
import io.helidon.builder.api.Prototype;

import jakarta.json.JsonObject;

/**
* Instance information retrieved from Imds
*/
@Prototype.Blueprint
@Prototype.Configured
interface ImdsInstanceInfoBlueprint {
/**
* Display Name.
*
* @return Display Name of the Instance
*/
@Option.Configured
String displayName();

/**
* Host Name.
*
* @return Host Name of the Instance
*/
@Option.Configured
String hostName();

/**
* Canonical Region Name.
*
* @return Canonical Region Name of where the Instance exists
*/
@Option.Configured
String canonicalRegionName();

/**
* Region Name.
*
* @return Short Region Name of where the Instance exists
*/
@Option.Configured
String region();

/**
* Oci Availability Domain Name.
*
* @return Physical Availaibility Domain Name where the Instance exists
*/
@Option.Configured
String ociAdName();

/**
* Fault Domain Name.
*
* @return Fault Domain Name where the Instance exists
*/
@Option.Configured
String faultDomain();

/**
* Compartment Id
*
* @return Compartment Id where the Instance was provisioned.
*/
@Option.Configured
String compartmentId();

/**
* Tenant Id
*
* @return Tenant Id where the Instance was provisioned.
*/
@Option.Configured
String tenantId();

/**
* Instance Data
*
* @return Full information about the Instance as a {@link jakarta.json.JsonObject}
*/
@Option.Configured
JsonObject jsonObject();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed 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 io.helidon.integrations.oci;

import java.net.URI;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.integrations.oci.spi.OciImdsInstanceInfo;
import io.helidon.service.registry.Service;

import jakarta.json.JsonObject;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Service.Provider
@Weight(Weighted.DEFAULT_WEIGHT - 100)
class ImdsInstanceInfoProvider implements OciImdsInstanceInfo {
private static final System.Logger LOGGER = System.getLogger(ImdsInstanceInfoProvider.class.getName());

// Commonly used key names for instance data that will be used for the getter methods
static final String DISPLAY_NAME = "displayName";
static final String HOST_NAME = "hostname";
static final String CANONICAL_REGION_NAME = "canonicalRegionName";
static final String OCI_AD_NAME = "ociAdName";
static final String FAULT_DOMAIN = "faultDomain";
static final String REGION = "region";
static final String COMPARTMENT_ID = "compartmentId";
static final String TENANT_ID = "tenantId";

private LazyValue<Optional<ImdsInstanceInfo>> instanceInfo;

ImdsInstanceInfoProvider(Supplier<OciConfig> config) {
this.instanceInfo = LazyValue.create(() -> Optional.ofNullable(loadInstanceMetadata(config.get())));
}

@Override
public Optional<ImdsInstanceInfo> instanceInfo() {
return instanceInfo.get();
}

ImdsInstanceInfo loadInstanceMetadata(OciConfig ociConfig) {
Optional<URI> uri = ociConfig.imdsBaseUri();
String instanceMetadataUri = uri.isPresent() ? uri.get().toString() : null;
Client client = ClientBuilder.newBuilder()
.connectTimeout(ociConfig.imdsTimeout().toMillis(), TimeUnit.MILLISECONDS)
.readTimeout(ociConfig.imdsTimeout().toMillis(), TimeUnit.MILLISECONDS)
.build();
try {
Response response = client.target(instanceMetadataUri)
.path("instance")
.request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer Oracle")
.get();
System.out.println("Instance Metadata response: " + response);
if (response.getStatus() >= 300) {
LOGGER.log(System.Logger.Level.TRACE,
String.format("Cannot obtain instance metadata: status = %d, entity = '%s'",
response.getStatus(),
response.readEntity(String.class)));
}
JsonObject instanceInfo = response.readEntity(JsonObject.class);
return ImdsInstanceInfo.builder()
.displayName(instanceInfo.getString(DISPLAY_NAME))
.hostName(instanceInfo.getString(HOST_NAME))
.canonicalRegionName(instanceInfo.getString(CANONICAL_REGION_NAME))
.region(instanceInfo.getString(REGION))
.ociAdName(instanceInfo.getString(OCI_AD_NAME))
.faultDomain(instanceInfo.getString(FAULT_DOMAIN))
.compartmentId(instanceInfo.getString(COMPARTMENT_ID))
.tenantId(instanceInfo.getString(TENANT_ID))
.jsonObject(instanceInfo)
.build();
} catch (Exception e) {
LOGGER.log(System.Logger.Level.TRACE,
"Cannot obtain instance metadata",
e);
} finally {
client.close();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed 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 io.helidon.integrations.oci.spi;

import java.util.Optional;

import io.helidon.integrations.oci.ImdsInstanceInfo;
import io.helidon.service.registry.Service;

/**
* An OCI Imds discovery mechanism to provide some relevant instance details as a service in Helidon
* service registry.
*/
@Service.Contract
public interface OciImdsInstanceInfo {

/**
* The Instance information from Imds, if it can be provided by this service.
*
* @return Instance Information retrieved from Imds
*/
Optional<ImdsInstanceInfo> instanceInfo();
}
3 changes: 3 additions & 0 deletions integrations/oci/oci/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
requires oci.java.sdk.common;
requires vavr;

requires jakarta.json;
requires jakarta.ws.rs;

exports io.helidon.integrations.oci;
exports io.helidon.integrations.oci.spi;
}
Loading

0 comments on commit b4217a3

Please sign in to comment.