Skip to content

Commit 848356e

Browse files
committed
chg: split logic out to make it fs independent
1 parent ac3bac9 commit 848356e

1 file changed

Lines changed: 66 additions & 48 deletions

File tree

src/datasync/services.py

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import pathlib
23
from collections import OrderedDict
34

45
import duckdb
@@ -8,7 +9,7 @@
89
import typer
910
import yaml
1011

11-
from .settings import env
12+
from .settings import env, log
1213

1314
app = typer.Typer(help="Miljødata Infrastructure as Code pipelines")
1415

@@ -167,48 +168,7 @@ def services_to_parquet(
167168
DASHBOARD_ORG = env("SERVICES_DASHBOARD_ORG", default="ninanor")
168169

169170

170-
@app.command(
171-
help="Produce a Homer Dashbord using the Miljødata Infrastructure as Code "
172-
"repository as data source"
173-
)
174-
def dashboard(
175-
org=SERVICES_ORG,
176-
repo: str | None = SERVICES_REPO,
177-
config_org=DASHBOARD_ORG,
178-
config_repo=DASHBOARD_REPO,
179-
bucket: str | None = AWS_BUCKET,
180-
endpoint: str | None = AWS_ENDPOINT,
181-
access_key: str | None = AWS_ACCESS_KEY,
182-
secret_key: str | None = AWS_SECRET_KEY,
183-
git_username=GIT_USERNAME,
184-
git_token=GIT_TOKEN,
185-
prefix=DASHBOARD_PREFIX,
186-
):
187-
s3 = s3fs.S3FileSystem(
188-
endpoint_url=f"https://{endpoint}",
189-
secret=secret_key,
190-
key=access_key,
191-
)
192-
193-
infrastucture_repo = fsspec.filesystem(
194-
"github",
195-
org=org,
196-
repo=repo,
197-
username=git_username,
198-
token=git_token,
199-
)
200-
201-
dashboard_repo = fsspec.filesystem(
202-
"github",
203-
org=config_org,
204-
repo=config_repo,
205-
username=git_username,
206-
token=git_token,
207-
)
208-
209-
with dashboard_repo.open("base-config.yml", "r") as base_conf_file:
210-
base_conf = yaml.load(base_conf_file, Loader=yaml.SafeLoader)
211-
171+
def parse_services(fs: fsspec.AbstractFileSystem):
212172
curated = OrderedDict()
213173
all_services = OrderedDict()
214174

@@ -228,16 +188,19 @@ def dashboard(
228188
"public": "is-success",
229189
}
230190

231-
services_paths = infrastucture_repo.glob(path="services/*/*/metadata.yml")
191+
services_paths = fs.glob(path="services/*/*/metadata.yml")
232192
for spath in services_paths:
233-
with infrastucture_repo.open(spath, "r") as f:
193+
log.info("found path", path=spath)
194+
with fs.open(spath, "r") as f:
234195
data = yaml.load(f, Loader=yaml.SafeLoader)
235-
_, project_name, module_name, _ = spath.split("/")
236-
for resource in data.get("resources"):
196+
parts = pathlib.Path(spath).parts
197+
project_name = parts[-3]
198+
for resource in data.get("resources", []):
199+
log.info("Parsing resource", resource=resource)
237200
if not resource["uri"].startswith("http") or resource.get(
238201
"external", False
239202
):
240-
print(resource)
203+
log.info("Skipping resource", resource=resource)
241204
# skip non http resources and external resources
242205
continue
243206

@@ -278,6 +241,52 @@ def dashboard(
278241
"icon": ICON[resource.get("type", "web")],
279242
}
280243
)
244+
return curated, all_services
245+
246+
247+
@app.command(
248+
help="Produce a Homer Dashbord using the Miljødata Infrastructure as Code "
249+
"repository as data source"
250+
)
251+
def dashboard(
252+
org=SERVICES_ORG,
253+
repo: str | None = SERVICES_REPO,
254+
config_org=DASHBOARD_ORG,
255+
config_repo=DASHBOARD_REPO,
256+
bucket: str | None = AWS_BUCKET,
257+
endpoint: str | None = AWS_ENDPOINT,
258+
access_key: str | None = AWS_ACCESS_KEY,
259+
secret_key: str | None = AWS_SECRET_KEY,
260+
git_username=GIT_USERNAME,
261+
git_token=GIT_TOKEN,
262+
prefix=DASHBOARD_PREFIX,
263+
):
264+
s3 = s3fs.S3FileSystem(
265+
endpoint_url=f"https://{endpoint}",
266+
secret=secret_key,
267+
key=access_key,
268+
)
269+
270+
infrastucture_repo = fsspec.filesystem(
271+
"github",
272+
org=org,
273+
repo=repo,
274+
username=git_username,
275+
token=git_token,
276+
)
277+
278+
dashboard_repo = fsspec.filesystem(
279+
"github",
280+
org=config_org,
281+
repo=config_repo,
282+
username=git_username,
283+
token=git_token,
284+
)
285+
286+
with dashboard_repo.open("base-config.yml", "r") as base_conf_file:
287+
base_conf = yaml.load(base_conf_file, Loader=yaml.SafeLoader)
288+
289+
curated, all_services = parse_services(infrastucture_repo)
281290

282291
curated_conf = copy.deepcopy(base_conf)
283292
curated_conf["services"] = [group for _, group in curated.items()]
@@ -298,3 +307,12 @@ def dashboard(
298307
all_conf,
299308
f,
300309
)
310+
311+
312+
@app.command(
313+
help="Test the parsing of the services metadata "
314+
"and the generation of the dashboard configuration"
315+
)
316+
def dashboard_test():
317+
fs = fsspec.filesystem("file")
318+
parse_services(fs)

0 commit comments

Comments
 (0)