Skip to content

Commit

Permalink
update a few Azure node packages
Browse files Browse the repository at this point in the history
  • Loading branch information
JaydenLiang committed Jul 17, 2023
1 parent a25ef88 commit 42a0695
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 316 deletions.
75 changes: 51 additions & 24 deletions fortigate-autoscale/azure/azure-platform-adaptee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ComputeManagementClient } from '@azure/arm-compute';
import { VirtualMachineScaleSetVM } from '@azure/arm-compute/esm/models';
import { NetworkManagementClient } from '@azure/arm-network';
import { NetworkInterface } from '@azure/arm-network/esm/models';
import { ComputeManagementClient, VirtualMachineScaleSetVM } from '@azure/arm-compute';
import { NetworkInterface, NetworkManagementClient } from '@azure/arm-network';
import {
CosmosClient,
CosmosClientOptions,
Expand All @@ -13,13 +11,13 @@ import {
} from '@azure/cosmos';
import { ClientSecretCredential } from '@azure/identity';
import { SecretClient } from '@azure/keyvault-secrets';
import * as msRestNodeAuth from '@azure/ms-rest-nodeauth';
import { BlobServiceClient, StorageSharedKeyCredential } from '@azure/storage-blob';
import * as DBDef from '@fortinet/autoscale-core/db-definitions';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import fs from 'fs';
import * as HttpStatusCodes from 'http-status-codes';
import path from 'path';

import {
AzureApiRequestCache,
AzureFortiGateAutoscaleSetting,
Expand Down Expand Up @@ -147,17 +145,19 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
* process.env.TENANT_ID: the tenant containing the App registration (service principal) app.
* @returns {Promise} void
*/
async init(): Promise<void> {
init(): Promise<void> {
const cosmosClientOptions: CosmosClientOptions = {
endpoint: `https://${process.env.AUTOSCALE_DB_ACCOUNT}.documents.azure.com/`,
key: process.env.AUTOSCALE_DB_PRIMARY_KEY
};
this.azureCosmosDB = new CosmosClient(cosmosClientOptions);
this.autoscaleDBRef = this.azureCosmosDB.database(process.env.AUTOSCALE_DB_NAME);
const creds = await msRestNodeAuth.loginWithServicePrincipalSecret(
// NOTE: migrated from @azure/ms-rest-nodeauth
// see for example: https://github.com/Azure/ms-rest-nodeauth/blob/master/migrate-to-identity-v2.md#authenticate-with-national-clouds
const creds = new ClientSecretCredential(
process.env.TENANT_ID,
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
process.env.TENANT_ID
process.env.CLIENT_SECRET
);
this.azureCompute = new ComputeManagementClient(creds, process.env.SUBSCRIPTION_ID);
this.azureNetwork = new NetworkManagementClient(creds, process.env.SUBSCRIPTION_ID);
Expand All @@ -176,6 +176,7 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
process.env.CLIENT_SECRET
)
);
return Promise.resolve();
}

async reloadSettings(invalidateCache: boolean): Promise<Settings> {
Expand Down Expand Up @@ -708,7 +709,22 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
process.env[RequiredEnvVars.RESOURCE_GROUP],
scalingGroupName
);
return (response && response._response.parsedBody) || null;
const list: VirtualMachineScaleSetVM[] = [];
let result: IteratorResult<VirtualMachineScaleSetVM, VirtualMachineScaleSetVM[]>;
if (response) {
do {
result = await response.next();
const value = result.value;
if (Array.isArray(value)) {
value.forEach(vm => {
list.push(vm);
});
} else {
list.push(value);
}
} while (!result.done);
}
return list.length > 0 ? list : null;
};
return await this.requestWithCaching<VirtualMachineScaleSetVM[]>(
req,
Expand Down Expand Up @@ -804,20 +820,16 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
]);

// ASSERT: all related caches are deleted. can delete the vm now
const response = await this.azureCompute.virtualMachineScaleSetVMs.deleteMethod(
process.env[RequiredEnvVars.RESOURCE_GROUP],
scalingGroupName,
String(instanceId)
);
if (
response._response.status === HttpStatusCodes.OK ||
response._response.status === HttpStatusCodes.ACCEPTED
) {
try {
const op = await this.azureCompute.virtualMachineScaleSetVMs.beginDelete(
process.env[RequiredEnvVars.RESOURCE_GROUP],
scalingGroupName,
String(instanceId)
);
await op.pollUntilDone();
return true;
} else if (response._response.status === HttpStatusCodes.NO_CONTENT) {
return false;
} else {
throw new Error(`Unkown response with status code: ${response._response.status}.`);
} catch (error) {
throw new Error(`Error in deleting a VM from VMSS: ${error}`);
}
}
/**
Expand Down Expand Up @@ -846,7 +858,22 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
scalingGroupName,
String(id)
);
return (response && response._response.parsedBody) || null;
const list: NetworkInterface[] = [];
let result: IteratorResult<NetworkInterface, NetworkInterface[]>;
if (response) {
do {
result = await response.next();
const value = result.value;
if (Array.isArray(value)) {
value.forEach(vm => {
list.push(vm);
});
} else {
list.push(value);
}
} while (!result.done);
}
return list.length > 0 ? list : null;
};
return await this.requestWithCaching<NetworkInterface[]>(
req,
Expand Down
4 changes: 2 additions & 2 deletions fortigate-autoscale/azure/azure-platform-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as AzureComputeModels from '@azure/arm-compute/esm/models';
import * as AzureNetworkModels from '@azure/arm-network/esm/models';
import * as AzureComputeModels from '@azure/arm-compute';
import * as AzureNetworkModels from '@azure/arm-network';
import path from 'path';
import {
ApiCache,
Expand Down
Loading

0 comments on commit 42a0695

Please sign in to comment.