<commit_before>package org.dasein.cloud.ci;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Nonnull;

import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.ci.Topology.VLANDevice;
import org.dasein.cloud.ci.Topology.VMDevice;
import org.dasein.cloud.ci.TopologyProvisionOptions.Disk;
import org.dasein.cloud.ci.TopologyProvisionOptions.Network;
import org.dasein.cloud.compute.Architecture;
import org.dasein.cloud.compute.VirtualMachineProduct;
import org.dasein.cloud.google.Google;
import org.dasein.cloud.google.GoogleException;
import org.dasein.cloud.google.GoogleMethod;
import org.dasein.cloud.google.GoogleOperationType;
import org.dasein.cloud.google.compute.server.ServerSupport;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.compute.Compute.InstanceTemplates;
import com.google.api.services.compute.model.AccessConfig;
import com.google.api.services.compute.model.AttachedDisk;
import com.google.api.services.compute.model.AttachedDiskInitializeParams;
import com.google.api.services.compute.model.InstanceProperties;
import com.google.api.services.compute.model.InstanceTemplate;
import com.google.api.services.compute.model.InstanceTemplateList;
import com.google.api.services.compute.model.Metadata;
import com.google.api.services.compute.model.Metadata.Items;
import com.google.api.services.compute.model.NetworkInterface;
import com.google.api.services.compute.model.Operation;
import com.google.api.services.compute.model.Scheduling;
import com.google.api.services.compute.model.Tags;

public class GoogleTopologySupport extends AbstractTopologySupport<Google> {
    private InstanceTemplates instanceTemplates = null;;

    public GoogleTopologySupport(Google provider) {
        super(provider);
        try {
            instanceTemplates = getProvider().getGoogleCompute().instanceTemplates();

        } catch ( CloudException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( InternalException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public String getProviderTermForTopology(Locale locale) {
        return "Instance Template";
    }

    @Override
    public boolean isSubscribed() throws CloudException, InternalException {
        return true;
    }

    @Override
    public Iterable<Topology> listTopologies(TopologyFilterOptions options) throws CloudException, InternalException {
        List<Topology> topologies = new ArrayList<Topology>();
        try {
            InstanceTemplateList templateList = instanceTemplates.list(getContext().getAccountNumber()).execute();
            for (InstanceTemplate template : templateList.getItems()) {
                InstanceProperties templateProperties = template.getProperties();
                VMDevice vmDevices = null;
                String machineType = templateProperties.getMachineType();
                ServerSupport server = new ServerSupport(getProvider());
                Iterable<VirtualMachineProduct> vmProducts = server.listProducts(Architecture.I64, "us-central1-f");
                for (VirtualMachineProduct vmProduct: vmProducts) {
                    if (vmProduct.getName().equals(machineType)) {
                        vmDevices = VMDevice.getInstance(machineType, machineType, vmProduct.getCpuCount(), vmProduct.getRamSize(), (String) null);
                    }
                }

                List<NetworkInterface> networkInterfaces = templateProperties.getNetworkInterfaces();
                String name = null;
                String deviceId = null;
                for (NetworkInterface networkInterface: networkInterfaces) {
                    deviceId = networkInterface.getNetwork();
                    name = deviceId.replaceAll(".*/", "");
                }

                Topology topology = Topology.getInstance(getContext().getAccountNumber(), null, template.getName(), TopologyState.ACTIVE, template.getName(), template.getDescription());

                if (null != vmDevices) {
                    topology = topology.withVirtualMachines(vmDevices);
                }

                if ((null != name) && (null != deviceId)) {
                    topology = topology.withVLANs(VLANDevice.getInstance(deviceId, name));
                }

                if ((null == options) || (options.matches(topology))) {
                    topologies.add(topology); 
                }
            }
        } catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return topologies;
    }

    @Override
    public boolean createTopology(@Nonnull TopologyProvisionOptions withTopologyOptions) throws CloudException, InternalException {
        InstanceTemplate newInstanceTemplate = new InstanceTemplate();

        newInstanceTemplate.setName(getCapabilities().getTopologyNamingConstraints().convertToValidName(withTopologyOptions.getProductName(), Locale.US));
        newInstanceTemplate.setDescription(withTopologyOptions.getProductDescription());
        InstanceProperties instanceProperties = new InstanceProperties();
        instanceProperties.setCanIpForward(withTopologyOptions.getCanIpForward());
        instanceProperties.setDescription(withTopologyOptions.getProductDescription());
        instanceProperties.setMachineType(withTopologyOptions.getMachineType());

        List<Disk> disks = withTopologyOptions.getDiskArray();
        List<AttachedDisk> attachedDisks = new ArrayList<AttachedDisk>();
        for (Disk topologyDisk : disks) {
            AttachedDisk disk =  new AttachedDisk();
            disk.setAutoDelete(topologyDisk.getAutoDelete());
            disk.setBoot(topologyDisk.getBootable());
            disk.setDeviceName(withTopologyOptions.getProductName());
            AttachedDiskInitializeParams attachedDiskInitializeParams = new AttachedDiskInitializeParams();
            attachedDiskInitializeParams.setSourceImage(topologyDisk.getDeviceSource());
            attachedDiskInitializeParams.setDiskType(topologyDisk.getDeviceType().toString());
            disk.setInitializeParams(attachedDiskInitializeParams);
            attachedDisks.add(disk);
        }
        instanceProperties.setDisks(attachedDisks);  //must provide at least one AttachedDisk definition.

        List<Network> topologyNetworksList = withTopologyOptions.getNetworkArray();
        List<NetworkInterface> networks = new ArrayList<NetworkInterface>();
        for (Network topologyNetwork : topologyNetworksList) {
            NetworkInterface networkInterface = new NetworkInterface();
            networkInterface.setName(topologyNetwork.getNetworkName());
            networkInterface.setNetwork(topologyNetwork.getNetworkSelfUrl());

            List<TopologyProvisionOptions.AccessConfig> topologyNetworksAccessConfig = topologyNetwork.getAccessConfig();
            List<AccessConfig> accessConfig = new ArrayList<AccessConfig>();

            for (TopologyProvisionOptions.AccessConfig topologyAccessConfig : topologyNetworksAccessConfig) {
                AccessConfig cfg = new AccessConfig();
                cfg.setName(topologyAccessConfig.getName());
                cfg.setKind(topologyAccessConfig.getKind());
                cfg.setType(topologyAccessConfig.getType());
                accessConfig.add(cfg);
            }
            networkInterface.setAccessConfigs(accessConfig);
            networks.add(networkInterface);
        }
        instanceProperties.setNetworkInterfaces(networks);

        Tags tags = new Tags();
        tags.setItems(withTopologyOptions.getTags());
        instanceProperties.setTags(tags);

        String[] sshKeys = withTopologyOptions.getSshKeys();
        Metadata metadata = new Metadata();
        List<Items> metadataItems = new ArrayList<Items>();
        for (String sshKey : sshKeys) {
            Metadata.Items  item = new Metadata.Items() ;
            item.setKey("sshKeys");
            item.setValue(sshKey);
            metadataItems.add(item);
        }

        Map<String, String> metaDataItems = withTopologyOptions.getMetadata();<commit_msg>factored some cloud specific bits from core into GCE<commit_after>package org.dasein.cloud.ci;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Nonnull;

import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.ci.Topology.VLANDevice;
import org.dasein.cloud.ci.Topology.VMDevice;
import org.dasein.cloud.ci.TopologyProvisionOptions.Disk;
import org.dasein.cloud.ci.TopologyProvisionOptions.Network;
import org.dasein.cloud.compute.Architecture;
import org.dasein.cloud.compute.VirtualMachineProduct;
import org.dasein.cloud.google.Google;
import org.dasein.cloud.google.GoogleException;
import org.dasein.cloud.google.GoogleMethod;
import org.dasein.cloud.google.GoogleOperationType;
import org.dasein.cloud.google.compute.server.ServerSupport;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.compute.Compute.InstanceTemplates;
import com.google.api.services.compute.model.AccessConfig;
import com.google.api.services.compute.model.AttachedDisk;
import com.google.api.services.compute.model.AttachedDiskInitializeParams;
import com.google.api.services.compute.model.InstanceProperties;
import com.google.api.services.compute.model.InstanceTemplate;
import com.google.api.services.compute.model.InstanceTemplateList;
import com.google.api.services.compute.model.Metadata;
import com.google.api.services.compute.model.Metadata.Items;
import com.google.api.services.compute.model.NetworkInterface;
import com.google.api.services.compute.model.Operation;
import com.google.api.services.compute.model.Scheduling;
import com.google.api.services.compute.model.Tags;

public class GoogleTopologySupport extends AbstractTopologySupport<Google> {
    private InstanceTemplates instanceTemplates = null;;

    public GoogleTopologySupport(Google provider) {
        super(provider);
        try {
            instanceTemplates = getProvider().getGoogleCompute().instanceTemplates();

        } catch ( CloudException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( InternalException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public String getProviderTermForTopology(Locale locale) {
        return "Instance Template";
    }

    @Override
    public boolean isSubscribed() throws CloudException, InternalException {
        return true;
    }

    @Override
    public Iterable<Topology> listTopologies(TopologyFilterOptions options) throws CloudException, InternalException {
        List<Topology> topologies = new ArrayList<Topology>();
        try {
            InstanceTemplateList templateList = instanceTemplates.list(getContext().getAccountNumber()).execute();
            for (InstanceTemplate template : templateList.getItems()) {
                InstanceProperties templateProperties = template.getProperties();
                VMDevice vmDevices = null;
                String machineType = templateProperties.getMachineType();
                ServerSupport server = new ServerSupport(getProvider());
                Iterable<VirtualMachineProduct> vmProducts = server.listProducts(Architecture.I64, "us-central1-f");
                for (VirtualMachineProduct vmProduct: vmProducts) {
                    if (vmProduct.getName().equals(machineType)) {
                        vmDevices = VMDevice.getInstance(machineType, machineType, vmProduct.getCpuCount(), vmProduct.getRamSize(), (String) null);
                    }
                }

                List<NetworkInterface> networkInterfaces = templateProperties.getNetworkInterfaces();
                String name = null;
                String deviceId = null;
                for (NetworkInterface networkInterface: networkInterfaces) {
                    deviceId = networkInterface.getNetwork();
                    name = deviceId.replaceAll(".*/", "");
                }

                Topology topology = Topology.getInstance(getContext().getAccountNumber(), null, template.getName(), TopologyState.ACTIVE, template.getName(), template.getDescription());

                if (null != vmDevices) {
                    topology = topology.withVirtualMachines(vmDevices);
                }

                if ((null != name) && (null != deviceId)) {
                    topology = topology.withVLANs(VLANDevice.getInstance(deviceId, name));
                }

                if ((null == options) || (options.matches(topology))) {
                    topologies.add(topology); 
                }
            }
        } catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return topologies;
    }

    @Override
    public boolean createTopology(@Nonnull TopologyProvisionOptions withTopologyOptions) throws CloudException, InternalException {
        InstanceTemplate newInstanceTemplate = new InstanceTemplate();

        newInstanceTemplate.setName(getCapabilities().getTopologyNamingConstraints().convertToValidName(withTopologyOptions.getProductName(), Locale.US));
        newInstanceTemplate.setDescription(withTopologyOptions.getProductDescription());
        InstanceProperties instanceProperties = new InstanceProperties();
        instanceProperties.setCanIpForward(withTopologyOptions.getCanIpForward());
        instanceProperties.setDescription(withTopologyOptions.getProductDescription());
        instanceProperties.setMachineType(withTopologyOptions.getMachineType());

        List<Disk> disks = withTopologyOptions.getDiskArray();
        List<AttachedDisk> attachedDisks = new ArrayList<AttachedDisk>();
        for (Disk topologyDisk : disks) {
            AttachedDisk disk =  new AttachedDisk();
            disk.setAutoDelete(topologyDisk.getAutoDelete());
            disk.setBoot(topologyDisk.getBootable());
            disk.setDeviceName(withTopologyOptions.getProductName());
            AttachedDiskInitializeParams attachedDiskInitializeParams = new AttachedDiskInitializeParams();
            attachedDiskInitializeParams.setSourceImage(topologyDisk.getDeviceSource());
            attachedDiskInitializeParams.setDiskType(topologyDisk.getDeviceType().toString());
            disk.setInitializeParams(attachedDiskInitializeParams);
            attachedDisks.add(disk);
        }
        instanceProperties.setDisks(attachedDisks);  //must provide at least one AttachedDisk definition.

        List<Network> topologyNetworksList = withTopologyOptions.getNetworkArray();
        List<NetworkInterface> networks = new ArrayList<NetworkInterface>();
        for (Network topologyNetwork : topologyNetworksList) {
            NetworkInterface networkInterface = new NetworkInterface();
            networkInterface.setName(topologyNetwork.getNetworkName());

            List<TopologyProvisionOptions.AccessConfig> topologyNetworksAccessConfig = topologyNetwork.getAccessConfig();
            List<AccessConfig> accessConfig = new ArrayList<AccessConfig>();

            for (TopologyProvisionOptions.AccessConfig topologyAccessConfig : topologyNetworksAccessConfig) {
                AccessConfig cfg = new AccessConfig();
                cfg.setName(topologyAccessConfig.getName());
                cfg.setKind("compute#accessConfig");
                cfg.setType(topologyAccessConfig.getType());
                accessConfig.add(cfg);
            }
            networkInterface.setAccessConfigs(accessConfig);
            networks.add(networkInterface);
        }
        instanceProperties.setNetworkInterfaces(networks);

        Tags tags = new Tags();
        tags.setItems(withTopologyOptions.getTags());
        instanceProperties.setTags(tags);

        String[] sshKeys = withTopologyOptions.getSshKeys();
        Metadata metadata = new Metadata();
        List<Items> metadataItems = new ArrayList<Items>();
        for (String sshKey : sshKeys) {
            Metadata.Items  item = new Metadata.Items() ;
            item.setKey("sshKeys");
            item.setValue(sshKey);
            metadataItems.add(item);
        }

        Map<String, String> metaDataItems = withTopologyOptions.getMetadata();
{'fraction_non_alphanumeric': 0.09401607621315868, 'fraction_numerical': 0.0003572491813039595, 'mean_word_length': 3.1379650160137964, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5281524', 'n_tokens_mistral': 4201, 'n_tokens_neox': 4084, 'n_words': 706}