-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Imds data retriever as a service provider
- Loading branch information
Showing
6 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...grations/oci/oci/src/main/java/io/helidon/integrations/oci/ImdsInstanceInfoBlueprint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
104 changes: 104 additions & 0 deletions
104
integrations/oci/oci/src/main/java/io/helidon/integrations/oci/ImdsInstanceInfoProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
integrations/oci/oci/src/main/java/io/helidon/integrations/oci/spi/OciImdsInstanceInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.