|
| 1 | +# Function that reads data using the OCI Node.js for Oracle NoSQL Database |
| 2 | + |
| 3 | +This function uses Resource Principals to securely authorize a function to make |
| 4 | +API calls to Oracle NoSQL Database. You can query all tables in a compartment |
| 5 | + |
| 6 | +As you make your way through this tutorial, look out for this icon . |
| 7 | +Whenever you see it, it's time for you to perform an action. |
| 8 | + |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +1. Before you deploy this sample function, make sure you have run steps A, B |
| 13 | +and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsquickstartcloudshell.htm) |
| 14 | + * A - Set up your tenancy |
| 15 | + * B - Create application |
| 16 | + * C - Set up your Cloud Shell dev environment |
| 17 | + |
| 18 | + |
| 19 | +## List Applications |
| 20 | + |
| 21 | +Assuming you have successfully completed the prerequisites, you should see your |
| 22 | +application in the list of applications. |
| 23 | + |
| 24 | +``` |
| 25 | +fn ls apps |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Create or Update your Dynamic Group |
| 30 | + |
| 31 | +In order to use other OCI Services, your function must be part of a dynamic |
| 32 | +group. For information on how to create a dynamic group, refer to the |
| 33 | +[documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Tasks/managingdynamicgroups.htm#To). |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +When specifying the *Matching Rules*, we suggest matching all functions in a compartment with: |
| 39 | + |
| 40 | +``` |
| 41 | +ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'} |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +## Create or Update IAM Policies |
| 46 | + |
| 47 | +Create a new policy that allows the dynamic group to `manage objects` in the functions related compartment. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +Your policy should look something like this: |
| 52 | +``` |
| 53 | +Allow dynamic-group <dynamic-group-name> to manage nosql-family in compartment <compartment-name> |
| 54 | +``` |
| 55 | +e.g. |
| 56 | +``` |
| 57 | +Allow dynamic-group demo-func-dyn-group to manage nosql-family in compartment demo-func-compartment |
| 58 | +``` |
| 59 | +For more information on how to create policies, go [here](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm). |
| 60 | + |
| 61 | + |
| 62 | +## Review and customize the function |
| 63 | + |
| 64 | +Review the following files in the current folder: |
| 65 | + |
| 66 | +- [package.json](./package.json) specifies all the dependencies for your function |
| 67 | +- [func.yaml](./func.yaml) that contains metadata about your function and declares properties |
| 68 | +- [func.js](./func.js) which is the Node.js function |
| 69 | + |
| 70 | +## Deploy the function |
| 71 | + |
| 72 | +In Cloud Shell, run the `fn deploy` command to build the function and its dependencies as a Docker image, |
| 73 | +push the image to the specified Docker registry, and deploy the function to Oracle Functions |
| 74 | +in the application created earlier: |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +``` |
| 79 | +COMP_ID="<your_cmpid>" |
| 80 | +fn config app <app-name> NOSQL_COMPARTMENT_ID $COMP_ID |
| 81 | +fn config app <app-name> NOSQL_REGION $OCI_REGION |
| 82 | +fn config app <app-name> FN_API_KEY <API key value> |
| 83 | +
|
| 84 | +``` |
| 85 | + |
| 86 | +e.g. |
| 87 | +``` |
| 88 | +COMP_ID=`oci iam compartment list --all --name demo-func-compartment | jq -r '."data"[].id'` |
| 89 | +fn config app myapp NOSQL_COMPARTMENT_ID $COMP_ID |
| 90 | +fn config app myapp NOSQL_REGION $OCI_REGION |
| 91 | +fn config app myapp FN_API_KEY "MY_FN_API_KEY_VALUE" |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +``` |
| 97 | +fn -v deploy --app <app-name> |
| 98 | +``` |
| 99 | +e.g. |
| 100 | +``` |
| 101 | +fn -v deploy --app myapp |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +## Create Nosql Tables |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +```` |
| 111 | +COMP_ID=`oci iam compartment list --all --name demo-func-compartment | jq -r '."data"[].id'` |
| 112 | +
|
| 113 | +DDL_TABLE="CREATE TABLE IF NOT EXISTS Tutorial (id LONG GENERATED BY DEFAULT AS IDENTITY (NO CYCLE), kv_json_ JSON, PRIMARY KEY( id ))" |
| 114 | +echo $DDL_TABLE |
| 115 | +
|
| 116 | +oci nosql table create --compartment-id "$COMP_ID" \ |
| 117 | +--name Tutorial --ddl-statement "$DDL_TABLE" \ |
| 118 | +--table-limits="{\"maxReadUnits\": 50, \"maxStorageInGBs\": 25, \"maxWriteUnits\": 50 }" \ |
| 119 | +--wait-for-state SUCCEEDED --wait-for-state FAILED |
| 120 | +
|
| 121 | +oci nosql row update --compartment-id "$COMP_ID" --table-name-or-id Tutorial \ |
| 122 | +--value '{"kv_json_": { "author": { "name": "Dario VEGA"}, "title": "Oracle Functions Samples with NOSQL DB"}}' |
| 123 | +```` |
| 124 | + |
| 125 | +## Test |
| 126 | + |
| 127 | + |
| 128 | +``` |
| 129 | +echo -n <JSON-object> | fn invoke <app-name> <function-name> |
| 130 | +``` |
| 131 | +e.g. |
| 132 | +``` |
| 133 | +echo '{"tableName":"Tutorial"}' | fn invoke myapp oci-apigw-nosql-node | jq |
| 134 | +``` |
| 135 | + |
| 136 | +You should see the following JSON document appear in the terminal. |
| 137 | +``` |
| 138 | +[ |
| 139 | + { |
| 140 | + "id": 1, |
| 141 | + "kv_json_": { |
| 142 | + "author": { |
| 143 | + "name": "Dario VEGA" |
| 144 | + }, |
| 145 | + "title": "Oracle Functions Samples with NOSQL DB" |
| 146 | + } |
| 147 | + } |
| 148 | +] |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +## Clean Up |
| 153 | + |
| 154 | +``` |
| 155 | +oci nosql table delete --compartment-id "$COMP_ID" --table-name-or-id Tutorial |
| 156 | +``` |
| 157 | + |
| 158 | + |
0 commit comments