all files / mlpm/lib/ ml-deploy.js

100% Statements 24/24
100% Branches 8/8
100% Functions 2/2
100% Lines 23/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                                                                                                                                                                                                                                   
'use strict'
 
// var log = require('winston')
var fs = require('fs')
var request = require('request')
var url = require('url')
var api = require('./api.js')
 
function deployAsset(connection, deploy, cb) {
  var endpoint, contentType
 
  switch (deploy.type) {
    case 'resource':
      endpoint = '/v1/config/resources/' + deploy.name
      break
    case 'transform':
      endpoint = '/v1/config/transforms/' + deploy.name
      break
    default:
      // TODO: validate deploy.location
      endpoint = '/v1' + deploy.location
  }
 
  // if ( deploy.type === 'resource' ) {
  //   endpoint = '/v1/config/resources/' + deploy.name
  // } else if ( deploy.type === 'transform' ) {
  //   endpoint = '/v1/config/transforms/' + deploy.name
  // }
 
  switch (deploy.format) {
    case 'xqy':
      contentType = 'application/xquery'
      break
    case 'sjs':
      contentType = 'application/vnd.marklogic-javascript'
      break
    // TODO: binary?
    default:
      contentType = 'text/plain'
  }
 
  // if ( deploy.format === 'xqy' ) {
  //   contentType = 'application/xquery'
  // } else if ( deploy.format === 'sjs' ) {
  //   contentType = 'application/vnd.marklogic-javascript'
  // }
 
  var config = {
    headers: { 'content-type': contentType },
    url: url.format({
      protocol: 'http:',
      port: connection.port,
      hostname: connection.host,
      pathname: endpoint
    }),
    auth: {
      user: connection.username,
      pass: connection.password,
      sendImmediately: false
    },
    // ugh
    body: fs.readFileSync( deploy.path, { encoding: 'utf-8' } )
  }
 
  // TODO: beats the crap out of me ...
  // fs.createReadStream( deploy.path, { encoding: 'utf-8' } ).pipe(
 
    request.put(config, function(err, response, body) {
      if (err) return cb(err)
 
      api.responseStatus(response, body, cb)
    })
  // )
}
 
 
// function registerNamespaces(connection, locations, cb) {
//   return cb(new Error('not implemented'))
//   // TODO: validate locations
 
// TODO: fail early?
// detect v7: /v1/config/properties/error-format
 
//   var auth = {
//     user: connection.username,
//     pass: connection.password,
//     sendImmediately: false
//   }
//   function manageEndpoint(endpoint) {
//     return url.format({
//       protocol: 'http:',
//       port: 8002,
//       hostname: connection.host,
//       pathname: endpoint
//     })
//   }
 
//   request.get( manageEndpoint('/v1/rest-apis'), {
//       json: true,
//       auth: auth
//     },
//     function(err, response, body) {
//       var server = _.filter(body['rest-apis'], function(instance) {
//         return instance.port === String(connection.port)
//       })[0]
 
//       if (!server) return cb(new Error('no server for port ' + connection.port))
 
//       var config = _.pick(server, 'name', 'group')
//       var payloads = _.map(_.flatten(locations), function(location) {
//         return {
//           'module-location': {
//             'namespace-uri': location.ns,
//             location: location.location
//           }
//         }
//       })
 
//       request.get(manageEndpoint('/manage/v2/servers/' + config.name + '/properties'), {
//           json: true,
//           auth: auth,
//           qs: { 'group-id': config.group }
//         },
//         function(err, response, body) {
//           // if (err) return callback(err)
 
//           console.log( body )
//           console.log( response.statusCode )
//         })
 
//       // TODO: manage API bug ?
//       // only supports one payload; then overwrites ...
 
//       // async.each(payloads,
//       //   function(payload, callback) {
//       //     request.put(manageEndpoint('/manage/v2/servers/' + config.name + '/properties'), {
//       //         json: { 'module-locations': payload },
//       //         auth: auth,
//       //         qs: { 'group-id': config.group }
//       //       },
//       //       function(err, response, body) {
//       //         if (err) return callback(err)
 
//       //         console.log( body )
//       //         console.log( response.statusCode )
//       //         callback(null, response.statusCode)
//       //       })
 
//       //   }, function(err, results) {
//       //     if (err) return cb(err)
 
//       //     console.log(results)
//       //     // cb(null, results)
//       //   })
//     })
// }
 
module.exports = {
  deployAsset: deployAsset
}