Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace with propper validation #4

Open
github-actions bot opened this issue Dec 3, 2023 · 0 comments
Open

replace with propper validation #4

github-actions bot opened this issue Dec 3, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Dec 3, 2023

// TODO replace with propper validation

     * @param  array    $args
     * @return Response Json result set.
     */
    public function buckets_r1(Request $request, Response $response, array $args = []): Response {
        $params = $request->getQueryParams();
        $q = "
        SELECT 
          bin_to_uuid(b.`uuid`, 1) AS `b.uuid`,
          b.name AS `b.name`,
          b.data AS `b.data`,
          bin_to_uuid(bd.`dg`, 1) AS `dg`,
          s.dgStatus
        FROM `t_stor_buckets` b
        LEFT JOIN t_stor_bucket_dgs bd ON bd.bucket = b.uuid
        LEFT JOIN (
          SELECT DISTINCT dgUuid, dgStatus
          FROM v_stor_status
        ) s ON s.dgUuid = bin_to_uuid(bd.dg, 1)
        ";

        $res = $this->mysqli->execute_query($q, []);
        $data = $res->fetch_all(MYSQLI_ASSOC);

        $data = [
                'timestamp' => microtime(),
                'status' => 'Buckets r1 OK',
                'data' => $data,
                'service' => basename(__ROOT__),
            ];
        return $response->withJson($data);
    }

    public function buckets_c1(Request $request, Response $response, array $args = []): Response {
        $params = $request->getQueryParams();
        $this->status();
        // TODO replace with propper validation
        $contentTypeHeader = $request->getHeaderLine('Content-Type') ?? '';
        if ($contentTypeHeader !== 'application/json') { throw new \Exception('Invalid Content-Type. Please set `Content-Type: application/json', 400); }
        $body = $request->getParsedBody();
        $uuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
        if (!isset($body['name'])) { throw new \Exception('Mandatory $.name not provided.'); }
        if (!isset($body['dgs'])) { throw new \Exception('Mandatory $.dgs not provided.'); }
        if (!is_array($body['dgs'])) { throw new \Exception('Mandatory $.dgs is not a list (array).'); }

        $q1 = "INSERT INTO `t_stor_buckets` (`uuid`, `data`) VALUES (uuid_to_bin(?,true), ?)";
        $q2 = "INSERT INTO `t_stor_bucket_dgs` (`bucket`,`dg`) VALUES (uuid_to_bin(?,true), uuid_to_bin(?,true))";

        try {
            $this->mysqli->begin_transaction();
            $this->mysqli->execute_query($q1, [ $uuid, json_encode($body) ]);
            foreach ($body['dgs'] as $dg) {
                $this->mysqli->execute_query($q2, [$uuid, $dg]);
            }
            $this->mysqli->commit();
        } catch (\mysqli_sql_exception $e) {
            if ($e->getCode() === 1452) { throw new \Exception("Unknown devicegroup UUID.", 400); }
            elseif ($e->getCode() === 1062) { throw new \Exception("The bucket:devicegroup association already exists.", 400);
            } else { throw $e; }
        }

        $data = [
            'timestamp' => microtime(),
            'status' => "Bucket {$uuid} ({$body['name']}) created.",
            'params' => $params,
            'service' => basename(__ROOT__),
        ];
@github-actions github-actions bot added the todo label Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants