-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathby-region.jq
129 lines (113 loc) · 2.89 KB
/
by-region.jq
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
#!/usr/bin/env jq
# Expects one or more service JSON files, slurped in to an array.
# Each service JSON file has a service display name, and one or more service
# plans that are available in one or more data centers:
#
# [
# {
# ...
# "displayName": "service display name",
# "servicePlans: [
# {
# "dataCenters": [
# ...
# {
# "region": "region code",
# "name": "region name"
# },
# ...
# ]
# },
# {
# ...
# }
# ]
# }
# ]
#
# This filter turns that data into a list of services by region:
#
# {
# "Australia (Sydney DR) (ap2)": [
# "SAP ASE service",
# "SAP BTP, Java server",
# "SAP HANA service for SAP BTP"
# ],
# "Australia (Sydney) (ap1)": [
# "SAP ASE service",
# "SAP BTP, Java server",
# "SAP HANA service for SAP BTP"
# ],
# "Australia (Sydney) (ap10)": [
# "API Management, API Business Hub Enterprise",
# ...
# ],
# ...
# }
#
# This function takes an object of key and value pairs that look like this
# (created by to_entries):
#
# [
# {
# "key": 1,
# "value": [
# {
# "service": "service display name",
# "region": "region identifier"
# },
# ...
# ]
# },
# ...
# ]
#
# and changes the keys and values so that they look like this:
#
# [
# {
# "key": "region identifier",
# "value": [
# "service displayname",
# "another service displayname",
# ...
# ]
# },
# ...
# ]
#
# It's designed to be used in a with_entries invocation:
# to_entries|map(arrange)|from_entries i.e. with_entries(arrange)
#
def arrange:
# Get the region identifier from the region property of the first object
# in the value property
.value[0].region as $region
# Update the key to be that region identifier
| .key |= $region
# Update the value to be an array of unique service display names
| .value |= (map(.service) | unique)
;
def main:
# Go through each of the services
map(
# Save the service display name for later
.displayName as $service
# Go through each of the plans for the service
| .servicePlans
# Build an array of regions for that service with the name and code
# as region identifier
| map(.dataCenters[]|("\(.name) (\(.region))"))
# Turn that into an array of pairs of service display name and region
| map({service: $service, region:.})
)
# Flatten all the arrays of pairs into a single array of pairs
| flatten
# Group the pair data by the region property
| group_by(.region)
# Finally, turn the pairs grouped by region into a single object, where
# the properties are the region identifiers and the values are arrays of
# the display names for services available in that region
| with_entries(arrange)
;
main