Skip to content

Commit

Permalink
Enable eslint standard
Browse files Browse the repository at this point in the history
- For using the same syntax at whole level of the project
  • Loading branch information
direnakkoc committed Feb 22, 2023
1 parent b896260 commit c4d71d0
Show file tree
Hide file tree
Showing 44 changed files with 528 additions and 620 deletions.
27 changes: 15 additions & 12 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
module.exports = {
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint'
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
extends: [
'standard',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended'
],
rules: {
'@typescript-eslint/no-var-requires': 0, // todo remove later complains for all js modules
'no-undef':0, // todo remove later
'@typescript-eslint/ban-ts-comment': 0, //todo remove ts-ignore in modules and remove this rule
'@typescript-eslint/no-explicit-any' : 0 // used for dimensions value
'no-undef': 0, // todo remove later
'@typescript-eslint/ban-ts-comment': 0, // todo remove ts-ignore in modules and remove this rule
'@typescript-eslint/no-explicit-any': 0 // used for dimensions value
},
ignorePatterns: ['core/coverage'],
ignorePatterns: ['core/coverage',
'serverless-plugin/dist/index.js',
'cf-macro/dist/index.js'
]
}
12 changes: 5 additions & 7 deletions cf-macro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import alarms from 'slic-watch-core/alarms/alarms'
import dashboard from 'slic-watch-core/dashboards/dashboard'
import CloudFormationTemplate from 'slic-watch-core/cf-template'
import defaultConfig from 'slic-watch-core/inputs/default-config'
import { slicWatchSchema} from 'slic-watch-core/inputs/config-schema'
import { slicWatchSchema } from 'slic-watch-core/inputs/config-schema'
import pino from 'pino'

const logger = pino({ name: 'macroHandler' })
Expand All @@ -19,14 +19,12 @@ type Event = {
}

type SlicWatchConfig = {
topicArn: string
topicArn: string
ActionsEnabled: boolean
}
export async function handler (event: Event) {
let status = 'success'
logger.info({ event })


let outputFragment = event.fragment
try {
const slicWatchConfig:SlicWatchConfig = (outputFragment.Metadata || {}).slicWatch || {}
Expand All @@ -35,9 +33,9 @@ export async function handler (event: Event) {
const ajv = new Ajv({
unicodeRegExp: false
})
const config = _.merge(defaultConfig, slicWatchConfig )
const config = _.merge(defaultConfig, slicWatchConfig)

const slicWatchValidate = ajv.compile(slicWatchSchema )
const slicWatchValidate = ajv.compile(slicWatchSchema)
const slicWatchValid = slicWatchValidate(slicWatchConfig)

if (!slicWatchValid) {
Expand Down Expand Up @@ -86,7 +84,7 @@ export async function handler (event: Event) {

return {
requestId: event.requestId,
status: status,
status,
fragment: outputFragment
}
}
2 changes: 1 addition & 1 deletion cf-macro/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cf-macro-slic-watch",
"description": "SLIC Watch CloudFormation Macro for automatic alarms and dashboards",
"main": "index.js",
"main": "dist/index.js",
"type": "module",
"repository": {
"type": "git",
Expand Down
17 changes: 7 additions & 10 deletions cf-macro/tests/cdk-cf.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-disable no-template-curly-in-string */
'use strict'

import { test } from 'tap'
import CloudFormationTemplate from 'slic-watch-core/cf-template'
import esmock from 'esmock'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const cdkStack = require('./resources/cdk-ecs-cf.json')
const cdkStack = require('./resources/cdk-ecs-cf.json')

const cfMacroHandler = await esmock('../index', {})

const cfMacroHandler = await esmock('../index', {})

/**
/**
* Test the synthesized output from the CDK ECS Stack in `cdk-test-project`
*/
test('ECS CDK stack', async (t) => {
Expand All @@ -24,20 +24,17 @@ test('ECS CDK stack', async (t) => {
test('alarms are generated', (t) => {
const alarms = transformedTemplate.getResourcesByType('AWS::CloudWatch::Alarm')
t.equal(Object.keys(alarms).length, 6)

const alarmNames = Object.values(alarms).map(alarm => alarm.Properties?.AlarmName).sort()
t.same(alarmNames, [
'ECS_CPUAlarm_${MyWebServerService2FE7341D.Name}',
'ECS_CPUAlarm_${MyWebServerService2FE7341D.Name}',
'ECS_MemoryAlarm_${MyWebServerService2FE7341D.Name}',
'LoadBalancerHTTPCodeELB5XXCountAlarm_MyWebServerLB3B5FD3AB',
'LoadBalancerHTTPCodeTarget5XXCountAlarm_MyWebServerLBPublicListenerECSGroup5AB9F1C3',
'LoadBalancerRejectedConnectionCountAlarm_MyWebServerLB3B5FD3AB',
'LoadBalancerHTTPCodeTarget5XXCountAlarm_MyWebServerLBPublicListenerECSGroup5AB9F1C3',
'LoadBalancerRejectedConnectionCountAlarm_MyWebServerLB3B5FD3AB',
'LoadBalancerUnHealthyHostCountAlarm_MyWebServerLBPublicListenerECSGroup5AB9F1C3'
])

t.end()
})

t.end()
})

11 changes: 2 additions & 9 deletions cf-macro/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import { test } from 'tap'
import _ from 'lodash'
import esmock from 'esmock'
import { CloudFormationTemplate } from "slic-watch-core/cf-template"
import { CloudFormationTemplate } from 'slic-watch-core/cf-template'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const template = require('./event.json')

const event = { fragment: template, templateParameterValues: { stack: 'sam-test-stack-project' } }


type TestState = {
dashboardCalled?:boolean,
alarmsCalled?: boolean,
Expand Down Expand Up @@ -41,7 +40,7 @@ const lambda = await esmock('../index', {

test('index', t => {
t.beforeEach(t => {
t.ok(true)
t.ok(true)
testState = {}
})

Expand Down Expand Up @@ -142,9 +141,3 @@ test('index', t => {

t.end()
})






4 changes: 2 additions & 2 deletions core/alarms/alarms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import albAlarms from './alb'
import albTargetAlarms from './alb-target-group'
import appSyncAlarms from './appsync'

export default function alarms (AlarmProperties:AlarmsCascade , functionAlarmPropertiess: FunctionAlarmPropertiess, context: Context) {
export default function alarms (AlarmProperties:AlarmsCascade, functionAlarmPropertiess: FunctionAlarmPropertiess, context: Context) {
const {
Lambda: lambdaConfig,
ApiGateway: apiGwConfig,
Expand All @@ -34,7 +34,7 @@ export default function alarms (AlarmProperties:AlarmsCascade , functionAlarmPro
AppSync: appSyncConfig
} = cascade(AlarmProperties)

const cascadedFunctionAlarmPropertiess = applyAlarmConfig(lambdaConfig, functionAlarmPropertiess )
const cascadedFunctionAlarmPropertiess = applyAlarmConfig(lambdaConfig, functionAlarmPropertiess)
const { createLambdaAlarms } = lambdaAlarms(cascadedFunctionAlarmPropertiess, context)
const { createApiGatewayAlarms } = apiGatewayAlarms(apiGwConfig, context)
const { createStatesAlarms } = stepFunctionAlarms(sfConfig, context)
Expand Down
101 changes: 48 additions & 53 deletions core/alarms/alb-target-group.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict'

import { CloudFormationTemplate } from '../cf-template'
import Resource from "cloudform-types/types/resource"
import Resource from 'cloudform-types/types/resource'
import { Context, createAlarm } from './default-config-alarms'
import { getStatisticName } from './get-statistic-name'
import { makeResourceName } from './make-name'
import { AlarmProperties} from "cloudform-types/types/cloudWatch/alarm"

import { AlarmProperties } from 'cloudform-types/types/cloudWatch/alarm'

export type AlbTargetAlarmProperties = AlarmProperties & {
HTTPCode_Target_5XX_Count: AlarmProperties
Expand All @@ -17,9 +16,8 @@ export type AlbTargetAlarmProperties = AlarmProperties & {

export type AlbTargetAlarm = AlarmProperties & {
TargetGroupResourceName: string
LoadBalancerLogicalId: string
}

LoadBalancerLogicalId: string
}
/**
* For a given target group defined by its CloudFormation resources Logical ID, find any load balancer
* that relates to the target group by finding associated ListenerRules, their Listener and each Listener's
Expand Down Expand Up @@ -53,7 +51,7 @@ export function findLoadBalancersForTargetGroup (targetGroupLogicalId: string, c
)

// Second, find ListenerRules with actions referncing the target group, then follow to the rules' listeners
for (const [listenerRuleLogicalId, listenerRule ] of Object.entries(listenerRuleResources)) {
for (const [listenerRuleLogicalId, listenerRule] of Object.entries(listenerRuleResources)) {
for (const action of listenerRule.Properties.Actions || []) {
const targetGroupArn = action.TargetGroupArn
if (targetGroupArn.Ref === targetGroupLogicalId) {
Expand Down Expand Up @@ -81,7 +79,6 @@ export function findLoadBalancersForTargetGroup (targetGroupLogicalId: string, c
return [...allLoadBalancerLogicalIds]
}


/**
* The fully resolved alarm configuration
*/
Expand Down Expand Up @@ -147,28 +144,26 @@ export default function ALBTargetAlarms (albTargetAlarmProperties: AlbTargetAlar
}
}



function createHTTPCodeTarget5XXCountAlarm (targetGroupResourceName: string, targetGroupResource: Resource , loadBalancerLogicalId: string, config: AlarmProperties) {
function createHTTPCodeTarget5XXCountAlarm (targetGroupResourceName: string, targetGroupResource: Resource, loadBalancerLogicalId: string, config: AlarmProperties) {
const threshold = config.Threshold
const albTargetAlarmProperties: AlbTargetAlarm = {
AlarmName: `LoadBalancerHTTPCodeTarget5XXCountAlarm_${targetGroupResourceName}`,
AlarmDescription: `LoadBalancer HTTP Code Target 5XX Count ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
AlarmDescription: `LoadBalancer HTTP Code Target 5XX Count ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
TargetGroupResourceName: targetGroupResourceName,
LoadBalancerLogicalId:loadBalancerLogicalId,
LoadBalancerLogicalId: loadBalancerLogicalId,
ComparisonOperator: config.ComparisonOperator,
Threshold: config.Threshold,
MetricName: 'HTTPCode_Target_5XX_Count',
Statistic: config.Statistic,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace:'AWS/ApplicationELB' ,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace: 'AWS/ApplicationELB',
Dimensions: [
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}`},
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}`}
]
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}` },
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}` }
]
}
return {
resourceName: makeResourceName('LoadBalancer', targetGroupResourceName, 'HTTPCodeTarget5XXCount'),
Expand All @@ -180,23 +175,23 @@ export default function ALBTargetAlarms (albTargetAlarmProperties: AlbTargetAlar
const threshold = config.Threshold
const albTargetAlarmProperties: AlbTargetAlarm = {
AlarmName: `LoadBalancerUnHealthyHostCountAlarm_${targetGroupResourceName}`,
AlarmDescription: `LoadBalancer UnHealthy Host Count ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
AlarmDescription: `LoadBalancer UnHealthy Host Count ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
TargetGroupResourceName: targetGroupResourceName,
LoadBalancerLogicalId:loadBalancerLogicalId,
LoadBalancerLogicalId: loadBalancerLogicalId,
ComparisonOperator: config.ComparisonOperator,
Threshold: config.Threshold,
MetricName: 'UnHealthyHostCount',
Statistic: config.Statistic,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace:'AWS/ApplicationELB' ,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace: 'AWS/ApplicationELB',
Dimensions: [
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}`},
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}`}
]
}
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}` },
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}` }
]
}
return {
resourceName: makeResourceName('LoadBalancer', targetGroupResourceName, 'UnHealthyHostCount'),
resource: createAlarm(albTargetAlarmProperties, context)
Expand All @@ -209,21 +204,21 @@ export default function ALBTargetAlarms (albTargetAlarmProperties: AlbTargetAlar
AlarmName: `LoadBalancerLambdaInternalErrorAlarm_${targetGroupResourceName}`,
AlarmDescription: `LoadBalancer Lambda Internal Error ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
TargetGroupResourceName: targetGroupResourceName,
LoadBalancerLogicalId:loadBalancerLogicalId,
LoadBalancerLogicalId: loadBalancerLogicalId,
ComparisonOperator: config.ComparisonOperator,
Threshold: config.Threshold,
MetricName: 'LambdaInternalError',
Statistic: config.Statistic,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace:'AWS/ApplicationELB' ,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace: 'AWS/ApplicationELB',
Dimensions: [
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}`},
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}`}
]
}
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}` },
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}` }
]
}
return {
resourceName: makeResourceName('LoadBalancer', targetGroupResourceName, 'LambdaInternalError'),
resource: createAlarm(albTargetAlarmProperties, context)
Expand All @@ -234,23 +229,23 @@ export default function ALBTargetAlarms (albTargetAlarmProperties: AlbTargetAlar
const threshold = config.Threshold
const albTargetAlarmProperties: AlbTargetAlarm = {
AlarmName: `LoadBalancerLambdaUserErrorAlarm_${targetGroupResourceName}`,
AlarmDescription: `LoadBalancer Lambda User Error ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
AlarmDescription: `LoadBalancer Lambda User Error ${getStatisticName(config)} for ${targetGroupResourceName} breaches ${threshold}`,
TargetGroupResourceName: targetGroupResourceName,
LoadBalancerLogicalId:loadBalancerLogicalId,
LoadBalancerLogicalId: loadBalancerLogicalId,
ComparisonOperator: config.ComparisonOperator,
Threshold: config.Threshold,
MetricName: 'LambdaUserError',
Statistic: config.Statistic,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace:'AWS/ApplicationELB' ,
Period: config.Period,
ExtendedStatistic: config.ExtendedStatistic,
EvaluationPeriods: config.EvaluationPeriods,
TreatMissingData: config.TreatMissingData,
Namespace: 'AWS/ApplicationELB',
Dimensions: [
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}`},
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}`}
]
}
{ Name: 'TargetGroup', Value: `\${${targetGroupResourceName}.TargetGroupFullName}` },
{ Name: 'LoadBalancer', Value: `\${${loadBalancerLogicalId}.LoadBalancerFullName}` }
]
}
return {
resourceName: makeResourceName('LoadBalancer', targetGroupResourceName, 'LambdaUserError'),
resource: createAlarm(albTargetAlarmProperties, context)
Expand Down
Loading

0 comments on commit c4d71d0

Please sign in to comment.