jx2lee commented on code in PR #50908: URL: https://github.com/apache/airflow/pull/50908#discussion_r2111660695
########## airflow-ctl/src/airflowctl/ctl/commands/variable_command.py: ########## @@ -0,0 +1,96 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import json +import os +import sys +from pathlib import Path + +import rich + +from airflow.api_fastapi.core_api.datamodels.common import BulkAction, BulkActionOnExistence +from airflowctl.api.client import NEW_API_CLIENT, ClientKind, provide_api_client +from airflowctl.api.datamodels.generated import ( + BulkBodyVariableBody, + BulkCreateActionVariableBody, + VariableBody, +) + + +@provide_api_client(kind=ClientKind.CLI) +def import_(args, api_client=NEW_API_CLIENT): + """Import variables from a given file.""" + success_message = "[green]Import successful! success: {success}, errors: {errors}[/green]" + if not os.path.exists(args.file): + rich.print(f"[red]Missing variable file: {args.file}") + sys.exit(1) + with open(args.file) as var_file: + try: + var_json = json.load(var_file) + except json.JSONDecodeError: + rich.print(f"[red]Invalid variable file: {args.file}") + sys.exit(1) + + action_on_existence = BulkActionOnExistence(args.action_on_existing_key) + vars_to_update = [] + for k, v in var_json.items(): + value, description = v, None + if isinstance(v, dict) and v.get("value"): + value, description = v["value"], v.get("description") + + vars_to_update.append( + VariableBody( + key=k, + value=value, + description=description, + ) + ) + + bulk_body = BulkBodyVariableBody( + actions=[ + BulkCreateActionVariableBody( + action=BulkAction.CREATE, Review Comment: Fixed it. But I didn't write test code on this, Do I need a test code about this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
