# Common Tasks

This page maps common Omni workflows (as documented for `omnictl` in Sidero docs) to `omnipy` and the Python SDK.

## 1) Inspect resources and types

CLI:

```bash
omnipy get Clusters.omni.sidero.dev
omnipy get Clusters.omni.sidero.dev my-cluster -o yaml
omnipy crd list --query cluster
omnipy crd explain clusterstatus
```

SDK:

```python
from omni import OmniClient

client = OmniClient()
clusters = client.resources.list({"namespace": "default", "type": "Clusters.omni.sidero.dev"})
cluster = client.resources.get(
    {"namespace": "default", "type": "Clusters.omni.sidero.dev", "id": "my-cluster"}
)
print(clusters)
print(cluster)
client.close()
```

## 2) Apply or remove resource manifests

CLI:

```bash
omnipy apply -f resources.yaml --verbose
omnipy delete Clusters.omni.sidero.dev my-cluster --teardown
```

SDK:

```python
import json
from omni import OmniClient

client = OmniClient()
resource = {
    "metadata": {
        "namespace": "default",
        "type": "ConfigPatches.omni.sidero.dev",
        "id": "cluster-a-patch",
    },
    "spec": json.dumps({"data": "example"}),
}
client.resources.create({"resource": resource})
client.close()
```

## 3) Check and manage cluster state

CLI:

```bash
omnipy cluster status my-cluster
omnipy cluster lock my-cluster
omnipy cluster unlock my-cluster
omnipy cluster kubernetes upgrade-pre-checks my-cluster --to 1.35.0
omnipy cluster kubernetes manifest-sync my-cluster --no-dry-run
```

SDK:

```python
from omni import OmniClient

client = OmniClient()
status = client.get_cluster_status("my-cluster")
prechecks = client.management.kubernetes_upgrade_pre_checks(
    {"new_version": "1.35.0"},
    metadata={"cluster": "my-cluster"},
)
print(status, prechecks)
client.close()
```

## 4) Access Kubernetes and Talos

CLI:

```bash
omnipy kubeconfig --cluster my-cluster > my-cluster.kubeconfig
kubectl --kubeconfig ./my-cluster.kubeconfig get nodes

omnipy talosconfig --cluster my-cluster > my-cluster.talosconfig
omnipy talos --cluster my-cluster version --client
omnipy talos --cluster my-cluster --nodes 10.0.0.11 get members
```

SDK:

```python
import base64
from pathlib import Path
from omni import OmniClient

client = OmniClient()
kcfg = client.management.kubeconfig(
    {"service_account": False, "break_glass": False},
    metadata={"cluster": "my-cluster"},
)
Path("my-cluster.kubeconfig").write_bytes(base64.b64decode(kcfg["kubeconfig"]))

talos_path = client.ensure_talosconfig(cluster="my-cluster")
print(talos_path)
client.close()
```

## 5) Join tokens and service accounts

CLI:

```bash
omnipy serviceaccount list
omnipy serviceaccount create ci-bot
omnipy serviceaccount renew ci-bot

omnipy jointoken create bootstrap --ttl 3600
omnipy jointoken list
omnipy jointoken renew bootstrap --ttl 3600
omnipy jointoken revoke bootstrap
omnipy jointoken unrevoke bootstrap
```

SDK:

```python
from omni import OmniClient

client = OmniClient()
created_sa = client.management.create_service_account({"name": "ci-bot"})
token = client.management.create_join_token({"name": "bootstrap", "ttl": "3600s"})
print(created_sa, token)
client.close()
```

## 6) Users and infra providers

CLI:

```bash
omnipy user list
omnipy user create dev@example.com --role Operator
omnipy user set-role dev@example.com --role Admin
omnipy infraprovider list
```

SDK:

```python
from omni import OmniClient

client = OmniClient()
users = client.resources.list({"namespace": "default", "type": "Users.omni.sidero.dev"})
providers = client.resources.list({"namespace": "default", "type": "InfraProviderCombinedStatuses.omni.sidero.dev"})
print(users, providers)
client.close()
```

## 7) Troubleshooting: support and audit logs

CLI:

```bash
omnipy support --cluster my-cluster --output support.zip
omnipy machine-logs <machine-id> --tail-lines 200
omnipy audit-log 2026-01-01 2026-01-07 > audit.jsonl
```

SDK:

```python
import asyncio
import base64
from omni import AsyncOmniClient

async def main() -> None:
    client = AsyncOmniClient()
    async for event in client.management.read_audit_log(
        {"start_time": "2026-01-01", "end_time": "2026-01-07"}
    ):
        chunk = event.get("audit_log")
        if isinstance(chunk, str):
            print(base64.b64decode(chunk).decode("utf-8"), end="")
    await client.aclose()

asyncio.run(main())
```
