Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cognite/pygen/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class PygenSettings(BaseModel):
client_id: Argument = Argument(default=None, help="Client ID for connecting to CDF")
client_secret: Argument = Argument(default=None, help="Client secret for connecting to CDF")
cdf_cluster: Argument = Argument(default=None, help="CDF Cluster to connect to")
cdf_url: Argument = Argument(
default=None, help="CDF base URL override, e.g. for private link (https://p001.plink.<cluster>.cognitedata.com)"
)
cdf_project: Argument = Argument(default=None, help="CDF Project to connect to")
output_dir: Argument = Argument(default=None, help="Output directory for generated SDK")
top_level_package: Argument = Argument(default="my_domain.client", help="Package name for the generated client.")
Expand Down
26 changes: 22 additions & 4 deletions cognite/pygen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def generate(
cdf_cluster: str = typer.Option(
default=loaded_settings.cdf_cluster.default, help=loaded_settings.cdf_cluster.help
),
cdf_url: Optional[str] = typer.Option(
default=loaded_settings.cdf_url.default, help=loaded_settings.cdf_url.help
),
cdf_project: str = typer.Option(
default=loaded_settings.cdf_project.default, help=loaded_settings.cdf_project.help
),
Expand Down Expand Up @@ -98,9 +101,16 @@ def generate(
)
client = CogniteClient(clientConfig)
else:
client = CogniteClient.default_oauth_client_credentials(
cdf_project, cdf_cluster, tenant_id, client_id, client_secret
credentials = OAuthClientCredentials.default_for_azure_ad(
tenant_id, client_id, client_secret, cdf_cluster
)
clientConfig = ClientConfig(
client_name="pygen",
project=cdf_project,
credentials=credentials,
base_url=cdf_url or f"https://{cdf_cluster}.cognitedata.com",
)
client = CogniteClient(clientConfig)
data_models: list[tuple[str, str, str]]
if settings and settings.data_models:
data_models = settings.data_models
Expand Down Expand Up @@ -140,6 +150,7 @@ def generate( # type: ignore[misc]
client_secret: Annotated[str, typer.Option(..., help="Client Secret for connecting to CDF")],
cdf_cluster: Annotated[str, typer.Option(..., help=default_settings.cdf_cluster.help)],
cdf_project: Annotated[str, typer.Option(..., help=default_settings.cdf_project.help)],
cdf_url: Annotated[Optional[str], typer.Option(..., help=default_settings.cdf_url.help)] = None,
tenant_id: Annotated[Optional[str], typer.Option(..., help=default_settings.tenant_id.help)] = None,
token_url: Annotated[Optional[str], typer.Option(..., help=default_settings.token_url.help)] = None,
scopes: Annotated[Optional[str], typer.Option(..., help=default_settings.scopes.help)] = None,
Expand Down Expand Up @@ -175,9 +186,16 @@ def generate( # type: ignore[misc]
)
client = CogniteClient(clientConfig)
elif tenant_id:
client = CogniteClient.default_oauth_client_credentials(
cdf_project, cdf_cluster, tenant_id, client_id, client_secret
credentials = OAuthClientCredentials.default_for_azure_ad(
tenant_id, client_id, client_secret, cdf_cluster
)
clientConfig = ClientConfig(
client_name="pygen",
project=cdf_project,
credentials=credentials,
base_url=cdf_url or f"https://{cdf_cluster}.cognitedata.com",
)
client = CogniteClient(clientConfig)
Comment on lines 188 to +198
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This client creation logic is duplicated from lines 103-113. This violates the style guide's principles of maintainability and consistency. To improve this, consider extracting the logic into a helper function.

This duplication appears to have led to an issue: the token_url authentication flow (lines 173-187) doesn't use the new cdf_url parameter for the base_url. This will likely prevent private link from working with that authentication method.

A single helper function for client creation would resolve the duplication and provide a single place to fix the base_url handling for all authentication flows.

References
  1. The style guide emphasizes maintainability and consistency (lines 10-11). Code duplication, as seen here, makes the code harder to modify and extend, and can lead to inconsistencies and bugs. (link)

else:
print("Either tenant-id or token-url is required parameters")
raise typer.Exit(code=1)
Expand Down
10 changes: 8 additions & 2 deletions cognite/pygen/utils/cdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def load_cognite_client_from_toml(
cdf_cluster = "<cdf-cluster>"
client_id = "<client-id>"
client_secret = "<client-secret>"
# Optional: override base URL, e.g. for private link
base_url = "https://<private-link-prefix>.plink.<cluster>.cognitedata.com"
```

Args:
Expand All @@ -73,10 +75,14 @@ def load_cognite_client_from_toml(
toml_content = toml_content[section]

login_flow = toml_content.pop("login_flow", None)
base_url = toml_content.pop("base_url", None)
if login_flow == "interactive":
return CogniteClient.default_oauth_interactive(**toml_content)
client = CogniteClient.default_oauth_interactive(**toml_content)
else:
return CogniteClient.default_oauth_client_credentials(**toml_content)
client = CogniteClient.default_oauth_client_credentials(**toml_content)
if base_url:
client.config.base_url = base_url
return client


class _CogniteCoreResourceAPI(Protocol[T_CogniteResourceList]):
Expand Down
Loading