Class: Ipecache::Plugins::AKAMAI

Inherits:
Plugin
  • Object
show all
Defined in:
lib/ipecache/plugins/akamai.rb

Instance Method Summary collapse

Methods inherited from Plugin

#continue_on_error, #enabled?, hook, hooks, #initialize, #log_file, name, #name, #plugin_puts, #plugin_puts_error, #quiet_mode, #urls

Constructor Details

This class inherits a constructor from Ipecache::Plugins::Plugin

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ipecache/plugins/akamai.rb', line 9

def perform
  safe_require 'json'
  safe_require 'akamai/edgegrid'
  safe_require 'net/http'
  safe_require 'uri'

  client_secret = config.client_secret
  host = config.host
  access_token = config.access_token
  client_token = config.client_token

  if client_secret.nil?
    plugin_puts("Akamai client_secret not specified, Exiting...")
    exit 1
  end

  if host.nil?
    plugin_puts("Akamai host not specified, Exiting...")
    exit 1
  end

  if access_token.nil?
    plugin_puts("Akamai access_token not specified, Exiting...")
    exit 1
  end

  if client_token.nil?
    plugin_puts("Akamai client_token not specified, Exiting...")
    exit 1
  end

  plugin_puts "Beginning URL Purge from Akamai..."

  baseuri = URI("https://#{host}/")

  http = Akamai::Edgegrid::HTTP.new(
    address=baseuri.host,
    port=baseuri.port
  )

  http.setup_edgegrid(
    :client_token => client_token,
    :client_secret => client_secret,
    :access_token => access_token,
    :max_body => 128 * 1024
  )

  urls.each do |u|
    url = u.chomp
    plugin_puts ("Purging #{url}")

    post_request = Net::HTTP::Post.new(
      URI.join(baseuri.to_s, "/ccu/v3/delete/url/production").to_s,
      initheader = { 'Content-Type' => 'application/json' }
    )

    post_request.body = {
      "objects" => ["#{url}"]
    }.to_json

    response = http.request(post_request)

    response_json = JSON.parse(response.body)
    response_httpStatus = response_json['httpStatus']

    if response_httpStatus != 201
      plugin_puts_error(url,"Purge failed!")
      plugin_puts response.body
      exit 1 unless continue_on_error
    else
      plugin_puts "Purge successful!"
    end
  end
end