-
Notifications
You must be signed in to change notification settings - Fork 235
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
Add Zone Subscription Creation #196
Merged
jacobbednarz
merged 5 commits into
cloudflare:master
from
phily245:190-Add-Zone-Subscription-Setup-Support
Oct 13, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2443c59
Add Zone Subscription Creation
phily245 0162727
Apply suggestions from code review
phily245 55a8dd6
Rename A Variable
phily245 ecf4da7
Handle Logic For Existing Zone Subscriptions
phily245 624d90b
Remove A Trailing Comma
phily245 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Cloudflare\API\Endpoints; | ||
|
||
use Cloudflare\API\Adapter\Adapter; | ||
use Cloudflare\API\Traits\BodyAccessorTrait; | ||
use stdClass; | ||
|
||
class ZoneSubscriptions implements API | ||
{ | ||
use BodyAccessorTrait; | ||
|
||
/** | ||
* @var Adapter | ||
*/ | ||
private $adapter; | ||
|
||
public function __construct(Adapter $adapter) | ||
{ | ||
$this->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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heh, this is one way of checking it 😛 |
||
|
||
$subscription = $this->adapter->{$method}('zones/' . $zoneId . '/subscription', $options); | ||
$this->body = json_decode($subscription->getBody()); | ||
|
||
return $this->body->result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
use Cloudflare\API\Adapter\Adapter; | ||
use Cloudflare\API\Endpoints\ZoneSubscriptions; | ||
|
||
class ZoneSubscriptionsTest extends TestCase | ||
{ | ||
public function testListZoneSubscriptions() | ||
{ | ||
$response = $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a little heads up that the method isn't always
POST
. see https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/cloudflare/resource_cloudflare_zone.go#L217-L228 for some logic and comments around the behaviour.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've matched this with my latest commits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobbednarz thanks for all the feedback! I think I've addressed this, but it would probably be worth you double checking as golang (from your terraform sample) isn't a language I use every day