oracle / oci-python-sdk

Oracle Cloud Infrastructure SDK for Python
https://cloud.oracle.com/cloud-infrastructure
Other
387 stars 280 forks source link

Please provide a simple method to obtain the instance metadata for the instance on which the SDK is being used #171

Open ghost opened 5 years ago

ghost commented 5 years ago

I know it's relatively simple to fetch http://169.254.169.254/opc/v1/instance/ and pass it through a json parser to get the metadata object. having this functionality in the SDK, where you're already doing these types of operations, removes a level of code duplication (and removes one more bit of tedious coding that everyone has to do).

jasonyin commented 5 years ago

hi @jearls, thanks for submitting the request. I am wondering what's your use cases for using metadata service? If you are trying to use InstanceAuth, the 'InstancePrincipalsSecurityTokenSigner' already handle that.

darkfoxprime commented 5 years ago

The InstancePrincipalsSecurityTokenSigner only provides the region short name from the metadata, and the tenancy information from the certificate. It does not provide the canonical region name, does not provide the instance id and compartment id, the tags, any custom metadata that's been defined, etc.

adizohar commented 4 years ago

Hi, all the metadata is in the instance object, region is in short version, but you can query the list_regions to get the full name, see example below:

import oci

signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner() config = {'region': signer.region, 'tenancy': signer.tenancy_id}

instance_id = "ocid1.instance.oc1.iad.abuwcljsd27ddpfvlq3boailo2cgrd27bgat7v4e53mk4d5zb4z2d33loohq"

compute_client = oci.core.ComputeClient(config, signer=signer) compute_meta = compute_client.get_instance(instance_id).data

identity_client = oci.identity.IdentityClient(config, signer=signer) regions = identity_client.list_regions().data

region = [k.name for k in regions if k.key == compute_meta.region.upper()][0]

compute_meta region

darkfoxprime commented 4 years ago

@adizohar your method requires that you know the instance id on which you are running. to get that, you need to access the metadata service. thus my original request - to provide a utility function that fetches the instance metadata from the metadata service, rather than each person who uses this library needing to do the same code on their own. (utility function or other standardized mechanism for such access)

adizohar commented 4 years ago

For the SDK you need authenticate, either by instance principles, delegation token or apikey You cannot get data without it (for security)

If you want the metadata When running on the host, use the request package and call the metadata url

I don’t understand what exactly the issue ? Please give an example what exactly you need and the use case

dldinternet commented 2 years ago

Simple way to visualize the ask

metadata = oci.core.ComputeInstanceMetadata() compartment_id = metadata['compartmentId']

ogrovlen commented 1 year ago

I came here looking for the same, but the comment from @adizohar realize I could achieve this with:

import requests
import json

headers = {'Authorization': 'Bearer Oracle'}
r = requests.get('http://169.254.169.254/opc/v2/instance/', headers=headers)
instance=r.json()['id']