Skip to content

Commit

Permalink
Julia/delete image (#29)
Browse files Browse the repository at this point in the history
* first fully functional version of delete through input bar AND right click

* refactored code to make it prettier!

* comments

* comments, added subscription function

* fixed to style guide

* style fixes, refactoring

* delete image after reviews

put my functions from azureCredentialsManager into two new files: utils/azure/acrTools.ts, and commands/utils/quick-pick-azure.ts

Edited code based on Esteban's and Bin's reviews

* One last little change to delete image

* moved repository, azureimage, and getsubscriptions to the correct places within deleteImage

* changes from PR reviews on delete image

* fixed authentication issue, got rid of azureAccount property for repository and image

**on constructor for repository, azurecredentialsmanager was being recreated and thus couldn't find the azureAccount. For this reason, I got rid of the azureAccount property of the classes Repository and AzureImage. This bug may lead to future problems (Esteban and I couldn't see why it was happening)

* minor fixes deleteImage

* delete a parentheses
  • Loading branch information
julialieberman authored Aug 4, 2018
1 parent 37d8a27 commit b80af87
Show file tree
Hide file tree
Showing 8 changed files with 482 additions and 11 deletions.
60 changes: 60 additions & 0 deletions commands/azureCommands/delete-azure-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Registry } from "azure-arm-containerregistry/lib/models";
import { SubscriptionModels } from 'azure-arm-resource';
import * as vscode from "vscode";
import { AzureImageNode } from '../../explorer/models/AzureRegistryNodes';
import { Repository } from "../../utils/Azure/models/repository";
import { AzureCredentialsManager } from '../../utils/azureCredentialsManager';
const teleCmdId: string = 'vscode-docker.deleteAzureImage';
import * as quickPicks from '../../commands/utils/quick-pick-azure';
import * as acrTools from '../../utils/Azure/acrTools';

/**
* function to delete an Azure repository and its associated images
* @param context : if called through right click on AzureRepositoryNode, the node object will be passed in. See azureRegistryNodes.ts for more info
*/
export async function deleteAzureImage(context?: AzureImageNode): Promise<void> {
if (!AzureCredentialsManager.getInstance().isLoggedIn()) {
vscode.window.showErrorMessage('You are not logged into Azure');
return;
}
let registry: Registry;
let subscription: SubscriptionModels.Subscription;
let repoName: string;
let username: string;
let password: string;
let tag: string;
if (!context) {
registry = await quickPicks.quickPickACRRegistry();
subscription = acrTools.getRegistrySubscription(registry);
let repository: Repository = await quickPicks.quickPickACRRepository(registry);
repoName = repository.name;
const image = await quickPicks.quickPickACRImage(repository);
tag = image.tag;
}

//ensure user truly wants to delete image
let opt: vscode.InputBoxOptions = {
ignoreFocusOut: true,
placeHolder: 'No',
value: 'No',
prompt: 'Are you sure you want to delete this image? Enter Yes to continue: '
};
let answer = await vscode.window.showInputBox(opt);
answer = answer.toLowerCase();
if (answer !== 'yes') { return; }

if (context) {
repoName = context.label;
subscription = context.subscription;
registry = context.registry;
let wholeName = repoName.split(':');
repoName = wholeName[0];
tag = wholeName[1];
}

let creds = await acrTools.loginCredentials(subscription, registry);
username = creds.username;
password = creds.password;
let path = `/v2/_acr/${repoName}/tags/${tag}`;
await acrTools.requestDataFromRegistry('delete', registry.loginServer, path, username, password); //official call to delete the image
}
58 changes: 58 additions & 0 deletions commands/utils/quick-pick-azure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ContainerRegistryManagementClient } from 'azure-arm-containerregistry';
import { Registry } from 'azure-arm-containerregistry/lib/models';
import * as vscode from "vscode";
import * as acrTools from '../../utils/Azure/acrTools';
import { AzureImage } from "../../utils/Azure/models/image";
import { Repository } from "../../utils/Azure/models/Repository";
import { AzureCredentialsManager } from '../../utils/azureCredentialsManager';

/**
* function to allow user to pick a desired image for use
* @param repository the repository to look in
* @returns an AzureImage object (see azureUtils.ts)
*/
export async function quickPickACRImage(repository: Repository): Promise<AzureImage> {
const repoImages: AzureImage[] = await acrTools.getAzureImages(repository);
let imageListNames: string[] = [];
for (let tempImage of repoImages) {
imageListNames.push(tempImage.tag);
}
let desiredImage = await vscode.window.showQuickPick(imageListNames, { 'canPickMany': false, 'placeHolder': 'Choose the image you want to delete' });
if (!desiredImage) { return; }
const image = repoImages.find((myImage): boolean => { return desiredImage === myImage.tag });
return image;
}

/**
* function to allow user to pick a desired repository for use
* @param registry the registry to choose a repository from
* @returns a Repository object (see azureUtils.ts)
*/
export async function quickPickACRRepository(registry: Registry): Promise<Repository> {
const myRepos: Repository[] = await acrTools.getAzureRepositories(registry);
let rep: string[] = [];
for (let repo of myRepos) {
rep.push(repo.name);
}
let desiredRepo = await vscode.window.showQuickPick(rep, { 'canPickMany': false, 'placeHolder': 'Choose the repository from which your desired image exists' });
if (!desiredRepo) { return; }
const repository = myRepos.find((currentRepo): boolean => { return desiredRepo === currentRepo.name });
return repository;
}

/**
* function to let user choose a registry for use
* @returns a Registry object
*/
export async function quickPickACRRegistry(): Promise<Registry> {
//first get desired registry
let registries = await AzureCredentialsManager.getInstance().getRegistries();
let reg: string[] = [];
for (let registryName of registries) {
reg.push(registryName.name);
}
let desired = await vscode.window.showQuickPick(reg, { 'canPickMany': false, 'placeHolder': 'Choose the Registry from which your desired image exists' });
if (!desired) { return; }
const registry = registries.find((currentReg): boolean => { return desired === currentReg.name });
return registry;
}
3 changes: 3 additions & 0 deletions dockerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as path from 'path';
import * as vscode from 'vscode';
import { AzureUserInput } from 'vscode-azureextensionui';
import { ConfigurationParams, DidChangeConfigurationNotification, DocumentSelector, LanguageClient, LanguageClientOptions, Middleware, ServerOptions, TransportKind } from 'vscode-languageclient';
import { deleteAzureImage } from './commands/azureCommands/delete-azure-image';
import { buildImage } from './commands/build-image';
import { composeDown, composeRestart, composeUp } from './commands/docker-compose';
import inspectImage from './commands/inspect-image';
Expand Down Expand Up @@ -117,7 +118,9 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.compose.down', composeDown));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.compose.restart', composeRestart));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.system.prune', systemPrune));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.deleteAzureImage', deleteAzureImage));
ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.createRegistry', createRegistry));

ctx.subscriptions.push(vscode.commands.registerCommand('vscode-docker.createWebApp', async (context?: AzureImageNode | DockerHubImageNode) => {
if (context) {
if (azureAccount) {
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"onCommand:vscode-docker.browseDockerHub",
"onCommand:vscode-docker.browseAzurePortal",
"onCommand:vscode-docker.explorer.refresh",
"onCommand:vscode-docker.deleteAzureImage",
"onView:dockerExplorer",
"onDebugInitialConfigurations"
],
Expand Down Expand Up @@ -256,6 +257,10 @@
"command": "vscode-docker.dockerHubLogout",
"when": "view == dockerExplorer && viewItem == dockerHubRootNode"
},
{
"command": "vscode-docker.deleteAzureImage",
"when": "view == dockerExplorer && viewItem == azureImageNode"
},
{
"command": "vscode-docker.browseDockerHub",
"when": "view == dockerExplorer && viewItem == dockerHubImageTag"
Expand Down Expand Up @@ -620,6 +625,11 @@
"command": "vscode-docker.browseAzurePortal",
"title": "Browse in the Azure Portal",
"category": "Docker"
},
{
"command": "vscode-docker.deleteAzureImage",
"title": "Delete Azure Image",
"category": "Docker"
}
],
"views": {
Expand Down
Loading

0 comments on commit b80af87

Please sign in to comment.