|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import json |
| 20 | +import os |
| 21 | +import sys |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import rich |
| 25 | + |
| 26 | +from airflow.api_fastapi.core_api.datamodels.common import BulkActionOnExistence |
| 27 | +from airflowctl.api.client import NEW_API_CLIENT, ClientKind, provide_api_client |
| 28 | +from airflowctl.api.datamodels.generated import ( |
| 29 | + BulkBodyVariableBody, |
| 30 | + BulkCreateActionVariableBody, |
| 31 | + VariableBody, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +@provide_api_client(kind=ClientKind.CLI) |
| 36 | +def import_(args, api_client=NEW_API_CLIENT): |
| 37 | + """Import variables from a given file.""" |
| 38 | + success_message = "[green]Import successful! success: {success}, errors: {errors}[/green]" |
| 39 | + if not os.path.exists(args.file): |
| 40 | + rich.print(f"[red]Missing variable file: {args.file}") |
| 41 | + sys.exit(1) |
| 42 | + with open(args.file) as var_file: |
| 43 | + try: |
| 44 | + var_json = json.load(var_file) |
| 45 | + except json.JSONDecodeError: |
| 46 | + rich.print(f"[red]Invalid variable file: {args.file}") |
| 47 | + sys.exit(1) |
| 48 | + |
| 49 | + action_on_existence = BulkActionOnExistence(args.action_on_existing_key) |
| 50 | + vars_to_update = [] |
| 51 | + for k, v in var_json.items(): |
| 52 | + value, description = v, None |
| 53 | + if isinstance(v, dict) and v.get("value"): |
| 54 | + value, description = v["value"], v.get("description") |
| 55 | + |
| 56 | + vars_to_update.append( |
| 57 | + VariableBody( |
| 58 | + key=k, |
| 59 | + value=value, |
| 60 | + description=description, |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + bulk_body = BulkBodyVariableBody( |
| 65 | + actions=[ |
| 66 | + BulkCreateActionVariableBody( |
| 67 | + action="create", |
| 68 | + entities=vars_to_update, |
| 69 | + action_on_existence=action_on_existence, |
| 70 | + ) |
| 71 | + ] |
| 72 | + ) |
| 73 | + result = api_client.variables.bulk(variables=bulk_body) |
| 74 | + rich.print(success_message.format(success=result.success, errors=result.errors)) |
| 75 | + return result.success, result.errors |
| 76 | + |
| 77 | + |
| 78 | +@provide_api_client(kind=ClientKind.CLI) |
| 79 | +def export(args, api_client=NEW_API_CLIENT): |
| 80 | + """Export all the variables to the file.""" |
| 81 | + success_message = "[green]Export successful! {total_entries} variable(s) to {file}[/green]" |
| 82 | + var_dict = {} |
| 83 | + variables = api_client.variables.list() |
| 84 | + |
| 85 | + for variable in variables.variables: |
| 86 | + if variable.description: |
| 87 | + var_dict[variable.key] = { |
| 88 | + "value": variable.value, |
| 89 | + "description": variable.description, |
| 90 | + } |
| 91 | + else: |
| 92 | + var_dict[variable.key] = variable.value |
| 93 | + |
| 94 | + with open(Path(args.file), "w") as var_file: |
| 95 | + json.dump(var_dict, var_file, sort_keys=True, indent=4) |
| 96 | + rich.print(success_message.format(total_entries=variables.total_entries, file=args.file)) |
0 commit comments