<commit_before>/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.proofpoint.cloudmanagement.service;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
import com.proofpoint.cloudmanagement.service.inventoryclient.InventoryClient;
import com.proofpoint.cloudmanagement.service.inventoryclient.InventorySystem;
import com.proofpoint.log.Logger;
import org.jclouds.Constants;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.openstack.nova.NovaClient;
import org.jclouds.openstack.nova.domain.Flavor;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class NovaInstanceConnector implements InstanceConnector
{
    private static final Logger log = Logger.get(NovaInstanceConnector.class);

    private final NovaClient novaClient;
    private static final String NOVA_PROVIDER_NAME = "nova";
    private final String defaultImageRef;
    private final InventoryClient inventoryClient;

    private final Cache<String, Instance> instanceCache;
    private final LoadingCache<String, Flavor> flavorCache;

    @Inject
    public NovaInstanceConnector(NovaConfig config, InventoryClient inventoryClient)
    {
        Properties overrides = new Properties();
        overrides.setProperty(Constants.PROPERTY_ENDPOINT, config.getLocation());

        this.inventoryClient = inventoryClient;

        ComputeServiceContext context = new ComputeServiceContextFactory().createContext(
                NOVA_PROVIDER_NAME,
                config.getUser(),
                config.getApiKey(),
                Collections.<Module>emptySet(), overrides);

        this.novaClient = (NovaClient) context.getProviderSpecificContext().getApi();

        Image defaultImage = novaClient.getImage(config.getDefaultImageId());
        Preconditions.checkNotNull(defaultImage, "No image found for default image id [" + config.getDefaultImageId() + "] please verify that this image exists");

        defaultImageRef = defaultImage.getSelfURI().toString();

        instanceCache = CacheBuilder.newBuilder()
                .expireAfterWrite(7, TimeUnit.DAYS)
                .build();

        flavorCache = CacheBuilder.newBuilder()
                .expireAfterWrite(1, TimeUnit.DAYS)
                .build(
                        new CacheLoader<String, Flavor>()
                        {

                            @Override
                            public Flavor load(String id)
                                    throws Exception
                            {
                                return novaClient.getFlavor(id);
                            }
                        }
                );
    }

    @PostConstruct
    public void prechargeCaches()
    {
        getSizes();
        getAllInstances();
    }

    public Instance createInstance(final String sizeName, String username)
    {
        Flavor flavor = getFlavorForSizeName(sizeName);
        Preconditions.checkNotNull(flavor, "No size found for name [" + sizeName + "] please verify that this is a valid size.");

        Server server = novaClient.createServer(username + "'s " + sizeName + " instance", defaultImageRef, flavor.getSelfURI().toString());

        try {
            String inventoryName = inventoryClient.getPcmSystemName(server.getUuid());
            InventorySystem inventorySystem = new InventorySystem(inventoryName);
            inventorySystem.setPicInstance(Integer.toString(server.getId()));
        }
        catch (Exception e) {
            log.error("Expected to get a server name from inventory for serverId [" + server.getUuid() + "] but caught exception " + e.getMessage(), e);
            throw new RuntimeException(e);<commit_msg>Put pic instance id in inventory<commit_after>/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.proofpoint.cloudmanagement.service;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
import com.proofpoint.cloudmanagement.service.inventoryclient.InventoryClient;
import com.proofpoint.cloudmanagement.service.inventoryclient.InventorySystem;
import com.proofpoint.log.Logger;
import org.jclouds.Constants;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.openstack.nova.NovaClient;
import org.jclouds.openstack.nova.domain.Flavor;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class NovaInstanceConnector implements InstanceConnector
{
    private static final Logger log = Logger.get(NovaInstanceConnector.class);

    private final NovaClient novaClient;
    private static final String NOVA_PROVIDER_NAME = "nova";
    private final String defaultImageRef;
    private final InventoryClient inventoryClient;

    private final Cache<String, Instance> instanceCache;
    private final LoadingCache<String, Flavor> flavorCache;

    @Inject
    public NovaInstanceConnector(NovaConfig config, InventoryClient inventoryClient)
    {
        Properties overrides = new Properties();
        overrides.setProperty(Constants.PROPERTY_ENDPOINT, config.getLocation());

        this.inventoryClient = inventoryClient;

        ComputeServiceContext context = new ComputeServiceContextFactory().createContext(
                NOVA_PROVIDER_NAME,
                config.getUser(),
                config.getApiKey(),
                Collections.<Module>emptySet(), overrides);

        this.novaClient = (NovaClient) context.getProviderSpecificContext().getApi();

        Image defaultImage = novaClient.getImage(config.getDefaultImageId());
        Preconditions.checkNotNull(defaultImage, "No image found for default image id [" + config.getDefaultImageId() + "] please verify that this image exists");

        defaultImageRef = defaultImage.getSelfURI().toString();

        instanceCache = CacheBuilder.newBuilder()
                .expireAfterWrite(7, TimeUnit.DAYS)
                .build();

        flavorCache = CacheBuilder.newBuilder()
                .expireAfterWrite(1, TimeUnit.DAYS)
                .build(
                        new CacheLoader<String, Flavor>()
                        {

                            @Override
                            public Flavor load(String id)
                                    throws Exception
                            {
                                return novaClient.getFlavor(id);
                            }
                        }
                );
    }

    @PostConstruct
    public void prechargeCaches()
    {
        getSizes();
        getAllInstances();
    }

    public Instance createInstance(final String sizeName, String username)
    {
        Flavor flavor = getFlavorForSizeName(sizeName);
        Preconditions.checkNotNull(flavor, "No size found for name [" + sizeName + "] please verify that this is a valid size.");

        Server server = novaClient.createServer(username + "'s " + sizeName + " instance", defaultImageRef, flavor.getSelfURI().toString());

        try {
            String inventoryName = inventoryClient.getPcmSystemName(server.getUuid());
            InventorySystem inventorySystem = new InventorySystem(inventoryName);
            inventorySystem.setPicInstance(Integer.toString(server.getId()));
            inventoryClient.patchSystem(inventorySystem);
        }
        catch (Exception e) {
            log.error("Expected to get a server name from inventory for serverId [" + server.getUuid() + "] but caught exception " + e.getMessage(), e);
            throw new RuntimeException(e);
{'fraction_non_alphanumeric': 0.08653175157013258, 'fraction_numerical': 0.0019938191606021336, 'mean_word_length': 3.0128, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1893948', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2528, 'n_words': 641}