diff --git a/secretmanager/composer.json b/secretmanager/composer.json index f1840b131..2a20b5355 100644 --- a/secretmanager/composer.json +++ b/secretmanager/composer.json @@ -1,5 +1,6 @@ { "require": { - "google/cloud-secret-manager": "^2.0.0" + "google/cloud-secret-manager": "^2.1.0", + "google/cloud-resource-manager": "^1.0" } } diff --git a/secretmanager/src/bind_tags_to_regional_secret.php b/secretmanager/src/bind_tags_to_regional_secret.php new file mode 100644 index 000000000..949a6f9aa --- /dev/null +++ b/secretmanager/src/bind_tags_to_regional_secret.php @@ -0,0 +1,92 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s' . PHP_EOL, $newSecret->getName()); + + // Specify regional endpoint. + $tagBindOptions = ['apiEndpoint' => "$locationId-cloudresourcemanager.googleapis.com"]; + $tagBindingsClient = new TagBindingsClient($tagBindOptions); + $tagBinding = (new TagBinding()) + ->setParent('//secretmanager.googleapis.com/' . $newSecret->getName()) + ->setTagValue($tagValue); + + // Build the request. + $request = (new CreateTagBindingRequest()) + ->setTagBinding($tagBinding); + + // Create the tag binding. + $operationResponse = $tagBindingsClient->createTagBinding($request); + $operationResponse->pollUntilComplete(); + + // Check if the operation succeeded. + if ($operationResponse->operationSucceeded()) { + printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue); + } else { + $error = $operationResponse->getError(); + printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage()); + } +} +// [END secretmanager_bind_tags_to_regional_secret] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/bind_tags_to_secret.php b/secretmanager/src/bind_tags_to_secret.php new file mode 100644 index 000000000..60616353b --- /dev/null +++ b/secretmanager/src/bind_tags_to_secret.php @@ -0,0 +1,92 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s' . PHP_EOL, $newSecret->getName()); + + $tagBindingsClient = new TagBindingsClient(); + $tagBinding = (new TagBinding()) + ->setParent('//secretmanager.googleapis.com/' . $newSecret->getName()) + ->setTagValue($tagValue); + + // Build the binding request. + $request = (new CreateTagBindingRequest()) + ->setTagBinding($tagBinding); + + // Create the tag binding. + $operationResponse = $tagBindingsClient->createTagBinding($request); + $operationResponse->pollUntilComplete(); + + // Check if the operation succeeded. + if ($operationResponse->operationSucceeded()) { + printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue); + } else { + $error = $operationResponse->getError(); + printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage()); + } +} +// [END secretmanager_bind_tags_to_secret] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_regional_secret_with_annotations.php b/secretmanager/src/create_regional_secret_with_annotations.php new file mode 100644 index 000000000..03d78d0eb --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_annotations.php @@ -0,0 +1,71 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // set the annotations. + $annotations = [$annotationKey => $annotationValue]; + $secret->setAnnotations($annotations); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with annotations', $newSecret->getName()); +} +// [END secretmanager_create_regional_secret_with_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_regional_secret_with_labels.php b/secretmanager/src/create_regional_secret_with_labels.php new file mode 100644 index 000000000..0ebf77e31 --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_labels.php @@ -0,0 +1,71 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // set the labels. + $labels = [$labelKey => $labelValue]; + $secret->setLabels($labels); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with labels', $newSecret->getName()); +} +// [END secretmanager_create_regional_secret_with_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_regional_secret_with_tags.php b/secretmanager/src/create_regional_secret_with_tags.php new file mode 100644 index 000000000..519e8b84f --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_tags.php @@ -0,0 +1,71 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // set the tags. + $tags = [$tagKey => $tagValue]; + $secret ->setTags($tags); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with tag', $newSecret->getName()); +} +// [END secretmanager_regional_create_secret_with_tags] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_secret_with_annotations.php b/secretmanager/src/create_secret_with_annotations.php new file mode 100644 index 000000000..40d8a44d0 --- /dev/null +++ b/secretmanager/src/create_secret_with_annotations.php @@ -0,0 +1,73 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // set the annoation. + $annotation = [$annotationKey => $annotationValue]; + $secret->setAnnotations($annotation); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with annotations', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_secret_with_labels.php b/secretmanager/src/create_secret_with_labels.php new file mode 100644 index 000000000..02edcdb98 --- /dev/null +++ b/secretmanager/src/create_secret_with_labels.php @@ -0,0 +1,73 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // set the labels. + $labels = [$labelKey => $labelValue]; + $secret->setLabels($labels); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with labels', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_secret_with_tags.php b/secretmanager/src/create_secret_with_tags.php new file mode 100644 index 000000000..33d7353c1 --- /dev/null +++ b/secretmanager/src/create_secret_with_tags.php @@ -0,0 +1,73 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // set the tags. + $tags = [$tagKey => $tagValue]; + $secret->setTags($tags); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with tag', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_tags] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_regional_secret_annotation.php b/secretmanager/src/delete_regional_secret_annotation.php new file mode 100644 index 000000000..c76fe6d1c --- /dev/null +++ b/secretmanager/src/delete_regional_secret_annotation.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // delete the annotation + unset($annotations[$annotationKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $secret = new Secret(); + $secret->setAnnotations($annotations); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_regional_secret_annotation] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_regional_secret_label.php b/secretmanager/src/delete_regional_secret_label.php new file mode 100644 index 000000000..00c9c18bb --- /dev/null +++ b/secretmanager/src/delete_regional_secret_label.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // delete the label + unset($labels[$labelKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $secret = new Secret(); + $secret->setLabels($labels); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_regional_secret_label] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_secret_annotation.php b/secretmanager/src/delete_secret_annotation.php new file mode 100644 index 000000000..57286ac62 --- /dev/null +++ b/secretmanager/src/delete_secret_annotation.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // delete the annotation + unset($annotations[$annotationKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $secret = new Secret(); + $secret->setAnnotations($annotations); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_secret_annotation] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_secret_label.php b/secretmanager/src/delete_secret_label.php new file mode 100644 index 000000000..9e337f1df --- /dev/null +++ b/secretmanager/src/delete_secret_label.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // delete the label + unset($labels[$labelKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $secret = new Secret(); + $secret->setLabels($labels); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_secret_label] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_regional_secret_annotations.php b/secretmanager/src/edit_regional_secret_annotations.php new file mode 100644 index 000000000..832022f75 --- /dev/null +++ b/secretmanager/src/edit_regional_secret_annotations.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // update the annotation - need to create a new annotations map with the updated values + $newAnnotations = []; + foreach ($annotations as $key => $value) { + $newAnnotations[$key] = $value; + } + $newAnnotations[$annotationKey] = $annotationValue; + $getSecret->setAnnotations($newAnnotations); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s annotations' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_regional_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_regional_secret_labels.php b/secretmanager/src/edit_regional_secret_labels.php new file mode 100644 index 000000000..fa2cfadef --- /dev/null +++ b/secretmanager/src/edit_regional_secret_labels.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // update the label - need to create a new labels map with the updated values + $newLabels = []; + foreach ($labels as $key => $value) { + $newLabels[$key] = $value; + } + $newLabels[$labelKey] = $labelValue; + $getSecret->setLabels($newLabels); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s labels' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_regional_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_secret_annotations.php b/secretmanager/src/edit_secret_annotations.php new file mode 100644 index 000000000..1a58cf521 --- /dev/null +++ b/secretmanager/src/edit_secret_annotations.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // update the annotation - need to create a new annotations map with the updated values + $newAnnotations = []; + foreach ($annotations as $key => $value) { + $newAnnotations[$key] = $value; + } + $newAnnotations[$annotationKey] = $annotationValue; + $getSecret->setAnnotations($newAnnotations); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s annotations' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_secret_labels.php b/secretmanager/src/edit_secret_labels.php new file mode 100644 index 000000000..cdbb79ce1 --- /dev/null +++ b/secretmanager/src/edit_secret_labels.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // update the label - need to create a new labels map with the updated values + $newLabels = []; + foreach ($labels as $key => $value) { + $newLabels[$key] = $value; + } + $newLabels[$labelKey] = $labelValue; + $getSecret->setLabels($newLabels); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s labels' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_regional_secret_annotations.php b/secretmanager/src/view_regional_secret_annotations.php new file mode 100644 index 000000000..d58c9e9d7 --- /dev/null +++ b/secretmanager/src/view_regional_secret_annotations.php @@ -0,0 +1,69 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // print the secret name + printf('Get secret %s with annotation:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the annotations + foreach ($annotations as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_regional_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_regional_secret_labels.php b/secretmanager/src/view_regional_secret_labels.php new file mode 100644 index 000000000..af04dfe72 --- /dev/null +++ b/secretmanager/src/view_regional_secret_labels.php @@ -0,0 +1,69 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // print the secret name + printf('Get secret %s with labels:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the labels + foreach ($labels as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_regional_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_secret_annotations.php b/secretmanager/src/view_secret_annotations.php new file mode 100644 index 000000000..e61aea65a --- /dev/null +++ b/secretmanager/src/view_secret_annotations.php @@ -0,0 +1,65 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // print the secret name + printf('Get secret %s with annotation:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the annotations + foreach ($annotations as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_secret_labels.php b/secretmanager/src/view_secret_labels.php new file mode 100644 index 000000000..d549ccf56 --- /dev/null +++ b/secretmanager/src/view_secret_labels.php @@ -0,0 +1,65 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // print the secret name + printf('Get secret %s with labels:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the labels + foreach ($labels as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/test/regionalsecretmanagerTest.php b/secretmanager/test/regionalsecretmanagerTest.php index 80c694620..01d5c7b48 100644 --- a/secretmanager/test/regionalsecretmanagerTest.php +++ b/secretmanager/test/regionalsecretmanagerTest.php @@ -20,6 +20,14 @@ namespace Google\Cloud\Samples\SecretManager; use Google\ApiCore\ApiException as GaxApiException; +use Google\Cloud\ResourceManager\V3\DeleteTagKeyRequest; +use Google\Cloud\ResourceManager\V3\DeleteTagValueRequest; +use Google\Cloud\ResourceManager\V3\Client\TagKeysClient; +use Google\Cloud\ResourceManager\V3\CreateTagKeyRequest; +use Google\Cloud\ResourceManager\V3\TagKey; +use Google\Cloud\ResourceManager\V3\Client\TagValuesClient; +use Google\Cloud\ResourceManager\V3\CreateTagValueRequest; +use Google\Cloud\ResourceManager\V3\TagValue; use Google\Cloud\SecretManager\V1\AddSecretVersionRequest; use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; use Google\Cloud\SecretManager\V1\CreateSecretRequest; @@ -36,6 +44,9 @@ class regionalsecretmanagerTest extends TestCase use TestTrait; private static $client; + private static $tagKeyClient; + private static $tagValuesClient; + private static $testSecret; private static $testSecretToDelete; private static $testSecretWithVersions; @@ -44,14 +55,29 @@ class regionalsecretmanagerTest extends TestCase private static $testSecretVersionToDestroy; private static $testSecretVersionToDisable; private static $testSecretVersionToEnable; + private static $testSecretWithTagToCreateName; + private static $testSecretBindTagToCreateName; + private static $testSecretWithLabelsToCreateName; + private static $testSecretWithAnnotationsToCreateName; private static $iamUser = 'user:kapishsingh@google.com'; private static $locationId = 'us-central1'; + private static $testLabelKey = 'test-label-key'; + private static $testLabelValue = 'test-label-value'; + private static $testUpdatedLabelValue = 'test-label-new-value'; + private static $testAnnotationKey = 'test-annotation-key'; + private static $testAnnotationValue = 'test-annotation-value'; + private static $testUpdatedAnnotationValue = 'test-annotation-new-value'; + + private static $testTagKey; + private static $testTagValue; public static function setUpBeforeClass(): void { $options = ['apiEndpoint' => 'secretmanager.' . self::$locationId . '.rep.googleapis.com' ]; self::$client = new SecretManagerServiceClient($options); + self::$tagKeyClient = new TagKeysClient(); + self::$tagValuesClient = new TagValuesClient(); self::$testSecret = self::createSecret(); self::$testSecretToDelete = self::createSecret(); @@ -61,7 +87,14 @@ public static function setUpBeforeClass(): void self::$testSecretVersionToDestroy = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDisable = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToEnable = self::addSecretVersion(self::$testSecretWithVersions); + self::$testSecretWithTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretBindTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretWithLabelsToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretWithAnnotationsToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::disableSecretVersion(self::$testSecretVersionToEnable); + + self::$testTagKey = self::createTagKey(self::randomSecretId()); + self::$testTagValue = self::createTagValue(self::randomSecretId()); } public static function tearDownAfterClass(): void @@ -73,6 +106,13 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretToDelete->getName()); self::deleteSecret(self::$testSecretWithVersions->getName()); self::deleteSecret(self::$testSecretToCreateName); + self::deleteSecret(self::$testSecretWithTagToCreateName); + self::deleteSecret(self::$testSecretBindTagToCreateName); + self::deleteSecret(self::$testSecretWithLabelsToCreateName); + self::deleteSecret(self::$testSecretWithAnnotationsToCreateName); + sleep(15); // Added a sleep to wait for the tag unbinding + self::deleteTagValue(); + self::deleteTagKey(); } private static function randomSecretId(): string @@ -122,6 +162,86 @@ private static function deleteSecret(string $name) } } + private static function createTagKey(string $short_name): string + { + $parent = self::$client->projectName(self::$projectId); + $tagKey = (new TagKey()) + ->setParent($parent) + ->setShortName($short_name); + + $request = (new CreateTagKeyRequest()) + ->setTagKey($tagKey); + + $operation = self::$tagKeyClient->createTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagKey = $operation->getResult(); + printf("Tag key created: %s\n", $createdTagKey->getName()); + return $createdTagKey->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag key: %s\n", $error->getMessage()); + return ''; + } + } + + private static function createTagValue(string $short_name): string + { + $tagValuesClient = new TagValuesClient(); + $tagValue = (new TagValue()) + ->setParent(self::$testTagKey) + ->setShortName($short_name); + + $request = (new CreateTagValueRequest()) + ->setTagValue($tagValue); + + $operation = self::$tagValuesClient->createTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagValue = $operation->getResult(); + printf("Tag value created: %s\n", $createdTagValue->getName()); + return $createdTagValue->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag value: %s\n", $error->getMessage()); + return ''; + } + } + + private static function deleteTagKey() + { + $request = (new DeleteTagKeyRequest()) + ->setName(self::$testTagKey); + + $operation = self::$tagKeyClient->deleteTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag key deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag key: %s\n", $error->getMessage()); + } + } + + private static function deleteTagValue() + { + $request = (new DeleteTagValueRequest()) + ->setName(self::$testTagValue); + + $operation = self::$tagValuesClient->deleteTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag value deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag value: %s\n", $error->getMessage()); + } + } + public function testAccessSecretVersion() { $name = self::$client->parseName(self::$testSecretVersion->getName()); @@ -324,4 +444,148 @@ public function testUpdateSecretWithAlias() $this->assertStringContainsString('Updated secret', $output); } + + public function testCreateSecretWithTags() + { + $name = self::$client->parseName(self::$testSecretWithTagToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_tags', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testTagKey, + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testBindTagsToSecret() + { + $name = self::$client->parseName(self::$testSecretBindTagToCreateName); + + $output = $this->runFunctionSnippet('bind_tags_to_regional_secret', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + $this->assertStringContainsString('Tag binding created for secret', $output); + } + + public function testCreateSecretWithLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_labels', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testLabelKey, + self::$testLabelValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testCreateSecretWithAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_annotations', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testAnnotationKey, + self::$testAnnotationValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testViewSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('view_regional_secret_annotations', [ + $name['project'], + $name['location'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testViewSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('view_regional_secret_labels', [ + $name['project'], + $name['location'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testEditSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('edit_regional_secret_labels', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testLabelKey, + self::$testUpdatedLabelValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testEditSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('edit_regional_secret_annotations', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testAnnotationKey, + self::$testUpdatedAnnotationValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretLabel() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('delete_regional_secret_label', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testLabelKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretAnnotation() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('delete_regional_secret_annotation', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testAnnotationKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } } diff --git a/secretmanager/test/secretmanagerTest.php b/secretmanager/test/secretmanagerTest.php index 48570fe25..a67d8cfa7 100644 --- a/secretmanager/test/secretmanagerTest.php +++ b/secretmanager/test/secretmanagerTest.php @@ -20,6 +20,14 @@ namespace Google\Cloud\Samples\SecretManager; use Google\ApiCore\ApiException as GaxApiException; +use Google\Cloud\ResourceManager\V3\DeleteTagKeyRequest; +use Google\Cloud\ResourceManager\V3\DeleteTagValueRequest; +use Google\Cloud\ResourceManager\V3\Client\TagKeysClient; +use Google\Cloud\ResourceManager\V3\CreateTagKeyRequest; +use Google\Cloud\ResourceManager\V3\TagKey; +use Google\Cloud\ResourceManager\V3\Client\TagValuesClient; +use Google\Cloud\ResourceManager\V3\CreateTagValueRequest; +use Google\Cloud\ResourceManager\V3\TagValue; use Google\Cloud\SecretManager\V1\AddSecretVersionRequest; use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; use Google\Cloud\SecretManager\V1\CreateSecretRequest; @@ -38,6 +46,9 @@ class secretmanagerTest extends TestCase use TestTrait; private static $client; + private static $tagKeyClient; + private static $tagValuesClient; + private static $testSecret; private static $testSecretToDelete; private static $testSecretWithVersions; @@ -47,24 +58,46 @@ class secretmanagerTest extends TestCase private static $testSecretVersionToDestroy; private static $testSecretVersionToDisable; private static $testSecretVersionToEnable; + private static $testSecretWithTagToCreateName; + private static $testSecretBindTagToCreateName; + private static $testSecretWithLabelsToCreateName; + private static $testSecretWithAnnotationsToCreateName; private static $iamUser = 'user:sethvargo@google.com'; + private static $testLabelKey = 'test-label-key'; + private static $testLabelValue = 'test-label-value'; + private static $testUpdatedLabelValue = 'test-label-new-value'; + private static $testAnnotationKey = 'test-annotation-key'; + private static $testAnnotationValue = 'test-annotation-value'; + private static $testUpdatedAnnotationValue = 'test-annotation-new-value'; + + private static $testTagKey; + private static $testTagValue; public static function setUpBeforeClass(): void { self::$client = new SecretManagerServiceClient(); + self::$tagKeyClient = new TagKeysClient(); + self::$tagValuesClient = new TagValuesClient(); self::$testSecret = self::createSecret(); self::$testSecretToDelete = self::createSecret(); self::$testSecretWithVersions = self::createSecret(); self::$testSecretToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testUmmrSecretToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretBindTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithLabelsToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithAnnotationsToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretVersion = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDestroy = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDisable = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToEnable = self::addSecretVersion(self::$testSecretWithVersions); self::disableSecretVersion(self::$testSecretVersionToEnable); + + self::$testTagKey = self::createTagKey(self::randomSecretId()); + self::$testTagValue = self::createTagValue(self::randomSecretId()); } public static function tearDownAfterClass(): void @@ -74,6 +107,13 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretWithVersions->getName()); self::deleteSecret(self::$testSecretToCreateName); self::deleteSecret(self::$testUmmrSecretToCreateName); + self::deleteSecret(self::$testSecretWithTagToCreateName); + self::deleteSecret(self::$testSecretBindTagToCreateName); + self::deleteSecret(self::$testSecretWithLabelsToCreateName); + self::deleteSecret(self::$testSecretWithAnnotationsToCreateName); + sleep(15); // Added a sleep to wait for the tag unbinding + self::deleteTagValue(); + self::deleteTagKey(); } private static function randomSecretId(): string @@ -127,6 +167,86 @@ private static function deleteSecret(string $name) } } + private static function createTagKey(string $short_name): string + { + $parent = self::$client->projectName(self::$projectId); + $tagKey = (new TagKey()) + ->setParent($parent) + ->setShortName($short_name); + + $request = (new CreateTagKeyRequest()) + ->setTagKey($tagKey); + + $operation = self::$tagKeyClient->createTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagKey = $operation->getResult(); + printf("Tag key created: %s\n", $createdTagKey->getName()); + return $createdTagKey->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag key: %s\n", $error->getMessage()); + return ''; + } + } + + private static function createTagValue(string $short_name): string + { + $tagValuesClient = new TagValuesClient(); + $tagValue = (new TagValue()) + ->setParent(self::$testTagKey) + ->setShortName($short_name); + + $request = (new CreateTagValueRequest()) + ->setTagValue($tagValue); + + $operation = self::$tagValuesClient->createTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagValue = $operation->getResult(); + printf("Tag value created: %s\n", $createdTagValue->getName()); + return $createdTagValue->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag value: %s\n", $error->getMessage()); + return ''; + } + } + + private static function deleteTagKey() + { + $request = (new DeleteTagKeyRequest()) + ->setName(self::$testTagKey); + + $operation = self::$tagKeyClient->deleteTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag key deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag key: %s\n", $error->getMessage()); + } + } + + private static function deleteTagValue() + { + $request = (new DeleteTagValueRequest()) + ->setName(self::$testTagValue); + + $operation = self::$tagValuesClient->deleteTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag value deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag value: %s\n", $error->getMessage()); + } + } + public function testAccessSecretVersion() { $name = self::$client->parseName(self::$testSecretVersion->getName()); @@ -328,4 +448,138 @@ public function testUpdateSecretWithAlias() $this->assertStringContainsString('Updated secret', $output); } + + public function testCreateSecretWithTags() + { + $name = self::$client->parseName(self::$testSecretWithTagToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_tags', [ + $name['project'], + $name['secret'], + self::$testTagKey, + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testBindTagsToSecret() + { + $name = self::$client->parseName(self::$testSecretBindTagToCreateName); + + $output = $this->runFunctionSnippet('bind_tags_to_secret', [ + $name['project'], + $name['secret'], + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + $this->assertStringContainsString('Tag binding created for secret', $output); + } + + public function testCreateSecretWithLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_labels', [ + $name['project'], + $name['secret'], + self::$testLabelKey, + self::$testLabelValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testCreateSecretWithAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_annotations', [ + $name['project'], + $name['secret'], + self::$testAnnotationKey, + self::$testAnnotationValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testViewSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('view_secret_annotations', [ + $name['project'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testViewSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('view_secret_labels', [ + $name['project'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testEditSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('edit_secret_labels', [ + $name['project'], + $name['secret'], + self::$testLabelKey, + self::$testUpdatedLabelValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testEditSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('edit_secret_annotations', [ + $name['project'], + $name['secret'], + self::$testAnnotationKey, + self::$testUpdatedAnnotationValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretLabel() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('delete_secret_label', [ + $name['project'], + $name['secret'], + self::$testLabelKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretAnnotation() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('delete_secret_annotation', [ + $name['project'], + $name['secret'], + self::$testAnnotationKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } } diff --git a/spanner/src/pg_update_dml_returning.php b/spanner/src/pg_update_dml_returning.php index 2a975b229..f5f11f57d 100644 --- a/spanner/src/pg_update_dml_returning.php +++ b/spanner/src/pg_update_dml_returning.php @@ -48,7 +48,7 @@ function pg_update_dml_returning(string $instanceId, string $databaseId): void $result = $transaction->execute( 'UPDATE Albums ' . 'SET MarketingBudget = MarketingBudget * 2 ' - . 'WHERE SingerId = 1 and AlbumId = 1' + . 'WHERE SingerId = 1 and AlbumId = 1 ' . 'RETURNING MarketingBudget' ); foreach ($result->rows() as $row) { diff --git a/storagecontrol/composer.json b/storagecontrol/composer.json index 01218016b..182f667f4 100644 --- a/storagecontrol/composer.json +++ b/storagecontrol/composer.json @@ -3,6 +3,6 @@ "google/cloud-storage-control": "1.3.0" }, "require-dev": { - "google/cloud-storage": "^1.41.3" + "google/cloud-storage": "^1.48.1" } } diff --git a/storagecontrol/src/create_anywhere_cache.php b/storagecontrol/src/create_anywhere_cache.php new file mode 100644 index 000000000..9e8ac273c --- /dev/null +++ b/storagecontrol/src/create_anywhere_cache.php @@ -0,0 +1,77 @@ +bucketName('_', $bucketName); + + $anywhereCache = new AnywhereCache([ + 'zone' => $zone, + ]); + + $request = new CreateAnywhereCacheRequest([ + 'parent' => $formattedName, + 'anywhere_cache' => $anywhereCache, + ]); + + // Start a create operation and block until it completes. Real applications + // may want to setup a callback, wait on a coroutine, or poll until it + // completes. + $operation = $storageControlClient->createAnywhereCache($request); + + printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); + $operation->pollUntilComplete([ + 'totalPollTimeoutMillis' => 5400000, + 'initialPollDelayMillis' => 1000, // Start with 1 second delay + 'pollDelayMultiplier' => 2, // Double delay each time + 'maxPollDelayMillis' => 60000, // Max 60 seconds delay between polls + ]); + + /** @var AnywhereCache $anywhereCacheResult */ + $anywhereCacheResult = $operation->getResult(); + printf('Created anywhere cache: %s' . PHP_EOL, $anywhereCacheResult->getName()); +} +# [END storage_control_create_anywhere_cache] + +// 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/src/disable_anywhere_cache.php b/storagecontrol/src/disable_anywhere_cache.php new file mode 100644 index 000000000..dbddf3c76 --- /dev/null +++ b/storagecontrol/src/disable_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new DisableAnywhereCacheRequest([ + 'name' => $formattedName + ]); + + $response = $storageControlClient->disableAnywhereCache($request); + + printf('Disabled anywhere cache: %s', $response->getName()); +} +# [END storage_control_disable_anywhere_cache] + +// 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/src/get_anywhere_cache.php b/storagecontrol/src/get_anywhere_cache.php new file mode 100644 index 000000000..7687cf6ca --- /dev/null +++ b/storagecontrol/src/get_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new GetAnywhereCacheRequest([ + 'name' => $formattedName, + ]); + + $response = $storageControlClient->getAnywhereCache($request); + + printf('Got anywhere cache: %s', $response->getName()); +} +# [END storage_control_get_anywhere_cache] + +// 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/src/list_anywhere_caches.php b/storagecontrol/src/list_anywhere_caches.php new file mode 100644 index 000000000..91ed6e29b --- /dev/null +++ b/storagecontrol/src/list_anywhere_caches.php @@ -0,0 +1,58 @@ +bucketName('_', $bucketName); + + $request = new ListAnywhereCachesRequest([ + 'parent' => $formattedName, + ]); + + $response = $storageControlClient->listAnywhereCaches($request); + + foreach ($response as $anywhereCache) { + printf('Anywhere cache name: %s' . PHP_EOL, $anywhereCache->getName()); + } +} +# [END storage_control_list_anywhere_caches] + +// 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/src/pause_anywhere_cache.php b/storagecontrol/src/pause_anywhere_cache.php new file mode 100644 index 000000000..260e7bb37 --- /dev/null +++ b/storagecontrol/src/pause_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new PauseAnywhereCacheRequest([ + 'name' => $formattedName, + ]); + + $response = $storageControlClient->pauseAnywhereCache($request); + + printf('Paused anywhere cache: %s', $response->getName()); +} +# [END storage_control_pause_anywhere_cache] + +// 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/src/resume_anywhere_cache.php b/storagecontrol/src/resume_anywhere_cache.php new file mode 100644 index 000000000..a95dd6f92 --- /dev/null +++ b/storagecontrol/src/resume_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new ResumeAnywhereCacheRequest([ + 'name' => $formattedName + ]); + + $response = $storageControlClient->resumeAnywhereCache($request); + + printf('Resumed anywhere cache: %s', $response->getName()); +} +# [END storage_control_resume_anywhere_cache] + +// 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/src/update_anywhere_cache.php b/storagecontrol/src/update_anywhere_cache.php new file mode 100644 index 000000000..99b262e00 --- /dev/null +++ b/storagecontrol/src/update_anywhere_cache.php @@ -0,0 +1,82 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $anywhereCache = new AnywhereCache([ + 'name' => $formattedName, + 'admission_policy' => $admission_policy, + ]); + + $updateMask = new FieldMask([ + 'paths' => ['admission_policy'], + ]); + + $request = new UpdateAnywhereCacheRequest([ + 'anywhere_cache' => $anywhereCache, + 'update_mask' => $updateMask, + ]); + + // Start an update operation. This returns an Operation object which can be polled. + $operation = $storageControlClient->updateAnywhereCache($request); + + printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); + $operation->pollUntilComplete([ + 'totalPollTimeoutMillis' => 5400000, + 'initialPollDelayMillis' => 1000, // Start with 1 second delay + 'pollDelayMultiplier' => 2, // Double delay each time + 'maxPollDelayMillis' => 60000, // Max 60 seconds delay between polls + ]); + + $anywhereCacheResult = $operation->getResult(); + printf('Updated anywhere cache: %s', $anywhereCacheResult->getName()); +} +# [END storage_control_update_anywhere_cache] + +// 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/anywhereCacheTest.php b/storagecontrol/test/anywhereCacheTest.php new file mode 100644 index 000000000..d36f5b613 --- /dev/null +++ b/storagecontrol/test/anywhereCacheTest.php @@ -0,0 +1,182 @@ +createBucket( + sprintf('php-gcscontrol-sample-%s', $uniqueBucketId), + [ + 'location' => self::$location, + 'hierarchicalNamespace' => ['enabled' => true], + 'iamConfiguration' => ['uniformBucketLevelAccess' => ['enabled' => true]] + ] + ); + self::$anywhereCacheName = self::$storageControlClient->anywhereCacheName( + '_', // Set project to "_" to signify global bucket + self::$sourceBucket->name(), + self::$cacheId + ); + } + + public static function tearDownAfterClass(): void + { + foreach (self::$sourceBucket->objects(['versions' => true]) as $object) { + $object->delete(); + } + self::$sourceBucket->delete(); + } + + public function testCreateAnywhereCache() + { + $output = $this->runFunctionSnippet('create_anywhere_cache', [ + self::$sourceBucket->name(), + self::$zone, + ]); + + $this->assertStringContainsString( + sprintf('Created anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testCreateAnywhereCache + */ + public function testGetAnywhereCache() + { + $output = $this->runFunctionSnippet('get_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Got anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testGetAnywhereCache + */ + public function testListAnywhereCaches() + { + $output = $this->runFunctionSnippet('list_anywhere_caches', [ + self::$sourceBucket->name(), + ]); + + $this->assertStringContainsString( + sprintf('Anywhere cache name: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testListAnywhereCaches + */ + public function testPauseAnywhereCache() + { + $output = $this->runFunctionSnippet('pause_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Paused anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testPauseAnywhereCache + */ + public function testResumeAnywhereCache() + { + $output = $this->runFunctionSnippet('resume_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Resumed anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testResumeAnywhereCache + */ + public function testUpdateAnywhereCache() + { + $admission_policy = 'admit-on-second-miss'; + $output = $this->runFunctionSnippet('update_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + $admission_policy + ]); + + $this->assertStringContainsString( + sprintf('Updated anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testUpdateAnywhereCache + */ + public function testDisableAnywhereCache() + { + $output = $this->runFunctionSnippet('disable_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Disabled anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } +}