import { Action , BaseResource , exceptions , handlerEvent , OperationStatus , Optional , ProgressEvent , ResourceHandlerRequest , SessionProxy } from 'cfn-rpdk' ; import { ResourceModel } from './models' ; import { ServiceQuotas } from 'aws-sdk' ; import { QuotaID , UpsertQuotas } from 'aws-resource-providers-common' ; const LOGGER = console ; const quotaCodeForPropertyName = { roles : { QuotaCode : 'L-FE177D64' , ServiceCode : 'iam' } , customerManagedPolicies : { QuotaCode : 'L-E95E4862' , ServiceCode : 'iam' } , } ; type CallbackContext = Record < string , any > ; class Resource extends BaseResource < ResourceModel > { param session param request param callbackContext @ handlerEvent ( Action . Create ) public async create ( session , request , callbackContext ) { const model = new ResourceModel ( request . desiredResourceState ) ; const progress = ProgressEvent . progress < ProgressEvent < ResourceModel , CallbackContext > > ( model ) ; LOGGER . info ( { handler : 'create' , request , callbackContext } ) ; model . resourceId = 'iam-quotas' ; if ( session instanceof SessionProxy ) { const serviceQuotas = session . client ( 'ServiceQuotas' ) as ServiceQuotas ; await UpsertQuotas ( serviceQuotas , new ResourceModel ( ) , model , quotaCodeForPropertyName , LOGGER ) ; } else { throw new exceptions . InvalidCredentials ( 'no aws session found - did you forget to register the execution role?' ) ; } progress . status = OperationStatus . Success ; return progress ; } param session param request param callbackContext @ handlerEvent ( Action . Update ) public async update ( session , request , callbackContext ) { const desired = new ResourceModel ( request . desiredResourceState ) ; const previous = new ResourceModel ( request . previousResourceState ) ; const progress = ProgressEvent . progress < ProgressEvent < ResourceModel , CallbackContext > > ( desired ) ; LOGGER . info ( { handler : 'update' , request , callbackContext } ) ; if ( session instanceof SessionProxy ) { const serviceQuotas = session . client ( 'ServiceQuotas' ) as ServiceQuotas ; await UpsertQuotas ( serviceQuotas , previous , desired , quotaCodeForPropertyName , LOGGER ) ; } else { throw new exceptions . InvalidCredentials ( 'no aws session found - did you forget to register the execution role?' ) ; } progress . status = OperationStatus . Success ; return progress ; } param session param request param callbackContext @ handlerEvent ( Action . Delete ) public async delete ( session ) { const progress = ProgressEvent . progress < ProgressEvent < ResourceModel , CallbackContext > > ( ) ; if ( session instanceof SessionProxy ) { session . client ( 'ServiceQuotas' ) as ServiceQuotas ; } else { throw new exceptions . InvalidCredentials ( 'no aws session found - did you forget to register the execution role?' ) ; } progress . status = OperationStatus . Success ; return progress ; } param session param request param callbackContext @ handlerEvent ( Action . Read ) public async read ( session , request ) { const model = new ResourceModel ( request . desiredResourceState ) ; const progress = ProgressEvent . success < ProgressEvent < ResourceModel , CallbackContext > > ( model ) ; if ( session instanceof SessionProxy ) { session . client ( 'ServiceQuotas' ) as ServiceQuotas ; } else { throw new exceptions . InvalidCredentials ( 'no aws session found - did you forget to register the execution role?' ) ; } return progress ; } } export const resource = new Resource ( ResourceModel . TYPE_NAME , ResourceModel ) ; export const entrypoint = resource . entrypoint ; export const testEntrypoint = resource . testEntrypoint ;