Class: Wdmc::Client

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wdmc/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/wdmc/client.rb', line 9

def initialize(*args)
  @config = Wdmc::Config.load
  @config[:verify_ssl] = (@config['validate_cert'].nil? or @config['validate_cert'] != false) ? true : false
  @config['api_net_nl_bug'] = @config['api_net_nl_bug'].nil? ? false : @config['api_net_nl_bug']
  @cookiefile = File.join(ENV['HOME'], '.wdmc_cookie')
  
end

Instance Method Details

#add_share(data) ⇒ Object

add new share



116
117
118
119
# File 'lib/wdmc/client.rb', line 116

def add_share( data )
  response = post("#{@config['url']}/api/2.1/rest/shares", data, {accept: :json, :cookies => cookies})
  return response.code
end

#add_user(data) ⇒ Object

add new user



197
198
199
200
# File 'lib/wdmc/client.rb', line 197

def add_user( data )
  response = post("#{@config['url']}/api/2.1/rest/users", data, {accept: :json, :cookies => cookies})
  return response.code
end

#all_sharesObject

working with shares get all shares



92
93
94
95
# File 'lib/wdmc/client.rb', line 92

def all_shares
  response = get("#{@config['url']}/api/2.1/rest/shares", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:shares][:share]
end

#all_usersObject

Users Get all users



173
174
175
176
# File 'lib/wdmc/client.rb', line 173

def all_users
  response = get("#{@config['url']}/api/2.1/rest/users", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:users][:user]
end

#cookiesObject



38
39
40
41
42
# File 'lib/wdmc/client.rb', line 38

def cookies
  file = File.read(@cookiefile)
  eval(file)
  #file = YAML.load_file(@cookiefile)
end

#delete_acl(data) ⇒ Object



150
151
152
153
154
155
# File 'lib/wdmc/client.rb', line 150

def delete_acl( data )
  # well, I know the code below is not very pretty...
  # if someone knows how this shitty delete with rest-client will work
  response = delete("#{@config['url']}/api/2.1/rest/share_access?share_name=#{data[:share_name]}&username=#{data[:username]}", {accept: :json, :cookies => cookies})
  return response
end

#delete_share(name) ⇒ Object

delete a share



128
129
130
131
# File 'lib/wdmc/client.rb', line 128

def delete_share( name )
  response = delete("#{@config['url']}/api/2.1/rest/shares/#{name}", {accept: :json, :cookies => cookies})
  return response.code
end

#delete_user(name) ⇒ Object

delete user



209
210
211
212
# File 'lib/wdmc/client.rb', line 209

def delete_user( name )
  response = delete("#{@config['url']}/api/2.1/rest/users/#{name}", {accept: :json, :cookies => cookies})
  return response.code
end

#device_descriptionObject



60
61
62
63
# File 'lib/wdmc/client.rb', line 60

def device_description
  response = get("#{@config['url']}/api/2.1/rest/device_description", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:device_description]
end

#find_share(name) ⇒ Object

find a share by name



98
99
100
101
102
103
104
# File 'lib/wdmc/client.rb', line 98

def find_share( name )
  result = []
  all_shares.each do |share|
    result.push share if share[:share_name] == name
  end
  return result
end

#find_user(name) ⇒ Object

find a user by name



179
180
181
182
183
184
185
# File 'lib/wdmc/client.rb', line 179

def find_user( name )
  result = []
  all_users.each do |user|
    result.push user if user[:username] == name
  end
  return result
end

#firmwareObject



55
56
57
58
# File 'lib/wdmc/client.rb', line 55

def firmware
  response = get("#{@config['url']}/api/2.1/rest/firmware_info", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:firmware_info]
end

#get_acl(name) ⇒ Object

working with ACL of a share get the specified share access



135
136
137
138
# File 'lib/wdmc/client.rb', line 135

def get_acl( name )
  response = get("#{@config['url']}/api/2.1/rest/share_access/#{name}", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:share_access_list]
end

#get_tmObject

TimeMachine Get TimeMachine Configuration



160
161
162
163
# File 'lib/wdmc/client.rb', line 160

def get_tm
  response = get("#{@config['url']}/api/2.1/rest/time_machine", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:time_machine]
end

#loginObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wdmc/client.rb', line 17

def 
  @url = @config['url']
  @username = @config['username']
  @password = @config['password']

  begin
    api = get("#{@url}/api/2.1/rest/local_login?username=#{@username}&password=#{@password}")
  rescue RestClient::SSLCertificateNotVerified => e
    if @config['validate_cert'] == 'warn'
      $stderr.puts("Warning: #{ e.class.name}: #{ e.message } for host URL: '#{ @url }'")
      @config[:verify_ssl] = false
      api = get("#{@url}/api/2.1/rest/local_login?username=#{@username}&password=#{@password}")
    else
      raise(e)
    end
  end

  cookie = api.cookies
  File.write(@cookiefile, api.cookies)
end

#modify_acl(data) ⇒ Object



145
146
147
148
# File 'lib/wdmc/client.rb', line 145

def modify_acl( data )
  response = put("#{@config['url']}/api/2.1/rest/share_access", data, {accept: :json, :cookies => cookies})
  return response.code
end

#modify_share(data) ⇒ Object

modifies a share



122
123
124
125
# File 'lib/wdmc/client.rb', line 122

def modify_share( data )
  response = put("#{@config['url']}/api/2.1/rest/shares", data, {accept: :json, :cookies => cookies})
  return response.code
end

#networkObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wdmc/client.rb', line 65

def network
  response = get("#{@config['url']}/api/2.1/rest/network_configuration", {accept: :json, :cookies => cookies})
  if @config['api_net_nl_bug']
    response = response.delete("\n")
  end
  begin
    JSON.parse(response, :symbolize_names => true)[:network_configuration]
  rescue JSON::ParserError => e
    unless @config['api_net_nl_bug']
      $stderr.puts("         Warning: Consider adding 'api_net_nl_bug: true' to your configuration file\n         to mitigate a known bug retrieving network configuration data.\n         EOL\n      )\n    end\n    raise e\n  end\nend\n".tr("\n", " ")

#set_acl(data) ⇒ Object



140
141
142
143
# File 'lib/wdmc/client.rb', line 140

def set_acl( data )
  response = post("#{@config['url']}/api/2.1/rest/share_access", data, {accept: :json, :cookies => cookies})
  return response.code
end

#set_tm(data) ⇒ Object

Set TimeMachine Configuration



166
167
168
169
# File 'lib/wdmc/client.rb', line 166

def set_tm( data )
  response = put("#{@config['url']}/api/2.1/rest/time_machine", data, {accept: :json, :cookies => cookies})
  return response
end

#share_exists?(name) ⇒ Boolean

check if share with exists

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/wdmc/client.rb', line 107

def share_exists?( name )
  result = []
  all_shares.each do |share|
    result.push share[:share_name] if share[:share_name].include?(name)
  end
  return result
end

#storage_usageObject

storage



85
86
87
88
# File 'lib/wdmc/client.rb', line 85

def storage_usage
  response = get("#{@config['url']}/api/2.1/rest/storage_usage", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:storage_usage]
end

#system_informationObject

device



45
46
47
48
# File 'lib/wdmc/client.rb', line 45

def system_information
  response = get("#{@config['url']}/api/2.1/rest/system_information", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:system_information]
end

#system_stateObject



50
51
52
53
# File 'lib/wdmc/client.rb', line 50

def system_state
  response = get("#{@config['url']}/api/2.1/rest/system_state", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:system_state]
end

#update_user(name, data) ⇒ Object

update an existing user



203
204
205
206
# File 'lib/wdmc/client.rb', line 203

def update_user( name, data )
  response = put("#{@config['url']}/api/2.1/rest/users/#{name}", data, {accept: :json, :cookies => cookies})
  return response.code
end

#user_exists?(name) ⇒ Boolean

check if user with name exists

Returns:

  • (Boolean)


188
189
190
191
192
193
194
# File 'lib/wdmc/client.rb', line 188

def user_exists?( name )
  result = []
  all_users.each do |user|
    result.push user[:username] if user[:username].include?(name)
  end
  return result
end

#volumesObject

Users



216
217
218
219
220
# File 'lib/wdmc/client.rb', line 216

def volumes
  
  response = get("#{@config['url']}/api/2.1/rest/volumes", {accept: :json, :cookies => cookies})
  JSON.parse(response, :symbolize_names => true)[:volumes][:volume]
end