/// <summary> /// Imports data into a Cloud SQL instance from a MySQL dump file in Google Cloud Storage. /// Documentation https://developers.google.com/sqladmin/v1beta4/reference/instances/import /// 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">Project ID of the project that contains the instance.</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 Import(SqladminService service, string project, string instance, InstancesImportRequest 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.Import(body, project, instance).Execute()); } catch (Exception ex) { throw new Exception("Request Instances.Import failed.", ex); } }
protected override void ProcessRecord() { if (!ImportFilePath.StartsWith("gs://")) { if (ShouldProcess($"{Project}/{Instance}/{ImportFilePath}", "Create a new Google Cloud Storage bucket and upload the file to it for import.", "Will be deleted after the import completes")) { _tempUploader = new GcsFileUploader(GetBaseClientServiceInitializer(), Project); Random rnd = new Random(); int bucketRnd = rnd.Next(1000000); string bucketName = "import" + bucketRnd.ToString(); WriteVerbose($"Creating a Google Cloud Storage Bucket for the file at {ImportFilePath}."); _tempGcsBucket = _tempUploader.CreateBucket(bucketName); try { WriteVerbose($"Uploading the file at {ImportFilePath} to the new Google Cloud Storage Bucket."); _tempGcsObject = _tempUploader.UploadLocalFile(ImportFilePath, bucketName); } catch (Exception e) { _tempUploader.DeleteBucket(_tempGcsBucket); throw e; } DatabaseInstance myInstance = Service.Instances.Get(Project, Instance).Execute(); WriteVerbose("Updating the permissions for the uploaded file."); _tempUploader.AdjustAcl(_tempGcsObject, myInstance.ServiceAccountEmailAddress); ImportFilePath = string.Format("gs://{0}/{1}", bucketName, "toImport"); } else return; } InstancesImportRequest body = new InstancesImportRequest { ImportContext = new ImportContext { Kind = "sql#importContext", Uri = ImportFilePath, FileType = ParameterSetName.ToString(), Database = Database, } }; if (ParameterSetName == ParameterSetNames.Csv) { body.ImportContext.CsvImportOptions = new ImportContext.CsvImportOptionsData { Columns = Column, Table = DestinationTable }; } InstancesResource.ImportRequest request = Service.Instances.Import(body, Project, Instance); WriteVerbose($"Importing the file at '{ImportFilePath}' to Instance '{Instance}'."); Operation result = request.Execute(); result = WaitForSqlOperation(result); if (_tempUploader != null) { WriteVerbose("Deleting the Google Cloud Storage Bucket that was created, along with uploaded file."); _tempUploader.DeleteObject(_tempGcsObject); _tempUploader.DeleteBucket(_tempGcsBucket); } if (result.Error != null) { foreach (OperationError error in result.Error.Errors) { throw new GoogleApiException("Google Cloud SQL API", error.Message + error.Code); } } }