function createTemplate()

in Code.gs [308:349]


function createTemplate(host,index,template_name) {
  Logger.log(typeof host.use_ssl);
  var url = [(host.use_ssl) ? 'https://' : 'http://',
             host.host,':',host.port,
            '/_template/',template_name].join('')
  Logger.log(url);
  var options = getDefaultOptions(host.username,host.password);
  options['muteHttpExceptions'] = true;
  var resp = null
  try {
    var resp = UrlFetchApp.fetch(url, options);
  } catch(e) {
    throw "There was an issue creating the template. Please check the names of the template or index and try again."
  }
  if(resp.getResponseCode() == 404) {
    options = getDefaultOptions(host.username,host.password);
    options.method = 'POST';
    default_template.template = index;
    options['payload'] = JSON.stringify(default_template);
    options.headers["Content-Type"] = "application/json";
    options['muteHttpExceptions'] = true;
    resp = null;
    try {
      resp = UrlFetchApp.fetch(url, options);
    } catch(e) {
      throw "There was an issue creating the template. Please check the names of the template or index and try again."
    }
    if(resp.getResponseCode() != 200) {
      var jsonData = JSON.parse(resp.getContentText());
      throw jsonData.message;
    }
  } else if(resp.getResponseCode() == 200) {
    var jsonResp = JSON.parse(resp.getContentText());
    if(jsonResp[template_name].template) {
      var re = new RegExp(jsonResp[template_name].template);
      if(!re.test(index)) {
        throw "The template specified will only be applied to indices matching the following naming pattern: '"+jsonResp[template_name].template+
              "' Please update the template or choose a new name.";
      }
    }
  }
}