<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Cookbook Name:: f5-bigip
# Provider:: ltm_monitor
#
# Copyright:: 2014, Target Corporation
#
# 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.
#

class Chef
  class Provider
    #
    # Chef Provider for F5 LTM Monitor
    #
    class F5LtmMonitor < Chef::Provider
      include F5::Loader

      # Support whyrun
      def whyrun_supported?
        false
      end

      def load_current_resource
        @current_resource = Chef::Resource::F5LtmMonitor.new(@new_resource.name)
        @current_resource.name(@new_resource.name)
        @current_resource.monitor_name(@new_resource.monitor_name)

        load_balancer.change_folder(@new_resource.monitor_name) 
        monitor = load_balancer.ltm.monitors.find { |m| m.name =~ /(^|\/)#{@new_resource.monitor_name}$/ or m.name == @new_resource.monitor_name }
        @current_resource.exists = !monitor.nil?
        return @current_resource unless @current_resource.exists

        populate_current_resource(monitor)
        fix_parent_template_value

        @current_resource
      end

      def action_create # rubocop:disable CyclomaticComplexity
        create_template unless current_resource.exists

        set_template_destination if current_resource.dest_addr_type != new_resource.dest_addr_type
        set_template_destination if current_resource.dest_addr_ip != new_resource.dest_addr_ip
        set_template_destination if current_resource.dest_addr_port != new_resource.dest_addr_port

        set_template_interval if current_resource.interval != new_resource.interval
        set_template_description if current_resource.description != new_resource.description
        set_template_timeout if current_resource.timeout != new_resource.timeout

        set_template_parent if current_resource.parent != new_resource.parent

        set_user_values if new_resource.user_values != {} && current_resource.user_values != new_resource.user_values
      end

      def action_delete
        delete_template if current_resource.exists
      end

      private

      #
      # Populate current_resource with values gathered via F5 API
      #
      # @param [F5::Monitor] monitor
      #   f5 monitor object with data loaded from f5 API
      #
      def populate_current_resource(monitor)
        %w(parent description interval timeout dest_addr_type dest_addr_ip dest_addr_port user_values type).each do |item|
          current_resource.send(item, monitor.send(item))
        end
      end

      #
      # Create a new monitor from new_resource attribtues
      #
      def create_template
        converge_by("Create #{new_resource}") do
          Chef::Log.info "Create #{new_resource}"
          load_balancer.client['LocalLB.Monitor'].create_template(monitor_template, common_attributes)

          # Reload monitor info from F5
          load_balancer.ltm.monitors.refresh_all

          # Reload state of current_resource
          load_current_resource

          new_resource.updated_by_last_action(true)
        end
      end

      #
      # Set monitor destination address
      #
      def set_template_destination
        converge_by("Update #{new_resource} destination") do
          Chef::Log.info "Update #{new_resource} destination"
          Chef::Log.info "Monitor destination for #{new_resource} will fail if monitor is currently associated"
          load_balancer.client['LocalLB.Monitor'].set_template_destination([new_resource.monitor_name], [monitor_ip_port])
          update_current_resource(%w(dest_addr_type dest_addr_ip dest_addr_port))

          new_resource.updated_by_last_action(true)
        end
      end

      #
      # Set monitor description
      #
      def set_template_description
        converge_by("Update #{new_resource} description") do
          Chef::Log.info "Update #{new_resource} description"
          current_resource.description(new_resource.description)

          new_resource.updated_by_last_action(true)
        end
      end


      #
      # Set monitor interval
      #
      def set_template_interval
        converge_by("Update #{new_resource} interval") do
          Chef::Log.info "Update #{new_resource} interval"
          set_integer_for('ITYPE_INTERVAL', new_resource.interval)
          current_resource.interval(new_resource.interval)

          new_resource.updated_by_last_action(true)
        end
      end
<commit_msg>support for monitor description: missed action<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Cookbook Name:: f5-bigip
# Provider:: ltm_monitor
#
# Copyright:: 2014, Target Corporation
#
# 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.
#

class Chef
  class Provider
    #
    # Chef Provider for F5 LTM Monitor
    #
    class F5LtmMonitor < Chef::Provider
      include F5::Loader

      # Support whyrun
      def whyrun_supported?
        false
      end

      def load_current_resource
        @current_resource = Chef::Resource::F5LtmMonitor.new(@new_resource.name)
        @current_resource.name(@new_resource.name)
        @current_resource.monitor_name(@new_resource.monitor_name)

        load_balancer.change_folder(@new_resource.monitor_name) 
        monitor = load_balancer.ltm.monitors.find { |m| m.name =~ /(^|\/)#{@new_resource.monitor_name}$/ or m.name == @new_resource.monitor_name }
        @current_resource.exists = !monitor.nil?
        return @current_resource unless @current_resource.exists

        populate_current_resource(monitor)
        fix_parent_template_value

        @current_resource
      end

      def action_create # rubocop:disable CyclomaticComplexity
        create_template unless current_resource.exists

        set_template_destination if current_resource.dest_addr_type != new_resource.dest_addr_type
        set_template_destination if current_resource.dest_addr_ip != new_resource.dest_addr_ip
        set_template_destination if current_resource.dest_addr_port != new_resource.dest_addr_port

        set_template_interval if current_resource.interval != new_resource.interval
        set_template_description if current_resource.description != new_resource.description
        set_template_timeout if current_resource.timeout != new_resource.timeout

        set_template_parent if current_resource.parent != new_resource.parent

        set_user_values if new_resource.user_values != {} && current_resource.user_values != new_resource.user_values
      end

      def action_delete
        delete_template if current_resource.exists
      end

      private

      #
      # Populate current_resource with values gathered via F5 API
      #
      # @param [F5::Monitor] monitor
      #   f5 monitor object with data loaded from f5 API
      #
      def populate_current_resource(monitor)
        %w(parent description interval timeout dest_addr_type dest_addr_ip dest_addr_port user_values type).each do |item|
          current_resource.send(item, monitor.send(item))
        end
      end

      #
      # Create a new monitor from new_resource attribtues
      #
      def create_template
        converge_by("Create #{new_resource}") do
          Chef::Log.info "Create #{new_resource}"
          load_balancer.client['LocalLB.Monitor'].create_template(monitor_template, common_attributes)

          # Reload monitor info from F5
          load_balancer.ltm.monitors.refresh_all

          # Reload state of current_resource
          load_current_resource

          new_resource.updated_by_last_action(true)
        end
      end

      #
      # Set monitor destination address
      #
      def set_template_destination
        converge_by("Update #{new_resource} destination") do
          Chef::Log.info "Update #{new_resource} destination"
          Chef::Log.info "Monitor destination for #{new_resource} will fail if monitor is currently associated"
          load_balancer.client['LocalLB.Monitor'].set_template_destination([new_resource.monitor_name], [monitor_ip_port])
          update_current_resource(%w(dest_addr_type dest_addr_ip dest_addr_port))

          new_resource.updated_by_last_action(true)
        end
      end

      #
      # Set monitor description
      #
      def set_template_description
        converge_by("Update #{new_resource} description") do
          Chef::Log.info "Update #{new_resource} description"
          current_resource.description(new_resource.description)
          load_balancer.client['LocalLB.Monitor'].set_description([new_resource.monitor_name], [description])

          new_resource.updated_by_last_action(true)
        end
      end


      #
      # Set monitor interval
      #
      def set_template_interval
        converge_by("Update #{new_resource} interval") do
          Chef::Log.info "Update #{new_resource} interval"
          set_integer_for('ITYPE_INTERVAL', new_resource.interval)
          current_resource.interval(new_resource.interval)

          new_resource.updated_by_last_action(true)
        end
      end

{'fraction_non_alphanumeric': 0.07272727272727272, 'fraction_numerical': 0.003616273229532898, 'mean_word_length': 3.117452440033085, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '6519309', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2677, 'n_words': 653}