Skip to content

oci-apigw-nosql-node #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2023
Merged
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
158 changes: 158 additions & 0 deletions samples/oci-apigw-nosql-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Function that reads data using the OCI Node.js for Oracle NoSQL Database

This function uses Resource Principals to securely authorize a function to make
API calls to Oracle NoSQL Database. You can query all tables in a compartment

As you make your way through this tutorial, look out for this icon ![user input icon](../../images/userinput.png).
Whenever you see it, it's time for you to perform an action.


## Prerequisites

1. Before you deploy this sample function, make sure you have run steps A, B
and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsquickstartcloudshell.htm)
* A - Set up your tenancy
* B - Create application
* C - Set up your Cloud Shell dev environment


## List Applications

Assuming you have successfully completed the prerequisites, you should see your
application in the list of applications.

```
fn ls apps
```


## Create or Update your Dynamic Group

In order to use other OCI Services, your function must be part of a dynamic
group. For information on how to create a dynamic group, refer to the
[documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Tasks/managingdynamicgroups.htm#To).

![user input icon](../../images/userinput.png)


When specifying the *Matching Rules*, we suggest matching all functions in a compartment with:

```
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
```


## Create or Update IAM Policies

Create a new policy that allows the dynamic group to `manage objects` in the functions related compartment.

![user input icon](../../images/userinput.png)

Your policy should look something like this:
```
Allow dynamic-group <dynamic-group-name> to manage nosql-family in compartment <compartment-name>
```
e.g.
```
Allow dynamic-group demo-func-dyn-group to manage nosql-family in compartment demo-func-compartment
```
For more information on how to create policies, go [here](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).


## Review and customize the function

Review the following files in the current folder:

- [package.json](./package.json) specifies all the dependencies for your function
- [func.yaml](./func.yaml) that contains metadata about your function and declares properties
- [func.js](./func.js) which is the Node.js function

## Deploy the function

In Cloud Shell, run the `fn deploy` command to build the function and its dependencies as a Docker image,
push the image to the specified Docker registry, and deploy the function to Oracle Functions
in the application created earlier:

![user input icon](../../images/userinput.png)

```
COMP_ID="<your_cmpid>"
fn config app <app-name> NOSQL_COMPARTMENT_ID $COMP_ID
fn config app <app-name> NOSQL_REGION $OCI_REGION
fn config app <app-name> FN_API_KEY <API key value>

```

e.g.
```
COMP_ID=`oci iam compartment list --all --name demo-func-compartment | jq -r '."data"[].id'`
fn config app myapp NOSQL_COMPARTMENT_ID $COMP_ID
fn config app myapp NOSQL_REGION $OCI_REGION
fn config app myapp FN_API_KEY "MY_FN_API_KEY_VALUE"
```



```
fn -v deploy --app <app-name>
```
e.g.
```
fn -v deploy --app myapp
```


## Create Nosql Tables

![user input icon](../../images/userinput.png)


````
COMP_ID=`oci iam compartment list --all --name demo-func-compartment | jq -r '."data"[].id'`

DDL_TABLE="CREATE TABLE IF NOT EXISTS Tutorial (id LONG GENERATED BY DEFAULT AS IDENTITY (NO CYCLE), kv_json_ JSON, PRIMARY KEY( id ))"
echo $DDL_TABLE

oci nosql table create --compartment-id "$COMP_ID" \
--name Tutorial --ddl-statement "$DDL_TABLE" \
--table-limits="{\"maxReadUnits\": 50, \"maxStorageInGBs\": 25, \"maxWriteUnits\": 50 }" \
--wait-for-state SUCCEEDED --wait-for-state FAILED

oci nosql row update --compartment-id "$COMP_ID" --table-name-or-id Tutorial \
--value '{"kv_json_": { "author": { "name": "Dario VEGA"}, "title": "Oracle Functions Samples with NOSQL DB"}}'
````

## Test

![user input icon](../../images/userinput.png)
```
echo -n <JSON-object> | fn invoke <app-name> <function-name>
```
e.g.
```
echo '{"tableName":"Tutorial"}' | fn invoke myapp oci-apigw-nosql-node | jq
```

You should see the following JSON document appear in the terminal.
```
[
{
"id": 1,
"kv_json_": {
"author": {
"name": "Dario VEGA"
},
"title": "Oracle Functions Samples with NOSQL DB"
}
}
]
```


## Clean Up

```
oci nosql table delete --compartment-id "$COMP_ID" --table-name-or-id Tutorial
```


Loading