Class: Puppet::Provider::DirectAdmin

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/directadmin.rb

Overview

This class currently has some duplicate code in it, this needs cleaning up.

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.connect(username, password, hostname, port, ssl = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/provider/directadmin.rb', line 11

def self.connect(username, password, hostname, port, ssl = false)
  @auth_username = username
  @auth_password = password
  @http = Net::HTTP.new(hostname, port)

  if ssl == true
    @http.use_ssl = true
    if hostname == 'localhost'
      @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
end

.post(uri, *args) ⇒ Object

We need to allow post requests to the API for certain actions such as removing accounts.



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
# File 'lib/puppet/provider/directadmin.rb', line 25

def self.post(uri, *args)
  options = args.first || {}
  uri = "/#{uri}"
  resp = ""
  
  @http.start() { |http|
    req = Net::HTTP::Post.new(uri)
    req.set_form_data(options)
    req.basic_auth @auth_username, @auth_password
    response = http.request(req)
    # URI.unescape misses a lot, for some reason.
    resp = URI.unescape(response.body).gsub("&#45","-").gsub("&#64", "@").gsub("&#95", "_").gsub("&#58", ":")
  }
  
  # Create a hash of the response. We need some additional magic to make sure lists are parsed properly.
  resp_hash = {}
  response = resp.split('&')
  i = 0
  response.each do |var|
    key,value = var.split("=")
    if key == "list[]"
      key = i
      i += 1
    end
    resp_hash[key] = value
  end
    
  if resp_hash["error"] == "1"
    text = resp_hash["text"]
    details = resp_hash["details"]
    raise(Puppet::Error, "#{text}. #{details}.")
  elsif resp =~ /DirectAdmin\ Login/
    raise(Puppet::Error, "Login failed. The specified username and/or password is not correct.")
  elsif resp =~ /authority\ level/
    raise(Puppet::Error, "The request you've made cannot be executed because it does not exist in your authority level.")
  else
    return resp_hash
  end
end

.query(uri, *args) ⇒ Object

In most cases we can send a GET request to DirectAdmin to do what we want.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet/provider/directadmin.rb', line 66

def self.query(uri, *args)
  options = args.first || {}
  params = ""
  options.each do |o|
    params << "#{o[0]}=#{o[1]}&"
  end
  uri = "/#{uri}?#{params}api=yes"
  resp = ""
  
  @http.start() { |http|
    req = Net::HTTP::Get.new(uri)
    req.basic_auth @auth_username, @auth_password
    response = http.request(req)
    # URI.unescape misses a lot, for some reason.
    resp = URI.unescape(response.body).gsub("&#45","-").gsub("&#64", "@").gsub("&#95", "_").gsub("&#58", ":")
  }
    
  # Create a hash of the response. We need some additional magic to make sure lists are parsed properly.
  resp_hash = {}
  response = resp.split('&')
  i = 0
  response.each do |var|
    key,value = var.split("=")
    if key == "list[]"
      key = i
      i += 1
    end
    resp_hash[key] = value
  end
    
  if resp_hash["error"] == "1"
    text = resp_hash["text"]
    details = resp_hash["details"]
    raise(Puppet::Error, "#{text}. #{details}.")
  elsif resp =~ /DirectAdmin\ Login/
    raise(Puppet::Error, "Login failed. The specified username and/or password is not correct.")
  elsif resp =~ /authority\ level/
    raise(Puppet::Error, "The request you've made cannot be executed because it does not exist in your authority level.")
  else
    return resp_hash
  end
end