__cmdname__ = "delete_rules"

import json
import os
import sys

import circonusapi
import log
import util


class Module(object):
    def __init__(self, api, account):
        self.api = api
        self.account = account

    def command(self, opts, pattern):
        """Removes rules for checks that match the given pattern

        Arguments:
            pattern         -- regex to match check names on
        """
        checks, groups = util.find_checks(self.api, pattern)
        check_ids = dict([(c['check_id'], c['name']) for c in checks])
        rules = self.api.list_rules()
        matching = [r for r in rules if r['check_id'] in check_ids]
        matching = sorted(matching, reverse=True,
                key=lambda x: (x['check_id'], x['metric_name'], x['order']))
        log.msg("About to delete the following rules:")
        for r in matching:
            log.msg("%s`%s (%s - %s %s)" % (check_ids[r['check_id']],
                r['metric_name'], r['order'], r['criteria'], r['value']))
        if util.confirm():
            for r in matching:
                log.msgnb("Deleting %s`%s (%s)..." % (
                    check_ids[r['check_id']], r['metric_name'], r['order']))
                try:
                    rv = self.api.remove_metric_rule(check_id=r['check_id'],
                            metric_name=r['metric_name'], order=r['order'])
                    log.msgnf("Success")
                except circonusapi.CirconusAPIError, e:
                    log.msgnf("Failed")
                    log.error(e.error)
        else:
            log.msg("Not deleting rules")

@@ -45,16 +45,26 @@
 port os%0A
+import re%0A
 import s
@@ -254,23 +254,50 @@
 , opts, 
+check_
 pattern
+, metric_pattern=None
 ):%0A     
@@ -388,16 +388,22 @@
         
+check_
 pattern 
@@ -408,59 +408,402 @@
 n   
-      -- regex to match check names on%0A        %22%22%22%0A
+-- regex to match check names on (optional)%0A            metric_pattern  -- regex to match metric names on (optional)%0A%0A        At least one of check_pattern or metric_pattern must be provided. If%0A        you want to leave out the check_pattern, then specify it as an empty%0A        string.%0A        %22%22%22%0A        rules = self.api.list_rules()%0A%0A        if check_pattern and check_pattern != '.':%0A    
     
@@ -850,16 +850,22 @@
 lf.api, 
+check_
 pattern)
@@ -865,32 +865,36 @@
 attern)%0A        
+    
 check_ids = dict
@@ -940,36 +940,127 @@
 hecks%5D)%0A        
-rule
+    matching = %5Br for r in rules if r%5B'check_id'%5D in check_ids%5D%0A        else:%0A            check
 s = self.api.lis
@@ -1057,28 +1057,29 @@
 lf.api.list_
-rule
+check
 s()%0A        
@@ -1082,67 +1082,244 @@
     
-matching = %5Br
+    check_ids = dict(%5B(c%5B'check_id'%5D, c%5B'name'%5D)
  for 
-r
+c
  in 
-rules if r%5B'check_id'%5D in check_ids%5D
+checks%5D)%0A            matching = rules%0A%0A        if metric_pattern:%0A            matching = %5Br for r in matching if%0A                    re.search(metric_pattern, r%5B'metric_name'%5D)%5D%0A
 %0A   

Allow deleting rules based on metric names as well as check names