apiVersion: v1
kind: Service
metadata:
  name: account-metadb
spec:
  selector:
    app.kubernetes.io/name: account-metadb
  ports:
    - name: mysql
      port: 3306
      targetPort: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: account-metadb
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: account-metadb
  template:
    metadata:
      labels:
        app.kubernetes.io/name: account-metadb
    spec:
      containers:
        - name: mysql
          image: mysql:8.0
          ports:
            - name: mysql
              containerPort: 3306
          env:
            - name: MYSQL_DATABASE
              value: account
            - name: MYSQL_ROOT_PASSWORD
              value: cp9-local
          volumeMounts:
            - name: account-iam-openapi-seed
              mountPath: /docker-entrypoint-initdb.d/20-account-iam-openapi-seed.sql
              subPath: 20-account-iam-openapi-seed.sql
          readinessProbe:
            exec:
              command: ["mysqladmin", "ping", "-h", "127.0.0.1", "-uroot", "-pcp9-local"]
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
        - name: account-iam-openapi-seed
          configMap:
            name: account-iam-openapi-seed
---
apiVersion: v1
kind: Service
metadata:
  name: account-redis
spec:
  selector:
    app.kubernetes.io/name: account-redis
  ports:
    - name: redis
      port: 6379
      targetPort: redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: account-redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: account-redis
  template:
    metadata:
      labels:
        app.kubernetes.io/name: account-redis
    spec:
      containers:
        - name: redis
          image: redis:7-alpine
          ports:
            - name: redis
              containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: account-dynamodb
spec:
  selector:
    app.kubernetes.io/name: account-dynamodb
  ports:
    - name: dynamodb
      port: 8000
      targetPort: dynamodb
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: account-dynamodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: account-dynamodb
  template:
    metadata:
      labels:
        app.kubernetes.io/name: account-dynamodb
    spec:
      containers:
        - name: dynamodb
          image: amazon/dynamodb-local:2.5.2
          args: ["-jar", "DynamoDBLocal.jar", "-inMemory", "-sharedDb"]
          ports:
            - name: dynamodb
              containerPort: 8000
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: account-iam-openapi-seed
data:
  20-account-iam-openapi-seed.sql: |
    -- Account's gormutil bootstrap probes the fixed "test" database before opening the service DB.
    CREATE DATABASE IF NOT EXISTS test;

    CREATE TABLE IF NOT EXISTS uni_api_keys (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      access_key VARCHAR(256) NOT NULL,
      secret_key VARCHAR(256) NOT NULL,
      source VARCHAR(50) NOT NULL,
      ext TEXT DEFAULT NULL,
      description VARCHAR(512) NOT NULL,
      created_by VARCHAR(256) NOT NULL,
      updated_by VARCHAR(256) NOT NULL,
      rate_limit_rpm INT UNSIGNED DEFAULT 0,
      status VARCHAR(10) NOT NULL,
      expired_at DATETIME NULL,
      biz_attr VARCHAR(512) NOT NULL,
      type VARCHAR(64) NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY uk_ak (access_key)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    CREATE TABLE IF NOT EXISTS role_bindings (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      target_type CHAR(16) NOT NULL,
      target_id BIGINT UNSIGNED NOT NULL,
      scope_type CHAR(16) NOT NULL,
      scope_id BIGINT UNSIGNED NOT NULL,
      role CHAR(32) NOT NULL,
      status CHAR(16) NOT NULL,
      role_id BIGINT UNSIGNED NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY role_value (target_type, target_id, scope_type, scope_id, role)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    CREATE TABLE IF NOT EXISTS rbac_roles (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      name VARCHAR(100) NOT NULL,
      display_name VARCHAR(100) NOT NULL,
      scope_type VARCHAR(50) NOT NULL,
      description VARCHAR(200) NOT NULL,
      status CHAR(16) NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY uk_rbac_roles_name_scope (name, scope_type)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    CREATE TABLE IF NOT EXISTS rbac_privileges (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      resource_uri VARCHAR(200) NOT NULL,
      url VARCHAR(200) NOT NULL,
      `system` VARCHAR(50) NOT NULL,
      description VARCHAR(200) NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY uk_rbac_privileges_uri (resource_uri)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    CREATE TABLE IF NOT EXISTS rbac_role_privileges (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      rbac_role_id BIGINT UNSIGNED NOT NULL,
      rbac_privilege_id BIGINT UNSIGNED NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY uk_rbac_role_privilege (rbac_role_id, rbac_privilege_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    INSERT INTO uni_api_keys
      (id, access_key, secret_key, source, description, created_by, updated_by, rate_limit_rpm, status, biz_attr, type)
    VALUES
      (9001, 'CP9IAMAK', '895e6c10bad41ad3adc353baa50138cd', 'cp9-local', 'cp9 local IAM OpenAPI proof key', 'cp9', 'cp9', 500, 'active', '', 'normal')
    ON DUPLICATE KEY UPDATE
      secret_key=VALUES(secret_key),
      description=VALUES(description),
      rate_limit_rpm=VALUES(rate_limit_rpm),
      status=VALUES(status),
      type=VALUES(type);

    INSERT INTO rbac_roles
      (id, name, display_name, scope_type, description, status)
    VALUES
      (7001, 'org:owner', 'Org Owner', 'ORG', 'cp9 local org owner role', 'active')
    ON DUPLICATE KEY UPDATE status=VALUES(status);

    INSERT INTO rbac_privileges
      (id, resource_uri, url, `system`, description)
    VALUES
      (7101, 'account/openapi/orgs/{org_id}/list_apikey', '/v1beta1/apikeys', 'account-openapi', 'List IAM API keys'),
      (7102, 'account/openapi/orgs/{org_id}/get_apikey', '/v1beta1/apikeys/{accessKey}', 'account-openapi', 'Get IAM API key'),
      (7103, 'account/openapi/orgs/{org_id}/create_apikey', '/v1beta1/apikeys', 'account-openapi', 'Create IAM API key')
    ON DUPLICATE KEY UPDATE url=VALUES(url), `system`=VALUES(`system`), description=VALUES(description);

    INSERT INTO rbac_role_privileges
      (rbac_role_id, rbac_privilege_id)
    VALUES
      (7001, 7101),
      (7001, 7102),
      (7001, 7103)
    ON DUPLICATE KEY UPDATE rbac_role_id=VALUES(rbac_role_id);

    INSERT INTO role_bindings
      (id, target_type, target_id, scope_type, scope_id, role, status, role_id)
    VALUES
      (7201, 'APIKEY', 9001, 'ORG', 1440002, 'org:owner', 'active', 7001)
    ON DUPLICATE KEY UPDATE status=VALUES(status), role_id=VALUES(role_id);
