From c3893d7c6c4f8261ced00fde2e15496d065328ff Mon Sep 17 00:00:00 2001 From: Doug Guthrie Date: Sun, 30 Jul 2023 06:57:02 -0600 Subject: [PATCH 1/2] Add methods for update and list env vars --- dbtc/cli.py | 69 ++++++++++++++++++++++++++++++++++ dbtc/client/admin.py | 89 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 156 insertions(+), 2 deletions(-) diff --git a/dbtc/cli.py b/dbtc/cli.py index f0bd4f5..6c463d2 100644 --- a/dbtc/cli.py +++ b/dbtc/cli.py @@ -928,6 +928,58 @@ def list_credentials( _dbt_cloud_request(ctx, 'list_credentials', account_id, project_id) +@app.command() +def list_environment_variables( + ctx: typer.Context, + account_id: int = ACCOUNT_ID, + project_id: int = PROJECT_ID, + resource_type: str = typer.Option( + 'environment', + '--resource-type', + help='The name of the resource to retrieve', + ), + environment_id: int = typer.Option( + None, + '--environment-id', + help='Numeric ID of the environment to retrieve', + ), + job_id: int = typer.Option( + None, + '--job-id', + help='Numeric ID of the job to retrieve', + ), + user_id: int = typer.Option(None, '--user-id', '-u', help='Numeric ID of the user'), + name: str = typer.Option( + None, + '--name', + help='The name of the environment', + ), + type: str = typer.Option( + None, + '--type', + help='The type of environment (deployment or development)', + ), + state: str = STATE, + offset: int = OFFSET, + limit: int = LIMIT, +): + _dbt_cloud_request( + ctx, + 'list_environment_variables', + account_id, + project_id, + resource_type=resource_type, + environment_id=environment_id, + job_id=job_id, + user_id=user_id, + name=name, + type=type, + state=state, + offset=offset, + limit=limit, + ) + + @app.command() def list_environments( ctx: typer.Context, @@ -1361,6 +1413,23 @@ def update_environment( ) +@app.command() +def update_environment_variables( + ctx: typer.Context, + account_id: int = ACCOUNT_ID, + project_id: int = PROJECT_ID, + payload: str = PAYLOAD, +): + """Update environment variables.""" + _dbt_cloud_request( + ctx, + 'update_environment_variables', + account_id, + project_id, + json.loads(payload), + ) + + @app.command() def update_job( ctx: typer.Context, diff --git a/dbtc/client/admin.py b/dbtc/client/admin.py index b07d65d..c2f564f 100644 --- a/dbtc/client/admin.py +++ b/dbtc/client/admin.py @@ -886,15 +886,29 @@ def list_audit_logs( ) @v3 - def list_connections(self, account_id: int, project_id: int) -> Dict: + def list_connections( + self, + account_id: int, + project_id: int, + *, + state: int = None, + offset: int = None, + limit: int = None, + ) -> Dict: """List connections for a specific account and project Args: account_id (int): Numeric ID of the account to retrieve project_id (int): Numeric ID of the project to retrieve + state (int, optional): 1 = active, 2 = deleted + offset (int, optional): The offset to apply when listing runs. + Use with limit to paginate results. + limit (int, optional): The limit to apply when listing runs. + Use with offset to paginate results. """ return self._simple_request( - f'accounts/{account_id}/projects/{project_id}/connections' + f'accounts/{account_id}/projects/{project_id}/connections', + params={'state': state, 'limit': limit, 'offset': offset}, ) @v3 @@ -962,6 +976,60 @@ def list_environments( }, ) + @v3 + def list_environment_variables( + self, + account_id: int, + project_id: int, + *, + resource_type: str = 'environment', + environment_id: int = None, + job_id: int = None, + limit: int = None, + offset: int = None, + name: str = None, + state: int = None, + type: str = None, + user_id: int = None, + ): + """List environment variables for a specific account and project + + Args: + account_id (int): Numeric ID of the account to retrieve + project_id (int): Numeric ID of he project to retrieve + resource_type (str, optional): The name of the resource to retrieve. Valid + resources include environment, job, and user + environment_id (int, optional): Numeric ID of the environment to retrieve + job_id (int, optional): Numeric ID of the job to retrieve + name (str, optional): Name of the environment to retrieve + type (str, optional): Type of the environment variable + state (int, optional): 1 = active, 2 = deleted + offset (int, optional): The offset to apply when listing runs. + Use with limit to paginate results. + limit (int, optional): The limit to apply when listing runs. + Use with offset to paginate results. + """ + valid_resource_types = ['environment', 'job', 'user'] + if resource_type not in valid_resource_types: + raise ValueError( + f'{resource_type} is not a valid argument for resource_type. Valid ' + f'resource types include {", ".join(valid_resource_types)}.' + ) + + return self._simple_request( + f'accounts/{account_id}/projects/{project_id}/environment-variables/{resource_type}', # noqa: E501 + params={ + 'environment_id': environment_id, + 'job_definition_id': job_id, + 'name': name, + 'type': type, + 'state': state, + 'offset': offset, + 'limit': limit, + 'user_id': user_id, + }, + ) + @v3 def list_feature_flags(self, account_id: int) -> Dict: """List feature flags for a specific account @@ -1699,6 +1767,23 @@ def update_environment( json=payload, ) + @v3 + def update_environment_variables( + self, account_id: int, project_id: int, payload: Dict + ): + """Update an environment variable + + Args: + account_id (int): Numeric ID of the account + project_id (int): Numeric ID of the project + payload (dict): Dictionary representing the environment to update + """ + return self._simple_request( + f'accounts/{account_id}/projects/{project_id}/environment-variables/bulk', # noqa: E501 + method='post', + json=payload, + ) + @v2 def update_job(self, account_id: int, job_id: int, payload: Dict) -> Dict: """Update a job by its ID. From f168bb4e04b3e300850a1379ce41aaf836a48ef0 Mon Sep 17 00:00:00 2001 From: Doug Guthrie Date: Sun, 30 Jul 2023 06:58:26 -0600 Subject: [PATCH 2/2] Bump version to 0.5.1 --- CHANGELOG.md | 5 +++++ dbtc/_version.py | 2 +- pyproject.toml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51fc926..e6d736b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [0.5.1] - 2023-07-30 + +### Added +- Methods to update and list environment variables + ## [0.5.0] - 2023-07-28 ### Fixed diff --git a/dbtc/_version.py b/dbtc/_version.py index 2b8877c..93b60a1 100644 --- a/dbtc/_version.py +++ b/dbtc/_version.py @@ -1 +1 @@ -__version__ = '0.5.0' +__version__ = '0.5.1' diff --git a/pyproject.toml b/pyproject.toml index f78e953..ea26d12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dbtc" -version = "0.5.0" +version = "0.5.1" description = "An unaffiliated python wrapper for dbt Cloud APIs" authors = ["Doug Guthrie "] documentation = "https://dbtc.dpguthrie.com"