<commit_before>package org.dasein.cloud.azure.compute.image;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.dasein.cloud.AsynchronousTask;
import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.Requirement;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.Tag;
import org.dasein.cloud.azure.Azure;
import org.dasein.cloud.azure.AzureConfigException;
import org.dasein.cloud.azure.AzureMethod;
import org.dasein.cloud.azure.AzureService;
import org.dasein.cloud.azure.compute.vm.AzureVM;
import org.dasein.cloud.compute.AbstractImageSupport;
import org.dasein.cloud.compute.Architecture;
import org.dasein.cloud.compute.ImageClass;
import org.dasein.cloud.compute.ImageCreateOptions;
import org.dasein.cloud.compute.ImageFilterOptions;
import org.dasein.cloud.compute.MachineImage;
import org.dasein.cloud.compute.MachineImageFormat;
import org.dasein.cloud.compute.MachineImageState;
import org.dasein.cloud.compute.MachineImageSupport;
import org.dasein.cloud.compute.MachineImageType;
import org.dasein.cloud.compute.Platform;
import org.dasein.cloud.compute.VirtualMachine;
import org.dasein.cloud.compute.VmState;
import org.dasein.cloud.identity.ServiceAction;
import org.dasein.util.Jiterator;
import org.dasein.util.JiteratorPopulator;
import org.dasein.util.PopulatorThread;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;

/**
 * Implements support for Azure OS images through the Dasein Cloud machine image API.
 * @author <NAME> (<EMAIL>)
 * @since 2012.04.1
 * @version 2012.04.1
 */
public class AzureOSImage extends AbstractImageSupport {
    static private final Logger logger = Azure.getLogger(AzureOSImage.class);

    static private final String IMAGES = "/services/images";
    static private final String MICROSOFT = "--microsoft--";

    private Azure provider;
    
    public AzureOSImage(Azure provider) {
        super(provider);
        this.provider = provider;
    }

    @Override
    public void bundleVirtualMachineAsync(@Nonnull String virtualMachineId, @Nonnull MachineImageFormat format, @Nonnull String bucket, @Nonnull String name, @Nonnull AsynchronousTask<String> trackingTask) throws CloudException, InternalException {
        throw new OperationNotSupportedException("No ability to bundle vms");
    }

    @Override
    protected @Nonnull MachineImage capture(@Nonnull ImageCreateOptions options, @Nullable AsynchronousTask<MachineImage> task) throws CloudException, InternalException {
        try {
            if( task != null ) {
                task.setStartTime(System.currentTimeMillis());
            }

            String vmid = options.getVirtualMachineId();
            String name = options.getName();
            String description = options.getDescription();

            VirtualMachine vm;

            vm = provider.getComputeServices().getVirtualMachineSupport().getVirtualMachine(options.getVirtualMachineId());
            if (vm == null) {
                throw new CloudException("Virtual machine not found: " + options.getVirtualMachineId());
            }
            if (!vm.getCurrentState().equals(VmState.STOPPED)) {
                throw new CloudException("Server must be stopped before making an image of it");
            }
            try {
                ProviderContext ctx = provider.getContext();

                if( ctx == null ) {
                    throw new AzureConfigException("No context was set for this request");
                }

                String vmId = vm.getProviderVirtualMachineId();
                String[] parts = vmId.split(":");
                String serviceName, deploymentName, roleName;

                if (parts.length == 3)    {
                    serviceName = parts[0];
                    deploymentName = parts[1];
                    roleName= parts[2];
                }
                else if( parts.length == 2 ) {
                    serviceName = parts[0];
                    deploymentName = parts[1];
                    roleName = serviceName;
                }
                else {
                    serviceName = vmId;
                    deploymentName = vmId;
                    roleName = vmId;
                }
                String resourceDir = AzureVM.HOSTED_SERVICES + "/" + serviceName + "/deployments/" +  deploymentName + "/roleInstances/" + roleName + "/Operations";
                AzureMethod method = new AzureMethod(provider);
                StringBuilder xml = new StringBuilder();

                xml.append("<CaptureRoleOperation xmlns=\"http://schemas.microsoft.com/windowsazure\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">");
                xml.append("<OperationType>CaptureRoleOperation</OperationType>\n");
                xml.append("<PostCaptureAction>Delete</PostCaptureAction>\n");
                xml.append("<TargetImageLabel>").append(name).append("</TargetImageLabel>\n");
                xml.append("<TargetImageName>").append(name).append("</TargetImageName>\n");
                xml.append("</CaptureRoleOperation>\n");

                method.post(ctx.getAccountNumber(), resourceDir, xml.toString());

                MachineImage img = getMachineImage(name);;

                if (img == null) {
                    throw new CloudException("Drive cloning completed, but no ID was provided for clone");<commit_msg>use server tags for imaging rather than splitting id<commit_after>package org.dasein.cloud.azure.compute.image;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.dasein.cloud.AsynchronousTask;
import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.Requirement;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.Tag;
import org.dasein.cloud.azure.Azure;
import org.dasein.cloud.azure.AzureConfigException;
import org.dasein.cloud.azure.AzureMethod;
import org.dasein.cloud.azure.AzureService;
import org.dasein.cloud.azure.compute.vm.AzureVM;
import org.dasein.cloud.compute.AbstractImageSupport;
import org.dasein.cloud.compute.Architecture;
import org.dasein.cloud.compute.ImageClass;
import org.dasein.cloud.compute.ImageCreateOptions;
import org.dasein.cloud.compute.ImageFilterOptions;
import org.dasein.cloud.compute.MachineImage;
import org.dasein.cloud.compute.MachineImageFormat;
import org.dasein.cloud.compute.MachineImageState;
import org.dasein.cloud.compute.MachineImageSupport;
import org.dasein.cloud.compute.MachineImageType;
import org.dasein.cloud.compute.Platform;
import org.dasein.cloud.compute.VirtualMachine;
import org.dasein.cloud.compute.VmState;
import org.dasein.cloud.identity.ServiceAction;
import org.dasein.util.Jiterator;
import org.dasein.util.JiteratorPopulator;
import org.dasein.util.PopulatorThread;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;

/**
 * Implements support for Azure OS images through the Dasein Cloud machine image API.
 * @author <NAME> (<EMAIL>)
 * @since 2012.04.1
 * @version 2012.04.1
 */
public class AzureOSImage extends AbstractImageSupport {
    static private final Logger logger = Azure.getLogger(AzureOSImage.class);

    static private final String IMAGES = "/services/images";
    static private final String MICROSOFT = "--microsoft--";

    private Azure provider;
    
    public AzureOSImage(Azure provider) {
        super(provider);
        this.provider = provider;
    }

    @Override
    public void bundleVirtualMachineAsync(@Nonnull String virtualMachineId, @Nonnull MachineImageFormat format, @Nonnull String bucket, @Nonnull String name, @Nonnull AsynchronousTask<String> trackingTask) throws CloudException, InternalException {
        throw new OperationNotSupportedException("No ability to bundle vms");
    }

    @Override
    protected @Nonnull MachineImage capture(@Nonnull ImageCreateOptions options, @Nullable AsynchronousTask<MachineImage> task) throws CloudException, InternalException {
        try {
            if( task != null ) {
                task.setStartTime(System.currentTimeMillis());
            }

            String vmid = options.getVirtualMachineId();
            String name = options.getName();
            String description = options.getDescription();

            VirtualMachine vm;

            vm = provider.getComputeServices().getVirtualMachineSupport().getVirtualMachine(options.getVirtualMachineId());
            if (vm == null) {
                throw new CloudException("Virtual machine not found: " + options.getVirtualMachineId());
            }
            if (!vm.getCurrentState().equals(VmState.STOPPED)) {
                throw new CloudException("Server must be stopped before making an image of it");
            }
            try {
                ProviderContext ctx = provider.getContext();

                if( ctx == null ) {
                    throw new AzureConfigException("No context was set for this request");
                }

                String vmId = vm.getProviderVirtualMachineId();
                String serviceName, deploymentName, roleName;

                serviceName = vm.getTag("serviceName").toString();
                deploymentName = vm.getTag("deploymentName").toString();
                roleName = vm.getTag("roleName").toString();

                String resourceDir = AzureVM.HOSTED_SERVICES + "/" + serviceName + "/deployments/" +  deploymentName + "/roleInstances/" + roleName + "/Operations";
                AzureMethod method = new AzureMethod(provider);
                StringBuilder xml = new StringBuilder();

                xml.append("<CaptureRoleOperation xmlns=\"http://schemas.microsoft.com/windowsazure\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">");
                xml.append("<OperationType>CaptureRoleOperation</OperationType>\n");
                xml.append("<PostCaptureAction>Delete</PostCaptureAction>\n");
                xml.append("<TargetImageLabel>").append(name).append("</TargetImageLabel>\n");
                xml.append("<TargetImageName>").append(name).append("</TargetImageName>\n");
                xml.append("</CaptureRoleOperation>\n");

                method.post(ctx.getAccountNumber(), resourceDir, xml.toString());

                MachineImage img = getMachineImage(name);;

                if (img == null) {
                    throw new CloudException("Drive cloning completed, but no ID was provided for clone");
{'fraction_non_alphanumeric': 0.10567617054669108, 'fraction_numerical': 0.004969918911849333, 'mean_word_length': 3.0935046395431836, 'pattern_counts': {'":': 1, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7151434', 'n_tokens_mistral': 3131, 'n_tokens_neox': 3007, 'n_words': 638}