in dm/monitoring-dashboard.py [0:0]
def create_dashboard_resource(context):
""" Creates the Cloud Monitoring Dashboard resource. """
properties = context.properties
project_id = properties.get("project", context.env["project"])
dash_name = "projects/{}/dashboards/{}".format(project_id, uuid.uuid4())
parent = "projects/{}".format(project_id)
config_file = properties.get("config_file")
# Read the dashboard json file. Fix potential trailing comma errors
with open(config_file, "r") as fh:
json_file = fh.read()
data = json.loads(re.sub(r"}\s*\,\s*}", "}}", json_file))
props = {
"parent": parent,
"name": dash_name,
}
props.update(data)
# The type provider 'monitoring-dashboardv1type' needs to be created first!
# The discovery API: https://monitoring.googleapis.com/$discovery/rest?version=v1
dashboard_config = [
{
"action": "{}/monitoring-dashboardv1type:monitoring.projects.dashboards.create".format(
project_id
),
"metadata": {"runtimePolicy": ["CREATE"]},
"name": "{}-create".format(context.env["name"]),
"properties": props,
},
{
"action": "{}/monitoring-dashboardv1type:monitoring.projects.dashboards.delete".format(
project_id
),
"metadata": {"runtimePolicy": ["DELETE"]},
"name": "{}-delete".format(context.env["name"]),
"properties": {"name": dash_name},
},
]
return (
dashboard_config,
[{"name": "project_id", "value": context.properties["projectId"]}],
)