<commit_before>#
# Author:: <NAME> <<EMAIL>>
# Cookbook Name:: loggly
# Library:: input
#
# Copyright 2011 Needle, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'net/http'
require 'json'

module Opscode
  module Loggly
    module Input

      # retrieve a list of inputs for the specified domain
      # returns JSON formatted data
      def get_inputs(domain)
        Chef::Log.debug("Loggly/#{domain}: Retreiving list of inputs...")
        begin
          http = Net::HTTP.new("#{domain}.loggly.com")
          request = Net::HTTP::Get.new("/api/inputs/")
          request.basic_auth node[:loggly][:username], node[:loggly][:password]
          response = http.request(request)
          Chef::Log.debug("Loggly/#{domain}: Recieved data on the following inputs:")
          Chef::Log.debug(response.body.inspect)
          return JSON.parse(response.body)
        rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ETIMEDOUT,
               Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, JSON::ParserError => e
          Chef::Log.error("Loggly/#{domain}: Error getting inputs from Loggly: #{e} ")
        end
      end

      # determine if a named input exists in a specified domain
      # returns true or nil
      def input_exists?(domain,input_name)
        answer = false
        get_inputs(domain).each do |input|
          if input["name"] == input_name
            answer = true
          end
        end
        return answer
      end

      # enables the discovery mode for an input
      def enable_input_discovery_mode(domain, input_name)
        Chef::Log.debug("Loggly/#{domain}: Attempting to enable discovery mode on input '#{input_name}'")
        if input_exists? domain, input_name
          begin
            http     = Net::HTTP.new "#{domain}.loggly.com"
            input_id = find_input_id domain, input_name
            request  = Net::HTTP::Post.new("/api/inputs/#{input_id)}")

            request.basic_auth node[:loggly][:username], node[:loggly][:password]
            response = http.request(request)
            Chef::Log.debug "Loggly/#{domain}: Received data on the following inputs:"
            Chef::Log.debug response.body.inspect
            true<commit_msg>remove unneeded parens and fix typo<commit_after>#
# Author:: <NAME> <<EMAIL>>
# Cookbook Name:: loggly
# Library:: input
#
# Copyright 2011 Needle, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'net/http'
require 'json'

module Opscode
  module Loggly
    module Input

      # retrieve a list of inputs for the specified domain
      # returns JSON formatted data
      def get_inputs(domain)
        Chef::Log.debug("Loggly/#{domain}: Retreiving list of inputs...")
        begin
          http = Net::HTTP.new("#{domain}.loggly.com")
          request = Net::HTTP::Get.new("/api/inputs/")
          request.basic_auth node[:loggly][:username], node[:loggly][:password]
          response = http.request(request)
          Chef::Log.debug("Loggly/#{domain}: Recieved data on the following inputs:")
          Chef::Log.debug(response.body.inspect)
          return JSON.parse(response.body)
        rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ETIMEDOUT,
               Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, JSON::ParserError => e
          Chef::Log.error("Loggly/#{domain}: Error getting inputs from Loggly: #{e} ")
        end
      end

      # determine if a named input exists in a specified domain
      # returns true or nil
      def input_exists?(domain,input_name)
        answer = false
        get_inputs(domain).each do |input|
          if input["name"] == input_name
            answer = true
          end
        end
        return answer
      end

      # enables the discovery mode for an input
      def enable_input_discovery_mode(domain, input_name)
        Chef::Log.debug "Loggly/#{domain}: Attempting to enable discovery mode on input '#{input_name}'"
        if input_exists? domain, input_name
          begin
            http     = Net::HTTP.new "#{domain}.loggly.com"
            input_id = find_input_id domain, input_name
            request  = Net::HTTP::Post.new "/api/inputs/#{input_id}"

            request.basic_auth node[:loggly][:username], node[:loggly][:password]
            response = http.request request
            Chef::Log.debug "Loggly/#{domain}: Received data on the following inputs:"
            Chef::Log.debug response.body.inspect
            true
{'fraction_non_alphanumeric': 0.10684684684684685, 'fraction_numerical': 0.002882882882882883, 'mean_word_length': 2.8151202749140896, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5799326', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1515, 'n_words': 489}