<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InstanceExtensionMethods.cs" company="Naos">
//   Copyright 2015 Naos
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Naos.AWS.Core
{
    using System;
    using System.Linq;

    using Amazon;
    using Amazon.EC2.Model;

    using Naos.AWS.Contract;

    using Newtonsoft.Json;

    using Instance = Naos.AWS.Contract.Instance;
    using InstanceState = Naos.AWS.Contract.InstanceState;
    using UserData = Naos.AWS.Contract.UserData;

    /// <summary>
    /// Operations to be performed on instances.
    /// </summary>
    public static class InstanceExtensionMethods
    {
        /// <summary>
        /// Create a new Instance.
        /// </summary>
        /// <param name="instance">Instance to create.</param>
        /// <param name="userData">User data to use for instance creation.</param>
        /// <param name="credentials">Credentials to use (will use the credentials from CredentialManager.Cached if null...).</param>
        /// <returns>Updated copy of the provided object.</returns>
        public static Instance Create(this Instance instance, UserData userData, CredentialContainer credentials = null)
        {
            var localInstance = instance.DeepClone();
            var awsCredentials = CredentialManager.GetAwsCredentials(credentials);
            var regionEndpoint = RegionEndpoint.GetBySystemName(localInstance.Region);

            var placement = new Placement(localInstance.ContainingSubnet.AvailabilityZone);
            var amiId = localInstance.Ami.DiscoverId(credentials);
            var blockDeviceMappings = localInstance.MappedVolumes.ToAwsBlockDeviceMappings();
            var request = new RunInstancesRequest()
                              {
                                  BlockDeviceMappings = blockDeviceMappings,
                                  ClientToken = Guid.NewGuid().ToString().ToUpper(),
                                  DisableApiTermination = localInstance.DisableApiTermination,
                                  ImageId = amiId,
                                  InstanceType = localInstance.InstanceType,
                                  KeyName = localInstance.Key.KeyName,
                                  MinCount = 1,
                                  MaxCount = 1,
                                  Placement = placement,<commit_msg>Added AmiId assignment to return object to expose the ImageId chosen by search.<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InstanceExtensionMethods.cs" company="Naos">
//   Copyright 2015 Naos
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Naos.AWS.Core
{
    using System;
    using System.Linq;

    using Amazon;
    using Amazon.EC2.Model;

    using Naos.AWS.Contract;

    using Newtonsoft.Json;

    using Instance = Naos.AWS.Contract.Instance;
    using InstanceState = Naos.AWS.Contract.InstanceState;
    using UserData = Naos.AWS.Contract.UserData;

    /// <summary>
    /// Operations to be performed on instances.
    /// </summary>
    public static class InstanceExtensionMethods
    {
        /// <summary>
        /// Create a new Instance.
        /// </summary>
        /// <param name="instance">Instance to create.</param>
        /// <param name="userData">User data to use for instance creation.</param>
        /// <param name="credentials">Credentials to use (will use the credentials from CredentialManager.Cached if null...).</param>
        /// <returns>Updated copy of the provided object.</returns>
        public static Instance Create(this Instance instance, UserData userData, CredentialContainer credentials = null)
        {
            var localInstance = instance.DeepClone();
            var awsCredentials = CredentialManager.GetAwsCredentials(credentials);
            var regionEndpoint = RegionEndpoint.GetBySystemName(localInstance.Region);

            var placement = new Placement(localInstance.ContainingSubnet.AvailabilityZone);
            var amiId = localInstance.Ami.DiscoverId(credentials);
            localInstance.Ami.Id = amiId; // assign for returning to caller (gives visibility for potential debug needs)
            var blockDeviceMappings = localInstance.MappedVolumes.ToAwsBlockDeviceMappings();
            var request = new RunInstancesRequest()
                              {
                                  BlockDeviceMappings = blockDeviceMappings,
                                  ClientToken = Guid.NewGuid().ToString().ToUpper(),
                                  DisableApiTermination = localInstance.DisableApiTermination,
                                  ImageId = amiId,
                                  InstanceType = localInstance.InstanceType,
                                  KeyName = localInstance.Key.KeyName,
                                  MinCount = 1,
                                  MaxCount = 1,
                                  Placement = placement,
{'fraction_non_alphanumeric': 0.1683835924330399, 'fraction_numerical': 0.002622213897733658, 'mean_word_length': 1.487191429902189, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2126734', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1152, 'n_words': 286}