Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

(#11617) Add f5_external_class support.

* Add f5_external_class type & provider.
* f5_external_class will autorequire f5_file.
* f5_external_class will not delete classes with name conflict.
* Add f5_external_class spec tests.
  • Loading branch information...
commit 0121f9d9732fa63af0a451f7b687b31d37bdb9bf 1 parent 2bb86c2
Nan Liu nanliu authored
88 lib/puppet/provider/f5_external_class/f5_external_class.rb
View
@@ -0,0 +1,88 @@
+require 'puppet/provider/f5'
+
+Puppet::Type.type(:f5_external_class).provide(:f5_external_class, :parent => Puppet::Provider::F5) do
+ @doc = "Manages f5 String classes (datagroups)"
+
+ confine :feature => :posix
+ defaultfor :feature => :posix
+
+ def self.wsdl
+ 'LocalLB.Class'
+ end
+
+ def wsdl
+ self.class.wsdl
+ end
+
+ def self.instances
+ transport[wsdl].get_external_class_list.collect do |ext|
+ new(:name => ext.class_name)
+ end
+ end
+
+ methods = [
+ 'file_format',
+ 'file_mode',
+ 'file_name',
+ ]
+
+ methods.each do |method|
+ define_method(method.to_sym) do
+ if transport[wsdl].respond_to?("get_external_class_#{method}".to_sym)
+ profile_string = transport[wsdl].send("get_external_class_#{method}", resource[:name]).first
+ end
+ end
+ end
+
+ methods.each do |method|
+ define_method("#{method}=") do |profile_string|
+ if transport[wsdl].respond_to?("set_external_class_#{method}".to_sym)
+ transport[wsdl].send("set_external_class_#{method}", resource[:name], [resource[method.to_sym]])
+ end
+ end
+ end
+
+ def data_separator
+ transport[wsdl].get_data_separator(resource[:name]).first
+ end
+
+ def data_separator=(value)
+ transport[wsdl].set_data_separator(resource[:name], [ resource[:data_separator] ])
+ end
+
+ def type
+ transport[wsdl].get_class_type(resource[:name]).first
+ end
+
+ def type=(value)
+ transport[wsdl].set_class_type(resource[:name], [ resource[:type] ])
+ end
+
+ def create
+ # F5 external class can not conflict with address, string, or value class
+ # namevar. Since Puppet can not enforce unique resource names, it is not
+ # safe to delete the class.
+ Puppet.debug("Puppet::Provider::F5_external_class: creating F5 string class #{resource[:name]}")
+
+ metainfo = {
+ :class_name => resource[:name],
+ :class_type => resource[:type],
+ :file_name => resource[:file_name],
+ :file_mode => resource[:file_mode],
+ :file_format => resource[:file_format]
+ }
+
+ transport[wsdl].create_external_class([metainfo])
+ end
+
+ def destroy
+ Puppet.debug("Puppet::Provider::F5_external_class: deleting F5 string class #{resource[:name]}")
+
+ transport[wsdl].delete_class(resource[:name])
+ end
+
+ def exists?
+ ext_class = transport[wsdl].get_external_class_list.collect{ |ext| ext.class_name }
+ ext_class.include?(resource[:name])
+ end
+end
68 lib/puppet/type/f5_external_class.rb
View
@@ -0,0 +1,68 @@
+Puppet::Type.newtype(:f5_external_class) do
+ @doc = "Manages F5 External classes (datagroups)"
+
+ apply_to_device
+
+ ensurable do
+ desc "F5 External Class resource state. Valid values are present, absent."
+
+ defaultto(:present)
+
+ newvalue(:present) do
+ provider.create
+ end
+
+ newvalue(:absent) do
+ provider.destroy
+ end
+ end
+
+ newparam(:name, :namevar=>true) do
+ desc "The external class name."
+ end
+
+ newproperty(:file_format) do
+ desc "The file format for the specified classes. This should only be called
+ for external classes, since it does not make sense for non-external classes."
+
+ newvalues(/^FILE_FORMAT_(UNKNOWN|CSV)$/)
+ defaultto('FILE_FORMAT_CSV')
+ end
+
+ newproperty(:file_mode) do
+ desc "The file modes for the specified classes. This should only be called
+ for external classes, since it does not make sense for non-external classes."
+
+ newvalues(/^FILE_MODE_(UNKNOWN|TYPE_READ|TYPE_READ_WRITE)$/)
+ defaultto('FILE_MODE_TYPE_READ_WRITE')
+ end
+
+ newproperty(:file_name) do
+ desc "The file names for the specified classes. This should only be called
+ for external classes, since it does not make sense for non-external classes."
+
+ validate do |value|
+ raise Puppet::Error, "Puppet::Type::F5_external_class: file_name must be absolute path." unless value = File.expand_path(value)
+ end
+ end
+
+ newproperty(:data_separator) do
+ desc "The class types for the specified classes."
+
+ newvalues(/^[[:punct:][:space:]]+$/)
+ defaultto(':=')
+ end
+
+ newproperty(:type) do
+ desc "The class types for the specified classes."
+
+ newvalues(/^CLASS_TYPE_(UNDEFINED|ADDRESS|STRING|VALUE)$/)
+ end
+
+ # auto require f5_file.
+ autorequire(:f5_file) do
+ if f5_file = self[:file_name]
+ f5_file
+ end
+ end
+end
59 spec/unit/puppet/type/f5_external_class_spec.rb
View
@@ -0,0 +1,59 @@
+#!/usr/bin/env rspec
+
+require 'spec_helper'
+
+res_type_name = :f5_external_class
+res_type = Puppet::Type.type(res_type_name)
+
+describe res_type do
+ let(:provider) {
+ prov = stub 'provider'
+ prov.stubs(:name).returns(res_type_name)
+ prov
+ }
+ let(:res_type) {
+ type = res_type
+ type.stubs(:defaultprovider).returns provider
+ type
+ }
+ let(:resource) {
+ res_type.new({:name => 'test_node'})
+ }
+
+ it 'should have :name be its namevar' do
+ res_type.key_attributes.should == [:name]
+ end
+
+ # This is just a simpler way of providing basic validation tests
+ # for people not familiar with rspec.
+ parameter_tests = {
+ :name => {
+ :valid => ['test_node', 'test node'],
+ :default => 'test', # just to make tests pass
+ },
+ :file_format => {
+ :valid => ['FILE_FORMAT_UNKNOWN', 'FILE_FORMAT_CSV'],
+ :invalid => ['FILE_FORMAT_TXT'],
+ :default => 'FILE_FORMAT_CSV',
+ },
+ :file_mode => {
+ :valid => ['FILE_MODE_UNKNOWN', 'FILE_MODE_TYPE_READ', 'FILE_MODE_TYPE_READ_WRITE'],
+ :invalid => ['FILE_MODE_WRITE'],
+ :default => 'FILE_MODE_TYPE_READ_WRITE',
+ },
+ :file_name => {
+ :valid => ['/config/file.txt', '/file.txt'],
+ },
+ :data_separator => {
+ :valid => [':=', '|', ' '],
+ :invalid => ['a'],
+ :default => ':=',
+ },
+ :type => {
+ :valid => ['CLASS_TYPE_UNDEFINED', 'CLASS_TYPE_ADDRESS', 'CLASS_TYPE_STRING', 'CLASS_TYPE_VALUE'],
+ :invalid => ['CLASS_TYPE_DATA'],
+ }
+ }
+ it_should_behave_like "a puppet type", parameter_tests, res_type_name
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.