<commit_before>include CirconusApiMixin

def load_current_resource
  if @new_resource.current_resource_ref then
    return @new_resource.current_resource_ref
  end

  @current_resource = Chef::Resource::CirconusRuleSet.new(new_resource.name)
  @new_resource.current_resource_ref(@current_resource) # Needed for rules to link to 

  # Try to find metric among existing resources
  new_metric_resource = run_context.resource_collection.find(:circonus_metric => @new_resource.metric)

  unless new_metric_resource then
    raise Chef::Exceptions::ConfigurationError, "Circonus rule set #{@new_resource.name} references metric #{@new_resource.metric}, which must exist as a resource (it doesn't)."
  end

  # OK, set metric backlinks
  @new_resource.metric_resource(new_metric_resource)
  current_metric_resource = new_metric_resource.current_resource_ref
  @current_resource.metric_resource(current_metric_resource)

  # Copy non volatile fields in 
  @current_resource.broker(@new_resource.broker)
  @current_resource.metric(@new_resource.metric)
  
  # If we are in fact disabled, return now
  unless (node['circonus']['enabled']) then
    return @current_resource
  end

  # Try to figure out if ruleset exists, and if so, get my ID
  if @current_resource.metric_resource.exists then
    # The ID for a ruleset is the check_id + '_' + metric name
    @current_resource.check_id(api.find_check_id(@current_resource.metric_resource.check_bundle_resource.id, @current_resource.broker))
    ruleset_id = @current_resource.check_id + '_' + (@current_resource.metric_resource.metric_name || @current_resource.metric_resource.name)

    @current_resource.id(ruleset_id)

    # It's legit for the metric to exist, but not have a ruleset
    begin
      @current_resource.payload(api.get_rule_set(ruleset_id))
      @current_resource.exists(true)
    rescue RuntimeError => ex
      if ex.to_s.include?('Circonus API error - HTTP 404') then
        @current_resource.exists(false)
      else
        # somthing else broke, re-raise
        raise ex
      end
    end

  end

  # If the ruleset currently exists, then copy in to the new resource.
  if @current_resource.exists then
    # Deep clone
    @new_resource.payload(Marshal.load(Marshal.dump(@current_resource.payload)))
    @new_resource.id(@current_resource.id)
    @new_resource.exists(true)
  else 
    init_empty_payload
  end

  copy_resource_attributes_into_payload

  @current_resource
end

def init_empty_payload
  payload = {
    'contact_groups' => {
      '1' => [], '2' => [], '3' => [], '4' => [],  '5' => [], 
    },
    'rules' => [],
    'link' => '',
    'notes' => '',
  }
  @new_resource.payload(payload)
end

def copy_resource_attributes_into_payload

  # Wha...?
  init_empty_payload if @new_resource.payload.nil?

  p = @new_resource.payload<commit_msg>hrm, odd initialization bug in rulesets, hacking around it for now<commit_after>include CirconusApiMixin

def load_current_resource
  if @new_resource.current_resource_ref then
    return @new_resource.current_resource_ref
  end

  @current_resource = Chef::Resource::CirconusRuleSet.new(new_resource.name)
  @new_resource.current_resource_ref(@current_resource) # Needed for rules to link to 

  # Try to find metric among existing resources
  new_metric_resource = run_context.resource_collection.find(:circonus_metric => @new_resource.metric)

  unless new_metric_resource then
    raise Chef::Exceptions::ConfigurationError, "Circonus rule set #{@new_resource.name} references metric #{@new_resource.metric}, which must exist as a resource (it doesn't)."
  end

  # OK, set metric backlinks
  @new_resource.metric_resource(new_metric_resource)
  current_metric_resource = new_metric_resource.current_resource_ref
  @current_resource.metric_resource(current_metric_resource)

  # Copy non volatile fields in 
  @current_resource.broker(@new_resource.broker)
  @current_resource.metric(@new_resource.metric)
  
  # If we are in fact disabled, return now
  unless (node['circonus']['enabled']) then
    return @current_resource
  end

  # Try to figure out if ruleset exists, and if so, get my ID
  if @current_resource.metric_resource.exists then
    # The ID for a ruleset is the check_id + '_' + metric name
    @current_resource.check_id(api.find_check_id(@current_resource.metric_resource.check_bundle_resource.id, @current_resource.broker))
    ruleset_id = @current_resource.check_id + '_' + (@current_resource.metric_resource.metric_name || @current_resource.metric_resource.name)

    @current_resource.id(ruleset_id)

    # It's legit for the metric to exist, but not have a ruleset
    begin
      @current_resource.payload(api.get_rule_set(ruleset_id))
      @current_resource.exists(true)
    rescue RuntimeError => ex
      if ex.to_s.include?('Circonus API error - HTTP 404') then
        @current_resource.exists(false)
      else
        # somthing else broke, re-raise
        raise ex
      end
    end

  end

  # If the ruleset currently exists, then copy in to the new resource.
  if @current_resource.exists then
    # Deep clone
    @new_resource.payload(Marshal.load(Marshal.dump(@current_resource.payload)))
    @new_resource.id(@current_resource.id)
    @new_resource.exists(true)
  else 
    init_empty_payload
  end

  copy_resource_attributes_into_payload

  @current_resource
end

def init_empty_payload
  payload = {
    'contact_groups' => {
      '1' => [], '2' => [], '3' => [], '4' => [],  '5' => [], 
    },
    'rules' => [],
    'link' => '',
    'notes' => '',
    'derive' => nil,
  }
  @new_resource.payload(payload)
end

def copy_resource_attributes_into_payload

  # Wha...?
  init_empty_payload if @new_resource.payload.nil?

  p = @new_resource.payload
{'fraction_non_alphanumeric': 0.09975541579315164, 'fraction_numerical': 0.002795248078266946, 'mean_word_length': 4.2619485294117645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6097541', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1755, 'n_words': 403}