Skip to content
Open
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
94 changes: 5 additions & 89 deletions knowledge-content/FinOps/files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Consumption information is reported in the FOCUS report, which resides
on the internal Oracle tenant.

Each day, a function retrieves this report and creates a copy in the
Cost\_Usage\_Report. As soon as the report is created, an event is
Cost_Usage_Report. As soon as the report is created, an event is
triggered, and the data is imported into Logging Analytics using a
specific FOCUS Log Source.

Expand Down Expand Up @@ -132,7 +132,7 @@ endorse group finOps to read objects in tenancy usage-report
allow group finOps to manage analytics-instances in compartment
<finOps compartment>

allow service metering\_overlay to manage objects in compartment
allow service metering_overlay to manage objects in compartment
<finOps compartment>

Allow group finOps to manage functions-family in compartment <finOps
Expand Down Expand Up @@ -190,92 +190,8 @@ fn init --runtime python copyusagereport

cd copyusagereport
```
Go to OCI Shell and edit the func.py
```
import io

import json

import logging

import oci

from datetime import datetime, timedelta

from fdk import response

def handler(ctx, data: io.BytesIO = None):

try:

reporting\_namespace = 'bling'

reporting\_bucket = '<Tenancy OCID>'

yesterday = datetime.now() - timedelta(days=3)

prefix\_file = f"FOCUS
Reports/{yesterday.year}/{yesterday.strftime('%m')}/{yesterday.strftime('%d')}"

print(f"prefix is {prefix\_file}")

destination\_path = '/tmp'

dest\_namespace='frxfz3gch4zb'

upload\_bucket\_name = 'Cost\_Usage\_Reports'

Signer = oci.auth.signers.get\_resource\_principals\_signer()

object\_storage = oci.object\_storage.ObjectStorageClient(config={},
signer=Signer)

report\_bucket\_objects =
oci.pagination.list\_call\_get\_all\_results(object\_storage.list\_objects,
reporting\_namespace, reporting\_bucket, prefix=prefix\_file)

for o in report\_bucket\_objects.data.objects:
Download the [func.py](./src/func.py), modify the teanct OCID, bucket namespace and replace it in the folder copyusagereport.

object\_details = object\_storage.get\_object(reporting\_namespace,
reporting\_bucket, o.name)

filename = o.name.rsplit('/', 1)\[-1\]

local\_file\_path = destination\_path+'/'+filename

with open(local\_file\_path, 'wb') as f:

for chunk in object\_details.data.raw.stream(1024 \* 1024,
decode\_content=False):

f.write(chunk)

with open(local\_file\_path, 'rb') as file\_content:

object\_storage.put\_object(

namespace\_name=dest\_namespace,

bucket\_name=upload\_bucket\_name,

object\_name=filename,

put\_object\_body=file\_content

)

except (Exception, ValueError) as ex:

logging.getLogger().info('error parsing payload: ' + str(ex))

return response.Response(

ctx, response\_data=json.dumps(

{"message": "Processed Files sucessfully"})

)
```
Deploy the function
```
fn -v deploy --app FinOpsX86
Expand Down Expand Up @@ -346,12 +262,12 @@ From OCI Shell console create the json file

"osNamespace": "frxfz3gch4zb",

"osBucketName": "Cost\_Usage\_Reports",
"osBucketName": "Cost_Usage_Reports",

"logGroupId":
"<LogGroup OCID>",

"logSourceName": "FOCUS\_OCI",
"logSourceName": "FOCUS_OCI",

"streamId":"<Stream OCID>"

Expand Down
52 changes: 52 additions & 0 deletions knowledge-content/FinOps/files/src/func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{\rtf1\ansi\ansicpg1252\cocoartf2822
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0

\f0\fs24 \cf0 import io\
import json\
import logging\
import oci\
from datetime import datetime, timedelta\
from fdk import response\
def handler(ctx, data: io.BytesIO = None):\
try:\
\
reporting_namespace = 'bling'\
reporting_bucket = \'91<Tenant OCID>\'92 \
yesterday = datetime.now() - timedelta(days=3)\
prefix_file = f"FOCUS Reports/\{yesterday.year\}/\{yesterday.strftime('%m')\}/\{yesterday.strftime('%d')\}"\
print(f"prefix is \{prefix_file\}")\
destination_path = '/tmp'\
\
dest_namespace=\'91<bucket namespace>\'92\
upload_bucket_name = 'Cost_Usage_Reports' # Replace with your target bucket for uploads\
\
\
\
Signer = oci.auth.signers.get_resource_principals_signer()\
object_storage = oci.object_storage.ObjectStorageClient(config=\{\}, signer=Signer)\
report_bucket_objects = oci.pagination.list_call_get_all_results(object_storage.list_objects, reporting_namespace, reporting_bucket, prefix=prefix_file)\
\
for o in report_bucket_objects.data.objects:\
object_details = object_storage.get_object(reporting_namespace, reporting_bucket, o.name)\
filename = o.name.rsplit('/', 1)[-1]\
local_file_path = destination_path+'/'+filename\
with open(local_file_path, 'wb') as f:\
for chunk in object_details.data.raw.stream(1024 * 1024, decode_content=False):\
f.write(chunk)\
with open(local_file_path, 'rb') as file_content:\
object_storage.put_object(\
namespace_name=dest_namespace,\
bucket_name=upload_bucket_name,\
object_name=filename,\
put_object_body=file_content\
)\
except (Exception, ValueError) as ex:\
logging.getLogger().info('error parsing payload: ' + str(ex))\
return response.Response(\
ctx, response_data=json.dumps(\
\{"message": "Processed Files successfully"\})\
)}