Merge pull request #2 from Edo-1234/rule-lists-implementation

Implementazione api relative alle rule lists
This commit is contained in:
SVDIGITAL
2024-12-31 10:29:22 +01:00
committed by GitHub
9 changed files with 433 additions and 0 deletions

103
src/Endpoints/RulesLists.php Executable file
View File

@@ -0,0 +1,103 @@
<?php
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
class RulesLists implements API
{
use BodyAccessorTrait;
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function getLists(string $accountId)
{
$response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists');
$this->body = json_decode($response->getBody());
return (object)['result' => $this->body->result];
}
public function getListDetails(string $accountId, string $listId)
{
$response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/' . $listId);
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function getListItems(string $accountId, string $listId, string $search = '', int $itemsPerPage = 20, string $cursor = '')
{
$options = [
'per_page' => $itemsPerPage,
];
if ($search) {
$options['search'] = $search;
}
if ($cursor) {
$options['cursor'] = $cursor;
}
$response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options);
$this->body = json_decode($response->getBody());
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
}
public function createList(string $accountId, string $kind, string $name, string $description = '')
{
$options = [
'kind' => $kind,
'name' => $name,
];
if ($description) {
$options['description'] = $description;
}
$response = $this->adapter->post('/accounts/' . $accountId . '/rules/lists', $options);
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function createListItem(string $accountId, string $listId, array $ip)
{
$options = [];
foreach ($ip as $ipAddress) {
$options['ip'] = $ipAddress;
}
$response = $this->adapter->post('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options);
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function deleteListItem(string $accountId, string $listId, string $item = '')
{
$response = $this->adapter->delete('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items' . ($item ? '/' . $item : ''));
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function getOperationStatus(string $accountId, string $operationId)
{
$response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/bulk_operations/' . $operationId);
$this->body = json_decode($response->getBody());
return $this->body->result;
}
}

View File

@@ -0,0 +1,172 @@
<?php
namespace Endpoints;
use TestCase;
class RulesListsTest extends TestCase
{
public function testCreateRulesList()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createRulesList.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);
$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists'),
$this->equalTo([
'kind' => 'ip',
'name' => 'ip-allowlist',
])
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->createList('01a7362d577a6c3019a474fd6f485823', 'ip', 'ip-allowlist');
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id);
$this->assertEquals('ip', $result->kind);
$this->assertEquals('ip-allowlist', $result->name);
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id);
}
public function testGetRulesLists()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listRulesLists.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists')
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->getLists('01a7362d577a6c3019a474fd6f485823');
$this->assertObjectHasAttribute('result', $result);
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->result[0]->id);
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result[0]->id);
}
public function testGetRulesListDetails()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getRulesListDetails.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e')
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->getListDetails('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e');
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id);
$this->assertEquals('ip', $result->kind);
$this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id);
}
public function testGetRulesListItems()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getRulesListItems.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items'),
$this->equalTo([
'per_page' => 20,
])
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->getListItems('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e');
$this->assertObjectHasAttribute('result', $result);
$this->assertObjectHasAttribute('result_info', $result);
$this->assertEquals('10.0.0.1', $result->result[0]);
$this->assertEquals('10.0.0.1', $rulesLists->getBody()->result[0]);
}
public function testCreateRulesListItem() {
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createRulesListItem.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);
$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items')
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->createListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e', [
'10.0.0.1'
]);
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id);
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id);
}
public function testDeleteRulesListItem() {
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRulesListItem.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('delete')->willReturn($response);
$mock->expects($this->once())
->method('delete')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items')
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->deleteListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e');
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id);
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id);
}
public function testGetOperationStatus() {
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getOperationStatus.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/bulk_operations/4da8780eeb215e6cb7f48dd981c4ea02')
);
$rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock);
$result = $rulesLists->getOperationStatus('01a7362d577a6c3019a474fd6f485823', '4da8780eeb215e6cb7f48dd981c4ea02');
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->id);
$this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->id);
}
}

View File

@@ -0,0 +1,25 @@
{
"success": true,
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e",
"created_on": "2020-01-01T08:00:00Z",
"description": "",
"kind": "ip",
"modified_on": "2020-01-10T14:00:00Z",
"name": "ip-allowlist",
"num_items": 10,
"num_referencing_filters": 2
}
}

View File

@@ -0,0 +1,18 @@
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"operation_id": "4da8780eeb215e6cb7f48dd981c4ea02"
},
"success": true
}

View File

@@ -0,0 +1,18 @@
{
"success": true,
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"operation_id": "4da8780eeb215e6cb7f48dd981c4ea02"
}
}

View File

@@ -0,0 +1,21 @@
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"id": "4da8780eeb215e6cb7f48dd981c4ea02",
"status": "pending",
"completed": "2020-01-01T08:00:00Z",
"error": "This list is at the maximum number of items"
},
"success": true
}

View File

@@ -0,0 +1,25 @@
{
"success": true,
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e",
"created_on": "2020-01-01T08:00:00Z",
"description": "This is a note",
"kind": "ip",
"modified_on": "2020-01-10T14:00:00Z",
"name": "list1",
"num_items": 10,
"num_referencing_filters": 2
}
}

View File

@@ -0,0 +1,24 @@
{
"success": true,
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": [
"10.0.0.1"
],
"result_info": {
"cursors": {
"after": "yyy",
"before": "xxx"
}
}
}

View File

@@ -0,0 +1,27 @@
{
"success": true,
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": [
{
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e",
"created_on": "2020-01-01T08:00:00Z",
"kind": "ip",
"modified_on": "2020-01-10T14:00:00Z",
"name": "list1",
"num_items": 10,
"description": "This is a note",
"num_referencing_filters": 2
}
]
}