<commit_before>
ProviderInterface = require './providerinterface'
KodingError       = require '../../error'
JVM               = require '../vm'

{argv}            = require 'optimist'
KONFIG            = require('koding-config-manager').load("main.#{argv.c}")

module.exports = class Koding extends ProviderInterface

  VMDefaultDiskSize = @VMDefaultDiskSize = 3072

  # This is currently using payment method
  # we will not use it any longer, for a reference keeping it
  @processNonce = (nonce, callback)->

    unless nonce
      return callback new KodingError
        message : "Payment requirements missing."

    JPaymentFulfillmentNonce  = require '../payment/nonce'
    JPaymentSubscription      = require '../payment/subscription'
    JPaymentPack              = require '../payment/pack'

    JPaymentFulfillmentNonce.one { nonce }, (err, nonceObject)->

      return callback err  if err

      unless nonceObject
        return callback message: "Unrecognized nonce!", nonce

      if nonceObject.action isnt "debit"
        return callback message: "Invalid nonce!", nonce

      { subscriptionCode } = nonceObject

      JPaymentSubscription.isFreeSubscripton subscriptionCode, \
      (err, isFreeSubscripton)=>
        return callback err if err

        nonceObject.update $set: action: "used", (err) =>
          return callback err  if err

          callback null, { isFreeSubscripton, nonceObject }


  @generateAliases = ({nickname, type, group})->

    domain = 'kd.io'
    type  ?= 'user'
    uid    = (require 'hat')(16)

    if type is 'user'

      aliases = ["vm-#{uid}.#{nickname}.#{group}.#{domain}"
                 "vm-#{uid}.#{nickname}.#{domain}"
                 "#{nickname}.#{group}.#{domain}"
                 "#{nickname}.#{domain}"]

    else if type is 'shared'

      aliases = ["shared-#{uid}.#{group}.#{domain}"
                 "shared.#{group}.#{domain}"
                 "#{group}.#{domain}"]

    return aliases ? []


  @ping = (client, options, callback)->

    callback null, "Koding is the best #{ client.r.account.profile.nickname }!"


  @create = (client, options, callback)->

    { r: { account, group } } = client
    { nickname } = account.profile

    group = group.slug

    hostnameAliases = @generateAliases { group, nickname }
    hostnameAlias   = hostnameAliases[0]
    region          = if argv.c is 'vagrant' then 'vagrant' else 'sj'
    { freeVM }      = KONFIG.defaultVMConfigs

    meta = {
      region
      hostnameAlias
      maxMemoryInMB : freeVM.ram ? 1024
      diskSizeInMB  : freeVM.storage ? VMDefaultDiskSize
      ldapPassword  : <PASSWORD>
      hostKite      : null
      alwaysOn      : no
      numCPUs       : freeVM.cpu ? 1
      webHome       : nickname
      vmType        : "user"
      ip            : null
      meta          : {}
    }

    callback null, {
      meta,
      postCreateOptions: {
        hostnameAliases, account, group
      }
    }


  @remove = (client, options, callback)->

    {hostnameAlias} = options
    JVM.removeByHostname client, hostnameAlias, callback


  @update = (client, options, callback)->

    callback new KodingError \
      "Update not supported for Koding VMs", "NotSupported"


  @fetchAvailable = (client, options, callback)->

    callback null, [
      {
        name  : "small"
        title : "Small 1x"
        spec  : {
          cpu : 1, ram: 1, storage: 4
        }
        price : 'free'
      }
      {
        name  : "large"
        title : "Large 2x"
        spec  : {
          cpu : 2, ram: 2, storage: 8
        }
      }
      {
        name  : "extra-large"
        title : "Extra Large 4x"
        spec  : {
          cpu : 4, ram: 4, storage: 16
        }
      }
    ]<commit_msg>Koding Provider: update provider for new kloud/koding requirements (aws)<commit_after>
ProviderInterface = require './providerinterface'
KodingError       = require '../../error'
JVM               = require '../vm'

{argv}            = require 'optimist'
KONFIG            = require('koding-config-manager').load("main.#{argv.c}")

module.exports = class Koding extends ProviderInterface

  VMDefaultDiskSize = @VMDefaultDiskSize = 3072

  # This is currently using payment method
  # we will not use it any longer, for a reference keeping it
  @processNonce = (nonce, callback)->

    unless nonce
      return callback new KodingError
        message : "Payment requirements missing."

    JPaymentFulfillmentNonce  = require '../payment/nonce'
    JPaymentSubscription      = require '../payment/subscription'
    JPaymentPack              = require '../payment/pack'

    JPaymentFulfillmentNonce.one { nonce }, (err, nonceObject)->

      return callback err  if err

      unless nonceObject
        return callback message: "Unrecognized nonce!", nonce

      if nonceObject.action isnt "debit"
        return callback message: "Invalid nonce!", nonce

      { subscriptionCode } = nonceObject

      JPaymentSubscription.isFreeSubscripton subscriptionCode, \
      (err, isFreeSubscripton)=>
        return callback err if err

        nonceObject.update $set: action: "used", (err) =>
          return callback err  if err

          callback null, { isFreeSubscripton, nonceObject }


  @generateAliases = ({nickname, type, group})->

    domain = 'kd.io'
    type  ?= 'user'
    uid    = (require 'hat')(16)

    if type is 'user'

      aliases = ["vm-#{uid}.#{nickname}.#{group}.#{domain}"
                 "vm-#{uid}.#{nickname}.#{domain}"
                 "#{nickname}.#{group}.#{domain}"
                 "#{nickname}.#{domain}"]

    else if type is 'shared'

      aliases = ["shared-#{uid}.#{group}.#{domain}"
                 "shared.#{group}.#{domain}"
                 "#{group}.#{domain}"]

    return aliases ? []


  @ping = (client, options, callback)->

    callback null, "Koding is the best #{ client.r.account.profile.nickname }!"

  @create = (client, options, callback)->

    { instanceType } = options

    # @fetchCredentialData credential, (err, cred)->
    #   return callback err  if err?

    meta =
      type          : "amazon"
      region        : "us-east-1"
      source_ami    : "ami-a6926dce"
      instance_type : instanceType

    callback null, { meta, credential: client.r.user.username }

  # @create = (client, options, callback)->

  #   { r: { account, group } } = client
  #   { nickname } = account.profile

  #   group = group.slug

  #   hostnameAliases = @generateAliases { group, nickname }
  #   hostnameAlias   = hostnameAliases[0]
  #   region          = if argv.c is 'vagrant' then 'vagrant' else 'sj'
  #   { freeVM }      = KONFIG.defaultVMConfigs

  #   meta = {
  #     region
  #     hostnameAlias
  #     maxMemoryInMB : freeVM.ram ? 1024
  #     diskSizeInMB  : freeVM.storage ? VMDefaultDiskSize
  #     ldapPassword  : <PASSWORD>
  #     hostKite      : null
  #     alwaysOn      : no
  #     numCPUs       : freeVM.cpu ? 1
  #     webHome       : nickname
  #     vmType        : "user"
  #     ip            : null
  #     meta          : {}
  #   }

  #   callback null, {
  #     meta,
  #     postCreateOptions: {
  #       hostnameAliases, account, group
  #     }
  #   }


  # @remove = (client, options, callback)->

  #   {hostnameAlias} = options
  #   JVM.removeByHostname client, hostnameAlias, callback


  # @update = (client, options, callback)->

  #   callback new KodingError \
  #     "Update not supported for Koding VMs", "NotSupported"


  @fetchAvailable = (client, options, callback)->

    callback null, [
      {
        name  : "t2.micro"
        title : "Small 1x"
        spec  : {
          cpu : 1, ram: 1, storage: 4
        }
        price : 'free'
      }
    ]
{'fraction_non_alphanumeric': 0.1211767755313634, 'fraction_numerical': 0.0060912389839294975, 'mean_word_length': 2.0659515295987285, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '5106654', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2307, 'n_words': 586}