リージョンの追加
Oracle Cloud Infrastructure SDKに新しいリージョンを追加できます。
SDKにリージョンを追加する方法は、大きく分けて3つあります:
- リージョンの情報を含むSDKが実行されているマシンでリージョン構成ファイルを作成します
OCI_REGION_METADATA
リージョン・メタデータ環境変数を設定します
- 対象のリージョン内のOCIインスタンスでSDKが実行されている場合は、プログラムでオプトインし、インスタンス・メタデータ・サービスからリージョンの情報を解決します
リージョンのメタデータ・スキーマ
どの方法を使用した場合も、1つのリージョンのメタデータ・スキーマは次のようになります:
{
"realmKey" : string,
"realmDomainComponent" : string,
"regionKey" : string,
"regionIdentifier" : string
}
上のフィールド名の値は、ここで説明されているとおりに、フィールド名/値に正確にマッピングされます。
次の例で示すのは、シドニーのOC1リージョンです:
{
"realmKey" : "OC1",
"realmDomainComponent" : "oraclecloud.com",
"regionKey" : "SYD",
"regionIdentifier" : "ap-sydney-1"
}
リージョン環境変数
OCI_REGION_METADATA
環境変数を設定して、プリンシパル・リージョンを指定できます。値はJSON blobで、文字列として格納されます。例:
export OCI_REGION_METADATA='{"realmKey":"OC1","realmDomainComponent":"oraclecloud.com","regionKey":"SYD","regionIdentifier":"ap-sydney-1"}'
リージョン構成ファイル
リージョン構成ファイル(~/.oci/regions-config.json
)を使用すると、SDKで認識されていないリージョンに関するメタデータを指定できます。
リージョン構成ファイルには、1つ以上のリージョンのメタデータが含まれています。ファイルのコンテンツはJSON配列で、配列内の各項目はリージョン・メタデータ・スキーマに一致するオブジェクトです。
次に、有効なリージョン構成ファイルの例を示します:
[
{
"realmKey" : "OC1",
"realmDomainComponent" : "oraclecloud.com",
"regionKey" : "SYD",
"regionIdentifier" : "ap-sydney-1"
}
]
インスタンス・メタデータ・サービスからのプログラムによる解決
インスタンス・メタデータ・サービスは、1つのリージョン(インスタンス・メタデータ・サービスをホストするインスタンスが所属するリージョン)のメタデータを返します。SDKがOCIインスタンス内で実行されていない可能性があるため、このオプションはデフォルトでは有効になっていません。この項の例では、インスタンス・メタデータ・サービスからのリージョン・メタデータの取得を有効にする方法を示します。
例
この項では、インスタンス・メタデータ・サービスにオプトインする方法の例を示します。
SDK for Java
public void optInInstanceMetadataService() {
//opt in for IMDS call by calling enableInstanceMetadataService
Region.enableInstanceMetadataService();
//set timeout settings for IMDS call(optional)
Region.setInstanceMetadataServiceClientConfig(
new ClientConfig()
.property(ClientProperties.CONNECT_TIMEOUT, 1000)
.property(ClientProperties.READ_TIMEOUT, 5000));
//use any of the below calls to set region in client
Region region = Region.fromRegionId("ap-sydney-1");
myClient.setRegion(region);
//or
Region.registerFromInstanceMetadataService();
myClient.setRegion("ap-sydney-1");
//or
Region.registerFromInstanceMetadataService();
myClient.setRegion(Region.getRegionFromImds());
}
SDK for Python
import oci
# Set up config
config = oci.config.from_file("~/.oci/config", "DEFAULT")
# Opt-in to IMDS lookup
oci.regions.enable_instance_metadata_service()
# Create a service client
identity = oci.identity.IdentityClient(config)
# Get the current user
user = identity.get_user(config["user"]).data
print(user)
SDK for Ruby
require 'oci'
# opt-in for IMDS call
OCI::Regions.enable_instance_metadata_service
# create instance principal signer
instance_principals_signer = OCI::Auth::Signers::InstancePrincipalsSecurityTokenSigner.new
# use instance principal signer to create client
identity = OCI::Identity::IdentityClient.new(signer: instance_principals_signer)
pp identity.get_compartment(config.tenancy)
SDK for Go
import (
"context"
"fmt"
"log"
"github.com/oracle/oci-go-sdk/common"
"github.com/oracle/oci-go-sdk/common/auth"
"github.com/oracle/oci-go-sdk/example/helpers"
"github.com/oracle/oci-go-sdk/identity"
)
func main() {
// Opt in instance metadata service region info lookup
common.EnableInstanceMetadataServiceLookup()
provider, err := auth.InstancePrincipalConfigurationProvider()
helpers.FatalIfError(err)
tenancyID := "exampleTenancyID"
request := identity.ListAvailabilityDomainsRequest{
CompartmentId: &tenancyID,
}
client, err := identity.NewIdentityClientWithConfigurationProvider(provider)
// Override the region, this is an optional step.
// the InstancePrincipalsConfigurationProvider defaults to the region
// in which the compute instance is currently running
client.SetRegion("syd")
r, err := client.ListAvailabilityDomains(context.Background(), request)
helpers.FatalIfError(err)
log.Printf("list of available domains: %v", r.Items)
}
SDK for .NET
using System;
using System.Threading.Tasks;
using Oci.Common;
using Oci.Common.Auth;
using Oci.IdentityService;
using Oci.IdentityService.Requests;
namespace Oci.Examples
{
public class RegionIMDSExample
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static async Task MainIMDS()
{
// Creates an Instance Principal provider that holds authentication details of the OCI Instance
// This helps in making API requests by the Instance without user involvement
var instanceProvider = new InstancePrincipalsAuthenticationDetailsProvider();
// Create a client for the service to enable using its APIs
var client = new IdentityClient(instanceProvider, new ClientConfiguration());
Region.EnableInstanceMetadataService();
//use any of the below calls to set region in client
Region region = Region.FromRegionId("ap-sydney-1");
client.SetRegion(region);
//OR
Region imdsRegion = Region.RegisterRegionFromInstanceMetadataService();
client.SetRegion(imdsRegion);
// Use the client to make calls to the endpoint for the new region
await ListAllRegions(client);
}
private static async Task ListAllRegions(IdentityClient client)
{
logger.Info("Querying for list of regions");
var response = await client.ListRegions(new ListRegionsRequest { });
foreach (var region in response.Items)
{
logger.Info($"Region: {region.Name}");
}
}
}
}
PowerShellモジュール
PS /> Import-Module OCI.PSModules.Common
PS /> Import-Module OCI.PSModules.Identity
PS /> $Region = Register-OCIRegion -EnableInstanceMetadataService
# Use this region on a cmdlet call.
PS /> Get-OCIIdentityUsersList -Region $Region.RegionId
# Or set the region as the default.
PS /> Set-OCIClientConfig -RegionID $Region.RegionId
CLI
export OCI_CLI_USE_INSTANCE_METADATA_SERVICE=true
Terraform
#Workaround for now is to use the `domain_name_override` environment setting to specify the region and realm.
# So in your Terraform configuration block, you can specify the region:
provider oci {
region = "ap-sydney-1"
}
# And outside of Terraform, set an environment variable with the correct realm for this region like this:
export domain_name_override=oraclecloud.com
export visitIMDS=true