|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * GENERATED CODE WARNING |
| 20 | + * This file was automatically generated - do not edit! |
| 21 | + */ |
| 22 | + |
| 23 | +require_once __DIR__ . '/../../../vendor/autoload.php'; |
| 24 | + |
| 25 | +// [START memorystore_v1_generated_Memorystore_StartMigration_sync] |
| 26 | +use Google\ApiCore\ApiException; |
| 27 | +use Google\ApiCore\OperationResponse; |
| 28 | +use Google\Cloud\Memorystore\V1\Client\MemorystoreClient; |
| 29 | +use Google\Cloud\Memorystore\V1\Instance; |
| 30 | +use Google\Cloud\Memorystore\V1\SelfManagedSource; |
| 31 | +use Google\Cloud\Memorystore\V1\StartMigrationRequest; |
| 32 | +use Google\Rpc\Status; |
| 33 | + |
| 34 | +/** |
| 35 | + * Initiates the migration of a source instance to the target Memorystore |
| 36 | + * instance. |
| 37 | + * |
| 38 | + * After the successful completion of this operation, the target instance |
| 39 | + * will: |
| 40 | + * 1. Set up replication with the source instance and replicate any writes to |
| 41 | + * the source instance. |
| 42 | + * 2. Only allow reads. |
| 43 | + * |
| 44 | + * @param string $selfManagedSourceIpAddress The IP address of the source instance. |
| 45 | + * This IP address should be a stable IP address that can be accessed by the |
| 46 | + * Memorystore instance throughout the migration process. |
| 47 | + * @param int $selfManagedSourcePort The port of the source instance. |
| 48 | + * This port should be a stable port that can be accessed by the Memorystore |
| 49 | + * instance throughout the migration process. |
| 50 | + * @param string $formattedSelfManagedSourceNetworkAttachment The resource name of the Private Service Connect Network |
| 51 | + * Attachment used to establish connectivity to the source instance. This |
| 52 | + * network attachment has the following requirements: |
| 53 | + * 1. It must be in the same project as the Memorystore instance. |
| 54 | + * 2. It must be in the same region as the Memorystore instance. |
| 55 | + * 3. The subnet attached to the network attachment must be in the same VPC |
| 56 | + * network as the source instance nodes. |
| 57 | + * |
| 58 | + * Format: |
| 59 | + * projects/{project}/regions/{region}/networkAttachments/{network_attachment} |
| 60 | + * Please see {@see MemorystoreClient::networkAttachmentName()} for help formatting this field. |
| 61 | + * @param string $formattedName The resource name of the instance to start migration on. |
| 62 | + * Format: projects/{project}/locations/{location}/instances/{instance} |
| 63 | + * Please see {@see MemorystoreClient::instanceName()} for help formatting this field. |
| 64 | + */ |
| 65 | +function start_migration_sample( |
| 66 | + string $selfManagedSourceIpAddress, |
| 67 | + int $selfManagedSourcePort, |
| 68 | + string $formattedSelfManagedSourceNetworkAttachment, |
| 69 | + string $formattedName |
| 70 | +): void { |
| 71 | + // Create a client. |
| 72 | + $memorystoreClient = new MemorystoreClient(); |
| 73 | + |
| 74 | + // Prepare the request message. |
| 75 | + $selfManagedSource = (new SelfManagedSource()) |
| 76 | + ->setIpAddress($selfManagedSourceIpAddress) |
| 77 | + ->setPort($selfManagedSourcePort) |
| 78 | + ->setNetworkAttachment($formattedSelfManagedSourceNetworkAttachment); |
| 79 | + $request = (new StartMigrationRequest()) |
| 80 | + ->setSelfManagedSource($selfManagedSource) |
| 81 | + ->setName($formattedName); |
| 82 | + |
| 83 | + // Call the API and handle any network failures. |
| 84 | + try { |
| 85 | + /** @var OperationResponse $response */ |
| 86 | + $response = $memorystoreClient->startMigration($request); |
| 87 | + $response->pollUntilComplete(); |
| 88 | + |
| 89 | + if ($response->operationSucceeded()) { |
| 90 | + /** @var Instance $result */ |
| 91 | + $result = $response->getResult(); |
| 92 | + printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString()); |
| 93 | + } else { |
| 94 | + /** @var Status $error */ |
| 95 | + $error = $response->getError(); |
| 96 | + printf('Operation failed with error data: %s' . PHP_EOL, $error->serializeToJsonString()); |
| 97 | + } |
| 98 | + } catch (ApiException $ex) { |
| 99 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Helper to execute the sample. |
| 105 | + * |
| 106 | + * This sample has been automatically generated and should be regarded as a code |
| 107 | + * template only. It will require modifications to work: |
| 108 | + * - It may require correct/in-range values for request initialization. |
| 109 | + * - It may require specifying regional endpoints when creating the service client, |
| 110 | + * please see the apiEndpoint client configuration option for more details. |
| 111 | + */ |
| 112 | +function callSample(): void |
| 113 | +{ |
| 114 | + $selfManagedSourceIpAddress = '[IP_ADDRESS]'; |
| 115 | + $selfManagedSourcePort = 0; |
| 116 | + $formattedSelfManagedSourceNetworkAttachment = MemorystoreClient::networkAttachmentName( |
| 117 | + '[PROJECT]', |
| 118 | + '[REGION]', |
| 119 | + '[NETWORK_ATTACHMENT]' |
| 120 | + ); |
| 121 | + $formattedName = MemorystoreClient::instanceName('[PROJECT]', '[LOCATION]', '[INSTANCE]'); |
| 122 | + |
| 123 | + start_migration_sample( |
| 124 | + $selfManagedSourceIpAddress, |
| 125 | + $selfManagedSourcePort, |
| 126 | + $formattedSelfManagedSourceNetworkAttachment, |
| 127 | + $formattedName |
| 128 | + ); |
| 129 | +} |
| 130 | +// [END memorystore_v1_generated_Memorystore_StartMigration_sync] |
0 commit comments