text: import logging
  import os, sys
  import splunk
  import splunk.entity
  import splunk.Intersplunk
  import json
  
  logger = logging.getLogger(__name__)
  
  splunkhome = os.environ['SPLUNK_HOME']
  sys.path.append(os.path.join(splunkhome, 'etc', 'apps', 'trackme', 'lib'))
  
  import rest_handler
  import splunklib.client as client
  
  class TrackMeHandlerTagPolicies_v1(rest_handler.RESTHandler):
      def __init__(self, command_line, command_arg):
          super(TrackMeHandlerTagPolicies_v1, self).__init__(command_line, command_arg, logger)
  
      # Get the entire collection as a Python array
      def get_tag_policies(self, request_info, **kwargs):
  
          describe = False
  
          # Retrieve from data
          try:
              resp_dict = json.loads(str(request_info.raw_args['payload']))
          except Exception as e:
              resp_dict = None
  
          if resp_dict is not None:
              try:
                  describe = resp_dict['describe']
                  if describe in ("true", "True"):
                      describe = True
              except Exception as e:
                  describe = False
  
          else:
              # body is not required in this endpoint, if not submitted do not describe the usage
              describe = False
  
          if describe:
  
              response = "{\"describe\": \"This endpoint retrieves the tag policies collection, it requires a GET call with no options required\"}"\
  
              return {
                  "payload": json.dumps(json.loads(str(response)), indent=1),
                  'status': 200 # HTTP status code
              }
  
          # Get splunkd port
          entity = splunk.entity.getEntity('/server', 'settings',
                                              namespace='trackme', sessionKey=request_info.session_key, owner='-')
          splunkd_port = entity['mgmtHostPort']
  
          try:
  
              collection_name = "kv_trackme_tags_policies"            
              service = client.connect(
                  owner="nobody",
                  app="trackme",
                  port=splunkd_port,
                  token=request_info.session_key
              )
              collection = service.kvstore[collection_name]
  
              # Render
              return {
                  "payload": json.dumps(collection.data.query(), indent=1),
                  'status': 200 # HTTP status code
              }
  
          except Exception as e:
              return {
                  'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
              }
  
  
      # Get model
      def get_tag_policies_by_id(self, request_info, **kwargs):
  
          # By id
          tags_policy_id = None
  
          # query_string to find records
          query_string = None
  
          describe = False
  
          # Retrieve from data
          try:
              resp_dict = json.loads(str(request_info.raw_args['payload']))
          except Exception as e:
              resp_dict = None
  
          if resp_dict is not None:
              try:
                  describe = resp_dict['describe']
                  if describe in ("true", "True"):
                      describe = True
              except Exception as e:
                  describe = False
              if not describe:
                  tags_policy_id = resp_dict['tags_policy_id']
  
          else:
              # body is required in this endpoint, if not submitted describe the usage
              describe = True
  
          if describe:
  
              response = "{\"describe\": \"This endpoint retrieves a tag policy by its id, it requires a GET call with the following data:\""\
                  + ", \"options\" : [ { "\
                  + "\"tags_policy_id\": \"ID of the tags policy\""\
                  + " } ] }"
  
              return {
                  "payload": json.dumps(json.loads(str(response)), indent=1),
                  'status': 200 # HTTP status code
              }
  
          # Define the KV query
          query_string = '{ "tags_policy_id": "' + tags_policy_id + '" }'
          
          # Get splunkd port
          entity = splunk.entity.getEntity('/server', 'settings',
                                              namespace='trackme', sessionKey=request_info.session_key, owner='-')
          splunkd_port = entity['mgmtHostPort']
  
          try:
  
              collection_name = "kv_trackme_tags_policies"            
              service = client.connect(
                  owner="nobody",
                  app="trackme",
                  port=splunkd_port,
                  token=request_info.session_key
              )
              collection = service.kvstore[collection_name]
  
              # Get the current record
              # Notes: the record is returned as an array, as we search for a specific record, we expect one record only
              
              try:
                  record = collection.data.query(query=str(query_string))
                  key = record[0].get('_key')
  
              except Exception as e:
                  key = None
  
              # Render result
              if key is not None and len(key)>2:
  
                  return {
                      "payload": json.dumps(collection.data.query_by_id(key), indent=1),
                      'status': 200 # HTTP status code
                  }
  
              else:
  
                  return {
                      "payload": 'Warn: resource not found ' + str(key),
                      'status': 404 # HTTP status code
                  }
  
          except Exception as e:
              return {
                  'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
              }
  
  
      # Add new policy
      def post_tag_policies_add(self, request_info, **kwargs):
  
          # Declare
          tags_policy_id = None
          tags_policy_value = None
          tags_policy_regex = None
  
          query_string = None
  
          describe = False
  
          # Retrieve from data
          try:
              resp_dict = json.loads(str(request_info.raw_args['payload']))
          except Exception as e:
              resp_dict = None
  
          if resp_dict is not None:
              try:
                  describe = resp_dict['describe']
                  if describe in ("true", "True"):
                      describe = True
              except Exception as e:
                  describe = False
              if not describe:
                  tags_policy_id = resp_dict['tags_policy_id']
                  tags_policy_value = resp_dict['tags_policy_value']
                  tags_policy_regex = resp_dict['tags_policy_regex']
  
          else:
              # body is required in this endpoint, if not submitted describe the usage
              describe = True
  
          if describe:
  
              response = "{\"describe\": \"This endpoint creates a new tag policy, it requires a POST call with the following data:\""\
                  + ", \"options\" : [ { "\
                  + "\"tags_policy_id\": \"ID of the tag policy\", "\
                  + "\"tags_policy_regex\": \"The regular expression to be used by the tags policy, special characters should be escaped.\", "\
                  + "\"tags_policy_value\": \"List of tags to be applied as a comma separated list of values\", "\
                  + "\"update_comment\": \"OPTIONAL: a comment for the update, comments are added to the audit record, if unset will be defined to: API update\""\
                  + " } ] }"
  
              return {
                  "payload": json.dumps(json.loads(str(response)), indent=1),
                  'status': 200 # HTTP status code
              }
  
          # Update comment is optional and used for audit changes
          try:
              update_comment = resp_dict['update_comment']
          except Exception as e:
              update_comment = "API update"
  
          # Define the KV query
          query_string = '{ "tags_policy_id": "' + tags_policy_id + '" }'
  
          # Get splunkd port
          entity = splunk.entity.getEntity('/server', 'settings',
                                              namespace='trackme', sessionKey=request_info.session_key, owner='-')
          splunkd_port = entity['mgmtHostPort']
  
          try:
  
              # Data collection
              collection_name = "kv_trackme_tags_policies"            
              service = client.connect(
                  owner="nobody",
                  app="trackme",
                  port=splunkd_port,
                  token=request_info.session_key
              )
              collection = service.kvstore[collection_name]
  
              # Audit collection
              collection_name_audit = "kv_trackme_audit_changes"            
              service_audit = client.connect(
                  owner="nobody",
                  app="trackme",
                  port=splunkd_port,
                  token=request_info.session_key
              )
              collection_audit = service_audit.kvstore[collection_name_audit]
  
              # Get the current record
              # Notes: the record is returned as an array, as we search for a specific record, we expect one record only
              
              try:
                  record = collection.data.query(query=str(query_string))
                  key = record[0].get('_key')
  
              except Exception as e:
                  key = None
                  
              # Render result
              if key is not None and len(key)>2:
  
                  # This record exists already
                  tags_policy_id = record[0].get('tags_policy_id')
  
                  # Record an audit change
                  import time
                  current_time = int(round(time.time() * 1000))
                  user = request_info.user
  
                  # Update the record
                  collection.data.update(str(key), json.dumps({"tags_policy_id": tags_policy_id, "tags_policy_value": tags_policy_value, "tags_policy_regex": tags_policy_regex, "mtime": current_time}))
  
                  # Store the record for audit purposes
                  record = str(json.dumps(collection.data.query_by_id(key), indent=1))
  
                  try:
  
                      # Insert the record
                      collection_audit.data.insert(json.dumps({    
                          "time": str(current_time),
                          "user": str(user),
                          "action": "success",
                          "change_type": "add tags policy",
                          "object": str(tags_policy_id),
                          "object_category": "data_source",
                          "object_attrs": str(record),
                          "result": "N/A",
                          "comment": str(update_comment)
                          }))
  
                  except Exception as e:
                      return {
                          'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
                      }
  
                  return {
                      "payload": str(record),
                      'status': 200 # HTTP status code
                  }
  
              else:
  
                  # This record does not exist yet
  
                  # Record an audit change
                  import time
                  current_time = int(round(time.time() * 1000))
                  user = request_info.user
  
                  # Insert the record
                  collection.data.insert(json.dumps({"tags_policy_id": tags_policy_id, "tags_policy_value": tags_policy_value, "tags_policy_regex": tags_policy_regex, "mtime": current_time}))
  
                  # Get record
                  record = json.dumps(collection.data.query(query=str(query_string)), indent=1)
  
                  try:
  
                      # Insert the record
                      collection_audit.data.insert(json.dumps({    
                          "time": str(current_time),
                          "user": str(user),
                          "action": "success",
                          "change_type": "add tags policy",
                          "object": str(tags_policy_id),
                          "object_category": "data_source",
                          "object_attrs": str(record),
                          "result": "N/A",
                          "comment": str(update_comment)
                          }))
  
                  except Exception as e:
                      return {
                          'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
                      }
  
                  return {
                      "payload": str(record),
                      'status': 200 # HTTP status code
                  }
  
          except Exception as e:
              return {
                  'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
              }
  
  
      # Delete a custom model
      def delete_tag_policies_del(self, request_info, **kwargs):
  
          # Declare
          tags_policy_id = None
          query_string = None
  
          describe = False
  
          # Retrieve from data
          try:
              resp_dict = json.loads(str(request_info.raw_args['payload']))
          except Exception as e:
              resp_dict = None
  
          if resp_dict is not None:
              try:
                  describe = resp_dict['describe']
                  if describe in ("true", "True"):
                      describe = True
              except Exception as e:
                  describe = False
              if not describe:
                  tags_policy_id = resp_dict['tags_policy_id']
  
          else:
              # body is required in this endpoint, if not submitted describe the usage
              describe = True
  
          if describe:
  
              response = "{\"describe\": \"This endpoint deletes a tag policy, it requires a DELETE call with the following data:\""\
                  + ", \"options\" : [ { "\
                  + "\"tags_policy_id\": \"ID of the tag policy\", "\
                  + "\"update_comment\": \"OPTIONAL: a comment for the update, comments are added to the audit record, if unset will be defined to: API update\""\
                  + " } ] }"
  
              return {
                  "payload": json.dumps(json.loads(str(response)), indent=1),
                  'status': 200 # HTTP status code
              }
  
          # Update comment is optional and used for audit changes
          try:
              update_comment = resp_dict['update_comment']
          except Exception as e:
              update_comment = "API update"
  
          # Define the KV query
          query_string = '{ "tags_policy_id": "' + tags_policy_id + '" }'
  
          # Get splunkd port
          entity = splunk.entity.getEntity('/server', 'settings',
                                              namespace='trackme', sessionKey=request_info.session_key, owner='-')
          splunkd_port = entity['mgmtHostPort']
  
          try:
  
              # Data collection
              collection_name = "kv_trackme_tags_policies"            
              service = client.connect(
                  owner="nobody",
                  app="trackme",
                  port=splunkd_port,
                  token=request_info.session_key
              )
              collection = service.kvstore[collection_name]
  
              # Audit collection
              collection_name_audit = "kv_trackme_audit_changes"            
              service_audit = client.connect(
                  owner="nobody",
                  app="trackme",
                  port=splunkd_port,
                  token=request_info.session_key
              )
              collection_audit = service_audit.kvstore[collection_name_audit]
  
              # Get the current record
              # Notes: the record is returned as an array, as we search for a specific record, we expect one record only
              
              try:
                  record = collection.data.query(query=str(query_string))
                  key = record[0].get('_key')
  
              except Exception as e:
                  key = None
                  
              # Render result
              if key is not None and len(key)>2:
  
                  # This record exists already
  
                  # Store the record for audit purposes
                  record = str(json.dumps(collection.data.query_by_id(key), indent=1))
  
                  # Record an audit change
                  import time
                  current_time = int(round(time.time() * 1000))
                  user = request_info.user
  
                  try:
  
                      # Remove the record
                      collection.data.delete(json.dumps({"_key":key}))
  
                      # Insert the record
                      collection_audit.data.insert(json.dumps({    
                          "time": str(current_time),
                          "user": str(user),
                          "action": "success",
                          "change_type": "delete tags policy",
                          "object": str(tags_policy_id),
                          "object_category": "data_source",
                          "object_attrs": str(record),
                          "result": "N/A",
                          "comment": str(update_comment)
                          }))
  
                  except Exception as e:
                      return {
                          'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
                      }
  
                  return {
                      "payload": "Record with _key " + str(key) + " was deleted from the collection.",
                      'status': 200 # HTTP status code
                  }
  
              else:
  
                  return {
                      "payload": 'Warn: resource not found ' + str(key),
                      'status': 404 # HTTP status code
                  }
  
          except Exception as e:
              return {
                  'payload': 'Warn: exception encountered: ' + str(e) # Payload of the request.
              }
  
  