 { _
-  daisy
+  async
   expect
   ObjectId
   withConvertedUser



    _createStack = (client, options, callback) ->
      stackData = { client, user, account, template : data.stackTemplate }
      group             = null
      stackTemplate     = null
      stackTemplateData = null

      _queue = [

        ->
          # adding group to stackData, will be needed while creating stack
          JGroup.one { slug : groupSlug }, (err, group_) ->
            expect(err?.message).to.not.exist
            stackData.group = group = group_
            _queue.next()

        ->
          _createStackTemplate client, options, (data_) ->
            stackData.template = stackTemplate = data_.stackTemplate
            stackTemplateData  = data_.stackTemplateData
            _queue.next()

        ->
          # create a stack from the given template
          ComputeProvider.generateStackFromTemplate stackData, options, (err, { stack }) ->
            expect(err?.message).to.not.exist
            callback { group, stack, stackTemplate, stackTemplateData }

      ]

      daisy _queue


    _createComputeProvider = (client, options, callback) ->
      computeProvider = null
      client.r        = { group : groupSlug, user, account }
      stack           = options.stack ? null
      label           = generateRandomString()
      provider        = 'aws'
      generatedFrom   = new ObjectId

      _queue = [

        ->
          return queue.next()  if stack
          _createStack client, options, (data_) ->
            { stack } = data_
            _queue.next()

        ->
          computeProviderOptions = {
            client, stack, provider, label, generatedFrom
            users : []
            provisioners : [data.provisioner ? 'devrim/koding-base']
          }

          ComputeProvider.create client, computeProviderOptions, (err, machine) ->
            expect(err?.message).to.not.exist
            callback { machine, stack }

      ]

      daisy _queue


    models.forEach (model) ->

        visibility     : 'visible'
        allowedDomains : ['koding.com']

      async.series [

        (next) ->
          JGroup.create client, groupData, account, (err, group_) ->
            group = group_
            next err

        (next) ->
          # if StackTemplate is in models, then create a StackTemplate
          # for the newly created group
          return next()  unless 'StackTemplate' in models
          _createStackTemplate client, options, ({ stackTemplate }) ->
            query = { $set : { stackTemplates : [stackTemplate._id] } }
            group.update query, next

      ], (err) ->
        expect(err).to.not.exist
        callback { group }


    _createCredential = (client, options, callback) ->
      createCredential client, options, (err, data_) ->
        expect(err).to.not.exist
        callback data_


    _createProvisioner = (client, options, callback) ->
      createProvisioner client, options, (err, data_) ->
        expect(err).to.not.exist
        callback data_


    _createStackTemplate = (client, options, callback) ->
      createStackTemplate client, options, (err, data_) ->
        expect(err).to.not.exist
        callback data_


