/// <summary> /// Failover the instance to its failover replica instance. /// Documentation https://developers.google.com/sqladmin/v1beta4/reference/instances/failover /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated Sqladmin service.</param> /// <param name="project">ID of the project that contains the read replica.</param> /// <param name="instance">Cloud SQL instance ID. This does not include the project ID.</param> /// <param name="body">A valid Sqladmin v1beta4 body.</param> /// <returns>OperationResponse</returns> public static Operation Failover(SqladminService service, string project, string instance, InstancesFailoverRequest body) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (body == null) { throw new ArgumentNullException("body"); } if (project == null) { throw new ArgumentNullException(project); } if (instance == null) { throw new ArgumentNullException(instance); } // Make the request. return(service.Instances.Failover(body, project, instance).Execute()); } catch (Exception ex) { throw new Exception("Request Instances.Failover failed.", ex); } }
protected override void ProcessRecord() { string projectName; string instanceName; DatabaseInstance instanceObject; switch (ParameterSetName) { case ParameterSetNames.ByName: projectName = Project; instanceName = Instance; InstancesResource.GetRequest instanceGetRequest = Service.Instances.Get(projectName, instanceName); instanceObject = instanceGetRequest.Execute(); break; case ParameterSetNames.ByInstance: projectName = InstanceObject.Project; instanceName = InstanceObject.Name; SettingsVersion = InstanceObject.Settings.SettingsVersion; instanceObject = InstanceObject; break; default: throw UnknownParameterSetException; } InstancesFailoverRequest failoverRequestBody = new InstancesFailoverRequest { FailoverContext = new FailoverContext { SettingsVersion = SettingsVersion } }; InstancesResource.FailoverRequest failoverRequest = Service.Instances.Failover(failoverRequestBody, projectName, instanceName); Operation failoverResponse; WriteVerbose($"Activating the Failover for the Instance '{instanceName}'."); try { failoverResponse = failoverRequest.Execute(); } catch (GoogleApiException failoverEx) { if (failoverEx.Error.Code == InvalidSettingsVersionErrCode) { throw new GoogleApiException("Google Cloud SQL API", failoverEx.Message + InvalidSettingsVersionErrMsg); } throw failoverEx; } WaitForSqlOperation(failoverResponse); // Wait for recreate operation in failover replica. OperationsResource.ListRequest opListRequest = Service.Operations.List(projectName, instanceObject.FailoverReplica.Name); WriteVerbose("Waiting for the Failover to be re-created."); do { OperationsListResponse opListResponse = opListRequest.Execute(); if (opListResponse.Items == null) { return; } foreach (Operation operation in opListResponse.Items) { if (operation.OperationType == "RECREATE_REPLICA") { WaitForSqlOperation(operation); return; } } opListRequest.PageToken = opListResponse.NextPageToken; } while (opListRequest.PageToken != null); }