8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PagerDutyAdvent Calendar 2023

Day 10

PagerDuty API を試してデータを理解する

Last updated at Posted at 2023-12-09

タイトルの通りで,PagerDuty API を使ってみて,ドキュメントを読んでデータを理解する

前提 / 準備

  • Docker
    • Ruby
  • PagerDuty account / API Key
$ docker run -it --rm -v "$PWD":/usr/src/app -w /usr/src/app ruby:3.2.2-bookworm bundle init
$ edit Gemfile
$ docker run -it --rm -v "$PWD":/usr/src/app -w /usr/src/app ruby:3.2.2-bookworm bundle install 
$ edit compose.yml Dockerfile
compose.yml
version: '3'
services:
  app:
    build: .
    volumes:
      - .:/usr/src/app
Dockerfile
FROM ruby:3.2.2-bookworm

WORKDIR /usr/src/app

COPY Gemfile Gemfile.lock /usr/src/app/
RUN bundle install
Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

gem 'dotenv'
gem "pager_duty-connection"

API 使ってみる

$ edit .env a.rb
$ sed -e 's/=.*/=/' .env
PAGERDUTY_TOKEN=
a.rb
#!/usr/bin/env ruby

# https://github.com/technicalpickles/pager_duty-connection/blob/master/examples/find-users.rb

require 'dotenv/load'
require 'pager_duty/connection'

token = ENV['PAGERDUTY_TOKEN'] || raise("Missing ENV['PAGERDUTY_TOKEN']")

pagerduty = PagerDuty::Connection.new(token)

response = pagerduty.get('users')
response['users'].each do |user|
  puts "#{user['name']}: #{user['email']}"
end
$ docker compose run --rm app ./a.rb
(snip)

これで準備ができました.

ドキュメントを読む

データ

試してみる

なんとなくわかったので試してみる

  • 起点は EscaltionPolicy にあると考えると,
    • EscalationPolicy
      • User
      • Schedule

の辺りを確認できると静的な部分の全容把握がやりやすそう.

ソース
#!/usr/bin/env ruby

require 'dotenv/load'
require 'pager_duty/connection'

def token
  @token ||= ENV['PAGERDUTY_TOKEN'] || raise("Missing ENV['PAGERDUTY_TOKEN']")
end

def debug
  true
end

def pagerduty
  @pagerduty ||=  PagerDuty::Connection.new(token)
end

def get_list(url:, key: nil)
  key ||= url
  if debug
    puts "GET #{url}"
  end
  response = pagerduty.get(url)
  list = []
  response[key].each do |e|
    item = e
    if block_given?
      item = yield(e)
    end
    list << item
  end
  list
end

module BaseFromHash
  def self.included(klass)
    klass.extend ClassMethods
  end

  module ClassMethods
    def from_hash(hash)
      params = hash
      if block_given?
        params = yield(hash)
      end
      new(**params.slice(*members))
    end
  end
end

Team = Struct.new(:id, :name, keyword_init: true) do
  include BaseFromHash

  def to_s
    "Team: #{name}"
  end
end

User = Struct.new(:summary, keyword_init: true) do
  include BaseFromHash

  def to_s
    "User: #{summary}"
  end
end

EscalationPolicy = Struct.new(:id, :name, :escalation_rules, :teams, keyword_init: true) do
  include BaseFromHash

  def to_s
    "EscalationPolicy: #{name}"
  end
end

Schedule = Struct.new(:id, :summary, keyword_init: true) do
  include BaseFromHash

  def to_s
    "Schedule: #{summary}"
  end
end

def main
  escalation_policies = get_list(url: 'escalation_policies') do |escalation_policy|
    EscalationPolicy.from_hash(escalation_policy) do |policy|
      policy[:escalation_rules] = policy[:escalation_rules].map do |rule|
        {
          targets: rule['targets'].map do |target|
            if target['type'] == 'user_reference'
              User.from_hash(target)
            elsif target['type'] == 'schedule_reference'
              Schedule.from_hash(target)
            end
          end
        }
      end
      policy[:teams] = policy[:teams].map { |team| Team.from_hash(team) }
      policy
    end
  end
  escalation_policies.each do |escalation_policy|
    puts escalation_policy
    escalation_policy.teams.each do |team|
      puts team
    end
    escalation_policy.escalation_rules.each do |rule|
      rule[:targets].each do |target|
        puts target
      end
    end
  end
end

main

REST API

NOTES

  • rate limitation があるので呼び出し過ぎに注意.

Refs

→ こちらは Event API を呼ぶっぽい

8
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?