Skip to content

Commit 91054a6

Browse files
ycombinatorJamie Hannaford
authored and
Jamie Hannaford
committed
Adding convenience method to retrieve domain by name.
1 parent edd6dd7 commit 91054a6

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright 2012-2014 Rackspace US, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCloud\Common\Exceptions;
19+
20+
class DomainNotFoundException extends \Exception
21+
{
22+
}

lib/OpenCloud/DNS/Service.php

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use OpenCloud\Common\Http\Message\Formatter;
2121
use OpenCloud\Common\Service\CatalogService;
22+
use OpenCloud\Common\Exceptions\DomainNotFoundException;
2223
use OpenCloud\DNS\Collection\DnsIterator;
2324
use OpenCloud\DNS\Resource\AsyncResponse;
2425
use OpenCloud\DNS\Resource\Domain;
@@ -55,6 +56,24 @@ public function domain($info = null)
5556
return $this->resource('Domain', $info);
5657
}
5758

59+
/**
60+
* Returns a domain, given a domain name
61+
*
62+
* @param string $domainName Domain name
63+
* @return Domain the domain object
64+
*/
65+
public function domainByName($domainName)
66+
{
67+
$domainList = $this->domainList(array("name" => $domainName));
68+
69+
if (count($domainList) != 1) {
70+
throw new DomainNotFoundException();
71+
}
72+
73+
return $this->resource('Domain', $domainList[0]);
74+
}
75+
76+
5877
/**
5978
* Returns a collection of domains
6079
*

tests/OpenCloud/Tests/DNS/ServiceTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function testDomain()
4040
$this->assertInstanceOf('OpenCloud\DNS\Resource\Domain', $this->service->domain());
4141
}
4242

43+
public function testDomainByName()
44+
{
45+
$this->addMockSubscriber($this->makeResponse('{"domains":[{"name":"region2.example.net","id":2725352,"updated":"2011-06-23T20:21:06.000+0000","created":"2011-06-23T19:24:27.000+0000"}],"totalEntries":114}', 200));
46+
$domain = $this->service->domainByName("region2.example.net");
47+
48+
$this->assertInstanceOf('OpenCloud\DNS\Resource\Domain', $domain);
49+
$this->assertEquals("region2.example.net", $domain->getName());
50+
}
51+
4352
/**
4453
* @mockFile Domain_List
4554
*/

0 commit comments

Comments
 (0)