Skip to content

Commit

Permalink
Image by name add architecture (#111)
Browse files Browse the repository at this point in the history
* Add architecture to Images->GetByName to allow getting the correct image for the architecture

Closes #110

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
LKaemmerling and StyleCIBot authored Dec 13, 2023
1 parent 97da52e commit 31b31c9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/Models/Images/ImageRequestOpts.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ class ImageRequestOpts extends RequestOpts
*/
public $name;

/** @var string */
public $architecture;

/**
* RequestOpts constructor.
*
* @param $name
* @param $perPage
* @param $page
* @param $labelSelector
* @param string|null $name
* @param int|null $perPage
* @param int|null $page
* @param string|null $labelSelector
* @param string|null $architecture
*/
public function __construct(string $name = null, int $perPage = null, int $page = null, string $labelSelector = null)
public function __construct(string $name = null, int $perPage = null, int $page = null, string $labelSelector = null, string $architecture = null)
{
parent::__construct($perPage, $page, $labelSelector);
$this->name = $name;
$this->architecture = $architecture;
}
}
16 changes: 10 additions & 6 deletions src/Models/Images/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LKDev\HetznerCloud\Models\Images;

use GuzzleHttp\Exception\GuzzleException;
use LKDev\HetznerCloud\APIException;
use LKDev\HetznerCloud\APIResponse;
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Contracts\Resources;
Expand Down Expand Up @@ -43,10 +45,11 @@ public function all(RequestOpts $requestOpts = null): array
*
* @see https://docs.hetzner.cloud/#resources-images-get
*
* @param RequestOpts $requestOpts
* @param RequestOpts|null $requestOpts
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
* @throws APIException
* @throws GuzzleException
*/
public function list(RequestOpts $requestOpts = null): ?APIResponse
{
Expand Down Expand Up @@ -92,13 +95,14 @@ public function getById(int $imageId): ?Image
* @see https://docs.hetzner.cloud/#resources-images-get-1
*
* @param string $name
* @return \LKDev\HetznerCloud\Models\Images\Image|null
* @param string|null $architecture
* @return Image|null
*
* @throws \LKDev\HetznerCloud\APIException
* @throws APIException
*/
public function getByName(string $name): ?Image
public function getByName(string $name, string $architecture = null): ?Image
{
$images = $this->list(new ImageRequestOpts($name));
$images = $this->list(new ImageRequestOpts($name, null, null, null, $architecture));

return (count($images->images) > 0) ? $images->images[0] : null;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Models/Images/ImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public function testGetByName()
$this->assertLastRequestQueryParametersContains('name', 'ubuntu-20.04');
}

public function testGetByNameWithArchitecture()
{
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/images.json')));
$image = $this->images->getByName('ubuntu-20.04', 'arm');
$this->assertEquals($image->id, 4711);
$this->assertEquals($image->name, 'ubuntu-20.04');

$this->assertEmpty($image->labels);
$this->assertLastRequestEquals('GET', '/images');
$this->assertLastRequestQueryParametersContains('name', 'ubuntu-20.04');
$this->assertLastRequestQueryParametersContains('architecture', 'arm');
}

public function testAll()
{
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/images.json')));
Expand Down

0 comments on commit 31b31c9

Please sign in to comment.