VmBuilder Class OverviewThe VmBuilder class is designed to build and configure an Azure Virtual Machine (VM) instance with specific configurations such as OS, size, login, network settings, encryption, and scheduling.
Initializes the VmBuilder with the provided arguments.
const vmBuilder = new VmBuilder({
name: 'myVmInstance',
group: { resourceGroupName: 'myResourceGroup' },
// other necessary arguments
});
enableEncryptionEnables encryption for the VM with the specified properties.
vmBuilder.enableEncryption({
diskEncryptionSetId: 'encryption-set-id',
});
withScheduleSets the schedule for the VM.
vmBuilder.withSchedule({
startOn: '2023-01-01T00:00:00Z',
stopOn: '2023-01-01T12:00:00Z',
});
withSubnetIdSets the subnet ID for the VM.
vmBuilder.withSubnetId('subnet-id');
withTagsSets the tags for the VM.
vmBuilder.withTags({
environment: 'production',
project: 'myProject',
});
generateLoginEnables the generation of a random login for the VM.
vmBuilder.generateLogin();
withLoginInfoSets the login information for the VM.
vmBuilder.withLoginInfo({
adminLogin: 'adminUser',
password: 'securePassword',
});
withSizeSets the size of the VM.
vmBuilder.withSize('Standard_DS1_v2');
withWindowsImageSets the Windows image for the VM.
vmBuilder.withWindowsImage({
publisher: 'MicrosoftWindowsServer',
offer: 'WindowsServer',
sku: '2019-Datacenter',
version: 'latest',
});
withLinuxImageSets the Linux image for the VM.
vmBuilder.withLinuxImage({
publisher: 'Canonical',
offer: 'UbuntuServer',
sku: '18.04-LTS',
version: 'latest',
});
ignoreChangesFromSpecifies properties to ignore changes for.
vmBuilder.ignoreChangesFrom('property1', 'property2');
buildLoginGenerates a random login if enabled.
This method is called internally by the build method and is not typically called directly.
buildVmCreates the VM instance with the specified configurations.
This method is called internally by the build method and is not typically called directly.
buildBuilds the VM instance and returns the resource information.
const resourceInfo = vmBuilder.build();
console.log(resourceInfo);
Here is a full example demonstrating how to use the VmBuilder class:
import VmBuilder from './Builder/VmBuilder';
import { VmBuilderArgs } from './types';
const args: VmBuilderArgs = {
name: 'myVmInstance',
group: { resourceGroupName: 'myResourceGroup' },
// other necessary arguments
};
const vmBuilder = new VmBuilder(args);
vmBuilder
.enableEncryption({
diskEncryptionSetId: 'encryption-set-id',
})
.withSchedule({
startOn: '2023-01-01T00:00:00Z',
stopOn: '2023-01-01T12:00:00Z',
})
.withSubnetId('subnet-id')
.withTags({
environment: 'production',
project: 'myProject',
})
.generateLogin()
.withLoginInfo({
adminLogin: 'adminUser',
password: 'securePassword',
})
.withSize('Standard_DS1_v2')
.withWindowsImage({
publisher: 'MicrosoftWindowsServer',
offer: 'WindowsServer',
sku: '2019-Datacenter',
version: 'latest',
})
.ignoreChangesFrom('property1', 'property2');
const resourceInfo = vmBuilder.build();
console.log(resourceInfo);
This guideline should help developers understand and reuse the methods in the VmBuilder class effectively.