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
});
enableEncryption
Enables encryption for the VM with the specified properties.
vmBuilder.enableEncryption({
diskEncryptionSetId: 'encryption-set-id',
});
withSchedule
Sets the schedule for the VM.
vmBuilder.withSchedule({
startOn: '2023-01-01T00:00:00Z',
stopOn: '2023-01-01T12:00:00Z',
});
withSubnetId
Sets the subnet ID for the VM.
vmBuilder.withSubnetId('subnet-id');
withTags
Sets the tags for the VM.
vmBuilder.withTags({
environment: 'production',
project: 'myProject',
});
generateLogin
Enables the generation of a random login for the VM.
vmBuilder.generateLogin();
withLoginInfo
Sets the login information for the VM.
vmBuilder.withLoginInfo({
adminLogin: 'adminUser',
password: 'securePassword',
});
withSize
Sets the size of the VM.
vmBuilder.withSize('Standard_DS1_v2');
withWindowsImage
Sets the Windows image for the VM.
vmBuilder.withWindowsImage({
publisher: 'MicrosoftWindowsServer',
offer: 'WindowsServer',
sku: '2019-Datacenter',
version: 'latest',
});
withLinuxImage
Sets the Linux image for the VM.
vmBuilder.withLinuxImage({
publisher: 'Canonical',
offer: 'UbuntuServer',
sku: '18.04-LTS',
version: 'latest',
});
ignoreChangesFrom
Specifies properties to ignore changes for.
vmBuilder.ignoreChangesFrom('property1', 'property2');
buildLogin
Generates a random login if enabled.
This method is called internally by the build
method and is not typically called directly.
buildVm
Creates the VM instance with the specified configurations.
This method is called internally by the build
method and is not typically called directly.
build
Builds 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.