class Lita::Handlers::AwsCloudwatch

Public Instance Methods

list_accounts(response) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 23
def list_accounts(response)
  message = accounts.keys.map{ |key| "#{key}: #{accounts[key]} ( #{fetch_room(key)} )" }.join("\n")
  response.reply(message)
end
receive(request, response) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 29
def receive(request, response)
  @request = request
  case params_data["Type"]
  when "SubscriptionConfirmation"
    alert_confirmation!
  when "Notification"
    alert_notification!
  else
    alert_unknowtype!
  end
  response.write("ok")
end
set_account(response) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 7
def set_account(response)
  account_id = response.matches[0][0]
  account_name = response.matches[0][1]
  redis.hset(redis_key, account_id, account_name)
  response.reply("AWS account: #{account_id} has been set name: #{account_name}")
end
set_account_room(response) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 15
def set_account_room(response)
  account_id = response.matches[0][0]
  room_name = response.matches[0][1]
  redis.hset(redis_key_for_room, account_id, room_name)
  response.reply("AWS account: #{account_id} has set room to: #{room_name} , please invite robot to the room.")
end

Private Instance Methods

accounts() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 83
def accounts
  @accounts ||= redis.hgetall(redis_key)
  @accounts
end
alert_confirmation!() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 44
def alert_confirmation!
  message = params_data["Message"].gsub("SubscribeURL", params_data["SubscribeURL"])
  account_id = params_data["TopicArn"].split(":")[-2]
  room = fetch_room(account_id)
  send_message_to_room(message, room)
  unless accounts[account_id]
    send_message_to_room("Your AWS Account ID is `#{account_id}`, type `aws account set #{account_id} [account name]` for sett name, and type `aws account room #{account_id} [room name]` to set reporting room", room)
  end
end
alert_notification!() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 60
def alert_notification!
  data = JSON.parse(params_data["Message"])
  account_id = data["AWSAccountId"]
  name = data["AlarmName"]
  state = data["NewStateValue"]
  reason = data["NewStateReason"]
  messages = []
  messages << "Account: #{accounts[account_id] || account_id}"
  messages << "State: #{state}"
  messages << "Name: #{name}"
  messages << "Reason: #{reason}"
  room = fetch_room(account_id)
  send_message_to_room(messages.join("\n"), room)
end
alert_unknowtype!() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 54
def alert_unknowtype!
  room = config.default_room
  message = "unknow type: #{params_data["Type"].inspect} , send to robot.\n DEBUG: #{params_data.inspect}"
  send_message_to_room(message, room)
end
fetch_room(account_id) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 88
def fetch_room(account_id)
  @rooms ||= redis.hgetall(redis_key_for_room)
  @rooms[account_id] || config.default_room
end
find_target_by_name(room_name) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 104
def find_target_by_name(room_name)
  case robot.config.robot.adapter.to_s.to_sym
  when :slack
    room = ::Lita::Room.find_by_name(room_name)
    room_id = room ? room.id : ::Lita::Room.find_by_name("general").id
    Source.new(room: room_id)
  when :flowdock
    FlowdockSource.new(room: room_name)
  else
    room_name
  end
end
params_data() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 93
def params_data
  @data ||= JSON.parse(@request.body.string)
  @data
end
redis_key() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 79
def redis_key
  "aws-cloudwatch"
end
redis_key_for_room() click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 75
def redis_key_for_room
  "aws-cloudwatch-accounts-room"
end
send_message_to_room(message, room_name = nil) click to toggle source
# File lib/lita/handlers/aws_cloudwatch.rb, line 98
def send_message_to_room(message, room_name = nil)
  room_name ||= config.default_room
  target = find_target_by_name(room_name)
  robot.send_messages(target, message)
end