From 5c42fef85b8ab4b102e82d25ee35725d78f0a794 Mon Sep 17 00:00:00 2001 From: Gagan Tunuguntla Date: Mon, 29 Apr 2019 09:30:23 -0400 Subject: [PATCH] loader, and move api methods --- api/authorizer/authorizer.py | 206 ++++++++++++++++++ .../src/app/app-routing.module.ts | 4 +- .../angular-code/src/app/app.component.html | 3 + .../angular-code/src/app/app.component.ts | 4 + client-ui/angular-code/src/app/app.module.ts | 8 +- .../angular-code/src/app/core/core.module.ts | 12 +- .../core/guardsAndResolvers/sheet.resolve.ts | 25 +++ .../core/interceptors/loader.interceptor.ts | 21 ++ .../src/app/core/services/loader.service.ts | 15 ++ .../src/app/core/services/sheet.service.ts | 18 +- .../src/app/loader/loader.component.html | 4 + .../src/app/loader/loader.component.scss | 17 ++ .../src/app/loader/loader.component.ts | 19 ++ .../src/environments/environment.ts | 2 +- docker-compose.yml | 12 + sam-local/Dockerfile | 2 + sam-local/sam-app/hello_world/__init__.py | 0 sam-local/sam-app/hello_world/app.py | 42 ---- sam-local/sam-app/sheet/_code_/app.py | 125 +++++++++++ .../_code_}/requirements.txt | 0 .../sam-app/sheet/{sheetName}/_code_/app.py | 170 +++++++++++++++ sam-local/sam-app/template.yaml | 54 ++++- 22 files changed, 694 insertions(+), 69 deletions(-) create mode 100644 api/authorizer/authorizer.py create mode 100644 client-ui/angular-code/src/app/core/guardsAndResolvers/sheet.resolve.ts create mode 100644 client-ui/angular-code/src/app/core/interceptors/loader.interceptor.ts create mode 100644 client-ui/angular-code/src/app/core/services/loader.service.ts create mode 100644 client-ui/angular-code/src/app/loader/loader.component.html create mode 100644 client-ui/angular-code/src/app/loader/loader.component.scss create mode 100644 client-ui/angular-code/src/app/loader/loader.component.ts delete mode 100644 sam-local/sam-app/hello_world/__init__.py delete mode 100644 sam-local/sam-app/hello_world/app.py create mode 100644 sam-local/sam-app/sheet/_code_/app.py rename sam-local/sam-app/{hello_world => sheet/_code_}/requirements.txt (100%) create mode 100644 sam-local/sam-app/sheet/{sheetName}/_code_/app.py diff --git a/api/authorizer/authorizer.py b/api/authorizer/authorizer.py new file mode 100644 index 0000000..2e00a78 --- /dev/null +++ b/api/authorizer/authorizer.py @@ -0,0 +1,206 @@ +""" +Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. +Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at + http://aws.amazon.com/apache2.0/ +or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +""" +from __future__ import print_function + +import re +import time +import pprint +import json + + +def lambda_handler(event, context): + print("Client token: " + event['authorizationToken']) + print("Method ARN: " + event['methodArn']) + + principalId = "user|a1b2c3d4" + tmp = event['methodArn'].split(':') + apiGatewayArnTmp = tmp[5].split('/') + awsAccountId = tmp[4] + + policy = AuthPolicy(principalId, awsAccountId) + policy.restApiId = apiGatewayArnTmp[0] + policy.region = tmp[3] + policy.stage = apiGatewayArnTmp[1] + + policy.allowMethod(HttpVerb.GET, "/sheet/*") + policy.allowMethod(HttpVerb.GET, "/sheet") + policy.denyMethod(HttpVerb.POST, "/sheet") + policy.denyMethod(HttpVerb.POST, "/sheet/*") + policy.denyMethod(HttpVerb.PUT, "/sheet") + policy.denyMethod(HttpVerb.PUT, "/sheet/*") + policy.denyMethod(HttpVerb.DELETE, "/sheet") + policy.denyMethod(HttpVerb.DELETE, "/sheet/*") + + # Finally, build the policy + authResponse = policy.build() + + context = { + 'user': sdad + } + # context['arr'] = ['foo'] <- this is invalid, APIGW will not accept it + # context['obj'] = {'foo':'bar'} <- also invalid + + authResponse['context'] = context + + return authResponse + +class HttpVerb: + GET = "GET" + POST = "POST" + PUT = "PUT" + PATCH = "PATCH" + HEAD = "HEAD" + DELETE = "DELETE" + OPTIONS = "OPTIONS" + ALL = "*" + +class AuthPolicy(object): + awsAccountId = "" + """The AWS account id the policy will be generated for. This is used to create the method ARNs.""" + principalId = "" + """The principal used for the policy, this should be a unique identifier for the end user.""" + version = "2012-10-17" + """The policy version used for the evaluation. This should always be '2012-10-17'""" + pathRegex = "^[/.a-zA-Z0-9-\*]+$" + """The regular expression used to validate resource paths for the policy""" + + """these are the internal lists of allowed and denied methods. These are lists + of objects and each object has 2 properties: A resource ARN and a nullable + conditions statement. + the build method processes these lists and generates the approriate + statements for the final policy""" + allowMethods = [] + denyMethods = [] + + restApiId = "*" + """The API Gateway API id. By default this is set to '*'""" + region = "*" + """The region where the API is deployed. By default this is set to '*'""" + stage = "*" + """The name of the stage used in the policy. By default this is set to '*'""" + + def __init__(self, principal, awsAccountId): + self.awsAccountId = awsAccountId + self.principalId = principal + self.allowMethods = [] + self.denyMethods = [] + + def _addMethod(self, effect, verb, resource, conditions): + """Adds a method to the internal lists of allowed or denied methods. Each object in + the internal list contains a resource ARN and a condition statement. The condition + statement can be null.""" + if verb != "*" and not hasattr(HttpVerb, verb): + raise NameError("Invalid HTTP verb " + verb + ". Allowed verbs in HttpVerb class") + resourcePattern = re.compile(self.pathRegex) + if not resourcePattern.match(resource): + raise NameError("Invalid resource path: " + resource + ". Path should match " + self.pathRegex) + + if resource[:1] == "/": + resource = resource[1:] + + resourceArn = ("arn:aws:execute-api:" + + self.region + ":" + + self.awsAccountId + ":" + + self.restApiId + "/" + + self.stage + "/" + + verb + "/" + + resource) + + if effect.lower() == "allow": + self.allowMethods.append({ + 'resourceArn' : resourceArn, + 'conditions' : conditions + }) + elif effect.lower() == "deny": + self.denyMethods.append({ + 'resourceArn' : resourceArn, + 'conditions' : conditions + }) + + def _getEmptyStatement(self, effect): + """Returns an empty statement object prepopulated with the correct action and the + desired effect.""" + statement = { + 'Action': 'execute-api:Invoke', + 'Effect': effect[:1].upper() + effect[1:].lower(), + 'Resource': [] + } + + return statement + + def _getStatementForEffect(self, effect, methods): + """This function loops over an array of objects containing a resourceArn and + conditions statement and generates the array of statements for the policy.""" + statements = [] + + if len(methods) > 0: + statement = self._getEmptyStatement(effect) + + for curMethod in methods: + if curMethod['conditions'] is None or len(curMethod['conditions']) == 0: + statement['Resource'].append(curMethod['resourceArn']) + else: + conditionalStatement = self._getEmptyStatement(effect) + conditionalStatement['Resource'].append(curMethod['resourceArn']) + conditionalStatement['Condition'] = curMethod['conditions'] + statements.append(conditionalStatement) + + statements.append(statement) + + return statements + + def allowAllMethods(self): + """Adds a '*' allow to the policy to authorize access to all methods of an API""" + self._addMethod("Allow", HttpVerb.ALL, "*", []) + + def denyAllMethods(self): + """Adds a '*' allow to the policy to deny access to all methods of an API""" + self._addMethod("Deny", HttpVerb.ALL, "*", []) + + def allowMethod(self, verb, resource): + """Adds an API Gateway method (Http verb + Resource path) to the list of allowed + methods for the policy""" + self._addMethod("Allow", verb, resource, []) + + def denyMethod(self, verb, resource): + """Adds an API Gateway method (Http verb + Resource path) to the list of denied + methods for the policy""" + self._addMethod("Deny", verb, resource, []) + + def allowMethodWithConditions(self, verb, resource, conditions): + """Adds an API Gateway method (Http verb + Resource path) to the list of allowed + methods and includes a condition for the policy statement. More on AWS policy + conditions here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition""" + self._addMethod("Allow", verb, resource, conditions) + + def denyMethodWithConditions(self, verb, resource, conditions): + """Adds an API Gateway method (Http verb + Resource path) to the list of denied + methods and includes a condition for the policy statement. More on AWS policy + conditions here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition""" + self._addMethod("Deny", verb, resource, conditions) + + def build(self): + """Generates the policy document based on the internal lists of allowed and denied + conditions. This will generate a policy with two main statements for the effect: + one statement for Allow and one statement for Deny. + Methods that includes conditions will have their own statement in the policy.""" + if ((self.allowMethods is None or len(self.allowMethods) == 0) and + (self.denyMethods is None or len(self.denyMethods) == 0)): + raise NameError("No statements defined for the policy") + + policy = { + 'principalId' : self.principalId, + 'policyDocument' : { + 'Version' : self.version, + 'Statement' : [] + } + } + + policy['policyDocument']['Statement'].extend(self._getStatementForEffect("Allow", self.allowMethods)) + policy['policyDocument']['Statement'].extend(self._getStatementForEffect("Deny", self.denyMethods)) + + return policy diff --git a/client-ui/angular-code/src/app/app-routing.module.ts b/client-ui/angular-code/src/app/app-routing.module.ts index f35b4a2..af79925 100644 --- a/client-ui/angular-code/src/app/app-routing.module.ts +++ b/client-ui/angular-code/src/app/app-routing.module.ts @@ -2,13 +2,15 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { SheetComponent } from './sheet/sheet.component'; import { AuthResolver } from './core/guardsAndResolvers/auth.resolve'; +import { SheetResolver } from './core/guardsAndResolvers/sheet.resolve'; const routes: Routes = [ { path: '', component: SheetComponent, resolve: { - auth: AuthResolver + auth: AuthResolver, + sheet: SheetResolver } } ]; diff --git a/client-ui/angular-code/src/app/app.component.html b/client-ui/angular-code/src/app/app.component.html index 97a7b52..c719160 100644 --- a/client-ui/angular-code/src/app/app.component.html +++ b/client-ui/angular-code/src/app/app.component.html @@ -1,5 +1,8 @@ +
+ +
diff --git a/client-ui/angular-code/src/app/app.component.ts b/client-ui/angular-code/src/app/app.component.ts index 40201e2..35159da 100644 --- a/client-ui/angular-code/src/app/app.component.ts +++ b/client-ui/angular-code/src/app/app.component.ts @@ -1,5 +1,6 @@ import { Component, HostListener } from '@angular/core'; import { MatSnackBar } from '@angular/material'; +import { AuthService } from './core/services/auth.service'; import { SheetService } from './core/services/sheet.service'; @Component({ @@ -11,6 +12,7 @@ export class AppComponent { constructor( private sheetService: SheetService, + private authService: AuthService, private snackBar: MatSnackBar ) { } @@ -20,6 +22,7 @@ export class AppComponent { // KeyBoard Events For The App @HostListener('document:keydown.meta.s', ['$event']) handleKeyboardEventCommandS(event: KeyboardEvent) { + if (!this.authService.isLoggedIn) return; if (!this.sheetService.disableSave && this.sheetService.currentSheetValue.isDirty) { this.sheetService.saveCurrentSheet().subscribe( () => { @@ -42,6 +45,7 @@ export class AppComponent { @HostListener('document:keydown.meta.e', ['$event']) handleKeyboardEventCommandE(event: KeyboardEvent) { + if (!this.authService.isLoggedIn) return; this.sheetService.editModeOn = !this.sheetService.editModeOn event.preventDefault(); } diff --git a/client-ui/angular-code/src/app/app.module.ts b/client-ui/angular-code/src/app/app.module.ts index 39ba0b4..3c078f5 100644 --- a/client-ui/angular-code/src/app/app.module.ts +++ b/client-ui/angular-code/src/app/app.module.ts @@ -1,6 +1,5 @@ import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; -import { HttpClientModule } from '@angular/common/http'; import { FormsModule } from '@angular/forms'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { @@ -11,6 +10,7 @@ import { MatDialogModule, MatIconModule, MatInputModule, + MatProgressSpinnerModule, MatSelectModule, MatSidenavModule, MatSnackBarModule, @@ -37,6 +37,7 @@ import { ConfirmationDialogComponent } from './confirmation-dialog/confirmation- import { EditIndexCardDialogComponent } from './edit-index-card-dialog/edit-index-card-dialog.component'; import { CreateSheetDialogComponent } from './create-sheet-dialog/create-sheet-dialog.component'; import { CreateIndexCardDialogComponent } from './create-index-card-dialog/create-index-card-dialog.component'; +import { LoaderComponent } from './loader/loader.component'; @NgModule({ declarations: [ @@ -49,14 +50,14 @@ import { CreateIndexCardDialogComponent } from './create-index-card-dialog/creat ConfirmationDialogComponent, EditIndexCardDialogComponent, CreateSheetDialogComponent, - CreateIndexCardDialogComponent + CreateIndexCardDialogComponent, + LoaderComponent ], imports: [ BrowserModule, BrowserAnimationsModule, AppRoutingModule, CoreModule, - HttpClientModule, FormsModule, // Angular Material imports MatAutocompleteModule, @@ -66,6 +67,7 @@ import { CreateIndexCardDialogComponent } from './create-index-card-dialog/creat MatDialogModule, MatIconModule, MatInputModule, + MatProgressSpinnerModule, MatSelectModule, MatSidenavModule, MatSnackBarModule, diff --git a/client-ui/angular-code/src/app/core/core.module.ts b/client-ui/angular-code/src/app/core/core.module.ts index 781858f..df5445f 100644 --- a/client-ui/angular-code/src/app/core/core.module.ts +++ b/client-ui/angular-code/src/app/core/core.module.ts @@ -1,8 +1,12 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; +import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { SheetService } from './services/sheet.service'; +import { SheetResolver } from './guardsAndResolvers/sheet.resolve'; import { AceEditorService } from './services/ace-editor.service'; import { AuthResolver } from './guardsAndResolvers/auth.resolve'; +import { LoaderService } from './services/loader.service'; +import { LoaderInterceptor } from './interceptors/loader.interceptor'; import { IfEditModeDirective, IfViewModeDirective, @@ -24,12 +28,16 @@ import { EditorDirective } from './directives/editor.directive'; EditorDirective ], imports: [ - CommonModule + CommonModule, + HttpClientModule ], providers: [ SheetService, + SheetResolver, AceEditorService, - AuthResolver + AuthResolver, + LoaderService, + { provide: HTTP_INTERCEPTORS, useClass: LoaderInterceptor, multi: true } ], exports: [ IfEditModeDirective, diff --git a/client-ui/angular-code/src/app/core/guardsAndResolvers/sheet.resolve.ts b/client-ui/angular-code/src/app/core/guardsAndResolvers/sheet.resolve.ts new file mode 100644 index 0000000..2fcbacb --- /dev/null +++ b/client-ui/angular-code/src/app/core/guardsAndResolvers/sheet.resolve.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@angular/core'; +import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; +import { SheetService } from '../services/sheet.service'; +import { Observable } from 'rxjs'; +import { map, mergeMap } from 'rxjs/operators'; +// import 'rxjs/add/operator/map'; + +@Injectable() +export class SheetResolver implements Resolve { + constructor(private sheetService: SheetService) { } + resolve() { + const request = this.sheetService.loadSheetMenu().pipe( + mergeMap(responseBody => { + const sheets = responseBody['result']['sheetNames']; + return this.sheetService.setSelectedSheet(sheets[0]); + }) + ); + // mergeMap() + // const request = this.sheetService.loadSheetMenu().subscribe((responseBody) => { + // const sheets = responseBody['result']['sheetNames']; + // this.sheetService.setSelectedSheet(sheets[0]); + // }, () => {}); + return request; + } +} diff --git a/client-ui/angular-code/src/app/core/interceptors/loader.interceptor.ts b/client-ui/angular-code/src/app/core/interceptors/loader.interceptor.ts new file mode 100644 index 0000000..2faf33f --- /dev/null +++ b/client-ui/angular-code/src/app/core/interceptors/loader.interceptor.ts @@ -0,0 +1,21 @@ +import { Injectable } from "@angular/core"; +import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http"; +import { Observable } from "rxjs"; +import { finalize } from "rxjs/operators"; + +import { LoaderService } from '../services/loader.service'; + +@Injectable() +export class LoaderInterceptor implements HttpInterceptor { + + constructor(public loaderService: LoaderService) { + } + + intercept(req: HttpRequest, next: HttpHandler): Observable> { + this.loaderService.show(); + console.log('Called?'); + return next.handle(req).pipe( + finalize(() => this.loaderService.hide()) + ); + } +} diff --git a/client-ui/angular-code/src/app/core/services/loader.service.ts b/client-ui/angular-code/src/app/core/services/loader.service.ts new file mode 100644 index 0000000..e92c70a --- /dev/null +++ b/client-ui/angular-code/src/app/core/services/loader.service.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@angular/core'; +import { Subject } from 'rxjs'; + +@Injectable() +export class LoaderService { + isLoading = new Subject(); + + show() { + this.isLoading.next(true); + } + + hide() { + this.isLoading.next(false); + } +} diff --git a/client-ui/angular-code/src/app/core/services/sheet.service.ts b/client-ui/angular-code/src/app/core/services/sheet.service.ts index 87fb633..592612a 100644 --- a/client-ui/angular-code/src/app/core/services/sheet.service.ts +++ b/client-ui/angular-code/src/app/core/services/sheet.service.ts @@ -2,7 +2,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { MatDialog } from '@angular/material'; import { environment } from '../../../environments/environment'; import Sheet from '../../models/sheet'; import BaseIndexCard from '../../models/base-index-card'; @@ -24,22 +23,13 @@ const dataTemplate = { export class SheetService { constructor( - private http: HttpClient, - private dialog: MatDialog - ) { - this.loadSheetMenu().subscribe((responseBody) => { - const sheets = responseBody['result']['sheetNames']; - this.setSelectedSheet(sheets[0]); - }, () => {}); - // this.currentSheetName='python'; - // this.currentSheetValue=this.parseSheet(dataTemplate); - // this.availableSheets = ['python']; - } + private http: HttpClient + ) {} // The following variable is a flag for when the page is in Edit mode. // With Edit Mode, one can rearrange, delete and edit index cards. // This property is two way bindable and initially set to False - private editModeOnValue: boolean = true; + private editModeOnValue: boolean = false; public $editModeOn: BehaviorSubject = new BehaviorSubject(this.editModeOnValue); get editModeOn(): boolean{ return this.editModeOnValue; @@ -51,7 +41,7 @@ export class SheetService { // The following variable is flag that tells whether or not the Sheet Menu // on the left side should be open or not. - private showSheetMenuValue:boolean = true; + private showSheetMenuValue:boolean = false; get showSheetMenu(): boolean { return this.showSheetMenuValue; } diff --git a/client-ui/angular-code/src/app/loader/loader.component.html b/client-ui/angular-code/src/app/loader/loader.component.html new file mode 100644 index 0000000..4bcfa19 --- /dev/null +++ b/client-ui/angular-code/src/app/loader/loader.component.html @@ -0,0 +1,4 @@ +
+ + +
diff --git a/client-ui/angular-code/src/app/loader/loader.component.scss b/client-ui/angular-code/src/app/loader/loader.component.scss new file mode 100644 index 0000000..7e66547 --- /dev/null +++ b/client-ui/angular-code/src/app/loader/loader.component.scss @@ -0,0 +1,17 @@ +.overlay { + position:fixed; + display:block; + width:100%; + height:100%; + top:0; + left:0; + background-color:rgba(74,74,74,.8); + z-index:99999; +} + +.spinner { + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%,-50%); +} diff --git a/client-ui/angular-code/src/app/loader/loader.component.ts b/client-ui/angular-code/src/app/loader/loader.component.ts new file mode 100644 index 0000000..a212bbe --- /dev/null +++ b/client-ui/angular-code/src/app/loader/loader.component.ts @@ -0,0 +1,19 @@ + +import { Component } from '@angular/core'; +import { Subject } from 'rxjs'; + +import { LoaderService } from '../core/services/loader.service'; + +@Component({ + selector: 'app-loader', + templateUrl: './loader.component.html', + styleUrls: ['./loader.component.scss'] +}) +export class LoaderComponent { + color = 'accent'; + mode = 'indeterminate'; + value = 50; + isLoading: Subject = this.loaderService.isLoading; + + constructor(private loaderService: LoaderService){} +} diff --git a/client-ui/angular-code/src/environments/environment.ts b/client-ui/angular-code/src/environments/environment.ts index dbbcadc..0395918 100644 --- a/client-ui/angular-code/src/environments/environment.ts +++ b/client-ui/angular-code/src/environments/environment.ts @@ -4,7 +4,7 @@ export const environment = { production: false, - apiUrl: 'http://localhost:10000', + apiUrl: 'http://localhost:3000', cognitoParams: { cognitoLoginUrl:'https://scratch-cheet-sheet.auth.us-east-1.amazoncognito.com/login', cognitoLogoutUrl:'https://scratch-cheet-sheet.auth.us-east-1.amazoncognito.com/logout', diff --git a/docker-compose.yml b/docker-compose.yml index f33d337..42cb548 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,3 +35,15 @@ services: cp -r ./cached-dependencies/* ./dependencies/python/lib/python3.7/site-packages exec start.sh jupyter notebook --NotebookApp.token='' & nodemon --exec python simple_lambda_server.py entrypoint: ["dash", "-c"] + + + sam-local: + build: ./sam-local + container_name: sam-local + privileged: true + ports: + - "3000:3000" + env_file: + .env + volumes: + - ./sam-local:/app diff --git a/sam-local/Dockerfile b/sam-local/Dockerfile index ff9c698..3728d8b 100644 --- a/sam-local/Dockerfile +++ b/sam-local/Dockerfile @@ -4,8 +4,10 @@ RUN apk add --update \ python-dev \ py-pip \ build-base \ + nodejs npm \ && pip install virtualenv \ && rm -rf /var/cache/apk/* +RUN npm install http-server -g RUN pip install --user aws-sam-cli ENV PATH=$PATH:/root/.local/bin RUN mkdir /app diff --git a/sam-local/sam-app/hello_world/__init__.py b/sam-local/sam-app/hello_world/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/sam-local/sam-app/hello_world/app.py b/sam-local/sam-app/hello_world/app.py deleted file mode 100644 index 0930620..0000000 --- a/sam-local/sam-app/hello_world/app.py +++ /dev/null @@ -1,42 +0,0 @@ -import json - -# import requests - - -def lambda_handler(event, context): - """Sample pure Lambda function - - Parameters - ---------- - event: dict, required - API Gateway Lambda Proxy Input Format - - Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format - - context: object, required - Lambda Context runtime methods and attributes - - Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html - - Returns - ------ - API Gateway Lambda Proxy Output Format: dict - - Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html - """ - - # try: - # ip = requests.get("http://checkip.amazonaws.com/") - # except requests.RequestException as e: - # # Send some context about this error to Lambda Logs - # print(e) - - # raise e - - return { - "statusCode": 200, - "body": json.dumps({ - "message": "hello world", - # "location": ip.text.replace("\n", "") - }), - } diff --git a/sam-local/sam-app/sheet/_code_/app.py b/sam-local/sam-app/sheet/_code_/app.py new file mode 100644 index 0000000..88dc925 --- /dev/null +++ b/sam-local/sam-app/sheet/_code_/app.py @@ -0,0 +1,125 @@ +import boto3 +import json +import datetime +from boto3 import client +s3_client = client('s3') + +available_file_types = [ + 'abap', 'abc', 'actionscript', 'ada', 'apache_conf', 'apex', 'applescript', 'asciidoc', 'asl', + 'assembly_x86', 'autohotkey', 'batchfile', 'bro', 'c9search', 'c_cpp', 'cirru', 'clojure', 'cobol', + 'coffee', 'coldfusion', 'csharp', 'csound_document', 'csound_orchestra', 'csound_score', 'csp', 'css', + 'curly', 'd', 'dart', 'diff', 'django', 'dockerfile', 'dot', 'drools', 'edifact', 'eiffel', 'ejs', + 'elixir', 'elm', 'erlang', 'forth', 'fortran', 'fsharp', 'fsl', 'ftl', 'gcode', 'gherkin', 'gitignore', + 'glsl', 'gobstones', 'golang', 'graphqlschema', 'groovy', 'haml', 'handlebars', 'haskell', + 'haskell_cabal', 'haxe', 'hjson', 'html', 'html_elixir', 'html_ruby', 'ini', 'io', 'jack', 'jade', 'java', + 'javascript', 'json', 'jsoniq', 'jsp', 'jssm', 'jsx', 'julia', 'kotlin', 'latex', 'less', 'liquid', + 'lisp', 'livescript', 'logiql', 'logtalk', 'lsl', 'lua', 'luapage', 'lucene', 'makefile', 'markdown', + 'mask', 'matlab', 'maze', 'mel', 'mixal', 'mushcode', 'mysql', 'nix', 'nsis', 'objectivec', 'ocaml', + 'pascal', 'perl', 'perl6', 'pgsql', 'php', 'php_laravel_blade', 'pig', 'plain_text', 'powershell', + 'praat', 'prolog', 'properties', 'protobuf', 'puppet', 'python', 'r', 'razor', 'rdoc', 'red', 'redshift', + 'rhtml', 'rst', 'ruby', 'rust', 'sass', 'scad', 'scala', 'scheme', 'scss', 'sh', 'sjs', 'slim', 'smarty', + 'snippets', 'soy_template', 'space', 'sparql', 'sql', 'sqlserver', 'stylus', 'svg', 'swift', 'tcl', + 'terraform', 'tex', 'text', 'textile', 'toml', 'tsx', 'turtle', 'twig', 'typescript', 'vala', 'vbscript', + 'velocity', 'verilog', 'vhdl', 'visualforce', 'wollok', 'xml', 'xquery', 'yaml' +] + +new_sheet = { + "defaultFileType": None, + "dateCreated": datetime.datetime.now().timestamp(), + "dateUpdated": datetime.datetime.now().timestamp(), + "leftIndexCards": [ + ], + "rightIndexCards": [ + ] +} + +user='default' +cheetsheet_bucket_name='scratch-cheetsheet-storage' + +s3_get_sheet_names_response = s3_client.list_objects( + Bucket = cheetsheet_bucket_name, + Prefix = f'{user}/', + Delimiter = '/' +)['CommonPrefixes'] +all_current_sheet_names = [obj['Prefix'][(len(user)+1):-1] for obj in s3_get_sheet_names_response] + + +def get(event, context): + """ + Get All Sheets + """ + return { + "statusCode": 200, + "body": json.dumps({ + 'result': { + 'sheetNames': all_current_sheet_names + } + }) + } + +def post(event, context): + """ + Create A New Sheet + """ + + if 'sheetName' not in event['body'] or event['body']['sheetName'] is None: + # Error if no sheet name was provided + return get_error_response('No name for sheet was provided') + + if event['body']['sheetName'] in all_current_sheet_names: + # Error if sheet name exists + return get_error_response(f'Sheet with name {event["body"]["sheetName"]} already exists') + + if 'defaultFileType' not in event['body'] or event['body']['sheetName'] is None: + # Error if Default File type was not provided + return get_error_response('No Default File Type was provided') + + if event['body']['defaultFileType'] not in available_file_types: + # Error if Default File type is not supported + return get_error_response(f'Default file type {event["body"]["defaultFileType"]} is not supported') + + # Create Empty sheet in S3 bucket + new_sheet['defaultFileType'] = event['body']['defaultFileType'] + s3_client.put_object( + ACL='private', + Body = (bytes(json.dumps(new_sheet).encode('UTF-8'))), + Bucket = cheetsheet_bucket_name, + Key=f'{user}/{event["body"]["sheetName"]}/sheet.json' + + ) + + return { + "statusCode": 200, + "body": json.dumps({ + 'results': { + 'sheetName': event["body"]["sheetName"], + 'sheetData': new_sheet + } + }) + } + +def main(event, context): + if event["httpMethod"] == "GET": + response = get(event, context) + elif event["httpMethod"] == "POST": + response = post(event, context) + else: + response = { + "statusCode": 405, + "body": json.dumps({ + "message": "Method Not Allowed" + }) + } + response["headers"] = {'Access-Control-Allow-Origin': "*"} + return response + + +# Helper Methods +def get_error_response(message, statusCode=400): + return { + 'statusCode': statusCode, + 'body': json.dumps({ + 'message': message + }), + 'headers': {'Access-Control-Allow-Origin': "*"} + } diff --git a/sam-local/sam-app/hello_world/requirements.txt b/sam-local/sam-app/sheet/_code_/requirements.txt similarity index 100% rename from sam-local/sam-app/hello_world/requirements.txt rename to sam-local/sam-app/sheet/_code_/requirements.txt diff --git a/sam-local/sam-app/sheet/{sheetName}/_code_/app.py b/sam-local/sam-app/sheet/{sheetName}/_code_/app.py new file mode 100644 index 0000000..475c029 --- /dev/null +++ b/sam-local/sam-app/sheet/{sheetName}/_code_/app.py @@ -0,0 +1,170 @@ +import boto3 +import json +import datetime +from boto3 import client +s3_client = client('s3') + +available_file_types = [ + 'abap', 'abc', 'actionscript', 'ada', 'apache_conf', 'apex', 'applescript', 'asciidoc', 'asl', + 'assembly_x86', 'autohotkey', 'batchfile', 'bro', 'c9search', 'c_cpp', 'cirru', 'clojure', 'cobol', + 'coffee', 'coldfusion', 'csharp', 'csound_document', 'csound_orchestra', 'csound_score', 'csp', 'css', + 'curly', 'd', 'dart', 'diff', 'django', 'dockerfile', 'dot', 'drools', 'edifact', 'eiffel', 'ejs', + 'elixir', 'elm', 'erlang', 'forth', 'fortran', 'fsharp', 'fsl', 'ftl', 'gcode', 'gherkin', 'gitignore', + 'glsl', 'gobstones', 'golang', 'graphqlschema', 'groovy', 'haml', 'handlebars', 'haskell', + 'haskell_cabal', 'haxe', 'hjson', 'html', 'html_elixir', 'html_ruby', 'ini', 'io', 'jack', 'jade', 'java', + 'javascript', 'json', 'jsoniq', 'jsp', 'jssm', 'jsx', 'julia', 'kotlin', 'latex', 'less', 'liquid', + 'lisp', 'livescript', 'logiql', 'logtalk', 'lsl', 'lua', 'luapage', 'lucene', 'makefile', 'markdown', + 'mask', 'matlab', 'maze', 'mel', 'mixal', 'mushcode', 'mysql', 'nix', 'nsis', 'objectivec', 'ocaml', + 'pascal', 'perl', 'perl6', 'pgsql', 'php', 'php_laravel_blade', 'pig', 'plain_text', 'powershell', + 'praat', 'prolog', 'properties', 'protobuf', 'puppet', 'python', 'r', 'razor', 'rdoc', 'red', 'redshift', + 'rhtml', 'rst', 'ruby', 'rust', 'sass', 'scad', 'scala', 'scheme', 'scss', 'sh', 'sjs', 'slim', 'smarty', + 'snippets', 'soy_template', 'space', 'sparql', 'sql', 'sqlserver', 'stylus', 'svg', 'swift', 'tcl', + 'terraform', 'tex', 'text', 'textile', 'toml', 'tsx', 'turtle', 'twig', 'typescript', 'vala', 'vbscript', + 'velocity', 'verilog', 'vhdl', 'visualforce', 'wollok', 'xml', 'xquery', 'yaml' +] + +user='default' +cheetsheet_bucket_name='scratch-cheetsheet-storage' + +s3_get_sheet_names_response = s3_client.list_objects( + Bucket = cheetsheet_bucket_name, + Prefix = f'{user}/', + Delimiter = '/' +)['CommonPrefixes'] +all_current_sheet_names = [obj['Prefix'][(len(user)+1):-1] for obj in s3_get_sheet_names_response] + + +def get(event, context): + """ + Get JSON data of a sheet + """ + + if 'sheetName' not in event['pathParameters'] or event['pathParameters']['sheetName'] is None: + # Error if no sheet name was provided + return get_error_response('No sheet name was provided') + if event["pathParameters"]["sheetName"] not in all_current_sheet_names: + # Error sheet name does not exist + return get_error_response(f'No sheet with name {event["pathParameters"]["sheetName"]} exists') + + s3_get_sheet_response = s3_client.get_object( + Bucket=cheetsheet_bucket_name, + Key=f'{user}/{event["pathParameters"]["sheetName"]}/sheet.json' + ) + sheet_data = json.loads(s3_get_sheet_response["Body"].read().decode()) + + return { + "statusCode": 200, + "body": json.dumps({ + 'result': { + 'sheetName': event["pathParameters"]["sheetName"], + 'sheetData': sheet_data + } + }) + } + +def delete(event, context): + """ + Delete a sheet + """ + + if 'sheetName' not in event['pathParameters'] or event['pathParameters']['sheetName'] is None: + # Error if no sheet name was provided + return get_error_response('No name for sheet was provided') + if event["pathParameters"]["sheetName"] not in all_current_sheet_names: + # Error sheet name does not exist + return get_error_response(f'No sheet with name {event["pathParameters"]["sheetName"]} exists') + + # Delete sheet in S3 bucket + s3_delete_sheet_response = s3_client.delete_object( + Bucket = cheetsheet_bucket_name, + Key=f'{user}/{event["pathParameters"]["sheetName"]}/sheet.json' + ) + + if s3_delete_sheet_response['ResponseMetadata']['HTTPStatusCode'] not in [200, 204]: + # Error if sheet was not properly deleted + return get_error_response(f'Sheet {event["pathParameters"]["sheetName"]} failed to be deleted') + + return { + "statusCode": 200, + "body": json.dumps({ + }) + } + +def put(event, context): + """ + Update an existing sheet + """ + if 'sheetName' not in event['pathParameters'] or event['pathParameters']['sheetName'] is None: + # Error if no sheet name was provided + return get_error_response('No name for sheet was provided') + if event["pathParameters"]["sheetName"] not in all_current_sheet_names: + # Error sheet name does not exist + return get_error_response(f'No sheet with name {event["pathParameters"]["sheetName"]} exists') + if 'sheetData' not in event['body'] or event['body']['sheetData'] is None: + # Error if no sheet data was provided + return get_error_response('No sheet data was provided') + + new_sheet_data = event['body']['sheetData'] + + if new_sheet_data['defaultFileType'] not in available_file_types: + # Error if Default File type is not supported + return get_error_response( + f'Default file type {event["body"]["sheetData"]["defaultFileType"]} is not supported' + ) + if set(['defaultFileType', 'leftIndexCards', 'rightIndexCards']) != set(new_sheet_data.keys()): + # Error if SheetData has unexpected keys + return get_error_response( + f'Sheet data not formatted properly' + ) + + # Get current data for sheet + s3_get_sheet_response = s3_client.get_object( + Bucket=cheetsheet_bucket_name, + Key=f'{user}/{event["pathParameters"]["sheetName"]}/sheet.json' + ) + sheet_data = json.loads(s3_get_sheet_response["Body"].read().decode()) + new_sheet_data["dateCreated"] = sheet_data["dateCreated"] + new_sheet_data["dateUpdated"] = datetime.datetime.now().timestamp() + s3_client.put_object( + ACL='private', + Body = (bytes(json.dumps(new_sheet_data).encode('UTF-8'))), + Bucket = cheetsheet_bucket_name, + Key=f'{user}/{event["pathParameters"]["sheetName"]}/sheet.json' + ) + + return { + "statusCode": 200, + "body": json.dumps({ + 'result': { + 'sheetName': event["pathParameters"]["sheetName"], + 'sheetData': new_sheet_data + } + }) + } + +def main(event, context): + print('TEST') + if event["httpMethod"] == "GET": + response = get(event, context) + elif event["httpMethod"] == "PUT": + response = put(event, context) + elif event["httpMethod"] == "DELETE": + response = delete(event, context) + else: + return { + "statusCode": 405, + "body": json.dumps({ + "message": "Method Not Allowed" + }) + } + response["headers"] = {'Access-Control-Allow-Origin': "*"} + return response + +# Helper Methods +def get_error_response(message, statusCode=400): + return { + 'statusCode': statusCode, + 'body': json.dumps({ + 'message': message + }) + } diff --git a/sam-local/sam-app/template.yaml b/sam-local/sam-app/template.yaml index ab96a33..fd1675c 100644 --- a/sam-local/sam-app/template.yaml +++ b/sam-local/sam-app/template.yaml @@ -8,21 +8,63 @@ Description: > # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: Function: + Runtime: python3.7 Timeout: 3 + MemorySize: 256 + Environment: + Variables: + CLIENT_UI_SUBDOMAIN: $CLIENT_UI_SUBDOMAIN + SHEET_DATA_S3_BUCKET: $SHEET_DATA_S3_BUCKET + COGNITO_CLIENT_ID: $COGNITO_CLIENT_ID + COGNITO_CLIENT_SECRET: $COGNITO_CLIENT_SECRET + Api: + Cors: + AllowMethods: "'*'" + AllowHeaders: "'*'" + AllowOrigin: "'*'" Resources: - HelloWorldFunction: + #/sheet + SheetFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: - CodeUri: hello_world/ - Handler: app.lambda_handler - Runtime: python3.7 + CodeUri: sheet/_code_ + Handler: app.main Events: - HelloWorld: + Get: Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api Properties: - Path: /hello + Path: /sheet Method: get + Post: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /sheet + Method: post + + #/sheet/{sheetName} + SheetSheetNameFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: sheet/{sheetName}/_code_ + Handler: app.main + Events: + Get: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /sheet/{sheetName} + Method: get + Delete: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /sheet/{sheetName} + Method: delete + Put: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /sheet/{sheetName} + Method: put + Outputs: # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function