POLARIS_CLIENT_SECRET := "xxx"

generate-rsa-key-pair:
    openssl genrsa -out private.pem 4096
    openssl rsa -in private.pem -pubout -out public.pem

seal-secret:
    cat secrets/hm-polaris-secret.unsealed.yaml | \
    kubeseal \
        --controller-namespace=production-hm-sealed-secrets \
        --controller-name=sealed-secrets \
        --format=yaml \
        > kubernetes-manifests/hm-polaris-secret.yaml

bootstrap-polaris:
    kubectl run polaris-bootstrap \
        --namespace=production-hm-polaris \
        --image=apache/polaris-admin-tool:1.3.0-incubating \
        --restart=Never \
        --rm \
        --stdin \
        --tty \
        --env="quarkus.datasource.username=$(kubectl get secret hm-polaris-secret --namespace=production-hm-polaris --output=jsonpath='{.data.POSTGRES_USER_NAME}' | base64 --decode)" \
        --env="quarkus.datasource.password=$(kubectl get secret hm-polaris-secret --namespace=production-hm-polaris --output=jsonpath='{.data.POSTGRES_PASSWORD}' | base64 --decode)" \
        --env="quarkus.datasource.jdbc.url=$(kubectl get secret hm-polaris-secret --namespace=production-hm-polaris --output=jsonpath='{.data.POSTGRES_JDBC_URL}' | base64 --decode)" \
        -- \
        bootstrap \
            --realm=POLARIS \
            --credential=POLARIS,polaris_admin_oryx,{{ POLARIS_CLIENT_SECRET }}

list-principals:
    #!/usr/bin/env bash
    TOKEN=$(curl --silent --fail --show-error --location "https://polaris.hongbomiao.com/api/catalog/v1/oauth/tokens" \
        --data "grant_type=client_credentials&client_id=polaris_admin_oryx&client_secret={{ POLARIS_CLIENT_SECRET }}&scope=PRINCIPAL_ROLE:service_admin" \
        | jq --raw-output '.access_token')
    curl --silent --fail --show-error --location --header "Authorization: Bearer $TOKEN" \
        "https://polaris.hongbomiao.com/api/management/v1/principals" | jq .

list-catalogs:
    #!/usr/bin/env bash
    TOKEN=$(curl --silent --fail --show-error --location "https://polaris.hongbomiao.com/api/catalog/v1/oauth/tokens" \
        --data "grant_type=client_credentials&client_id=polaris_admin_oryx&client_secret={{ POLARIS_CLIENT_SECRET }}&scope=PRINCIPAL_ROLE:service_admin" \
        | jq --raw-output '.access_token')
    curl --silent --fail --show-error --location --header "Authorization: Bearer $TOKEN" \
        "https://polaris.hongbomiao.com/api/management/v1/catalogs" | jq .

# just create-catalog production production-hm-iceberg
create-catalog catalog_name object_storage_bucket:
    #!/usr/bin/env bash
    TOKEN=$(curl --silent --fail --show-error --location "https://polaris.hongbomiao.com/api/catalog/v1/oauth/tokens" \
        --data "grant_type=client_credentials&client_id=polaris_admin_oryx&client_secret={{ POLARIS_CLIENT_SECRET }}&scope=PRINCIPAL_ROLE:service_admin" \
        | jq --raw-output '.access_token')
    curl --silent --fail --show-error --location \
        --header "Authorization: Bearer $TOKEN" \
        --header "Content-Type: application/json" \
        "https://polaris.hongbomiao.com/api/management/v1/catalogs" \
        --data '{
            "catalog": {
                "name": "{{ catalog_name }}",
                "type": "INTERNAL",
                "properties": {
                "default-base-location": "s3://{{ object_storage_bucket }}/"
                },
                "storageConfigInfo": {
                "storageType": "S3",
                "allowedLocations": ["s3://{{ object_storage_bucket }}/"]
                }
            }
        }' | jq .

# just drop-catalog production
drop-catalog catalog_name:
    #!/usr/bin/env bash
    TOKEN=$(curl --silent --fail --show-error --location "https://polaris.hongbomiao.com/api/catalog/v1/oauth/tokens" \
        --data "grant_type=client_credentials&client_id=polaris_admin_oryx&client_secret={{ POLARIS_CLIENT_SECRET }}&scope=PRINCIPAL_ROLE:service_admin" \
        | jq --raw-output '.access_token')
    curl --silent --fail --show-error --location --request DELETE \
        --header "Authorization: Bearer $TOKEN" \
        "https://polaris.hongbomiao.com/api/management/v1/catalogs/{{ catalog_name }}" | jq .

# just drop-namespace production motor_db
drop-namespace catalog_name namespace_name:
    #!/usr/bin/env bash
    TOKEN=$(curl --silent --fail --show-error --location "https://polaris.hongbomiao.com/api/catalog/v1/oauth/tokens" \
        --data "grant_type=client_credentials&client_id=polaris_admin_oryx&client_secret={{ POLARIS_CLIENT_SECRET }}&scope=PRINCIPAL_ROLE:service_admin" \
        | jq --raw-output '.access_token')
    curl --silent --fail --show-error --location --request DELETE \
        --header "Authorization: Bearer $TOKEN" \
        "https://polaris.hongbomiao.com/api/catalog/v1/{{ catalog_name }}/namespaces/{{ namespace_name }}" | jq .

# just drop-table production motor_db motor_data
drop-table catalog_name namespace_name table_name:
    #!/usr/bin/env bash
    TOKEN=$(curl --silent --fail --show-error --location "https://polaris.hongbomiao.com/api/catalog/v1/oauth/tokens" \
        --data "grant_type=client_credentials&client_id=polaris_admin_oryx&client_secret={{ POLARIS_CLIENT_SECRET }}&scope=PRINCIPAL_ROLE:service_admin" \
        | jq --raw-output '.access_token')
    curl --silent --fail --show-error --location --request DELETE \
        --header "Authorization: Bearer $TOKEN" \
        "https://polaris.hongbomiao.com/api/catalog/v1/{{ catalog_name }}/namespaces/{{ namespace_name }}/tables/{{ table_name }}" | jq .
