Skip to content

Commit

Permalink
Merge branch 'openstack' into 'master'
Browse files Browse the repository at this point in the history
Add support for the OpenStack product

See merge request transip/restapi-php-library!156
  • Loading branch information
samihsoylu committed Dec 23, 2021
2 parents da9c517 + a716c01 commit 8ded616
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .secretsignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
composer.lock
ci/deploy_to_github.sh
src/TransipAPI.php
src/Entity/OpenStackProject.php
src/Entity/OpenStackUser.php
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

6.12.0
-----
* Added OpenStack resource

6.11.0
-----
* Added destination big storage name for backup restore
Expand Down
85 changes: 85 additions & 0 deletions src/Entity/OpenStackProject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Transip\Api\Library\Entity;

class OpenStackProject extends AbstractEntity
{
/**
* @description Identifier
* @example `7a7a3bcb46c6450f95c53edb8dcebc7b`
* @type string
* @readonly
*/
protected $id;

/**
* @description Name of the project
* @example `hosting101-datacenter`
* @type string
*/
protected $name;

/**
* @description Describes this project
* @example This is an example project
* @type string
*/
protected $description;

/**
* @description When an ongoing process blocks the project from being modified, this is set to `true`
* @example false
* @type boolean
* @readonly
*/
protected $isLocked;

/**
* @description Set to `true` when a project has been administratively blocked
* @example false
* @type boolean
* @readonly
*/
protected $isBlocked;

public function getId(): string
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function getDescription(): string
{
return $this->description;
}

public function isLocked(): bool
{
return $this->isLocked;
}

public function isBlocked(): bool
{
return $this->isBlocked;
}

/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}

/**
* @param string $description
*/
public function setDescription(string $description): void
{
$this->description = $description;
}
}
67 changes: 67 additions & 0 deletions src/Entity/OpenStackUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Transip\Api\Library\Entity;

class OpenStackUser extends AbstractEntity
{
/**
* @description Identifier
* @example 6322872d9c7e445dbbb49c1f9ca28adc
* @readonly
* @type string
* @var string
*/
protected $id;

/**
* @description Login name
* @example `hosting101-support`
* @readonly
* @type string
*/
protected $username;

/**
* @description Description
* @example `Supporter account`
* @type string
*/
protected $description;

/**
* @description Email address
* @example `[email protected]`
* @type string
*/
protected $email;

public function getId(): string
{
return $this->id;
}

public function getUsername(): string
{
return $this->username;
}

public function getDescription(): string
{
return $this->description;
}

public function getEmail(): string
{
return $this->email;
}

public function setDescription(string $description): void
{
$this->description = $description;
}

public function setEmail(string $email): void
{
$this->email = $email;
}
}
51 changes: 51 additions & 0 deletions src/Repository/OpenStack/Project/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Transip\Api\Library\Repository\OpenStack\Project;

use Transip\Api\Library\Entity\OpenStackUser;
use Transip\Api\Library\Repository\ApiRepository;
use Transip\Api\Library\Repository\OpenStack\ProjectRepository;

class UserRepository extends ApiRepository
{
public const RESOURCE_NAME = 'users';

public const RESOURCE_PARAMETER_PLURAL = 'users';

protected function getRepositoryResourceNames(): array
{
return [ProjectRepository::RESOURCE_NAME, self::RESOURCE_NAME];
}

/**
* @param string $projectId
* @return OpenStackUser[]
*/
public function getByProjectId(string $projectId): array
{
$users = [];
$response = $this->httpClient->get($this->getResourceUrl($projectId));
$usersArray = $this->getParameterFromResponse($response, self::RESOURCE_PARAMETER_PLURAL);

foreach ($usersArray as $userArray) {
$users[] = new OpenStackUser($userArray);
}

return $users;
}

public function grantUserAccessToProject(string $projectId, string $userId): void
{
$url = $this->getResourceUrl($projectId);
$parameters = ['userId' => $userId];

$this->httpClient->post($url, $parameters);
}

public function revokeUserAccessFromProject(string $projectId, string $userId): void
{
$this->httpClient->delete(
$this->getResourceUrl($projectId, $userId)
);
}
}
88 changes: 88 additions & 0 deletions src/Repository/OpenStack/ProjectRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Transip\Api\Library\Repository\OpenStack;

use Transip\Api\Library\Entity\OpenStackProject;
use Transip\Api\Library\Repository\ApiRepository;

class ProjectRepository extends ApiRepository
{
public const RESOURCE_NAME = 'openstack/projects';

public const RESOURCE_PARAMETER_SINGUlAR = 'project';
public const RESOURCE_PARAMETER_PLURAL = 'projects';

protected function getRepositoryResourceNames(): array
{
return [self::RESOURCE_NAME];
}

/**
* @return OpenStackProject[]
*/
public function getAll(): array
{
$projects = [];
$response = $this->httpClient->get($this->getResourceUrl());
$projectsArray = $this->getParameterFromResponse($response, self::RESOURCE_PARAMETER_PLURAL);

foreach ($projectsArray as $projectArray) {
$projects[] = new OpenStackProject($projectArray);
}

return $projects;
}

public function create(string $name, string $description): void
{
$parameters = [
'name' => $name,
'description' => $description,
];

$this->httpClient->post(
$this->getResourceUrl(),
$parameters
);
}

public function getByProjectId(string $projectId): OpenStackProject
{
$response = $this->httpClient->get($this->getResourceUrl($projectId));
$projectArray = $this->getParameterFromResponse($response, self::RESOURCE_PARAMETER_SINGUlAR);

return new OpenStackProject($projectArray);
}

public function updateProject(OpenStackProject $project): void
{
$parameters = [
'project' => $project,
];

$this->httpClient->put(
$this->getResourceUrl($project->getId()),
$parameters
);
}

public function handover(string $projectID, string $targetCustomerName): void
{
$parameters = [
'action' => 'handover',
'targetCustomerName' => $targetCustomerName,
];

$this->httpClient->patch(
$this->getResourceUrl($projectID),
$parameters
);
}

public function cancel(string $projectID): void
{
$this->httpClient->delete(
$this->getResourceUrl($projectID)
);
}
}
Loading

0 comments on commit 8ded616

Please sign in to comment.