Skip to content
This repository has been archived by the owner on Mar 19, 2018. It is now read-only.

Add functionality #27

Merged
merged 13 commits into from
Dec 6, 2015
Merged
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Gemfile.lock
.buildpath
.project
.ruby-version
.bundle/
.yardoc/
94 changes: 94 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Offense count: 2
Lint/HandleExceptions:
Exclude:
- 'lib/mms/resource/group.rb'

# Offense count: 2
Lint/RescueException:
Exclude:
- 'lib/mms/cli.rb'

# Offense count: 84
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 159

# Offense count: 11
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 24

# Offense count: 13
Metrics/AbcSize:
Max: 51

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 120

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 9

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 9

# Offense count: 17
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/ClassAndModuleChildren:
Exclude:
- 'lib/mms/cli.rb'
- 'lib/mms/resource/alert.rb'
- 'lib/mms/resource/backup_config.rb'
- 'lib/mms/resource/cluster.rb'
- 'lib/mms/resource/group.rb'
- 'lib/mms/resource/host.rb'
- 'lib/mms/resource/metric.rb'
- 'lib/mms/resource/restore_job.rb'
- 'lib/mms/resource/snapshot.rb'
- 'lib/mms/resource/snapshot_schedule.rb'

# Offense count: 1
# Configuration parameters: Exclude.
Style/FileName:
Exclude:
- 'bin/mms-api'

# Offense count: 2
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/mms/cli.rb'
- 'lib/mms/resource.rb'

# Offense count: 5
# Cop supports --auto-correct.
Style/NumericLiterals:
MinDigits: 9

# Offense count: 5
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
Style/PredicateName:
Exclude:
- 'lib/mms/resource/backup_config.rb'
- 'lib/mms/resource/restore_job.rb'
- 'lib/mms/resource/snapshot.rb'

# Offense count: 3
# Configuration parameters: SupportedStyles.
Style/RaiseArgs:
EnforcedStyle: compact

# Offense count: 27
# Configuration parameters: Exclude.
Style/Documentation:
Enabled: false

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, Whitelist.
Style/TrivialAccessors:
Exclude:
- 'lib/mms/resource.rb'
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: ruby

sudo: false
rvm:
- 2.0.0
- 1.9.3
- 2.0.0
- 2.2.3
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The MMS Public API follows the principles of the REST architectural style to exp
|Resource |Get All |Get One |Create |Update |Delete |
|:--------------|:------:|:------:|:-----:|:-----:|:-----:|
|Groups | + | + | | | |
|Hosts | + | + | | | |
|Hosts | + | + | + | + |+ |
|Clusters | + | + | | | |
|Snapshots | + | + | | | |
|Alerts | + | + | | | |
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require 'bundler/gem_tasks'
Dir.glob(File.expand_path('../tasks/*.rake', __FILE__)).each do |task|
load task
end

task default: [:rubocop, :spec]
10 changes: 5 additions & 5 deletions lib/mms.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module MMS
require 'rubygems' # For ruby < 1.9
require 'rubygems' # For ruby < 1.9

require "uri"
require "json"
require "cgi"
require "net/http"
require 'uri'
require 'json'
require 'cgi'
require 'net/http'
require 'net/http/digest_auth'
require 'terminal-table'
require 'pathname'
Expand Down
85 changes: 74 additions & 11 deletions lib/mms/agent.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module MMS

class Agent

attr_accessor :client

# @param [MMS::Client] client
Expand All @@ -10,7 +8,7 @@ def initialize(client)
end

# @param [String] apiurl
def set_apiurl(apiurl)
def apiurl(apiurl)
@client.url = apiurl
end

Expand All @@ -19,8 +17,8 @@ def groups
group_list = []
client.get('/groups').each do |group|
g = MMS::Resource::Group.new
g.set_client(client)
g.set_data(group)
g.client(client)
g.data(group)

group_list.push g
end
Expand All @@ -36,6 +34,72 @@ def hosts
host_list
end

# @param [String] groupid
# @param [String] hostname
# @param [Integer] port
# @option options [String] username Required if authMechanismName is MONGODB_CR. Otherwise illegal.
# @option options [String] password Required if authMechanismName is MONGODB_CR. Otherwise illegal.
# @option options [TrueClass, FalseClass] sslEnabled Must be true if the authMechanismName is MONGODB_X509. Default is false if omitted.
# @option options [TrueClass, FalseClass] logsEnabled Default is false if omitted.
# @option options [TrueClass, FalseClass] alertsEnabled Default is true if omitted.
# @option options [TrueClass, FalseClass] profilerEnabled Default is false if omitted.
# @option options [Integer] muninPort Default is 0 and Munin stats are not collected if omitted.
# @option options [String] authMechanismName Default is NONE if omitted. If set to MONGODB_CR then you must provide the username and password.
# @return [<MMS::Resource::Host>]
def host_create(groupid, hostname, port, options = {})
data = {}
data[:hostname] = hostname
data[:port] = port
data[:username] = options[:username] || nil
data[:password] = options[:password] || nil
data[:sslEnabled] = options[:sslEnabled] || false
data[:logsEnabled] = options[:logsEnabled] || false
data[:alertsEnabled] = options[:alertsEnabled] || true
data[:profilerEnabled] = options[:profilerEnabled] || false
data[:muninPort] = options[:muninPort] || 0
data[:authMechanismName] = options[:authMechanismName] || nil
ret_host = client.post("/groups/#{groupid}/hosts", data)
host = MMS::Resource::Host.new
host._from_hash(ret_host)
host
end

# @param [String] groupid
# @param [String] hostid
# @option options [String] username Required if authMechanismName is MONGODB_CR. Otherwise illegal.
# @option options [String] password Required if authMechanismName is MONGODB_CR. Otherwise illegal.
# @option options [TrueClass, FalseClass] sslEnabled Must be true if the authMechanismName is MONGODB_X509. Default is false if omitted.
# @option options [TrueClass, FalseClass] logsEnabled Default is false if omitted.
# @option options [TrueClass, FalseClass] alertsEnabled Default is true if omitted.
# @option options [TrueClass, FalseClass] profilerEnabled Default is false if omitted.
# @option options [Integer] muninPort Default is 0 and Munin stats are not collected if omitted.
# @option options [String] authMechanismName Default is NONE if omitted. If set to MONGODB_CR then you must provide the username and password.
# @return [<MMS::Resource::Host>]
def host_update(groupid, hostid, options = {})
data = {}
data[:username] = options[:username] if options.include?(:username)
data[:password] = options[:password] if options.include?(:password)
data[:sslEnabled] = options[:sslEnabled] if options.include?(:sslEnabled)
data[:logsEnabled] = options[:logsEnabled] if options.include?(:logsEnabled)
data[:alertsEnabled] = options[:alertsEnabled] if options.include?(:alertsEnabled)
data[:profilerEnabled] = options[:profilerEnabled] if options.include?(:profilerEnabled)
data[:muninPort] = options[:muninPort] if options.include?(:muninPort)
data[:authMechanismName] = options[:authMechanismName] if options.include?(:authMechanismName)
ret_host = client.patch("/groups/#{groupid}/hosts/#{hostid}", data)
host = MMS::Resource::Host.new
host._from_hash(ret_host)
host
end

# @param [String] groupid
# @param [String] hostid
# @return [TrueClass, FalseClass]
def host_delete(groupid, hostid)
client.delete("/groups/#{groupid}/hosts/#{hostid}")
host = client.delete("/groups/#{groupid}/hosts/#{hostid}")
host == {} ? true : false
end

# @return [Array<MMS::Resource::Cluster>]
def clusters
cluster_list = []
Expand All @@ -51,7 +115,7 @@ def snapshots
clusters.each do |cluster|
snapshot_list.concat cluster.snapshots
end
snapshot_list.sort_by { |snapshot| snapshot.created_date }.reverse
snapshot_list.sort_by(&:created_date).reverse
end

# @return [Array<MMS::Resource::Alert>]
Expand All @@ -60,7 +124,7 @@ def alerts
groups.each do |group|
alert_list.concat group.alerts
end
alert_list.sort_by { |alert| alert.created }.reverse
alert_list.sort_by(&:created).reverse
end

# @return [Array<MMS::Resource::RestoreJob>]
Expand All @@ -69,7 +133,7 @@ def restorejobs
clusters.each do |cluster|
restorejob_list.concat cluster.restorejobs
end
restorejob_list.sort_by { |job| job.created }.reverse
restorejob_list.sort_by(&:created).reverse
end

# @param [String] type_value
Expand All @@ -79,8 +143,8 @@ def restorejobs
def restorejob_create(type_value, group_id, cluster_id)
if type_value.length == 24
find_group(group_id).cluster(cluster_id).snapshot(type_value).create_restorejob
elsif datetime = (type_value == 'now' ? DateTime.now : DateTime.parse(type_value))
raise('Invalid datetime. Correct `YYYY-MM-RRTH:m:sZ`') if datetime.nil?
elsif datetime == (type_value == 'now' ? DateTime.now : DateTime.parse(type_value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shortdudey123 I think this is a bug. I see that rubocop suggest to use == but the bug is there from the beginning:)! We should use else instead of elsif otherwise datetime will be lost...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay for fixing bugs :p

Something like this?

else
  datetime = (type_value == 'now' ? DateTime.now : DateTime.parse(type_value))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, exactly like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

fail('Invalid datetime. Correct `YYYY-MM-RRTH:m:sZ`') if datetime.nil?
datetime_string = [[datetime.year, datetime.month, datetime.day].join('-'), 'T', [datetime.hour, datetime.minute, datetime.second].join(':'), 'Z'].join
find_group(group_id).cluster(cluster_id).create_restorejob(datetime_string)
end
Expand Down Expand Up @@ -110,6 +174,5 @@ def alert_ack(alert_id, timestamp, group_id)
def find_group(id)
MMS::Resource::Group.find(@client, id)
end

end
end
9 changes: 2 additions & 7 deletions lib/mms/cache.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
require 'singleton'

module MMS

class Cache

include Singleton

attr_accessor :storage
attr_reader :storage

def initialize
@storage = Hash.new {|hash, key| hash[key] = nil }
@storage = Hash.new { |hash, key| hash[key] = nil }
end

# @param [String] key
Expand All @@ -32,9 +31,5 @@ def delete(key)
def clear
initialize
end

def storage
@storage
end
end
end
Loading