瀏覽代碼

Create initial base provider class

DarkMorford 6 年之前
父節點
當前提交
d1da6889fe
共有 1 個文件被更改,包括 86 次插入0 次删除
  1. 86 0
      lib/puppet/provider/openldap.rb

+ 86 - 0
lib/puppet/provider/openldap.rb

@@ -0,0 +1,86 @@
+require 'tempfile'
+
+# Base provider class for OpenLDAP defined types
+class Puppet::Provider::Openldap < Puppet::Provider
+  # Initialize provider structure
+  initvars
+
+  # Declare binaries that we use
+  commands original_ldapadd:    'ldapadd',
+           original_ldapmodify: 'ldapmodify',
+           original_slapcat:    'slapcat'
+
+  # Class-level wrapper methods for ldap binaries
+  def self.ldapadd(path)
+    original_ldapadd('-cQY', 'EXTERNAL', '-H', 'ldapi:///', '-f', path)
+  end
+
+  def self.ldapmodify(path)
+    original_ldapmodify('-Y', 'EXTERNAL', '-H', 'ldapi:///', '-f', path)
+  end
+
+  def self.slapcat(filter, dn = '', base = 'cn=config')
+    arguments = [
+      '-b', base,
+      '-o', 'ldif-wrap=no',
+      '-H', "ldap:///#{dn}???#{filter}"
+    ]
+
+    original_slapcat(*arguments)
+  end
+
+  # Instance-level wrapper methods
+  def ldapadd(*args)
+    self.class.ldapadd(*args)
+  end
+
+  def ldapmodify(*args)
+    self.class.ldapmodify(*args)
+  end
+
+  def slapcat(*args)
+    self.class.slapcat(*args)
+  end
+
+  # Create temporary LDIF file
+  def self.temp_ldif(name = 'openldap_ldif')
+    Tempfile.new(name)
+  end
+
+  def temp_ldif(*args)
+    self.class.temp_ldif(*args)
+  end
+
+  # Methods to generate LDIF fragments
+  def delimit
+    "-\n"
+  end
+
+  def cn_config
+    dn('cn=config')
+  end
+
+  def dn(dn)
+    "dn: #{dn}\n"
+  end
+
+  def changetype(t)
+    "changetype: #{t}\n"
+  end
+
+  def add(key)
+    "add: olc#{key}\n"
+  end
+
+  def del(key)
+    "delete: olc#{key}\n"
+  end
+
+  def replace(key)
+    "replace: olc#{key}\n"
+  end
+
+  def key_with_value(key, value)
+    "olc#{key}: #{value}\n"
+  end
+end