-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_azure_stack.py
257 lines (210 loc) · 8.16 KB
/
gen_azure_stack.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import dataclasses
import os
import logging
import platform
import re
import json
import subprocess
from typing import List
# controls
TOGGLE_PATCH_README = True
TOGGLE_GENERATE_SDK = True
TOGGLE_GENERATE_ARM_CODE = True
SPEC_REPO = "c:/github/azure-rest-api-specs"
PROFILE_PATH = "profile/2020-09-01-hybrid.json"
TAG_PROFILE = "package-profile-2020-09-01-hybrid"
SDK_OUTPUT_PATH = "c:/github_lab/azure-stack-java-samples/azure-resourcemanager-hybrid"
SDK_AZS_NAMESPACE = "hybrid"
AUTOREST_JAVA_PATH = "c:/github_fork/autorest.java"
AUTOREST_CORE_VERSION = "3.1.3"
OS_WINDOWS = platform.system().lower() == "windows"
@dataclasses.dataclass
class SpecInfo:
type: str
api_version: str
spec_path: str
@dataclasses.dataclass
class SpecInfoCollection:
provider: str
spec_info_list: List[SpecInfo] = dataclasses.field(default_factory=list)
@dataclasses.dataclass
class SpecTag:
sdk_name: str
sdk_namespace: str = dataclasses.field(compare=False)
readme_path: str = dataclasses.field(compare=False)
tag: str = dataclasses.field(default=TAG_PROFILE, compare=False)
@dataclasses.dataclass
class Manager:
class_name: str
variable_name: str
method_name: str
def main():
logging.basicConfig(level=logging.INFO)
process_profile()
def process_profile():
spec_info_collection_dict = {}
profile_path = os.path.join(SPEC_REPO, PROFILE_PATH)
with open(profile_path, encoding="utf-8") as f:
profile_dict = json.load(f)
resource_manager_dict = profile_dict["resource-manager"]
for entry in resource_manager_dict.items():
resource_provider = entry[0]
if resource_provider not in spec_info_collection_dict:
spec_info_collection_dict[resource_provider] = SpecInfoCollection(resource_provider)
resource_version_dict = entry[1]
for entry1 in resource_version_dict.items():
resource_version = entry1[0]
resource_type_array = entry1[1]
for item in resource_type_array:
resource_type = item["resourceType"]
spec_path = item["path"]
spec_info_collection_dict[resource_provider].spec_info_list.append(
SpecInfo(resource_type, resource_version, spec_path)
)
spec_tag_list = []
for item in spec_info_collection_dict.values():
spec_tag = process_spec(item)
spec_tag_list.append(spec_tag)
if TOGGLE_GENERATE_SDK:
for item in spec_tag_list:
codegen(item, SDK_OUTPUT_PATH)
if TOGGLE_GENERATE_ARM_CODE:
generate_arm_code(spec_tag_list)
def process_spec(spec_info_collection: SpecInfoCollection) -> SpecTag:
provider_name = spec_info_collection.provider.split(".")[1]
sdk_name_set = set()
sdk_name = None
for item in spec_info_collection.spec_info_list:
sdk_name = re.match(".*/specification/(.*)/resource-manager/.*", item.spec_path).group(1)
sdk_name_set.add(sdk_name)
if len(sdk_name_set) == 1:
sdk_name = sdk_name_set.pop()
else:
for name in sdk_name_set:
if name == provider_name:
sdk_name = name
break
# hack
sdk_namespace = sdk_name
if sdk_name == "web":
sdk_namespace = "appservice"
elif sdk_name == "eventhub":
sdk_namespace = "eventhubs"
readme_path = os.path.join(SPEC_REPO, "specification", sdk_name, "resource-manager/readme.md")
if TOGGLE_PATCH_README:
spec_path_list = [item.spec_path for item in spec_info_collection.spec_info_list]
spec_path_list = list(set(spec_path_list))
patch_readme(readme_path, spec_path_list)
return SpecTag(sdk_name, sdk_namespace, readme_path, TAG_PROFILE)
def patch_readme(readme_path: str, spec_path_list: List[str]):
logging.info(f"patch readme.md file {readme_path} for tag {TAG_PROFILE}")
lines = []
with open(readme_path, "r", encoding="utf-8") as f:
lines.extend(f.readlines())
lines.append("```yaml $(tag) == '" + TAG_PROFILE + "'\n")
lines.append("input-file:\n")
for spec_path in spec_path_list:
lines.append(" - " + spec_path + "\n")
with open(readme_path, "w", encoding="utf-8") as f:
content = "".join(lines)
f.write(content)
def codegen(spec_tag: SpecTag, output_sdk_dir: str) -> subprocess.CompletedProcess:
logging.info(f"generate code for RP: {spec_tag.sdk_name}")
namespace = f"com.azure.resourcemanager.{SDK_AZS_NAMESPACE}.{spec_tag.sdk_namespace}".lower()
namespace = re.sub("[^a-z.]", "", namespace)
command = [
"autorest" + (".cmd" if OS_WINDOWS else ""),
# '--verbose',
"--version=" + AUTOREST_CORE_VERSION,
"--java",
"--use=" + AUTOREST_JAVA_PATH,
"--tag=" + TAG_PROFILE,
"--regenerate-pom=false",
"--pipeline.modelerfour.additional-checks=false",
"--pipeline.modelerfour.lenient-model-deduplication=true",
"--azure-arm",
"--fluent=lite",
"--java.fluent=lite",
"--java.license-header=MICROSOFT_MIT_SMALL",
"--java.output-folder=" + output_sdk_dir,
"--java.namespace=" + namespace,
spec_tag.readme_path,
]
# hack
if spec_tag.sdk_name == "resources":
command.append("--title=ResourceManagementClient")
if spec_tag.sdk_name == "authorization":
command.append("--title=AuthorizationManagementClient")
logging.info(" ".join(command))
result = subprocess.run(command, capture_output=False, text=True, encoding="utf-8")
return result
def generate_arm_code(spec_tag_list: List[SpecTag]):
manager_list = []
for spec_tag in spec_tag_list:
code_path = os.path.join(
SDK_OUTPUT_PATH, "src/main/java/com/azure/resourcemanager", SDK_AZS_NAMESPACE, spec_tag.sdk_namespace
)
for file in os.listdir(code_path):
if file.endswith("Manager.java"):
class_name = file.split(".")[0]
variable_name = class_name[0].lower() + class_name[1:]
method_name = variable_name[0 : len(variable_name) - len("Manager")]
manager_list.append(Manager(class_name, variable_name, method_name))
break
# variables
for manager in manager_list:
print(f"private final {manager.class_name} {manager.variable_name};\n")
# methods
for manager in manager_list:
print(
(
f"/** @return the {{@link {manager.class_name}}}. */\n"
f"public {manager.class_name} {manager.method_name}() {{\n"
f" return {manager.variable_name};\n"
f"}}\n"
)
)
# initialization
for manager in manager_list:
print(
f"{manager.class_name}.Configurable {manager.variable_name}Configurable = {manager.class_name}.configure();"
)
print()
print(
"""HttpClient httpClient = configurable.httpClient;
if (httpClient == null) {
httpClient = HttpClient.createDefault();
}
if (httpClient != null) {"""
)
for manager in manager_list:
print(f" {manager.variable_name}Configurable.withHttpClient(httpClient);")
print(
"""}
if (configurable.httpLogOptions != null) {"""
)
for manager in manager_list:
print(f" {manager.variable_name}Configurable.withLogOptions(configurable.httpLogOptions);")
print(
"""}
if (configurable.retryPolicy != null) {"""
)
for manager in manager_list:
print(f" {manager.variable_name}Configurable.withRetryPolicy(configurable.retryPolicy);")
print(
"""}
for (HttpPipelinePolicy policy : configurable.policies) {"""
)
for manager in manager_list:
print(f" {manager.variable_name}Configurable.withPolicy(policy);")
print(
"""}
if (configurable.defaultPollInterval != null) {"""
)
for manager in manager_list:
print(f" {manager.variable_name}Configurable.withDefaultPollInterval(configurable.defaultPollInterval);")
print("}")
print()
for manager in manager_list:
print(f"this.{manager.variable_name} = {manager.variable_name}Configurable.authenticate(credential, profile);")
main()