|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. |
| 4 | + |
| 5 | +# This example uses functions service to get the rpv2.2 token, for how to use functions, please refer fn Tutorials: https://fnproject.io/tutorials/ |
| 6 | +# Users can set up Fn locally or with oci cloud shell, the tutoials can be found here: https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html |
| 7 | + |
| 8 | +import io |
| 9 | +import json |
| 10 | +import logging |
| 11 | +import oci |
| 12 | +# used by functions service |
| 13 | +from fdk import response |
| 14 | + |
| 15 | + |
| 16 | +def list_regions(client): |
| 17 | + try: |
| 18 | + resp = client.list_regions() |
| 19 | + logging.getLogger().info("region info:", resp.data) |
| 20 | + except oci.exceptions.ServiceError as e: |
| 21 | + logging.getLogger().error("service error: {0}, error code: {1}, opc-request-id: {2}".format(e.message, e.code, e.request_id)) |
| 22 | + return e |
| 23 | + return resp.data |
| 24 | + |
| 25 | + |
| 26 | +def handler(ctx, data: io.BytesIO = None): # noqa: E999 |
| 27 | + rpv2 = oci.auth.signers.get_resource_principals_signer() |
| 28 | + # Print the Resource Principal Security Token |
| 29 | + logging.getLogger().info("resource principle token:", rpv2.get_security_token()) |
| 30 | + |
| 31 | + # Note the config is passed in as an empty dictionary. A populated config |
| 32 | + # is not needed when using an EphemeralResourcePrincipalSigner |
| 33 | + iam_client = oci.identity.IdentityClient({}, signer=rpv2) |
| 34 | + |
| 35 | + resp = list_regions(iam_client) |
| 36 | + return response.Response( |
| 37 | + ctx, response_data=json.dumps( |
| 38 | + {"regions:": str(resp)}), |
| 39 | + headers={"Content-Type": "application/json"} |
| 40 | + ) |
0 commit comments