-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add restore from S3 url #25216
Merged
Merged
Add restore from S3 url #25216
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
59c6169
credential dialog
barbaravaldez ca5b985
add s3 support
barbaravaldez 34e7431
Update extensions/mssql/src/objectManagement/localizedConstants.ts
barbaravaldez 56dad78
remvoe url definition
barbaravaldez 9528bcc
add aws library
barbaravaldez 264458b
add select file dialog
barbaravaldez d172582
rename dialog
barbaravaldez 6e1a7ff
Merge branch 'main' into bavaldez/restoreS3Url
barbaravaldez 685a48d
pr comments
barbaravaldez 27a1726
fix endpoint name
barbaravaldez 548b3ae
reset url path
barbaravaldez 7d96516
encode uri
barbaravaldez 92f9945
pr comments
barbaravaldez 5fdad3c
remove unused dependencies
barbaravaldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
extensions/mssql/src/objectManagement/ui/S3AddBackupFileDialog.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as azdata from 'azdata'; | ||
import * as vscode from 'vscode'; | ||
import * as localizedConstants from '../localizedConstants'; | ||
import { DefaultInputWidth, DialogBase } from '../../ui/dialogBase'; | ||
import * as awsClient from '@aws-sdk/client-s3'; | ||
import { IObjectManagementService } from 'mssql'; | ||
|
||
export interface S3AddBackupFileDialogResult { | ||
s3Url: vscode.Uri; | ||
secretKey: string; | ||
accessKey: string; | ||
backupFilePath: string; | ||
} | ||
|
||
export class S3AddBackupFileDialog extends DialogBase<S3AddBackupFileDialogResult> { | ||
private s3UrlInputBox: azdata.InputBoxComponent; | ||
private secretKeyInputBox: azdata.InputBoxComponent; | ||
private accessKeyInputBox: azdata.InputBoxComponent; | ||
private credentialButton: azdata.ButtonComponent; | ||
private regionInputBox: azdata.InputBoxComponent; | ||
private bucketDropdown: azdata.DropDownComponent; | ||
private backupFilesDropdown: azdata.DropDownComponent; | ||
private result: S3AddBackupFileDialogResult; | ||
private objectManagementService: IObjectManagementService; | ||
private credentialInfo: azdata.CredentialInfo; | ||
private connectionUri: string; | ||
private s3Client: awsClient.S3Client; | ||
|
||
constructor(objectManagementService: IObjectManagementService, connectionUri: string) { | ||
super(localizedConstants.SelectS3BackupFileDialogTitle, localizedConstants.SelectS3BackupFileDialogTitle); | ||
this.result = { | ||
s3Url: undefined, | ||
secretKey: undefined, | ||
accessKey: undefined, | ||
backupFilePath: undefined | ||
}; | ||
|
||
// Relabel Cancel button to Back, since clicking cancel on an inner dialog makes it seem like it would close the whole dialog overall | ||
this.dialogObject.cancelButton.label = localizedConstants.BackButtonLabel; | ||
this.dialogObject.okButton.label = localizedConstants.AddButton; | ||
this.dialogObject.okButton.enabled = false; | ||
|
||
this.objectManagementService = objectManagementService; | ||
this.connectionUri = connectionUri; | ||
|
||
this.dialogObject.okButton.onClick(async () => { | ||
this.result.backupFilePath = `s3://${this.bucketDropdown.value}.s3.${this.regionInputBox.value}.amazonaws.com/${this.backupFilesDropdown.value}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do any of these values need to be escaped? |
||
this.credentialInfo = { | ||
secret: `${this.result.accessKey}:${this.result.secretKey}`, | ||
identity: 'S3 Access Key', | ||
name: this.result.backupFilePath, | ||
createDate: undefined, | ||
dateLastModified: undefined, | ||
providerName: 'MSSQL', | ||
id: undefined | ||
} | ||
await this.objectManagementService.createCredential(this.connectionUri, this.credentialInfo); | ||
}); | ||
} | ||
|
||
protected async initialize(): Promise<void> { | ||
this.s3UrlInputBox = this.createInputBox(async (value) => { | ||
this.result.s3Url = vscode.Uri.parse(value); | ||
this.regionInputBox.value = this.result.s3Url.toString().split(".")[1]; | ||
this.enableCredentialButton(); | ||
}, { | ||
ariaLabel: localizedConstants.RegionSpecificEndpointText, | ||
inputType: 'text', | ||
placeHolder: 'https://s3.{region}.amazonaws.com' | ||
}); | ||
const s3UrlContainer = this.createLabelInputContainer(localizedConstants.RegionSpecificEndpointText, this.s3UrlInputBox, true); | ||
|
||
this.secretKeyInputBox = this.createInputBox(async (value) => { | ||
this.result.secretKey = value; | ||
this.enableCredentialButton(); | ||
}, { | ||
ariaLabel: localizedConstants.SecretKeyText, | ||
inputType: 'password', | ||
}); | ||
const secretKeyContainer = this.createLabelInputContainer(localizedConstants.SecretKeyText, this.secretKeyInputBox, true); | ||
|
||
this.accessKeyInputBox = this.createInputBox(async (value) => { | ||
this.result.accessKey = value; | ||
this.enableCredentialButton(); | ||
}, { | ||
ariaLabel: localizedConstants.AccessKeyText, | ||
inputType: 'password', | ||
}); | ||
const accessKeyContainer = this.createLabelInputContainer(localizedConstants.AccessKeyText, this.accessKeyInputBox, true); | ||
|
||
this.credentialButton = this.createButton(localizedConstants.AddCredentialsText, localizedConstants.AddCredentialsText, async () => { | ||
this.createS3Client(); | ||
await this.setBucketDropdown(); | ||
}, false, DefaultInputWidth); | ||
const credentialButtonContainer = this.createLabelInputContainer(' ', this.credentialButton); | ||
|
||
this.regionInputBox = this.createInputBox(async () => { }, { | ||
ariaLabel: localizedConstants.RegionText, | ||
inputType: 'text', | ||
enabled: false | ||
}); | ||
const regionInputContainer = this.createLabelInputContainer(localizedConstants.RegionText, this.regionInputBox, true); | ||
|
||
this.bucketDropdown = this.createDropdown(localizedConstants.SelectS3BucketText, async (newValue) => { | ||
await this.setBackupFilesDropdown(newValue); | ||
}, [], '', false); | ||
const bucketContainer = this.createLabelInputContainer(localizedConstants.SelectS3BucketText, this.bucketDropdown, true); | ||
|
||
this.backupFilesDropdown = this.createDropdown(localizedConstants.SelectBackupFileText, async () => { | ||
// enable ok button once we have a backup file to restore | ||
this.dialogObject.okButton.enabled = true; | ||
}, [], '', false); | ||
const backupFilesContainer = this.createLabelInputContainer(localizedConstants.SelectBackupFileText, this.backupFilesDropdown, true); | ||
|
||
this.formContainer.addItems([s3UrlContainer, secretKeyContainer, accessKeyContainer, credentialButtonContainer, regionInputContainer, bucketContainer, backupFilesContainer]); | ||
} | ||
|
||
protected override async validateInput(): Promise<string[]> { | ||
const errors = await super.validateInput(); | ||
if (!this.result.s3Url.toString().includes('s3') && (this.result.s3Url.scheme !== 'https' || this.result.s3Url.scheme !== 'https')) { | ||
errors.push(localizedConstants.InvalidS3UrlError); | ||
} | ||
return errors; | ||
} | ||
|
||
private createS3Client(): void { | ||
this.s3Client = new awsClient.S3Client({ | ||
forcePathStyle: true, | ||
region: this.regionInputBox.value, | ||
endpoint: this.result.s3Url.toString(), | ||
credentials: { | ||
accessKeyId: this.dialogResult.accessKey, | ||
secretAccessKey: this.dialogResult.secretKey | ||
} | ||
}); | ||
} | ||
|
||
private async getBucketList(): Promise<string[]> { | ||
let command = new awsClient.ListBucketsCommand({}); | ||
let response = await this.s3Client.send(command); | ||
return response.Buckets.map(r => r.Name); | ||
} | ||
/** | ||
* Gets a list of all the backup files in S3 storage bucket | ||
*/ | ||
private async getBackupFiles(bucket: string): Promise<string[]> { | ||
const input: awsClient.ListObjectVersionsCommandInput = { | ||
Bucket: bucket | ||
}; | ||
let command = new awsClient.ListObjectsV2Command(input); | ||
let response = await this.s3Client.send(command); | ||
return response.Contents.filter(r => r.Key.endsWith('.bak')).map(r => r.Key); | ||
} | ||
|
||
private async setBackupFilesDropdown(bucket: string): Promise<void> { | ||
this.backupFilesDropdown.values = await this.getBackupFiles(bucket); | ||
this.backupFilesDropdown.enabled = true; | ||
} | ||
|
||
private async setBucketDropdown(): Promise<void> { | ||
this.bucketDropdown.values = await this.getBucketList(); | ||
this.bucketDropdown.enabled = true; | ||
} | ||
|
||
private enableCredentialButton(): void { | ||
this.credentialButton.enabled = (this.result.s3Url && this.result.accessKey && this.result.secretKey) !== undefined; | ||
} | ||
|
||
public override get dialogResult(): S3AddBackupFileDialogResult | undefined { | ||
return this.result; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a description here