-
Notifications
You must be signed in to change notification settings - Fork 17
Add Organization Feature Flags & Vault Support #299
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||
| <?php | ||||||
|
|
||||||
| namespace WorkOS\Resource; | ||||||
|
|
||||||
| /** | ||||||
| * Class VaultObject. | ||||||
| */ | ||||||
| class VaultObject extends BaseWorkOSResource | ||||||
| { | ||||||
| public const RESOURCE_TYPE = "vault_object"; | ||||||
|
|
||||||
| public const RESOURCE_ATTRIBUTES = [ | ||||||
| "id", | ||||||
| "name", | ||||||
| "updatedAt", | ||||||
| "value", | ||||||
| "metadata" | ||||||
| ]; | ||||||
|
|
||||||
| public const RESPONSE_TO_RESOURCE_KEY = [ | ||||||
| "id" => "id", | ||||||
| "name" => "name", | ||||||
| "updated_at" => "updatedAt", | ||||||
|
||||||
| "updated_at" => "updatedAt", | |
| "updated_at" => "updatedAt", |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| namespace WorkOS; | ||
|
|
||
| /** | ||
| * Class Vault. | ||
| * | ||
| * This class facilitates the use of WorkOS Vault operations. | ||
| */ | ||
| class Vault | ||
| { | ||
| public const DEFAULT_PAGE_SIZE = 10; | ||
|
|
||
| /** | ||
| * Get a Vault Object. | ||
| * | ||
| * @param string $vaultObjectId The unique identifier for the vault object. | ||
| * | ||
| * @throws Exception\WorkOSException | ||
| * | ||
| * @return Resource\VaultObject | ||
| */ | ||
| public function getVaultObject($vaultObjectId) | ||
| { | ||
| $vaultObjectPath = "vault/v1/kv/{$vaultObjectId}"; | ||
|
|
||
| $response = Client::request(Client::METHOD_GET, $vaultObjectPath, null, null, true); | ||
|
|
||
| return Resource\VaultObject::constructFromResponse($response); | ||
| } | ||
|
|
||
| /** | ||
| * List Vault Objects. | ||
| * | ||
| * @param int $limit Maximum number of records to return | ||
| * @param null|string $before Vault Object ID to look before | ||
| * @param null|string $after Vault Object ID to look after | ||
| * @param Resource\Order $order The Order in which to paginate records | ||
| * | ||
| * @return array{?string, ?string, Resource\VaultObject[]} An array containing the Vault Object ID to use as before and after cursor, and an array of VaultObject instances | ||
| * | ||
| * @throws Exception\WorkOSException | ||
| */ | ||
| public function listVaultObjects( | ||
| $limit = self::DEFAULT_PAGE_SIZE, | ||
| $before = null, | ||
| $after = null, | ||
| $order = null | ||
| ) { | ||
| $vaultObjectsPath = "vault/v1/kv"; | ||
| $params = [ | ||
| "limit" => $limit, | ||
| "before" => $before, | ||
| "after" => $after, | ||
| "order" => $order | ||
| ]; | ||
|
|
||
| $response = Client::request( | ||
| Client::METHOD_GET, | ||
| $vaultObjectsPath, | ||
| null, | ||
| $params, | ||
| true | ||
| ); | ||
|
|
||
| $vaultObjects = []; | ||
| list($before, $after) = Util\Request::parsePaginationArgs($response); | ||
| foreach ($response["data"] as $responseData) { | ||
| \array_push($vaultObjects, Resource\VaultObject::constructFromResponse($responseData)); | ||
| } | ||
|
|
||
| return [$before, $after, $vaultObjects]; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| <?php | ||
|
|
||
| namespace WorkOS; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class VaultTest extends TestCase | ||
| { | ||
| use TestHelper { | ||
| setUp as protected traitSetUp; | ||
| } | ||
|
|
||
| /** | ||
| * @var Vault | ||
| */ | ||
| protected $vault; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->traitSetUp(); | ||
|
|
||
| $this->withApiKey(); | ||
| $this->vault = new Vault(); | ||
| } | ||
|
|
||
| public function testGetVaultObject() | ||
| { | ||
| $vaultObjectPath = "vault/v1/kv/vault_obj_01EHQMYV6MBK39QC5PZXHY59C3"; | ||
|
|
||
| $result = $this->vaultObjectResponseFixture(); | ||
|
|
||
| $this->mockRequest( | ||
| Client::METHOD_GET, | ||
| $vaultObjectPath, | ||
| null, | ||
| null, | ||
| true, | ||
| $result | ||
| ); | ||
|
|
||
| $vaultObject = $this->vaultObjectFixture(); | ||
|
|
||
| $response = $this->vault->getVaultObject("vault_obj_01EHQMYV6MBK39QC5PZXHY59C3"); | ||
| $this->assertSame($vaultObject, $response->toArray()); | ||
| } | ||
|
|
||
| public function testListVaultObjects() | ||
| { | ||
| $vaultObjectsPath = "vault/v1/kv"; | ||
|
|
||
| $result = $this->vaultObjectsResponseFixture(); | ||
|
|
||
| $params = [ | ||
| "limit" => 10, | ||
| "before" => null, | ||
| "after" => null, | ||
| "order" => null | ||
| ]; | ||
|
|
||
| $this->mockRequest( | ||
| Client::METHOD_GET, | ||
| $vaultObjectsPath, | ||
| null, | ||
| $params, | ||
| true, | ||
| $result | ||
| ); | ||
|
|
||
| $vaultObjects = $this->vaultObjectsFixture(); | ||
|
|
||
| list($before, $after, $response) = $this->vault->listVaultObjects(); | ||
| $this->assertSame($vaultObjects, $response[0]->toArray()); | ||
|
Comment on lines
+71
to
+72
Contributor
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. logic: Inconsistent test logic - comparing single fixture object against first item in array. Should either compare arrays or single objects consistently. |
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // Fixtures | ||
|
|
||
| private function vaultObjectResponseFixture() | ||
| { | ||
| return json_encode([ | ||
| "id" => "vault_obj_01EHQMYV6MBK39QC5PZXHY59C3", | ||
| "name" => "Test Vault Object", | ||
| "updated_at" => "2024-01-01T00:00:00.000Z", | ||
| "value" => null, | ||
| "metadata" => [] | ||
| ]); | ||
| } | ||
|
|
||
| private function vaultObjectFixture() | ||
| { | ||
| return [ | ||
| "id" => "vault_obj_01EHQMYV6MBK39QC5PZXHY59C3", | ||
| "name" => "Test Vault Object", | ||
| "updatedAt" => "2024-01-01T00:00:00.000Z", | ||
| "value" => null, | ||
| "metadata" => [] | ||
| ]; | ||
| } | ||
|
|
||
| private function vaultObjectsResponseFixture() | ||
| { | ||
| return json_encode([ | ||
| "object" => "list", | ||
| "data" => [ | ||
| [ | ||
| "id" => "vault_obj_01EHQMYV6MBK39QC5PZXHY59C3", | ||
| "name" => "Test Vault Object", | ||
| "updated_at" => "2024-01-01T00:00:00.000Z", | ||
| "value" => null, | ||
| "metadata" => [] | ||
| ] | ||
| ], | ||
| "list_metadata" => [ | ||
| "before" => null, | ||
| "after" => null | ||
| ] | ||
| ]); | ||
| } | ||
|
|
||
| private function vaultObjectsFixture() | ||
| { | ||
| return [ | ||
| "id" => "vault_obj_01EHQMYV6MBK39QC5PZXHY59C3", | ||
| "name" => "Test Vault Object", | ||
| "updatedAt" => "2024-01-01T00:00:00.000Z", | ||
| "value" => null, | ||
| "metadata" => [] | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
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.
style: Returns raw response array instead of structured objects like other methods. Consider whether feature flags should have their own Resource class for consistency.