-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbicep_deployment.py
More file actions
89 lines (78 loc) · 2.59 KB
/
bicep_deployment.py
File metadata and controls
89 lines (78 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright (c) Alianza, Inc. All rights reserved.
# Licensed under the MIT License.
"""Manages Bicep deployments."""
import logging
from pathlib import Path
from typing import Any, Dict
from apt_package_function.azcmd import AzCmdJson, AzCmdNone
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class BicepDeployment:
"""Class to manage a Bicep deployment."""
def __init__(
self,
deployment_name: str,
resource_group_name: str,
template_file: Path,
parameters: Dict[str, Any],
description: str,
) -> None:
"""Create a BicepDeployment object."""
self.deployment_name = deployment_name
self.resource_group_name = resource_group_name
self.template_file = template_file
self.description = description
# Convert the set of parameters to a list of flags
self.parameters = []
for key, value in parameters.items():
self.parameters.extend(["--parameter", f"{key}={value}"])
def create(self) -> None:
"""Create the deployment."""
cmd = AzCmdNone(
[
"az",
"deployment",
"group",
"create",
"--name",
self.deployment_name,
"--resource-group",
self.resource_group_name,
"--template-file",
str(self.template_file),
*self.parameters,
]
)
log.info(
"Deploying: %s (in resource group: %s)",
self.description,
self.resource_group_name,
)
cmd.run()
log.info("Finished deploying %s", self.description)
def outputs(self) -> Dict[str, Any]:
"""Get the outputs of the deployment."""
cmd = AzCmdJson(
[
"az",
"deployment",
"group",
"show",
"--name",
self.deployment_name,
"--resource-group",
self.resource_group_name,
"--query",
"properties.outputs",
]
)
data = cmd.run_expect_dict()
# The returned outputs are a dictionary of dictionaries, with type
# information and values. We just want the values.
outputs = {}
for key, info in data.items():
if info["type"] == "String":
outputs[key] = str(info["value"])
else:
raise ValueError(f"Unsupported value type: {info['type']}")
return outputs