Skip to content
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

Azure provider MVP #258

Merged
merged 29 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e198469
[BOOST-652]
rdoria1 May 20, 2020
941552e
[BOOST-652]
rdoria1 May 20, 2020
7970d0e
[BOOST-652]
rdoria1 Jun 1, 2020
0a2ac35
Merge branch 'BOOST-652-skeleton-azure-infra' of github.com:theam/boo…
MarcAstr0 Jun 5, 2020
9a41a39
Added more Azure resources
MarcAstr0 Jun 11, 2020
95bcf56
More Azure resources for REST API
MarcAstr0 Jun 19, 2020
96efa15
Azure adapters for API and GraphQL
MarcAstr0 Jun 22, 2020
af9202d
Cosmos DB added to infrastructure
MarcAstr0 Jun 28, 2020
ef4c502
Implemented events adapter
MarcAstr0 Jun 30, 2020
2fdc897
Implemented read model and searcher adapter
MarcAstr0 Jul 8, 2020
7c4dbf2
Unit tests for Azure provider
MarcAstr0 Jul 8, 2020
6589972
Refactored Azure provider infrastructure
MarcAstr0 Jul 8, 2020
a97970b
Some fixes to the events and read model adapters
MarcAstr0 Jul 9, 2020
b89e0df
Merge branch 'master-mirror' into BOOST-730-implement-azure-resources
MarcAstr0 Jul 9, 2020
700c306
Several fixes after merging with master-mirror
MarcAstr0 Jul 10, 2020
907ffa7
Fixed yarn.lock
MarcAstr0 Jul 14, 2020
4a7b39b
Cleaned up utils.ts in the Azure infra package
MarcAstr0 Jul 14, 2020
e9d66c5
Merge branch 'master-mirror' into BOOST-730-implement-azure-resources
MarcAstr0 Jul 14, 2020
9339fa4
Added @ts-ignore to conflicting lines
MarcAstr0 Jul 14, 2020
e3ec4fa
Set template paths as class-level constants
MarcAstr0 Jul 14, 2020
8ad46bc
Refactored template loading in Azure infra
MarcAstr0 Jul 14, 2020
89062e0
Fixed debug message placement in searcher-adapter
MarcAstr0 Jul 14, 2020
09e369c
Pass Azure credentials as function parameters
MarcAstr0 Jul 16, 2020
44cf4cf
Small string change in searcher-adapter
MarcAstr0 Jul 16, 2020
edaa1d4
Use path.join in packageAzureFunction method
MarcAstr0 Jul 16, 2020
ba62f26
Refactored contants.ts to resemble AWS provider
MarcAstr0 Jul 16, 2020
df27b67
Removed unnecessary type conversion
MarcAstr0 Jul 16, 2020
13fc284
Simplified returns in the events adapter
MarcAstr0 Jul 16, 2020
3676337
Merge branch 'master-mirror' into BOOST-730-implement-azure-resources
MarcAstr0 Jul 20, 2020
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
Prev Previous commit
Next Next commit
Azure adapters for API and GraphQL
  • Loading branch information
MarcAstr0 committed Jun 22, 2020
commit 96efa15082e61a76c404024dcde89550f1585c75
1 change: 1 addition & 0 deletions packages/framework-provider-azure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"url": "git+https://github.com/boostercloud/booster.git"
},
"dependencies": {
"@azure/functions": "^1.2.2",
"@boostercloud/framework-types": "^0.3.3"
},
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions packages/framework-provider-azure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ProviderInfrastructure, ProviderLibrary } from '@boostercloud/framework-types'
import { requestFailed, requestSucceeded } from './library/api-adapter'
import { rawGraphQLRequestToEnvelope } from './library/graphql-adapter'

export const Provider: ProviderLibrary = {
// ProviderEventsLibrary
Expand All @@ -22,17 +24,17 @@ export const Provider: ProviderLibrary = {
// ProviderGraphQLLibrary
graphQL: {
authorizeRequest: undefined as any,
rawToEnvelope: undefined as any,
handleResult: undefined as any,
rawToEnvelope: rawGraphQLRequestToEnvelope,
handleResult: requestSucceeded,
},
// ProviderAuthLibrary
auth: {
rawToEnvelope: undefined as any,
},
// ProviderAPIHandling
api: {
requestSucceeded: undefined as any,
requestFailed: undefined as any,
requestSucceeded,
requestFailed,
},
// ProviderInfrastructureGetter
infrastructure: () =>
Expand Down
39 changes: 39 additions & 0 deletions packages/framework-provider-azure/src/library/api-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Cookie } from '@azure/functions'
import { httpStatusCodeFor, toClassTitle } from '@boostercloud/framework-types'

/**
* See https://docs.microsoft.com/es-es/azure/azure-functions/functions-reference-node#response-object
*/
export interface ContextResponse {
body: string
headers: object
isRaw?: boolean
status: number
cookies?: Cookie[]
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function requestSucceeded(body?: any): Promise<ContextResponse> {
return {
headers: {
'Access-Control-Allow-Origin': '*',
},
status: 200,
body: body ? JSON.stringify(body) : '',
}
}

export async function requestFailed(error: Error): Promise<ContextResponse> {
const status = httpStatusCodeFor(error)
return {
headers: {
'Access-Control-Allow-Origin': '*',
},
status,
body: JSON.stringify({
status,
title: toClassTitle(error),
reason: error.message,
}),
}
}
19 changes: 19 additions & 0 deletions packages/framework-provider-azure/src/library/graphql-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GraphQLRequestEnvelope, Logger } from '@boostercloud/framework-types'
import { Context } from '@azure/functions'

export async function rawGraphQLRequestToEnvelope(context: Context, logger: Logger): Promise<GraphQLRequestEnvelope> {
logger.debug('Received GraphQL request: ', context.req)
let graphQLBody = undefined
let graphQLVariables = undefined
if (context.req) {
graphQLBody = context.req.body.query
graphQLVariables = context.req.body.variables
}

return {
requestID: context.executionContext.invocationId,
eventType: 'MESSAGE',
value: graphQLBody,
variables: graphQLVariables,
}
}
6 changes: 6 additions & 0 deletions packages/framework-provider-azure/test/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as chai from 'chai'

chai.use(require('sinon-chai'))
chai.use(require('chai-as-promised'))

export const expect = chai.expect
59 changes: 59 additions & 0 deletions packages/framework-provider-azure/test/library/api-adapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from '../expect'
import {
InvalidVersionError,
InvalidParameterError,
NotAuthorizedError,
NotFoundError,
} from '@boostercloud/framework-types'
import { requestFailed } from '../../src/library/api-adapter'

describe('the requestFailed method', () => {
interface TestCase {
input: Error
expectedOutput: {
status: number
title: string
}
}

it('returns a proper body with several errors', async () => {
const testCases: Array<TestCase> = [
{
input: new InvalidParameterError('error message'),
expectedOutput: {
status: 400,
title: 'Invalid Parameter Error',
},
},
{
input: new NotAuthorizedError('error message'),
expectedOutput: {
status: 401,
title: 'Not Authorized Error',
},
},
{
input: new NotFoundError('error message'),
expectedOutput: {
status: 404,
title: 'Not Found Error',
},
},
{
input: new InvalidVersionError('error message'),
expectedOutput: {
status: 422,
title: 'Invalid Version Error',
},
},
]

for (const testCase of testCases) {
const testDescription = `In test case '${testCase.input.constructor.name}'`
const got = await requestFailed(testCase.input)
expect(got.status).to.be.equal(testCase.expectedOutput.status, testDescription)
const body = JSON.parse(got.body)
expect(body.title).to.be.equal(testCase.expectedOutput.title, testDescription)
}
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect } from '../expect'
import { GraphQLRequestEnvelope } from '@boostercloud/framework-types'
import { rawGraphQLRequestToEnvelope } from '../../src/library/graphql-adapter'
import { Context } from '@azure/functions'

describe('the graphql-adapter', () => {
describe('the `rawGraphQLRequestToEnvelope`', () => {
it('generates an envelope correctly from an Azure event', async () => {
const expectedQuery = 'GraphQL query'
const expectedVariables = {
varOne: 3,
varTwo: 'test',
}
const request: Context = {
req: {
body: {
query: expectedQuery,
variables: expectedVariables,
},
},
executionContext: {
invocationId: '123',
},
} as any

const expectedOutput: GraphQLRequestEnvelope = {
requestID: '123',
eventType: 'MESSAGE',
value: expectedQuery,
variables: expectedVariables,
}
const gotOutput = await rawGraphQLRequestToEnvelope(request, console)

expect(gotOutput).to.be.deep.equal(expectedOutput)
})
})
})
Loading