diff --git a/spanner/src/create_backup_schedule.php b/spanner/src/create_backup_schedule.php new file mode 100644 index 000000000..875318466 --- /dev/null +++ b/spanner/src/create_backup_schedule.php @@ -0,0 +1,88 @@ + setEncryptionType(EncryptionType::USE_DATABASE_ENCRYPTION); + $backupSchedule = new BackupSchedule([ + 'full_backup_spec' => new FullBackupSpec(), + 'retention_duration' => (new Duration()) + ->setSeconds(24 * 60 * 60), + 'spec' => new BackupScheduleSpec([ + 'cron_spec' => new CrontabSpec([ + 'text' => '30 12 * * *' + ]), + ]), + 'encryption_config' => $encryptionConfig, + ]); + $request = new CreateBackupScheduleRequest([ + 'parent' => $databaseFullName, + 'backup_schedule_id' => $backupScheduleId, + 'backup_schedule' => $backupSchedule, + ]); + + $operation = $databaseAdminClient->createBackupSchedule($request); + + printf('Created backup scehedule %s' . PHP_EOL, $operation->getName()); +} +// [END spanner_create_backup_schedule] + +// 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/spanner/src/delete_backup_schedule.php b/spanner/src/delete_backup_schedule.php new file mode 100644 index 000000000..d36c12225 --- /dev/null +++ b/spanner/src/delete_backup_schedule.php @@ -0,0 +1,68 @@ + strval($backupScheduleName), + ]); + + $databaseAdminClient->deleteBackupSchedule($request); + printf('Deleted backup scehedule %s' . PHP_EOL, $backupScheduleName); +} +// [END spanner_delete_backup_schedule] + +// 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); \ No newline at end of file diff --git a/spanner/src/get_backup_schedule.php b/spanner/src/get_backup_schedule.php new file mode 100644 index 000000000..c23e643dd --- /dev/null +++ b/spanner/src/get_backup_schedule.php @@ -0,0 +1,69 @@ + $backupScheduleName, + ]); + + $operation = $databaseAdminClient->getBackupSchedule($request); + + printf('Fetched backup scehedule %s' . PHP_EOL, $operation->getName()); +} +// [END spanner_get_backup_schedule] + +// 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); \ No newline at end of file diff --git a/spanner/src/list_backup_schedules.php b/spanner/src/list_backup_schedules.php new file mode 100644 index 000000000..482e5d4c5 --- /dev/null +++ b/spanner/src/list_backup_schedules.php @@ -0,0 +1,65 @@ + $databaseFullName, + ]); + $operation = $databaseAdminClient->listBackupSchedules($request); + + printf("Backup schedules for database %s" . PHP_EOL, $databaseFullName); + foreach($operation as $schedule) { + printf("Backup schedule: %s" . PHP_EOL, $schedule->getName()); + } +} +// [END spanner_list_backup_schedules] + +// 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/spanner/src/update_backup_schedule.php b/spanner/src/update_backup_schedule.php new file mode 100644 index 000000000..887097361 --- /dev/null +++ b/spanner/src/update_backup_schedule.php @@ -0,0 +1,101 @@ + EncryptionType::USE_DATABASE_ENCRYPTION, + ]); + $backupScheduleName = sprintf( + 'projects/%s/instances/%s/databases/%s/backupSchedules/%s', + $projectId, + $instanceId, + $databaseId, + $backupScheduleId); + $backupSchedule = new BackupSchedule([ + 'name' => $backupScheduleName, + 'full_backup_spec' => new FullBackupSpec(), + 'retention_duration' => (new Duration()) + ->setSeconds(48 * 60 * 60), + 'spec' => new BackupScheduleSpec([ + 'cron_spec' => new CrontabSpec([ + 'text' => '45 15 * * *' + ]), + ]), + 'encryption_config' => $encryptionConfig, + ]); + $fieldMask = (new FieldMask()) + ->setPaths([ + 'retention_duration', + 'spec.cron_spec.text', + 'encryption_config', + ]); + + $request = new UpdateBackupScheduleRequest([ + 'backup_schedule' => $backupSchedule, + 'update_mask' => $fieldMask, + ]); + + $operation = $databaseAdminClient->updateBackupSchedule($request); + + printf('Updated backup scehedule %s' . PHP_EOL, $operation->getName()); +} +// [END spanner_update_backup_schedule] + +// 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/spanner/test/spannerBackupScheduleTest.php b/spanner/test/spannerBackupScheduleTest.php new file mode 100644 index 000000000..5ceaed4b3 --- /dev/null +++ b/spanner/test/spannerBackupScheduleTest.php @@ -0,0 +1,171 @@ + self::$projectId, + ]); + + self::$databaseId = 'your-database-id'; + self::$backupScheduleId = 'backup-schedule-' . self::$databaseId; + self::$instance = $spanner->instance(self::$instanceId); + } + + /** + * @test + */ + public function testCreateBackupSchedule() + { + $output = $this->runFunctionSnippet('create_backup_schedule', [ + self::$databaseId, + self::$backupScheduleId, + ]); + $this->assertStringContainsString(self::$projectId, $output); + $this->assertStringContainsString(self::$instanceId, $output); + $this->assertStringContainsString(self::$databaseId, $output); + $this->assertStringContainsString(self::$backupScheduleId, $output); + } + + /** + * @test + * @depends testCreateBackupSchedule + */ + public function testGetBackupSchedule() + { + $output = $this->runFunctionSnippet('get_backup_schedule', [ + self::$databaseId, + self::$backupScheduleId, + ]); + $this->assertStringContainsString(self::$projectId, $output); + $this->assertStringContainsString(self::$instanceId, $output); + $this->assertStringContainsString(self::$databaseId, $output); + $this->assertStringContainsString(self::$backupScheduleId, $output); + } + + /** + * @test + * @depends testCreateBackupSchedule + */ + public function testListBackupSchedules() + { + $output = $this->runFunctionSnippet('list_backup_schedules', [ + self::$databaseId, + ]); + $this->assertStringContainsString(self::$projectId, $output); + $this->assertStringContainsString(self::$instanceId, $output); + $this->assertStringContainsString(self::$databaseId, $output); + } + + /** + * @test + * @depends testCreateBackupSchedule + */ + public function testUpdateBackupSchedule() + { + $output = $this->runFunctionSnippet('update_backup_schedule', [ + self::$databaseId, + self::$backupScheduleId, + ]); + $this->assertStringContainsString(self::$projectId, $output); + $this->assertStringContainsString(self::$instanceId, $output); + $this->assertStringContainsString(self::$databaseId, $output); + $this->assertStringContainsString(self::$backupScheduleId, $output); + } + + /** + * @test + * @depends testCreateBackupSchedule + */ + public function testDeleteBackupSchedule() + { + $output = $this->runFunctionSnippet('delete_backup_schedule', [ + self::$databaseId, + self::$backupScheduleId, + ]); + $this->assertStringContainsString(self::$projectId, $output); + $this->assertStringContainsString(self::$instanceId, $output); + $this->assertStringContainsString(self::$databaseId, $output); + $this->assertStringContainsString(self::$backupScheduleId, $output); + } + + private function runFunctionSnippet($sampleName, $params = []) + { + return $this->traitRunFunctionSnippet( + $sampleName, + array_merge([self::$projectId, self::$instanceId], array_values($params)) + ); + } + + public static function tearDownAfterClass(): void + { + if (self::$instance->exists()) { + $backoff = new ExponentialBackoff(3); + + /** @var Database $db */ + foreach (self::$instance->databases() as $db) { + if (false !== strpos($db->name(), self::$databaseId)) { + $backoff->execute(function () use ($db) { + $db->drop(); + }); + } + } + } + } +}