-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and serverless implementation to api gateway
- Loading branch information
Showing
11 changed files
with
275 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
USERNAME= | ||
PASSWORD= | ||
API_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Lambda Deployment Guide | ||
|
||
Follow these steps to deploy your Lambda function using the Serverless Framework. | ||
|
||
## Prerequisites | ||
|
||
- Make sure you have Node.js and npm installed on your machine. | ||
- You need to have an AWS account and obtain your AWS access key and secret. | ||
|
||
## Installation Steps | ||
|
||
### 1. Install Serverless Framework | ||
|
||
```bash | ||
npm install -g serverless | ||
``` | ||
|
||
### 2. Install Serverless Plugins | ||
|
||
```bash | ||
serverless plugin install -n serverless-python-requirements | ||
``` | ||
|
||
### 3. Configure AWS Credentials | ||
|
||
```bash | ||
serverless config credentials --provider aws --key YOUR_KEY --secret YOUR_SECRET | ||
``` | ||
|
||
### 4. Configure Environment Variables | ||
|
||
```bash | ||
cp .env.example .env | ||
``` | ||
|
||
### 5. Deploy Lambda | ||
|
||
```bash | ||
sls deploy | ||
``` |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from os import getenv | ||
|
||
ENVIRONMENT_VARIABLES = { | ||
"USERNAME": getenv("API_USERNAME"), | ||
"PASSWORD": getenv("API_PASSWORD"), | ||
"API_URL": getenv("API_URL"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import os | ||
|
||
import requests | ||
|
||
from common.constants.environment_variables import ENVIRONMENT_VARIABLES | ||
|
||
from requests.auth import HTTPBasicAuth | ||
|
||
|
||
def handle_history(event, context): | ||
# Logic for /servicenow/history | ||
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/sysapproval_approver?sysparm_limit=10" | ||
|
||
|
||
try: | ||
response = requests.get( | ||
url, | ||
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD']) | ||
) | ||
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx) | ||
|
||
data = response.json() | ||
# Process the data as needed | ||
print(data) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(data) | ||
} | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps({'error': str(e)}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import json | ||
|
||
import requests | ||
from requests.auth import HTTPBasicAuth | ||
|
||
from common.constants.environment_variables import ENVIRONMENT_VARIABLES | ||
|
||
|
||
def handle_item(event, context): | ||
if 'path' not in event: | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({'error': 'Invalid request'}) | ||
} | ||
item_id = event['path'].split('/')[-1] | ||
|
||
# Logic for /servicenow/item/<item_id> | ||
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/x_rnod_csod_serv_0_service_ops_request/{item_id}" | ||
|
||
|
||
try: | ||
response = requests.get( | ||
url, | ||
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD']) | ||
) | ||
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx) | ||
|
||
data = response.json() | ||
# Process the data as needed | ||
print(data) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(data) | ||
} | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps({'error': str(e)}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
|
||
import requests | ||
from requests.auth import HTTPBasicAuth | ||
|
||
from common.constants.environment_variables import ENVIRONMENT_VARIABLES | ||
|
||
|
||
def handle_items(): | ||
# Logic for /servicenow/items | ||
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/sysapproval_approver?sysparm_limit=10&sysparm_display_value=true" | ||
|
||
|
||
try: | ||
response = requests.get( | ||
url, | ||
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD']) | ||
) | ||
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx) | ||
|
||
data = response.json() | ||
# Process the data as needed | ||
print(data) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(data) | ||
} | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps({'error': str(e)}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
|
||
import requests | ||
from requests.auth import HTTPBasicAuth | ||
|
||
from common.constants.environment_variables import ENVIRONMENT_VARIABLES | ||
|
||
|
||
def handle_patch_item(event, context): | ||
# Logic for handling PATCH on /servicenow/item/<item_id> | ||
if 'path' not in event or 'body' not in event: | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({'error': 'Invalid request'}) | ||
} | ||
item_id = event['path'].split('/')[-1] | ||
patch_data = event['body'] | ||
url = f"{ENVIRONMENT_VARIABLES['API_URL']}/sysapproval_approver/{item_id}" | ||
|
||
try: | ||
response = requests.patch( | ||
url, | ||
auth=HTTPBasicAuth(ENVIRONMENT_VARIABLES['USERNAME'], ENVIRONMENT_VARIABLES['PASSWORD']), | ||
headers={'Content-Type': 'application/json'}, | ||
data=patch_data | ||
) | ||
response.raise_for_status() # Raise an exception for bad responses (4xx, 5xx) | ||
|
||
patched_data = response.json() | ||
# Process the patched data as needed | ||
print(patched_data) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(patched_data) | ||
} | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps({'error': str(e)}) | ||
} |
Oops, something went wrong.