Azure Bicep RPC 客户端
用于程序互动的节点js库 双子CLI 通过JSON-RPC.
开始
安装软件包
npm install @azure/bicep-rpc-client
初始化客户端
Bicep.install() 下载 Bicep CLI(如果需要)并返回其路径。 默认情况下, 通过一个目录, 如 os.tmpdir() 或者说 ~/.bicep 作为基础路径。 那就打电话 Bicep.initialize() 打开 JSON-RPC 连接.
import os from 'os';
import { Bicep } from '@azure/bicep-rpc-client';
const bicepPath = await Bicep.install(os.tmpdir());
const bicep = await Bicep.initialize(bicepPath);
// ... use bicep ...
bicep.dispose();
键入特定的比策版本
const bicepPath = await Bicep.install(os.tmpdir(), '0.38.5');
使用现有的 Bicep 安装
const bicep = await Bicep.initialize('/usr/local/bin/bicep');
可用业务
所有的方法都是同Bicep CLI在JSON-RPC上进行协同并交流. 打电话 bicep.dispose() 当关闭连接时.
编译
编译 a .bicep 文件为 ARM 模板 JSON 字符串.
const result = await bicep.compile({ path: './main.bicep' });
if (result.success) {
// result.contents contains the ARM template JSON
console.log(result.contents);
}
编译 Params
编译 a .bicepparam 输入 ARM 部署参数。 您可以选择覆盖参数值 .
const result = await bicep.compileParams({
path: './main.bicepparam',
parameterOverrides: {},
});
if (result.success) {
console.log(result.parameters); // ARM parameters JSON
console.log(result.template); // ARM template JSON (if resolvable)
// result.templateSpecId is set when the params file references a template spec
}
格式
按照标准Bicep格式化规则格式化 Bicep 文件。 需要双子CLI 0.37.0 (单位:千美元) 或后会.
const result = await bicep.format({ path: './main.bicep' });
fs.writeFileSync('./main.bicep', result.contents);
获取元数据
从 Bicep 文件获取参数、输出、导出和文件级元数据.
const result = await bicep.getMetadata({ path: './main.bicep' });
for (const param of result.parameters) {
console.log(`param ${param.name}: ${param.type?.name} — ${param.description}`);
}
for (const output of result.outputs) {
console.log(`output ${output.name}: ${output.type?.name}`);
}
for (const exp of result.exports) {
console.log(`@export() ${exp.kind} ${exp.name}`);
}
for (const meta of result.metadata) {
console.log(`metadata ${meta.name} = '${meta.value}'`);
}
获取文件参考
返回 Bicep 文件所引用的所有文件路径——模块、已装入的文件和文件本身.
const result = await bicep.getFileReferences({ path: './main.bicep' });
for (const filePath of result.filePaths) {
console.log(filePath);
}
获得部署
返回 Bicep 文件的资源依赖图,用于可视化.
const result = await bicep.getDeploymentGraph({ path: './main.bicep' });
for (const node of result.nodes) {
const existing = node.isExisting ? ' (existing)' : '';
console.log(` ${node.name}: ${node.type}${existing}`);
}
for (const edge of result.edges) {
console.log(` ${edge.source} -> ${edge.target}`);
}
获取图像
生成一个快照 .bicepparam 带有 Azure 部署上下文的文件。 需要双子CLI 0.36.1 国家 或后会.
const result = await bicep.getSnapshot({
path: './main.bicepparam',
metadata: {
tenantId: undefined,
subscriptionId: '00000000-0000-0000-0000-000000000000',
resourceGroup: 'my-rg',
location: 'eastus',
deploymentName: 'my-deployment',
},
externalInputs: undefined,
});
console.log(result.snapshot);
版本
返回连接的 Bicep CLI 版本.
const ver = await bicep.version();
console.log(`Bicep CLI version: ${ver}`);