sysdig 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3790850725c9f17424b02c5a0338ab5a9b8b757
4
- data.tar.gz: b2aded05c614ab191c4ce75632550e8550f6d172
3
+ metadata.gz: cec8df61179b00aff072de7f58a82be69c8c3d54
4
+ data.tar.gz: 8b2580b9f30040ce0802abbe7efcc4995c6221a7
5
5
  SHA512:
6
- metadata.gz: c95da8e9830383b4b3571724226f044f9e87827d455c2eece59a0ff31430de3bafad91647a8b4435320cbaf5cad7433e1b1ce8d08f856c0db7641783e5ad0bb0
7
- data.tar.gz: 1ea57891abfef711cc68979bc9061521e54afaec4d943984bef3ed6a93dec08edf1da6dc8b2fbddf279c80b18479d9957471b5873fe645c5f00dd8c44dd463ed
6
+ metadata.gz: f1d69d7e24782bea00e538acb84e8c1cfca952d23f833f67542712d295f49356e0c4286b20f80027122d5b52130e7438727e50bc6d625c19c4e25b7de5f3dce9
7
+ data.tar.gz: 0c6d24e5e651494f2837f4cda6b151225290d40182bab38aadd30854a6a79f230c994aca428ebb79f1ba9f510d9eb354dc497bffbfb14f4c15ff1d11698db5ac
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
+ spec/examples.txt
data/Gemfile CHANGED
@@ -1,3 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :test do
6
+ gem 'pry-nav', '~> 0.2'
7
+ gem 'faker'
8
+ end
data/lib/sysdig.rb CHANGED
@@ -11,8 +11,7 @@ require "ey/logger/faraday"
11
11
  class Sysdig
12
12
  include Cistern::Client
13
13
 
14
- recognizes :url, :adapter, :logger
15
- requires :username, :password
14
+ recognizes :url, :adapter, :logger, :token, :username, :password
16
15
 
17
16
  end
18
17
 
@@ -27,11 +26,20 @@ require 'sysdig/login'
27
26
  require 'sysdig/alert'
28
27
  require 'sysdig/alerts'
29
28
  require 'sysdig/create_alert'
29
+ require 'sysdig/destroy_alert'
30
30
  require 'sysdig/get_alert'
31
31
  require 'sysdig/get_alerts'
32
32
  require 'sysdig/update_alert'
33
33
 
34
- require 'sysdig/get_notification'
35
- require 'sysdig/get_notifications'
36
- require 'sysdig/notification'
37
- require 'sysdig/notifications'
34
+ require 'sysdig/get_alert_notification'
35
+ require 'sysdig/get_alert_notifications'
36
+ require 'sysdig/alert_notification'
37
+ require 'sysdig/alert_notifications'
38
+
39
+ require 'sysdig/get_user_notifications'
40
+ require 'sysdig/update_user_notifications'
41
+ require 'sysdig/user_notification'
42
+ require 'sysdig/user_notifications'
43
+ require 'sysdig/sns_notification'
44
+ require 'sysdig/email_notification'
45
+ require 'sysdig/pager_duty_notification'
data/lib/sysdig/alert.rb CHANGED
@@ -19,12 +19,12 @@ class Sysdig::Alert < Sysdig::Model
19
19
  attribute :description
20
20
  attribute :enabled, type: :boolean, default: true
21
21
  attribute :filter, parser: lambda { |v, _| load_filter(v) }
22
- attribute :group_aggregations, type: :array, alias: "groupAggregations"
23
- attribute :group_by, type: :array, alias: "groupBy"
22
+ attribute :group_aggregations, type: :array, alias: "groupAggregations"
23
+ attribute :group_by, type: :array, alias: "groupBy"
24
24
  attribute :group_condition, alias: "groupCondition"
25
25
  attribute :modified_at, alias: "modifiedOn", parser: method(:epoch_time)
26
26
  attribute :name
27
- attribute :notification_count, type: :integer, alias: "notificationCount"
27
+ attribute :notification_count, type: :integer, alias: "notificationCount"
28
28
  attribute :notify, parser: lambda { |v, _| Array(v).map { |x| x.upcase } }
29
29
  attribute :severity, type: :integer
30
30
  attribute :segment_by, alias: "segmentBy"
@@ -34,17 +34,28 @@ class Sysdig::Alert < Sysdig::Model
34
34
  attribute :type, parser: lambda { |v, _| v.to_s.upcase }
35
35
  attribute :version, type: :integer
36
36
 
37
+ def destroy
38
+ requires :identity
39
+
40
+ service.destroy_alert(identity)
41
+ end
42
+
37
43
  def save
38
44
  params = {
39
- "name" => self.name,
40
- "description" => self.description,
41
- "enabled" => self.enabled,
42
- "filter" => self.class.dump_filter(filter || {}),
43
- "type" => self.type,
44
- "condition" => self.condition,
45
- "timespan" => self.timespan,
46
- "severity" => self.severity,
47
- "notify" => self.notify,
45
+ "condition" => self.condition,
46
+ "description" => self.description,
47
+ "enabled" => self.enabled,
48
+ "filter" => self.class.dump_filter(filter || {}),
49
+ "groupAggregations" => self.group_aggregations,
50
+ "groupBy" => self.group_by,
51
+ "groupCondition" => self.group_condition,
52
+ "name" => self.name,
53
+ "notify" => self.notify,
54
+ "segmentBy" => self.segment_by,
55
+ "segmentCondition" => self.segment_condition,
56
+ "severity" => self.severity,
57
+ "timespan" => self.timespan,
58
+ "type" => self.type,
48
59
  }
49
60
 
50
61
  data = (
@@ -1,4 +1,4 @@
1
- class Sysdig::Notification < Sysdig::Model
1
+ class Sysdig::AlertNotification < Sysdig::Model
2
2
 
3
3
  identity :id, type: :integer
4
4
 
@@ -1,6 +1,6 @@
1
- class Sysdig::Notifications < Sysdig::Collection
1
+ class Sysdig::AlertNotifications < Sysdig::Collection
2
2
 
3
- model Sysdig::Notification
3
+ model Sysdig::AlertNotification
4
4
 
5
5
  def all(options={})
6
6
  to = options[:to] || Time.now
@@ -1,4 +1,8 @@
1
- class Sysdig::CreateAlerts < Sysdig::Request
1
+ class Sysdig::CreateAlert < Sysdig::Request
2
+ def self.params
3
+ %w[name description enabled filter type condition timespan severity notify segmentBy segmentCondition groupCondition groupBy groupAggregations]
4
+ end
5
+
2
6
  def real(alert)
3
7
  service.request(
4
8
  :method => :post,
@@ -6,4 +10,16 @@ class Sysdig::CreateAlerts < Sysdig::Request
6
10
  :body => { "alert" => alert },
7
11
  )
8
12
  end
13
+
14
+ def mock(alert)
15
+ alert_id = service.serial_id
16
+ body = Cistern::Hash.slice(Cistern::Hash.stringify_keys(alert), *self.class.params)
17
+
18
+ service.data[:alerts][alert_id] = body.merge!("id" => alert_id)
19
+
20
+ service.response(
21
+ :status => 201,
22
+ :body => {"alert" => body},
23
+ )
24
+ end
9
25
  end
@@ -0,0 +1,17 @@
1
+ class Sysdig::DestroyAlert < Sysdig::Request
2
+ def real(identity)
3
+ service.request(
4
+ :method => :delete,
5
+ :path => File.join("/api/alerts", identity.to_s)
6
+ )
7
+ end
8
+
9
+ def mock(identity)
10
+ service.data[:alerts].fetch(identity)
11
+ service.data[:alerts].delete(identity)
12
+
13
+ service.response(
14
+ :status => 204,
15
+ )
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ class Sysdig::EmailNotification < Sysdig::UserNotification
2
+
3
+ type :email
4
+
5
+ attribute :recipients, type: :array
6
+
7
+ def save
8
+ notification = service.update_user_notifications("email" => {
9
+ "enabled" => self.enabled,
10
+ "recipients" => self.receipents,
11
+ }).body.fetch("userNotification")
12
+
13
+ merge_attributes(notification.fetch("email"))
14
+ end
15
+ end
@@ -1,4 +1,4 @@
1
- class Sysdig::GetNotification < Sysdig::Request
1
+ class Sysdig::GetAlertNotification < Sysdig::Request
2
2
  def real(notification_id)
3
3
  service.request(
4
4
  :path => "/api/notifications/#{notification_id}",
@@ -1,4 +1,4 @@
1
- class Sysdig::GetNotifications < Sysdig::Request
1
+ class Sysdig::GetAlertNotifications < Sysdig::Request
2
2
  def real(from, to)
3
3
  from_i = from.to_i * 1_000_000
4
4
  to_i = to.to_i * 1_000_000
@@ -1,8 +1,27 @@
1
1
  class Sysdig::GetAlerts < Sysdig::Request
2
- def real(params={})
2
+ def real(from: nil, to: nil)
3
+ params = {}
4
+
5
+ if from
6
+ params.merge!(from.to_i * 1_000_000)
7
+ end
8
+
9
+ if to
10
+ params.merge!(to.to_i * 1_000_000)
11
+ end
12
+
3
13
  service.request(
4
14
  :method => :get,
5
15
  :path => "/api/alerts",
16
+ :params => params,
17
+ )
18
+ end
19
+
20
+ def mock(from: nil, to: nil)
21
+ alerts = service.data[:alerts].values
22
+
23
+ service.response(
24
+ :body => { "alerts" => alerts },
6
25
  )
7
26
  end
8
27
  end
@@ -0,0 +1,14 @@
1
+ class Sysdig::GetUserNotifications < Sysdig::Request
2
+ def real
3
+ service.request(
4
+ :method => :get,
5
+ :path => "/api/settings/notifications",
6
+ )
7
+ end
8
+
9
+ def mock
10
+ service.response(
11
+ :body => { "userNotification" => service.data[:user_notifications] }
12
+ )
13
+ end
14
+ end
data/lib/sysdig/mock.rb CHANGED
@@ -1,8 +1,30 @@
1
1
  class Sysdig::Mock
2
+ def self.reset!
3
+ super
4
+
5
+ @id = 0
6
+ end
7
+
2
8
  def self.data
3
9
  @@data ||= Hash.new { |h,url|
4
10
  h[url] = {
5
11
  :alerts => {},
12
+ :user_notifications => {
13
+ "email" => {
14
+ "enabled" => false,
15
+ "recipients" => []
16
+ },
17
+ "sns" => {
18
+ "enabled" => false,
19
+ "topics" => []
20
+ },
21
+ "pagerDuty" => {
22
+ "enabled" => false,
23
+ "integrated" => false,
24
+ "resolveOnOk" => false,
25
+ "connectUrl" => "https://connect.pagerduty.com/connect?vendor=x&callback=https://app.sysdigcloud.com/api/pagerDuty/callback/y/z"
26
+ }
27
+ }
6
28
  }
7
29
  }
8
30
  end
@@ -44,4 +66,9 @@ class Sysdig::Mock
44
66
  def data
45
67
  self.class.data[self.url]
46
68
  end
69
+
70
+ def serial_id
71
+ @id ||= 0
72
+ @id += 1
73
+ end
47
74
  end
@@ -0,0 +1,20 @@
1
+ class Sysdig::PagerDutyNotification < Sysdig::UserNotification
2
+
3
+ type :pagerDuty
4
+
5
+ attribute :enabled, type: :boolean
6
+ attribute :integrated, type: :boolean
7
+ attribute :bind_resolution, type: :boolean, alias: "resolveOnOk"
8
+ attribute :connect_url, alias: "connectUrl"
9
+
10
+ def save
11
+ notification = service.update_user_notifications("pagerDuty" => {
12
+ "enabled" => self.enabled,
13
+ "integrated" => self.integrated,
14
+ "resolveOnOk" => self.bind_resolution,
15
+ "connectUrl" => self.connect_url,
16
+ }).body.fetch("userNotification")
17
+
18
+ merge_attributes(notification.fetch("pagerDuty"))
19
+ end
20
+ end
data/lib/sysdig/real.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  class Sysdig::Real
2
- attr_reader :url, :path, :connection, :parser, :logger, :adapter, :authentication
2
+
3
+ attr_reader :url, :path, :connection, :parser, :logger, :adapter, :authentication, :token
3
4
 
4
5
  def initialize(options={})
5
6
  @url = URI.parse(options[:url] || "https://app.sysdigcloud.com")
@@ -8,6 +9,15 @@ class Sysdig::Real
8
9
  connection_options = options[:connection_options] || {}
9
10
  logger, @logger = options[:logger] || Logger.new(nil)
10
11
 
12
+ @username, @password = *options.values_at(:username, :password)
13
+
14
+ unless @username && @password
15
+ token = @token = options.fetch(:token)
16
+ end
17
+
18
+ @authentication = Mutex.new
19
+ @authenticated = false
20
+
11
21
  @connection = Faraday.new({url: @url}.merge(connection_options)) do |builder|
12
22
  # response
13
23
  builder.response :json
@@ -20,22 +30,22 @@ class Sysdig::Real
20
30
  :backoff_factor => 2
21
31
  builder.request :multipart
22
32
  builder.request :json
23
- builder.use :cookie_jar
33
+
34
+ if token
35
+ builder.authorization :Bearer, token
36
+ else
37
+ builder.use :cookie_jar
38
+ end
24
39
 
25
40
  builder.use Faraday::Response::RaiseError
26
41
  builder.use Ey::Logger::Faraday, format: :machine, device: logger
27
42
 
28
43
  builder.adapter(*adapter)
29
44
  end
30
-
31
- @username, @password = *options.values_at(:username, :password)
32
-
33
- @authentication = Mutex.new
34
- @authenticated = false
35
45
  end
36
46
 
37
47
  def request_with_authentication(options={})
38
- if !@authenticated
48
+ if !@authenticated && token.nil?
39
49
  @authentication.synchronize {
40
50
  if !@authenticated
41
51
  login(@username, @password)
@@ -0,0 +1,15 @@
1
+ class Sysdig::SnsNotification < Sysdig::UserNotification
2
+
3
+ type :sns
4
+
5
+ attribute :topics, type: :array
6
+
7
+ def save
8
+ notification = service.update_user_notifications(self.identity => {
9
+ "enabled" => self.enabled,
10
+ "topics" => self.topics,
11
+ }).body.fetch("userNotification")
12
+
13
+ merge_attributes(notification.fetch(self.identity))
14
+ end
15
+ end
@@ -1,7 +1,6 @@
1
1
  class Sysdig::UpdateAlert < Sysdig::Request
2
+ # @note alert[version] might mean lock version which should be fun
2
3
  def real(alert_id, alert)
3
- #{"alert":{"version":7,"createdOn":1438965688000,"modifiedOn":1442349562000,"name":"Memory usage critical","description":"Memory utilization is currently >= 98%","enabled":false,"filter":null,"type":"MANUAL","condition":"timeAvg(memory.used.percent) >= 98","timespan":60000000,"severity":2,"notify":["SNS"],"segmentBy":["agent.tag.id"],"segmentCondition":{"type":"ANY"}}}
4
- # @todo alert[version] might mean lock version which should be fun
5
4
  service.request(
6
5
  :method => :put,
7
6
  :path => File.join("/api/alerts", alert_id.to_s),
@@ -0,0 +1,31 @@
1
+ class Sysdig::UpdateUserNotifications < Sysdig::Request
2
+ def real(notification)
3
+ service.request(
4
+ :method => :put,
5
+ :path => "/api/settings/notifications",
6
+ :body => { "userNotification" => notification },
7
+ )
8
+ end
9
+
10
+ def mock(notification)
11
+ user_notification = Cistern::Hash.slice(Cistern::Hash.stringify_keys(notification), "sns", "email", "pagerDuty")
12
+
13
+ schema = {
14
+ "sns" => %w[enabled topics],
15
+ "email" => %w[enabled recipients],
16
+ "pagerDuty" => %w[enabled integrated resolveOnOk connectUrl],
17
+ }
18
+
19
+ sliced = schema.each_with_object({}) { |(type, keys), r|
20
+ u = user_notification[type]
21
+
22
+ if u
23
+ r.merge!(type => Cistern::Hash.slice(u, *keys))
24
+ end
25
+ }
26
+
27
+ service.response(
28
+ :body => { "userNotification" => service.data[:user_notifications].merge!(sliced) },
29
+ )
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ class Sysdig::UserNotification < Sysdig::Model
2
+
3
+ def self.types
4
+ @_types ||= Hash.new
5
+ end
6
+
7
+ def self.type(k=nil)
8
+ if k.nil?
9
+ @key
10
+ else
11
+ key = k.to_s
12
+ Sysdig::UserNotification.types[key] = self
13
+ @key = key
14
+ end
15
+ end
16
+
17
+ def self.inherited(klass)
18
+ klass.identity :category
19
+
20
+ super
21
+ end
22
+
23
+ attribute :enabled, type: :boolean
24
+
25
+ def initialize(attributes={})
26
+ self.identity = self.class.type
27
+
28
+ super
29
+ end
30
+
31
+ end
@@ -0,0 +1,31 @@
1
+ class Sysdig::UserNotifications < Sysdig::Collection
2
+
3
+ model Sysdig::UserNotification
4
+
5
+ def all(options={})
6
+ load(service.get_user_notifications.body.fetch("userNotification")).tap { |_| self.records.compact! }
7
+ end
8
+
9
+ def get(type)
10
+ all.find { |r| r.identity == type.to_s }
11
+ end
12
+
13
+ def new(args)
14
+ type, attributes = *args
15
+
16
+ unless attributes.is_a?(::Hash)
17
+ raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
18
+ end
19
+
20
+ concrete_model = model.types[type]
21
+
22
+ if concrete_model
23
+ concrete_model.new(
24
+ {
25
+ :collection => self,
26
+ :service => service,
27
+ }.merge(attributes)
28
+ )
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  class Sysdig
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sysdig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-20 00:00:00.000000000 Z
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,20 +143,28 @@ files:
143
143
  - bin/setup
144
144
  - lib/sysdig.rb
145
145
  - lib/sysdig/alert.rb
146
+ - lib/sysdig/alert_notification.rb
147
+ - lib/sysdig/alert_notifications.rb
146
148
  - lib/sysdig/alerts.rb
147
149
  - lib/sysdig/create_alert.rb
150
+ - lib/sysdig/destroy_alert.rb
151
+ - lib/sysdig/email_notification.rb
148
152
  - lib/sysdig/get_alert.rb
153
+ - lib/sysdig/get_alert_notification.rb
154
+ - lib/sysdig/get_alert_notifications.rb
149
155
  - lib/sysdig/get_alerts.rb
150
- - lib/sysdig/get_notification.rb
151
- - lib/sysdig/get_notifications.rb
156
+ - lib/sysdig/get_user_notifications.rb
152
157
  - lib/sysdig/login.rb
153
158
  - lib/sysdig/mock.rb
154
159
  - lib/sysdig/model.rb
155
- - lib/sysdig/notification.rb
156
- - lib/sysdig/notifications.rb
160
+ - lib/sysdig/pager_duty_notification.rb
157
161
  - lib/sysdig/real.rb
158
162
  - lib/sysdig/response.rb
163
+ - lib/sysdig/sns_notification.rb
159
164
  - lib/sysdig/update_alert.rb
165
+ - lib/sysdig/update_user_notifications.rb
166
+ - lib/sysdig/user_notification.rb
167
+ - lib/sysdig/user_notifications.rb
160
168
  - lib/sysdig/version.rb
161
169
  - sysdig.gemspec
162
170
  homepage: https://github.com/engineyard/sysdig-client