Skip to content

Commit

Permalink
Classg remove api link (#1983)
Browse files Browse the repository at this point in the history
* class_generator: remove api-link option

* class_generator: remove api-link option

* Fix tests

* Remove --api-link from readme
  • Loading branch information
myakove authored Jul 30, 2024
1 parent 7cf6927 commit 4c388b4
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 46 deletions.
10 changes: 5 additions & 5 deletions scripts/resource/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ poetry install

###### Call the script

- Running in normal mode with `--kind` and `--api-link` flags:
- Running in normal mode with `--kind` flags:

```bash
poetry run python scripts/resource/class_generator.py --kind <kind> --api-link <link to resource API or DOC>
poetry run python scripts/resource/class_generator.py --kind <kind>

```

Expand All @@ -33,10 +33,9 @@ poetry run python scripts/resource/class_generator.py --interactive

- Generate the resource file the debug file by adding `--debug` flag
- Replace `Pod` with the kind you want to test
- `--api-link` must be `https://debug.explain`

```bash
poetry run python scripts/resource/class_generator.py --api-link https://debug.explain --kind Pod --output-file /tmp/pod_res.py --debug
poetry run python scripts/resource/class_generator.py --kind Pod --output-file /tmp/pod_res.py --debug
```

- Move the resource file under [tests manifests dir](scripts/resource/tests/manifests/)
Expand All @@ -46,6 +45,7 @@ mv /tmp/pod_res.py scripts/resource/tests/manifests/
```

- Move the debug file under [tests manifests dir](scripts/resource/tests/manifests/)

```bash
mv scripts/resource/debug/Pod_debug.json scripts/resource/tests/manifests/
```
Expand All @@ -55,7 +55,7 @@ mv scripts/resource/debug/Pod_debug.json scripts/resource/tests/manifests/
- Running with debug mode and `--debug` flag:

```bash
poetry run python scripts/resource/class_generator.py --kind <kind> --api-link <link to resource API or DOC> --debug
poetry run python scripts/resource/class_generator.py --kind <kind> --debug
```

`<kind>-debug.json` will be located under `scripts/resource/debug`
Expand Down
36 changes: 5 additions & 31 deletions scripts/resource/class_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ def generate_resource_file_from_dict(


def parse_explain(
api_link: str,
output: str,
namespaced: Optional[bool] = None,
debug: bool = False,
Expand All @@ -377,7 +376,6 @@ def parse_explain(
sections: List[str] = []
resource_dict: Dict[str, Any] = {
"BASE_CLASS": "NamespacedResource" if namespaced else "Resource",
"API_LINK": api_link,
}
new_sections_words: Tuple[str, str, str] = ("KIND:", "VERSION:", "GROUP:")

Expand Down Expand Up @@ -537,35 +535,23 @@ def parse_explain(
return resource_dict


def validate_api_link_schema(value: str) -> str:
if not value.startswith("https://"):
raise click.BadParameter("Resource API linkn must start with https://")

return value


def check_kind_exists(kind: str) -> bool:
return run_command(
command=shlex.split(f"oc explain {kind}"),
check=False,
)[0]


def get_user_args_from_interactive() -> Tuple[str, str]:
def get_user_args_from_interactive() -> str:
kind = Prompt.ask(prompt="Enter the resource kind to generate class for")
if not kind:
return "", ""

api_link = Prompt.ask(prompt="Enter the resource API link")
if not api_link:
return "", ""
return ""

return kind, api_link
return kind


def class_generator(
kind: str = "",
api_link: str = "",
overwrite: bool = False,
interactive: bool = False,
dry_run: bool = False,
Expand All @@ -587,7 +573,6 @@ def class_generator(
"data": debug_content["explain"],
"namespaced": debug_content["namespace"].strip() == "1",
}
api_link = "https://debug.explain"

else:
if not check_cluster_available():
Expand All @@ -597,14 +582,12 @@ def class_generator(
return ""

if interactive:
kind, api_link = get_user_args_from_interactive()
kind = get_user_args_from_interactive()

if not kind or not api_link:
if not kind:
LOGGER.error("Kind or API link not provided")
return ""

validate_api_link_schema(value=api_link)

if not check_kind_exists(kind=kind):
return ""

Expand All @@ -620,7 +603,6 @@ def class_generator(
resource_dict = parse_explain(
output=kind_data["data"],
namespaced=kind_data["namespaced"],
api_link=api_link,
debug=debug,
output_debug_file_path=output_debug_file_path,
debug_content=debug_content,
Expand Down Expand Up @@ -657,19 +639,13 @@ def class_generator(
type=click.STRING,
help="The Kind to generate the class for, Needs working cluster with admin privileges",
)
@click.option(
"-l",
"--api-link",
help="A link to the resource doc/api in the web",
)
@click.option(
"-o",
"--output-file",
help="The full filename path to generate a python resourece file. If not sent, resourece kind will be used",
type=click.Path(),
)
@click.option(
"-o",
"--overwrite",
is_flag=True,
help="Output file overwrite existing file if passed",
Expand All @@ -689,7 +665,6 @@ def class_generator(
)
def main(
kind: str,
api_link: str,
overwrite: bool,
interactive: bool,
dry_run: bool,
Expand All @@ -701,7 +676,6 @@ def main(
_ = pdb
return class_generator(
kind=kind,
api_link=api_link,
overwrite=overwrite,
interactive=interactive,
dry_run=dry_run,
Expand Down
2 changes: 0 additions & 2 deletions scripts/resource/manifests/class_generator_template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ from ocp_resources.resource import {{ BASE_CLASS }}, MissingRequiredArgumentErro
class {{ KIND }}({{ BASE_CLASS }}):
"""
{{ DESCRIPTION }}

API Link: {{ API_LINK }}
"""

{% if GROUP %}
Expand Down
2 changes: 0 additions & 2 deletions scripts/resource/tests/manifests/api_server_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class APIServer(Resource):
'cluster'.
Compatibility level 1: Stable within a major release for a minimum of 12
months or 3 minor releases (whichever is longer).
API Link: https://debug.explain
"""

api_group: str = Resource.ApiGroup.CONFIG_OPENSHIFT_IO
Expand Down
2 changes: 0 additions & 2 deletions scripts/resource/tests/manifests/config_map_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
class ConfigMap(NamespacedResource):
"""
ConfigMap holds configuration data for pods to consume.
API Link: https://debug.explain
"""

api_version: str = NamespacedResource.ApiVersion.V1
Expand Down
2 changes: 0 additions & 2 deletions scripts/resource/tests/manifests/deployment_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
class Deployment(NamespacedResource):
"""
Deployment enables declarative updates for Pods and ReplicaSets.
API Link: https://debug.explain
"""

api_group: str = NamespacedResource.ApiGroup.APPS
Expand Down
2 changes: 0 additions & 2 deletions scripts/resource/tests/manifests/pod_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class Pod(NamespacedResource):
"""
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
API Link: https://debug.explain
"""

api_version: str = NamespacedResource.ApiVersion.V1
Expand Down

0 comments on commit 4c388b4

Please sign in to comment.