From 7f72427fa1dbea4ecbe94132de9952755c89708b Mon Sep 17 00:00:00 2001 From: Phil Young Date: Fri, 1 Oct 2021 01:16:27 +0100 Subject: [PATCH 1/6] Add Account Creation (#191) This is used on the tenant API but is currently undocumented on the main documentation --- src/Endpoints/Accounts.php | 13 +++++ tests/Endpoints/AccountsTest.php | 51 ++++++++++++++++++- .../Endpoints/createEnterpriseAccount.json | 13 +++++ .../Endpoints/createStandardAccount.json | 13 +++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/Endpoints/createEnterpriseAccount.json create mode 100644 tests/Fixtures/Endpoints/createStandardAccount.json diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 4d4d80a..838afb9 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -21,6 +21,19 @@ class Accounts implements API $this->adapter = $adapter; } + public function addAccount(string $name, string $type = 'standard'): \stdClass + { + $options = [ + 'name' => $name, + 'type' => $type, + ]; + + $account = $this->adapter->post('accounts', $options); + $this->body = json_decode($account->getBody()); + + return $this->body->result; + } + public function listAccounts( int $page = 1, int $perPage = 20, diff --git a/tests/Endpoints/AccountsTest.php b/tests/Endpoints/AccountsTest.php index db2b1be..24f1f3c 100644 --- a/tests/Endpoints/AccountsTest.php +++ b/tests/Endpoints/AccountsTest.php @@ -1,4 +1,7 @@ listAccounts(1, 20, 'desc'); $this->assertObjectHasAttribute('result', $result); @@ -34,4 +37,50 @@ class AccountsTest extends TestCase $this->assertEquals(1, $result->result_info->page); $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $accounts->getBody()->result[0]->id); } + + public function testAddAccountWithDefaultType() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createStandardAccount.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'), + $this->equalTo([ + 'name' => 'Foo Bar', + 'type' => 'standard', + ]) + ); + + $accounts = new Accounts($mock); + + $accounts->addAccount('Foo Bar'); + $this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id); + } + + public function testAddAccountWithCustomType() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createEnterpriseAccount.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'), + $this->equalTo([ + 'name' => 'Foo Bar', + 'type' => 'enterprise', + ]) + ); + + $accounts = new Accounts($mock); + + $accounts->addAccount('Foo Bar', 'enterprise'); + $this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id); + } } diff --git a/tests/Fixtures/Endpoints/createEnterpriseAccount.json b/tests/Fixtures/Endpoints/createEnterpriseAccount.json new file mode 100644 index 0000000..8f41a7a --- /dev/null +++ b/tests/Fixtures/Endpoints/createEnterpriseAccount.json @@ -0,0 +1,13 @@ +{ + "result": { + "id": "2bab6ace8c72ed3f09b9eca6db1396bb", + "name": "Foo Bar", + "type": "enterprise", + "settings": { + "enforce_twofactor": false + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/createStandardAccount.json b/tests/Fixtures/Endpoints/createStandardAccount.json new file mode 100644 index 0000000..e970a50 --- /dev/null +++ b/tests/Fixtures/Endpoints/createStandardAccount.json @@ -0,0 +1,13 @@ +{ + "result": { + "id": "2bab6ace8c72ed3f09b9eca6db1396bb", + "name": "Foo Bar", + "type": "standard", + "settings": { + "enforce_twofactor": false + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file From bf796b9ec8e141fe63f0613668e90cef73018977 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Fri, 1 Oct 2021 01:19:15 +0100 Subject: [PATCH 2/6] Add Account Member Creation (#192) We can now create account members threough the SDK --- src/Endpoints/AccountMembers.php | 34 +++++++++ tests/Endpoints/AccountMembersTest.php | 32 ++++++++ .../Endpoints/createAccountMember.json | 74 +++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/Endpoints/AccountMembers.php create mode 100644 tests/Endpoints/AccountMembersTest.php create mode 100644 tests/Fixtures/Endpoints/createAccountMember.json diff --git a/src/Endpoints/AccountMembers.php b/src/Endpoints/AccountMembers.php new file mode 100644 index 0000000..c9f1c3c --- /dev/null +++ b/src/Endpoints/AccountMembers.php @@ -0,0 +1,34 @@ +adapter = $adapter; + } + + public function addAccountMember(string $accountId, string $email, array $roles): \stdClass + { + $options = [ + 'email' => $email, + 'roles' => $roles, + ]; + + $account = $this->adapter->post('accounts/' . $accountId . '/members', $options); + $this->body = json_decode($account->getBody()); + + return $this->body->result; + } +} diff --git a/tests/Endpoints/AccountMembersTest.php b/tests/Endpoints/AccountMembersTest.php new file mode 100644 index 0000000..c43c96a --- /dev/null +++ b/tests/Endpoints/AccountMembersTest.php @@ -0,0 +1,32 @@ +getPsr7JsonResponseForFixture('Endpoints/createAccountMember.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/members'), + $this->equalTo([ + 'email' => 'user@example.com', + 'roles' => [ + '3536bcfad5faccb999b47003c79917fb', + ], + ]) + ); + + $accountMembers = new AccountMembers($mock); + $accountMembers->addAccountMember('01a7362d577a6c3019a474fd6f485823', 'user@example.com', ['3536bcfad5faccb999b47003c79917fb']); + + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $accountMembers->getBody()->result->id); + } +} diff --git a/tests/Fixtures/Endpoints/createAccountMember.json b/tests/Fixtures/Endpoints/createAccountMember.json new file mode 100644 index 0000000..c7e310f --- /dev/null +++ b/tests/Fixtures/Endpoints/createAccountMember.json @@ -0,0 +1,74 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "4536bcfad5faccb111b47003c79917fa", + "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", + "user": { + "id": "7c5dae5552338874e5053f2534d2767a", + "first_name": "John", + "last_name": "Appleseed", + "email": "user@example.com", + "two_factor_authentication_enabled": false + }, + "status": "accepted", + "roles": [ + { + "id": "3536bcfad5faccb999b47003c79917fb", + "name": "Account Administrator", + "description": "Administrative access to the entire Account", + "permissions": { + "analytics": { + "read": true, + "write": true + }, + "billing": { + "read": true, + "write": true + }, + "cache_purge": { + "read": true, + "write": true + }, + "dns": { + "read": true, + "write": true + }, + "dns_records": { + "read": true, + "write": true + }, + "lb": { + "read": true, + "write": true + }, + "logs": { + "read": true, + "write": true + }, + "organization": { + "read": true, + "write": true + }, + "ssl": { + "read": true, + "write": true + }, + "waf": { + "read": true, + "write": true + }, + "zones": { + "read": true, + "write": true + }, + "zone_settings": { + "read": true, + "write": true + } + } + } + ] + } +} From 99d8cfb71c3bd01ed7839b1ccfa04ab56081ebb5 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Mon, 11 Oct 2021 01:42:54 +0100 Subject: [PATCH 3/6] Add Account Member Listing (#194) --- src/Endpoints/AccountMembers.php | 16 ++++ tests/Endpoints/AccountMembersTest.php | 27 ++++++ .../Endpoints/listAccountMembers.json | 82 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 tests/Fixtures/Endpoints/listAccountMembers.json diff --git a/src/Endpoints/AccountMembers.php b/src/Endpoints/AccountMembers.php index c9f1c3c..ec9e90a 100644 --- a/src/Endpoints/AccountMembers.php +++ b/src/Endpoints/AccountMembers.php @@ -31,4 +31,20 @@ class AccountMembers implements API return $this->body->result; } + + public function listAccountMembers(string $accountId, int $page = 1, int $perPage = 20): \stdClass + { + $query = [ + 'page' => $page, + 'per_page' => $perPage, + ]; + + $zone = $this->adapter->get('accounts/' . $accountId . '/members', $query); + $this->body = json_decode($zone->getBody()); + + return (object)[ + 'result' => $this->body->result, + 'result_info' => $this->body->result_info, + ]; + } } diff --git a/tests/Endpoints/AccountMembersTest.php b/tests/Endpoints/AccountMembersTest.php index c43c96a..431e263 100644 --- a/tests/Endpoints/AccountMembersTest.php +++ b/tests/Endpoints/AccountMembersTest.php @@ -29,4 +29,31 @@ class AccountMembersTest extends TestCase $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $accountMembers->getBody()->result->id); } + + public function testListAccountMembers() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/listAccountMembers.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/023e105f4ecef8ad9ca31a8372d0c353/members'), + $this->equalTo([ + 'page' => 1, + 'per_page' => 20, + ]) + ); + + $accountMembers = new AccountMembers($mock); + $result = $accountMembers->listAccountMembers('023e105f4ecef8ad9ca31a8372d0c353', 1, 20); + + $this->assertObjectHasAttribute('result', $result); + + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $result->result[0]->id); + $this->assertEquals(1, $result->result_info->count); + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $accountMembers->getBody()->result[0]->id); + } } diff --git a/tests/Fixtures/Endpoints/listAccountMembers.json b/tests/Fixtures/Endpoints/listAccountMembers.json new file mode 100644 index 0000000..5096fa0 --- /dev/null +++ b/tests/Fixtures/Endpoints/listAccountMembers.json @@ -0,0 +1,82 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "4536bcfad5faccb111b47003c79917fa", + "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", + "user": { + "id": "7c5dae5552338874e5053f2534d2767a", + "first_name": "John", + "last_name": "Appleseed", + "email": "user@example.com", + "two_factor_authentication_enabled": false + }, + "status": "accepted", + "roles": [ + { + "id": "3536bcfad5faccb999b47003c79917fb", + "name": "Account Administrator", + "description": "Administrative access to the entire Account", + "permissions": { + "analytics": { + "read": true, + "write": true + }, + "billing": { + "read": true, + "write": true + }, + "cache_purge": { + "read": true, + "write": true + }, + "dns": { + "read": true, + "write": true + }, + "dns_records": { + "read": true, + "write": true + }, + "lb": { + "read": true, + "write": true + }, + "logs": { + "read": true, + "write": true + }, + "organization": { + "read": true, + "write": true + }, + "ssl": { + "read": true, + "write": true + }, + "waf": { + "read": true, + "write": true + }, + "zones": { + "read": true, + "write": true + }, + "zone_settings": { + "read": true, + "write": true + } + } + } + ] + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 200 + } +} From 12ac2bbd76736db5e22b086c09ce51e8bdd3ae5f Mon Sep 17 00:00:00 2001 From: Phil Young Date: Wed, 13 Oct 2021 04:31:32 +0100 Subject: [PATCH 4/6] Add Account Id To Zone Creation (#195) --- src/Endpoints/Zones.php | 10 ++- tests/Endpoints/ZoneCacheTest.php | 78 +++++++++++++++++++ tests/Endpoints/ZonesTest.php | 105 ++++++++------------------ tests/Fixtures/Endpoints/addZone.json | 4 + 4 files changed, 118 insertions(+), 79 deletions(-) create mode 100644 tests/Endpoints/ZoneCacheTest.php diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index 2335b1c..fd9e18b 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -27,18 +27,20 @@ class Zones implements API * * @param string $name * @param bool $jumpStart - * @param string $organizationID + * @param string $accountId * @return \stdClass */ - public function addZone(string $name, bool $jumpStart = false, string $organizationID = ''): \stdClass + public function addZone(string $name, bool $jumpStart = false, string $accountId = ''): \stdClass { $options = [ 'name' => $name, 'jump_start' => $jumpStart ]; - if (!empty($organizationID)) { - $options['organization'] = ['id' => $organizationID]; + if (!empty($accountId)) { + $options['account'] = [ + 'id' => $accountId, + ]; } $user = $this->adapter->post('zones', $options); diff --git a/tests/Endpoints/ZoneCacheTest.php b/tests/Endpoints/ZoneCacheTest.php new file mode 100644 index 0000000..bdefeec --- /dev/null +++ b/tests/Endpoints/ZoneCacheTest.php @@ -0,0 +1,78 @@ +getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), + $this->equalTo(['purge_everything' => true]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); + } + + public function testCachePurgeHost() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeHost.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), + $this->equalTo( + [ + 'files' => [], + 'tags' => [], + 'hosts' => ['dash.cloudflare.com'] + ] + ) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], ['dash.cloudflare.com']); + + $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); + } + + public function testCachePurge() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), + $this->equalTo(['files' => [ + 'https://example.com/file.jpg', + ] + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [ + 'https://example.com/file.jpg', + ]); + + $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); + } +} diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 02addeb..b4412f7 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -40,7 +40,9 @@ class ZonesTest extends TestCase $this->equalTo([ 'name' => 'example.com', 'jump_start' => true, - 'organization' => ['id' => '01a7362d577a6c3019a474fd6f485823'] + 'account' => [ + 'id' => '01a7362d577a6c3019a474fd6f485823', + ], ]) ); @@ -49,6 +51,33 @@ class ZonesTest extends TestCase $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id); } + public function testAddZoneWithAccountId() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/addZone.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones'), + $this->equalTo([ + 'name' => 'example.com', + 'jump_start' => false, + 'account' => [ + 'id' => '023e105f4ecef8ad9ca31a8372d0c353', + ], + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->addZone('example.com', false, '023e105f4ecef8ad9ca31a8372d0c353'); + + $this->assertObjectHasAttribute('id', $result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->account->id); + } + public function testActivationTest() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/activationTest.json'); @@ -188,78 +217,4 @@ class ZonesTest extends TestCase $this->assertTrue($result); $this->assertEquals('development_mode', $zones->getBody()->result->id); } - - public function testCachePurgeEverything() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json'); - - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('post')->willReturn($response); - - $mock->expects($this->once()) - ->method('post') - ->with( - $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), - $this->equalTo(['purge_everything' => true]) - ); - - $zones = new \Cloudflare\API\Endpoints\Zones($mock); - $result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); - - $this->assertTrue($result); - $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); - } - - public function testCachePurgeHost() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeHost.json'); - - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('post')->willReturn($response); - - $mock->expects($this->once()) - ->method('post') - ->with( - $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), - $this->equalTo( - [ - 'files' => [], - 'tags' => [], - 'hosts' => ['dash.cloudflare.com'] - ] - ) - ); - - $zones = new \Cloudflare\API\Endpoints\Zones($mock); - $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], ['dash.cloudflare.com']); - - $this->assertTrue($result); - $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); - } - - public function testCachePurge() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json'); - - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('post')->willReturn($response); - - $mock->expects($this->once()) - ->method('post') - ->with( - $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), - $this->equalTo(['files' => [ - 'https://example.com/file.jpg', - ] - ]) - ); - - $zones = new \Cloudflare\API\Endpoints\Zones($mock); - $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [ - 'https://example.com/file.jpg', - ]); - - $this->assertTrue($result); - $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); - } } diff --git a/tests/Fixtures/Endpoints/addZone.json b/tests/Fixtures/Endpoints/addZone.json index d3903c1..685e8cd 100644 --- a/tests/Fixtures/Endpoints/addZone.json +++ b/tests/Fixtures/Endpoints/addZone.json @@ -23,6 +23,10 @@ "email": "user@example.com", "owner_type": "user" }, + "account": { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "Demo Account" + }, "permissions": [ "#zone:read", "#zone:edit" From 5024c2ab53f7384703d23605f8a00fe9908bea8e Mon Sep 17 00:00:00 2001 From: Phil Young Date: Wed, 13 Oct 2021 04:39:47 +0100 Subject: [PATCH 5/6] Add Zone Subscription Creation (#196) Consuming the tenant API requires this as a part of the provisioning --- src/Endpoints/ZoneSubscriptions.php | 51 ++++++++++++ tests/Endpoints/ZoneSubscriptionsTest.php | 81 +++++++++++++++++++ .../Endpoints/createZoneSubscription.json | 40 +++++++++ .../Endpoints/listEmptyZoneSubscriptions.json | 6 ++ .../Endpoints/listZoneSubscriptions.json | 42 ++++++++++ 5 files changed, 220 insertions(+) create mode 100644 src/Endpoints/ZoneSubscriptions.php create mode 100644 tests/Endpoints/ZoneSubscriptionsTest.php create mode 100644 tests/Fixtures/Endpoints/createZoneSubscription.json create mode 100644 tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json create mode 100644 tests/Fixtures/Endpoints/listZoneSubscriptions.json diff --git a/src/Endpoints/ZoneSubscriptions.php b/src/Endpoints/ZoneSubscriptions.php new file mode 100644 index 0000000..6149dd9 --- /dev/null +++ b/src/Endpoints/ZoneSubscriptions.php @@ -0,0 +1,51 @@ +adapter = $adapter; + } + + public function listZoneSubscriptions(string $zoneId): \stdClass + { + $user = $this->adapter->get('zones/' . $zoneId . '/subscriptions'); + $this->body = json_decode($user->getBody()); + + return (object)[ + 'result' => $this->body->result, + ]; + } + + public function addZoneSubscription(string $zoneId, string $ratePlanId = ''): stdClass + { + $options = []; + + if (empty($ratePlanId) === false) { + $options['rate_plan'] = [ + 'id' => $ratePlanId, + ]; + } + + $existingSubscription = $this->listZoneSubscriptions($zoneId); + $method = empty($existingSubscription->result) ? 'post' : 'put'; + + $subscription = $this->adapter->{$method}('zones/' . $zoneId . '/subscription', $options); + $this->body = json_decode($subscription->getBody()); + + return $this->body->result; + } +} diff --git a/tests/Endpoints/ZoneSubscriptionsTest.php b/tests/Endpoints/ZoneSubscriptionsTest.php new file mode 100644 index 0000000..bd4b7c4 --- /dev/null +++ b/tests/Endpoints/ZoneSubscriptionsTest.php @@ -0,0 +1,81 @@ +getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscriptions') + ); + + $zoneSubscriptions = new ZoneSubscriptions($mock); + $zoneSubscriptions->listZoneSubscriptions('023e105f4ecef8ad9ca31a8372d0c353'); + + $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result[0]->id); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result[0]->zone->id); + } + + public function testAddZoneSubscriptionIfMissing() + { + $postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json'); + $getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listEmptyZoneSubscriptions.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('post')->willReturn($postResponse); + $mock->method('get')->willReturn($getResponse); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'), + $this->equalTo([ + 'rate_plan' => [ + 'id' => 'PARTNER_PRO', + ], + ]) + ); + + $zoneSubscriptions = new ZoneSubscriptions($mock); + $zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO'); + + $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id); + } + + public function testAddZoneSubscriptionIfExisting() + { + $postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json'); + $getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('put')->willReturn($postResponse); + $mock->method('get')->willReturn($getResponse); + + $mock->expects($this->once()) + ->method('put') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'), + $this->equalTo([ + 'rate_plan' => [ + 'id' => 'PARTNER_PRO', + ], + ]) + ); + + $zoneSubscriptions = new ZoneSubscriptions($mock); + $zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO'); + + $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id); + } +} diff --git a/tests/Fixtures/Endpoints/createZoneSubscription.json b/tests/Fixtures/Endpoints/createZoneSubscription.json new file mode 100644 index 0000000..b16f022 --- /dev/null +++ b/tests/Fixtures/Endpoints/createZoneSubscription.json @@ -0,0 +1,40 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "app": { + "install_id": null + }, + "id": "506e3185e9c882d175a2d0cb0093d9f2", + "state": "Paid", + "price": 20, + "currency": "USD", + "component_values": [ + { + "name": "page_rules", + "value": 20, + "default": 5, + "price": 5 + } + ], + "zone": { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "example.com" + }, + "frequency": "monthly", + "rate_plan": { + "id": "free", + "public_name": "Business Plan", + "currency": "USD", + "scope": "zone", + "sets": [ + {} + ], + "is_contract": false, + "externally_managed": false + }, + "current_period_end": "2014-03-31T12:20:00Z", + "current_period_start": "2014-05-11T12:20:00Z" + } +} diff --git a/tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json b/tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json new file mode 100644 index 0000000..8e0c15e --- /dev/null +++ b/tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json @@ -0,0 +1,6 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listZoneSubscriptions.json b/tests/Fixtures/Endpoints/listZoneSubscriptions.json new file mode 100644 index 0000000..080f4d7 --- /dev/null +++ b/tests/Fixtures/Endpoints/listZoneSubscriptions.json @@ -0,0 +1,42 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "app": { + "install_id": null + }, + "id": "506e3185e9c882d175a2d0cb0093d9f2", + "state": "Paid", + "price": 20, + "currency": "USD", + "component_values": [ + { + "name": "page_rules", + "value": 20, + "default": 5, + "price": 5 + } + ], + "zone": { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "example.com" + }, + "frequency": "monthly", + "rate_plan": { + "id": "free", + "public_name": "Business Plan", + "currency": "USD", + "scope": "zone", + "sets": [ + {} + ], + "is_contract": false, + "externally_managed": false + }, + "current_period_end": "2014-03-31T12:20:00Z", + "current_period_start": "2014-05-11T12:20:00Z" + } + ] +} \ No newline at end of file From fdfc656aa8b78016f4722acec30b54136c027d8e Mon Sep 17 00:00:00 2001 From: Phil Young Date: Wed, 13 Oct 2021 04:40:33 +0100 Subject: [PATCH 6/6] Add Account Role Listings (#198) I want to list the roles availablew in an account so I can add a member to the account --- src/Endpoints/AccountRoles.php | 33 +++++++++ tests/Endpoints/AccountRolesTest.php | 32 +++++++++ .../Fixtures/Endpoints/listAccountRoles.json | 69 +++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/Endpoints/AccountRoles.php create mode 100644 tests/Endpoints/AccountRolesTest.php create mode 100644 tests/Fixtures/Endpoints/listAccountRoles.json diff --git a/src/Endpoints/AccountRoles.php b/src/Endpoints/AccountRoles.php new file mode 100644 index 0000000..aca7615 --- /dev/null +++ b/src/Endpoints/AccountRoles.php @@ -0,0 +1,33 @@ +adapter = $adapter; + } + + public function listAccountRoles(string $accountId): stdClass + { + $roles = $this->adapter->get('accounts/' . $accountId . '/roles'); + $this->body = json_decode($roles->getBody()); + + return (object)[ + 'result' => $this->body->result, + 'result_info' => $this->body->result_info, + ]; + } +} diff --git a/tests/Endpoints/AccountRolesTest.php b/tests/Endpoints/AccountRolesTest.php new file mode 100644 index 0000000..acb16be --- /dev/null +++ b/tests/Endpoints/AccountRolesTest.php @@ -0,0 +1,32 @@ +getPsr7JsonResponseForFixture('Endpoints/listAccountRoles.json'); + + $adapter = $this->getMockBuilder(Adapter::class)->getMock(); + $adapter->method('get')->willReturn($response); + + $adapter->expects($this->once()) + ->method('get') + ->with($this->equalTo('accounts/023e105f4ecef8ad9ca31a8372d0c353/roles')); + + $roles = new AccountRoles($adapter); + $result = $roles->listAccountRoles('023e105f4ecef8ad9ca31a8372d0c353'); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals('3536bcfad5faccb999b47003c79917fb', $result->result[0]->id); + $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('3536bcfad5faccb999b47003c79917fb', $roles->getBody()->result[0]->id); + } +} diff --git a/tests/Fixtures/Endpoints/listAccountRoles.json b/tests/Fixtures/Endpoints/listAccountRoles.json new file mode 100644 index 0000000..b4f7224 --- /dev/null +++ b/tests/Fixtures/Endpoints/listAccountRoles.json @@ -0,0 +1,69 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "3536bcfad5faccb999b47003c79917fb", + "name": "Account Administrator", + "description": "Administrative access to the entire Account", + "permissions": { + "analytics": { + "read": true, + "write": true + }, + "billing": { + "read": true, + "write": true + }, + "cache_purge": { + "read": true, + "write": true + }, + "dns": { + "read": true, + "write": true + }, + "dns_records": { + "read": true, + "write": true + }, + "lb": { + "read": true, + "write": true + }, + "logs": { + "read": true, + "write": true + }, + "organization": { + "read": true, + "write": true + }, + "ssl": { + "read": true, + "write": true + }, + "waf": { + "read": true, + "write": true + }, + "zones": { + "read": true, + "write": true + }, + "zone_settings": { + "read": true, + "write": true + } + } + } + ], + "result_info": { + "page": 1, + "per_page": 1, + "total_pages": 1, + "count": 1, + "total_count": 1 + } +}