Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ This is the Python SDK for Oracle Cloud Infrastructure. Supported Python version

__ https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/pythonsdk.htm#pythonsdk_topic-supported_python_versions

Quickstart
==========

.. code-block:: pycon

>>> import oci
# Set up config
>>> config = oci.config.from_file(
... "~/.oci/config",
... "DEFAULT")
# Create a service client
>>> config = oci.config.from_file()
>>> identity = oci.identity.IdentityClient(config)
# Get the current user
>>> user = identity.get_user(config["user"]).data
>>> print(user)
{
Expand All @@ -31,6 +29,26 @@ __ https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/pythonsdk.htm#pythonsd
"time_created": "2016-08-30T23:46:44.680000+00:00"
}

If you need to load a different profile or config file:

.. code-block:: pycon

>>> config = oci.config.from_file("~/.oci/config", "DEFAULT")

You can also point the SDK at a config file via environment variable:

.. code-block:: sh

OCI_CONFIG_FILE=~/.oci/config python your_script.py

Instance principals (running on an OCI compute instance) can be used without a config file:

.. code-block:: python

import oci
signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()
identity = oci.identity.IdentityClient(config={}, signer=signer)

The project is open source and maintained by Oracle Corp. The home page for the project is `here`__.

__ https://docs.oracle.com/en-us/iaas/tools/python/latest/index.html
Expand Down
24 changes: 24 additions & 0 deletions examples/quickstart_identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import oci


def main() -> None:
config = oci.config.from_file()
oci.config.validate_config(config)

identity = oci.identity.IdentityClient(config)
tenancy_id = config["tenancy"]

compartments = identity.list_compartments(
compartment_id=tenancy_id,
compartment_id_in_subtree=True,
access_level="ACCESSIBLE",
).data

print("Accessible compartments:")
for c in compartments:
print(f"- {c.name} ({c.id})")


if __name__ == "__main__":
main()

10 changes: 8 additions & 2 deletions src/oci/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ def from_file(file_location=DEFAULT_LOCATION, profile_name=DEFAULT_PROFILE):
raise ConfigFileNotFound("Could not find config file at {}, please follow the instructions in the link to setup the config file https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm".format(expanded_file_location))

if profile_name not in parser:
raise ProfileNotFound("Profile '{}' not found in config file {} ".format(profile_name, expanded_file_location) + CONFIG_FILE_DEBUG_INFORMATION_LOG)
available_profiles = ", ".join(parser.sections()) if parser.sections() else "(none)"
raise ProfileNotFound(
"Profile '{}' not found in config file {}. Available profiles: {}. ".format(
profile_name, expanded_file_location, available_profiles
)
+ CONFIG_FILE_DEBUG_INFORMATION_LOG
)

config = dict(DEFAULT_CONFIG)
config.update(parser[profile_name])
Expand Down Expand Up @@ -136,7 +142,7 @@ def validate_config(config, **kwargs):
validator_function(config)
return

"""Raises ValueError if required fields are missing or malformed."""
"""Raises InvalidConfig if required fields are missing or malformed."""
errors = {}
for required_key in REQUIRED:
fallback_key = REQUIRED_FALLBACKS.get(required_key)
Expand Down
19 changes: 18 additions & 1 deletion src/oci/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,24 @@ def __init__(self, errors):
self.errors = errors

def __str__(self):
return str(self.errors)
if not isinstance(self.errors, dict):
return str(self.errors)

missing = sorted([k for k, v in self.errors.items() if v == "missing"])
malformed = sorted([k for k, v in self.errors.items() if v == "malformed"])
other = {k: v for k, v in self.errors.items() if v not in ("missing", "malformed")}

parts = ["Invalid OCI config."]
if missing:
parts.append("Missing required keys: {}".format(", ".join(missing)))
if malformed:
parts.append("Malformed values for keys: {}".format(", ".join(malformed)))
if other:
parts.append("Other issues: {}".format(other))
parts.append(
"Config reference: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm"
)
return " ".join(parts)


class InvalidAlloyConfig(ClientError):
Expand Down