Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6b1eb6

Browse files
committed
adding oci-oic-hcm-object-upload
1 parent d97ec51 commit b6b1eb6

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
__pycache__
3+
test.py
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Simple Object Upload Function
2+
This function is used to simply demonstrate connectivity with SaaS applications through OIC. It takes POST request parameters and puts data into a csv file that is uploaded onto Object Storage.
3+
4+
As you make your way through this tutorial, look out for this icon ![user input icon](./images/userinput.png).
5+
Whenever you see it, it's time for you to perform an action.
6+
7+
8+
## Prerequisites
9+
Before you deploy this sample function, make sure you have run step A, B and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html)
10+
* A - Set up your tenancy
11+
* B - Create application
12+
* C - Set up your Cloud Shell dev environment
13+
14+
15+
## List Applications
16+
Assuming your have successfully completed the prerequisites, you should see your
17+
application in the list of applications.
18+
```
19+
fn ls apps
20+
```
21+
22+
23+
## Create or Update your Dynamic Group
24+
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).
25+
26+
When specifying the *Matching Rules*, we suggest matching all functions in a compartment with:
27+
```
28+
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
29+
```
30+
Please check the [Accessing Other Oracle Cloud Infrastructure Resources from Running Functions](https://docs.cloud.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsaccessingociresources.htm) for other *Matching Rules* options.
31+
32+
33+
## Create or Update IAM Policies
34+
Create a new policy that allows the dynamic group to manage compute instances. We will grant `inspect/use/manage` access to `xxx` in the compartment.
35+
36+
![user input icon](./images/userinput.png)
37+
38+
Your policy should look something like this:
39+
```
40+
Allow dynamic-group <dynamic-group-name> to xxx XXX in compartment <compartment-name>
41+
```
42+
43+
For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).
44+
45+
46+
## Review and customize the function
47+
Review the following files in the current folder:
48+
* the code of the function, [func.py](./func.py)
49+
* its dependencies, [requirements.txt](./requirements.txt)
50+
* the function metadata, [func.yaml](./func.yaml)
51+
52+
53+
## Deploy the function
54+
In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image,
55+
push the image to OCIR, and deploy the function to Oracle Functions in your application.
56+
57+
![user input icon](./images/userinput.png)
58+
```
59+
fn -v deploy --app <app-name>
60+
```
61+
62+
## Set the function configuration values
63+
The function requires the following configuration values to be set:
64+
- OCI_NAMESPACE
65+
66+
![user input icon](./images/userinput.png)
67+
68+
Use the *fn CLI* to set the config value:
69+
```
70+
fn config function <app-name> <function-name> OCI_NAMESPACE <NAMESPACE>
71+
```
72+
e.g.
73+
```
74+
fn config function myapp OCI_NAMESPACE mynamespace
75+
```
76+
77+
## Expected Function Input
78+
```bash
79+
# the function expects the following payload:
80+
{
81+
"firstname": "",
82+
"lastname": "",
83+
"workemail": "",
84+
"hiredate": "",
85+
"effectivestartdate": "",
86+
"personid": ""
87+
}
88+
```
2.96 KB
Loading[フレーム]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# oci-oic-hsm-object-upload version 1.0.
3+
#
4+
# Copyright (c) 2020 Oracle, Inc.
5+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
6+
#
7+
8+
from fdk import response
9+
import os
10+
import io
11+
import json
12+
import sys
13+
import oci.object_storage
14+
15+
16+
def handler(ctx, data: io.BytesIO = None):
17+
18+
signer = oci.auth.signers.get_resource_principals_signer()
19+
bucketname = "saas-oic-oci-poc"
20+
21+
try:
22+
cfg = ctx.Config()
23+
cfg_namespace = cfg["OCI_NAMESPACE"]
24+
except Exception as ex:
25+
print("Error: Configuration key has not been set.", ex, flush=True)
26+
raise
27+
28+
try:
29+
info = json.load(data)
30+
firstname = info.get("firstname")
31+
lastname = info.get("lastname")
32+
workemail = info.get("workemail")
33+
hiredate = info.get("hiredate")
34+
effectivestartdate = info.get("effectivestartdate")
35+
personid = info.get("personid")
36+
37+
except (Exception, ValueError) as ex:
38+
return str(ex)
39+
40+
filename = personid + "_data.csv"
41+
personinfo = personid + ", " + firstname + ", " + lastname + ", " + workemail + ", " + hiredate + ", " + effectivestartdate
42+
43+
return_output = put_object(signer, bucketname, filename, personinfo, cfg_namespace=cfg_namespace)
44+
return response.Response(
45+
ctx,
46+
response_data=json.dumps(return_output),
47+
headers={"Content-Type": "application/json"}
48+
)
49+
50+
51+
def put_object(signer, bucketname, objectname, content, cfg_namespace):
52+
client = oci.object_storage.ObjectStorageClient(config={}, signer=signer)
53+
try:
54+
client.put_object(cfg_namespace, bucketname, objectname, content)
55+
output = "Success State"
56+
except Exception as e:
57+
output = "Failed State"
58+
response = {"state": output}
59+
return response
60+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
schema_version: 20180708
2+
name: processemployee
3+
version: 0.0.1
4+
runtime: python
5+
entrypoint: /python/bin/fdk /function/func.py handler
6+
memory: 256
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fdk
2+
requests
3+
oci

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /