This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzoo_yml.py
124 lines (111 loc) Β· 3.34 KB
/
zoo_yml.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from typing import Dict, Union
import structlog
from jsonschema import ValidationError
from jsonschema import validate as schema_validate
from yaml import FullLoader, dump, load
from zoo.services.models import Service
log = structlog.get_logger()
ZOO_JSON_SCHEMA = """
type: object
properties:
type:
type: string
name:
type: string
owner:
type: string
impact:
type: ["string", "null"]
enum: ["profit", "customers", "employees"]
status:
type: ["string", "null"]
enum: ["beta", "production", "deprecated", "discontinued"]
docs_url:
type: ["string", "null"]
slack_channel:
type: ["string", "null"]
sentry_project:
type: ["string", "null"]
sonarqube_project:
type: ["string", "null"]
pagerduty_service:
type: string
tags:
type: array
items:
type: string
environments:
type: array
items:
type: object
properties:
name:
type: string
dashboard_url:
type: ["string", "null"]
service_urls:
type: array
items:
type: string
health_check_url:
type: ["string", "null"]
links:
type: array
items:
type: object
properties:
name:
type: string
url:
type: string
icon:
type: ["string", "null"]
additionalProperties: false
required:
- type
- name
- owner
"""
def validate(yml: str) -> bool:
try:
schema_validate(load(yml, Loader=FullLoader), load(ZOO_JSON_SCHEMA, FullLoader))
except ValidationError as err:
log.info("repos.sync_zoo_yml.validation_error", error=err)
return False
else:
return True
def parse(yaml: str) -> Union[Dict, None]:
return load(yaml, Loader=FullLoader)
def generate(service: Service) -> str:
result = {
"type": "service",
"name": service.name,
"owner": service.owner,
"impact": service.impact,
"status": service.status,
"docs_url": service.docs_url,
"slack_channel": service.slack_channel,
"sentry_project": service.sentry_project,
"sonarqube_project": service.sonarqube_project,
"pagerduty_service": service.pagerduty_service,
"tags": service.tags,
"environments": [],
"links": [],
}
for env in service.environments.all():
environ = {
"name": env.name,
"dashboard_url": env.dashboard_url,
"health_check_url": env.health_check_url,
"service_urls": env.service_urls,
}
result["environments"].append(environ)
for link in service.links.all():
result["links"].append(
{
"name": link.name,
"url": link.url,
"icon": link.icon,
}
)
return dump(result, sort_keys=False)