import { ContainerRegistryManagementClient } from "@azure/arm-containerregistry" ; import { loginWithServicePrincipalSecret } from "@azure/ms-rest-nodeauth" ; import { logger } from "../../logger" ; let client ; export interface RegistryItem { id : string ; name : string ; resourceGroup : string ; } param servicePrincipalId param servicePrincipalPassword param servicePrincipalTenantId param subscriptionId const getClient = async ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId ) : Promise < ContainerRegistryManagementClient > => { if ( client ) { return client ; } const creds = await loginWithServicePrincipalSecret ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId ) ; client = new ContainerRegistryManagementClient ( creds , subscriptionId , { } ) ; return client ; } ; param servicePrincipalId param servicePrincipalPassword param servicePrincipalTenantId param subscriptionId export const getContainerRegistries = async ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId ) : Promise < RegistryItem [ ] > => { logger . info ( "attempting to get Azure container registries" ) ; await getClient ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId ) ; const registries = await client . registries . list ( ) ; logger . info ( "Successfully acquired Azure container registries" ) ; return registries . map ( ( r ) => { const id = r . id ! as string ; const match = id . match ( /\/resourceGroups\/(.+?)\// ) ; return { id , name : r . name ! , resourceGroup : match ? match [ 1 ] : "" , } ; } ) ; } ; param servicePrincipalId param servicePrincipalPassword param servicePrincipalTenantId param subscriptionId export const getContainerRegistry = async ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId , resourceGroup , name ) : Promise < RegistryItem | undefined > => { const registries = await getContainerRegistries ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId ) ; return registries . find ( ( r ) => r . resourceGroup === resourceGroup && r . name === name ) ; } ; param servicePrincipalId param servicePrincipalPassword param servicePrincipalTenantId param subscriptionId param resourceGroup param name export const isExist = async ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId , name ) : Promise < boolean > => { const registries = await getContainerRegistries ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId ) ; return ( registries || [ ] ) . some ( ( r ) => r . name === name ) ; } ; param servicePrincipalId param servicePrincipalPassword param servicePrincipalTenantId param subscriptionId param resourceGroup param name export const create = async ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId , resourceGroup , name , location ) : Promise < boolean > => { logger . info ( `attempting to create Azure container registry, ${ name } in ${ resourceGroup }` ) ; const exist = await isExist ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId , name ) ; if ( exist ) { logger . info ( `Azure container registry, ${ name } already exists in subscription` ) ; return false ; } await getClient ( servicePrincipalId , servicePrincipalPassword , servicePrincipalTenantId , subscriptionId ) ; await client . registries . create ( resourceGroup , name , { location , sku : { name : "Standard" , tier : "Standard" } , } ) ; logger . info ( `Successfully create Azure container registry, ${ name } in ${ resourceGroup }.` ) ; return true ; } ;