Skip to content
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

Julia/delete image #29

Merged
merged 14 commits into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registryRepos would be a more accurate name?

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' });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const when items are not changed

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' });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Const

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