|
| 1 | +import { |
| 2 | + ContainerAppsAPIClient, |
| 3 | + ManagedEnvironment, |
| 4 | + Certificate, |
| 5 | + DaprComponent, |
| 6 | + ManagedEnvironmentStorage, |
| 7 | +} from '@azure/arm-appcontainers' |
| 8 | + |
| 9 | +import CloudGraph from '@cloudgraph/sdk' |
| 10 | +import azureLoggerText from '../../properties/logger' |
| 11 | +import { AzureServiceInput, TagMap } from '../../types' |
| 12 | +import { tryCatchWrapper } from '../../utils/index' |
| 13 | +import { regionMap } from '../../enums/regions' |
| 14 | + |
| 15 | +const { logger } = CloudGraph |
| 16 | +const lt = { ...azureLoggerText } |
| 17 | +const serviceName = 'ContainerAppEnvironment' |
| 18 | + |
| 19 | +export interface RawAzureAppEnvironment |
| 20 | + extends Omit<ManagedEnvironment, 'location' | 'tags'> { |
| 21 | + resourceGroupId?: string |
| 22 | + certificates: Certificate[] |
| 23 | + daprComponents: DaprComponent[] |
| 24 | + storages: ManagedEnvironmentStorage[] |
| 25 | + region: string |
| 26 | + defaultDomain?: string |
| 27 | + eventStreamEndpoint?: string |
| 28 | + infrastructureResourceGroup?: string |
| 29 | + provisioningState?: string |
| 30 | + staticIp?: string |
| 31 | + subscriptionId?: string |
| 32 | + zoneRedundant?: boolean |
| 33 | + Tags: TagMap |
| 34 | +} |
| 35 | + |
| 36 | +async function getDaprComponents( |
| 37 | + client: ContainerAppsAPIClient, |
| 38 | + resourceGroupName: string, |
| 39 | + resource: ManagedEnvironment |
| 40 | +): Promise<DaprComponent[]> { |
| 41 | + const daprComponents = [] as DaprComponent[] |
| 42 | + await tryCatchWrapper( |
| 43 | + async () => { |
| 44 | + for await (const daprComponent of client.daprComponents.list( |
| 45 | + resourceGroupName, |
| 46 | + resource.name |
| 47 | + )) { |
| 48 | + daprComponents.push(daprComponent) |
| 49 | + } |
| 50 | + }, |
| 51 | + { |
| 52 | + service: serviceName, |
| 53 | + client, |
| 54 | + scope: 'daprComponents', |
| 55 | + operation: 'list', |
| 56 | + } |
| 57 | + ) |
| 58 | + return daprComponents |
| 59 | +} |
| 60 | + |
| 61 | +async function getCertificates( |
| 62 | + client: ContainerAppsAPIClient, |
| 63 | + resourceGroupName: string, |
| 64 | + resource: ManagedEnvironment |
| 65 | +): Promise<Certificate[]> { |
| 66 | + const certificates = [] as Certificate[] |
| 67 | + await tryCatchWrapper( |
| 68 | + async () => { |
| 69 | + for await (const certificate of client.certificates.list( |
| 70 | + resourceGroupName, |
| 71 | + resource.name |
| 72 | + )) { |
| 73 | + certificates.push(certificate) |
| 74 | + } |
| 75 | + }, |
| 76 | + { |
| 77 | + service: serviceName, |
| 78 | + client, |
| 79 | + scope: 'certificates', |
| 80 | + operation: 'list', |
| 81 | + } |
| 82 | + ) |
| 83 | + return certificates |
| 84 | +} |
| 85 | + |
| 86 | +async function getStorages( |
| 87 | + client: ContainerAppsAPIClient, |
| 88 | + resourceGroupName: string, |
| 89 | + resource: ManagedEnvironment |
| 90 | +): Promise<ManagedEnvironmentStorage[]> { |
| 91 | + const storages = [] as ManagedEnvironmentStorage[] |
| 92 | + await tryCatchWrapper( |
| 93 | + async () => { |
| 94 | + const storagesCollection = await client.managedEnvironmentsStorages.list( |
| 95 | + resourceGroupName, |
| 96 | + resource.name |
| 97 | + ) |
| 98 | + for (const storage of storagesCollection.value) { |
| 99 | + storages.push(storage) |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + service: serviceName, |
| 104 | + client, |
| 105 | + scope: 'managedEnvironmentsStorages', |
| 106 | + operation: 'list', |
| 107 | + } |
| 108 | + ) |
| 109 | + return storages |
| 110 | +} |
| 111 | + |
| 112 | +export default async ({ |
| 113 | + config, |
| 114 | +}: AzureServiceInput): Promise<{ |
| 115 | + [property: string]: RawAzureAppEnvironment[] |
| 116 | +}> => { |
| 117 | + try { |
| 118 | + const { tokenCredentials, subscriptionId } = config |
| 119 | + const client = new ContainerAppsAPIClient(tokenCredentials, subscriptionId) |
| 120 | + const resources: RawAzureAppEnvironment[] = [] |
| 121 | + const result = { global: [] } |
| 122 | + await tryCatchWrapper( |
| 123 | + async () => { |
| 124 | + for await (const resource of client.managedEnvironments.listBySubscription()) { |
| 125 | + if (resource) { |
| 126 | + const { tags, ...rest } = resource |
| 127 | + |
| 128 | + // "/subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.App/containerApps/YYY" |
| 129 | + const [, , , , resourceGroupName] = resource.id.split('/') |
| 130 | + const certificates = await getCertificates( |
| 131 | + client, |
| 132 | + resourceGroupName, |
| 133 | + resource |
| 134 | + ) |
| 135 | + |
| 136 | + const daprComponents = await getDaprComponents( |
| 137 | + client, |
| 138 | + resourceGroupName, |
| 139 | + resource |
| 140 | + ) |
| 141 | + |
| 142 | + const storages = await getStorages( |
| 143 | + client, |
| 144 | + resourceGroupName, |
| 145 | + resource |
| 146 | + ) |
| 147 | + |
| 148 | + resources.push({ |
| 149 | + ...rest, |
| 150 | + region: resource.location || regionMap.global, |
| 151 | + certificates, |
| 152 | + daprComponents, |
| 153 | + storages, |
| 154 | + Tags: tags || {}, |
| 155 | + }) |
| 156 | + } |
| 157 | + } |
| 158 | + }, |
| 159 | + { |
| 160 | + service: serviceName, |
| 161 | + client, |
| 162 | + scope: 'managedEnvironments', |
| 163 | + operation: 'listBySubscription', |
| 164 | + } |
| 165 | + ) |
| 166 | + logger.debug(lt.foundContainerAppEnvironment(resources.length)) |
| 167 | + |
| 168 | + resources.map(({ region, ...rest }) => { |
| 169 | + result.global.push({ |
| 170 | + ...rest, |
| 171 | + region, |
| 172 | + }) |
| 173 | + }) |
| 174 | + return result |
| 175 | + } catch (e) { |
| 176 | + logger.error(e) |
| 177 | + return {} |
| 178 | + } |
| 179 | +} |
0 commit comments