diff --git a/storagecontrol/composer.json b/storagecontrol/composer.json index 46deccbf4c..76be4bb14f 100644 --- a/storagecontrol/composer.json +++ b/storagecontrol/composer.json @@ -1,6 +1,6 @@ { "require": { - "google/cloud-storage-control": "1.6.1" + "google/cloud-storage-control": "^1.9" }, "require-dev": { "google/cloud-storage": "^1.48.1" diff --git a/storagecontrol/src/delete_folder_recursive.php b/storagecontrol/src/delete_folder_recursive.php new file mode 100644 index 0000000000..02475d24bf --- /dev/null +++ b/storagecontrol/src/delete_folder_recursive.php @@ -0,0 +1,65 @@ +folderName('_', $bucketName, $folderName); + + $request = new DeleteFolderRecursiveRequest([ + 'name' => $formattedName, + ]); + + $operation = $storageControlClient->deleteFolderRecursive($request); + $operation->pollUntilComplete(); + if (!$operation->operationSucceeded()) { + $error = $operation->getError(); + throw new \Exception(sprintf( + 'DeleteFolderRecursive operation failed: %s', + $error ? $error->getMessage() : 'Unknown error' + )); + } + + printf('Deleted folder recursively: %s', $folderName); +} +# [END storage_control_delete_folder_recursive] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/test/StorageControlTest.php b/storagecontrol/test/StorageControlTest.php index f32230e9d1..ae5fc7ee56 100644 --- a/storagecontrol/test/StorageControlTest.php +++ b/storagecontrol/test/StorageControlTest.php @@ -17,9 +17,13 @@ namespace Google\Cloud\Samples\StorageControl; +use Google\ApiCore\ApiException; use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; +use Google\Cloud\Storage\Control\V2\CreateFolderRequest; +use Google\Cloud\Storage\Control\V2\GetFolderRequest; use Google\Cloud\Storage\StorageClient; use Google\Cloud\TestUtils\TestTrait; +use Google\Cloud\TestUtils\EventuallyConsistentTestTrait; use PHPUnit\Framework\TestCase; /** @@ -28,6 +32,7 @@ class StorageControlTest extends TestCase { use TestTrait; + use EventuallyConsistentTestTrait; private static $sourceBucket; private static $folderId; @@ -206,4 +211,67 @@ public function testDeleteFolder() $output ); } + + /** + * @depends testDeleteFolder + */ + public function testDeleteFolderRecursive() + { + $parentFolderId = 'test-parent-' . time() . rand(); + $childFolderId = $parentFolderId . '/test-child-' . time() . rand(); + + $bucketName = self::$sourceBucket->name(); + $bucketResourceName = self::$storageControlClient->bucketName('_', $bucketName); + + // Create parent folder + $createParentRequest = new CreateFolderRequest([ + 'parent' => $bucketResourceName, + 'folder_id' => $parentFolderId, + ]); + self::$storageControlClient->createFolder($createParentRequest); + + // Create child folder + $createChildRequest = new CreateFolderRequest([ + 'parent' => $bucketResourceName, + 'folder_id' => $childFolderId, + ]); + self::$storageControlClient->createFolder($createChildRequest); + + // Call the delete folder recursive snippet + $output = $this->runFunctionSnippet('delete_folder_recursive', [ + $bucketName, $parentFolderId + ]); + + $this->assertStringContainsString( + sprintf('Deleted folder recursively: %s', $parentFolderId), + $output + ); + + // Verify folder and child folder are gone by trying to get them + $formattedParentName = self::$storageControlClient->folderName('_', $bucketName, $parentFolderId); + $getParentRequest = new GetFolderRequest([ + 'name' => $formattedParentName, + ]); + + $formattedChildName = self::$storageControlClient->folderName('_', $bucketName, $childFolderId); + $getChildRequest = new GetFolderRequest([ + 'name' => $formattedChildName, + ]); + + $this->runEventuallyConsistentTest(function () use ($getParentRequest, $getChildRequest) { + try { + self::$storageControlClient->getFolder($getParentRequest); + $this->fail('Expected getFolder to throw ApiException for deleted folder'); + } catch (ApiException $e) { + $this->assertEquals(404, $e->getCode()); + } + + try { + self::$storageControlClient->getFolder($getChildRequest); + $this->fail('Expected getFolder to throw ApiException for deleted child folder'); + } catch (ApiException $e) { + $this->assertEquals(404, $e->getCode()); + } + }); + } }