Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Configurations/LogpushJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Created by PhpStorm.
* User: Jens Beltofte
* Date: 15/04/2021
* Time: 12:58
*/

namespace Cloudflare\API\Configurations;

class LogpushJob implements Configurations
{
private $configs = [];

public function setDestinationConf(string $destinationConf)
{
if ($destinationConf) {
$this->configs['destination_conf'] = $destinationConf;
}
}

public function setOwnershipChallenge(string $ownershipChallenge)
{
if ($ownershipChallenge) {
$this->configs['ownership_challenge'] = $ownershipChallenge;
}
}

public function setLogpullOptions(string $logpullOptions)
{
if ($logpullOptions) {
$this->configs['logpull_options'] = $logpullOptions;
}
}

public function setName(string $name)
{
if ($name) {
$this->configs['name'] = $name;
}
}

public function setEnabled()
{
$this->configs['enabled'] = true;
}

public function setDisabled()
{
$this->configs['enabled'] = false;
}

public function setDataset(string $dataset)
{
if ($dataset) {
$this->configs['dataset'] = $dataset;
}
}

public function setFrequency(string $frequency)
{
if ($frequency) {
$this->configs['frequency'] = $frequency;
}
}

public function getArray(): array
{
return $this->configs;
}
}
209 changes: 209 additions & 0 deletions src/Endpoints/LogpushJobs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?php
/**
* Created by PhpStorm.
* User: Jens Beltofte
* Date: 15/04/2021
* Time: 11:35
*/

namespace Cloudflare\API\Endpoints;

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Configurations\LogpushJob as Configs;

class LogpushJobs implements API
{
private $adapter;

public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}

public function listLogpushJobs(
string $zoneID,
string $dataset = null
): \stdClass {
$endpoint = 'zones/' . $zoneID . '/logpush/jobs';

if ($dataset) {
$endpoint = 'zones/' . $zoneID . '/logpush/datasets/' . $dataset .'/jobs';
}

$jobs = $this->adapter->get($endpoint);

$body = json_decode($jobs->getBody());

return (object)['result' => $body->result];
}

public function listFields(
string $zoneID,
string $dataset
): \stdClass {
$endpoint = 'zones/' . $zoneID . '/logpush/datasets/' . $dataset . '/fields';

$fields = $this->adapter->get($endpoint);

$body = json_decode($fields->getBody());

return (object)['result' => $body->result];
}

public function getOwnershipChallenge(
string $zoneID,
string $destinationConf
): \stdClass {
$options = new Configs();
$options->setDestinationConf($destinationConf);

$endpoint = 'zones/' . $zoneID . '/logpush/ownership';

$ownership = $this->adapter->post($endpoint, $options->getArray());

$body = json_decode($ownership->getBody());

return (object)['result' => $body->result];
}

public function validateOwnershipChallenge(
string $zoneID,
string $destinationConf,
string $ownershipChallenge
): bool {
$options = new Configs();
$options->setDestinationConf($destinationConf);
$options->setOwnershipChallenge($ownershipChallenge);

$endpoint = 'zones/' . $zoneID . '/logpush/ownership/validate';

$validate = $this->adapter->post($endpoint, $options->getArray());

$body = json_decode($validate->getBody());

if (isset($body->result->valid)) {
return $body->result->valid;
}

return false;
}

public function validateOrigin(
string $zoneID,
string $logpullOptions
): bool {
$options = new Configs();
$options->setLogpullOptions($logpullOptions);

$endpoint = 'zones/' . $zoneID . '/logpush/validate/origin';

$validate = $this->adapter->post($endpoint, $options->getArray());

$body = json_decode($validate->getBody());

if (isset($body->result->valid)) {
return $body->result->valid;
}

return false;
}

public function createLogpushJob(
string $zoneID,
string $destinationConf,
string $ownershipChallenge,
string $name = '',
bool $status = null,
string $dataset = '',
string $logpullOptions = '',
string $frequency = ''
): \stdClass {
$options = new Configs();
$options->setDestinationConf($destinationConf);
$options->setOwnershipChallenge($ownershipChallenge);
$options->setName($name);
$status == true ? $options->setEnabled() : $options->setDisabled();
$options->setDataset($dataset);
$options->setLogpullOptions($logpullOptions);
$options->setFrequency($frequency);

$endpoint = 'zones/' . $zoneID . '/logpush/jobs';

$job = $this->adapter->post($endpoint, $options->getArray());

$body = json_decode($job->getBody());

return (object)['result' => $body->result];
}

public function getLogpushJob(
string $zoneID,
int $jobId
): \stdClass {
$endpoint = 'zones/' . $zoneID . '/logpush/jobs/' . $jobId;

$job = $this->adapter->get($endpoint);

$body = json_decode($job->getBody());

return (object)['result' => $body->result];
}

public function updateLogpushJob(
string $zoneID,
int $jobId,
string $destinationConf = '',
string $ownershipChallenge = '',
bool $status = null,
string $logpullOptions = '',
string $frequency = ''
): \stdClass {
$options = new Configs();
$options->setDestinationConf($destinationConf);
$options->setOwnershipChallenge($ownershipChallenge);
$status == true ? $options->setEnabled() : $options->setDisabled();
$options->setLogpullOptions($logpullOptions);
$options->setFrequency($frequency);

$endpoint = 'zones/' . $zoneID . '/logpush/jobs/' . $jobId;

$job = $this->adapter->put($endpoint, $options->getArray());

$body = json_decode($job->getBody());

return (object)['result' => $body->result];
}

public function deleteLogpushJob(
string $zoneID,
int $jobId
): bool {
$endpoint = 'zones/' . $zoneID . '/logpush/jobs/' . $jobId;

$job = $this->adapter->delete($endpoint);

$body = json_decode($job->getBody());

return $body->success;
}

public function checkDestinationExists(
string $zoneID,
string $destinationConf
): bool {
$options = new Configs();
$options->setDestinationConf($destinationConf);

$endpoint = 'zones/' . $zoneID . '/logpush/validate/destination/exists';

$check = $this->adapter->post($endpoint, $options->getArray());

$body = json_decode($check->getBody());

if (isset($body->result->exists)) {
return $body->result->exists;
}

return false;
}
}
28 changes: 28 additions & 0 deletions tests/Configurations/LogpushJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use PHPUnit\Framework\TestCase;
use Cloudflare\API\Configurations\LogpushJob;

class LogpushJobTest extends TestCase
{
public function testGetArray()
{
$logpushJob = new LogpushJob();
$logpushJob->setName('example.com');
$logpushJob->setDestinationConf('s3://mybucket/logs?region=us-west-2');
$logpushJob->setOwnershipChallenge('00000000000000000000');
$logpushJob->setDisabled();
$logpushJob->setDataset('http_requests');
$logpushJob->setLogpullOptions('fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339');
$logpushJob->setFrequency('high');

$array = $logpushJob->getArray();
$this->assertEquals('example.com', $array['name']);
$this->assertEquals('s3://mybucket/logs?region=us-west-2', $array['destination_conf']);
$this->assertEquals('00000000000000000000', $array['ownership_challenge']);
$this->assertFalse($array['enabled']);
$this->assertEquals('http_requests', $array['dataset']);
$this->assertEquals('fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339', $array['logpull_options']);
$this->assertEquals('high', $array['frequency']);
}
}
Loading