Skip to content

Commit

Permalink
Entry position ordering take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bmhughes committed Aug 24, 2023
1 parent fdb6696 commit 54a40ee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
25 changes: 18 additions & 7 deletions libraries/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,20 @@ def remove(entry)
@entries.reject! { |e| e.eql?(entry) }
end

def move(entry, position)
raise PgHbaInvalidEntryType unless entry.is_a?(PgHbaFileEntry)

return unless include?(entry)

index = @entries.index(entry)
existing_entry = @entries.delete_at(index)
@entries.insert(position, existing_entry)
end

def include?(entry)
raise unless entry.is_a?(PgHbaFileEntry)

@entries.any? { |e| e.eql?(entry) }
!@entries.index(entry).nil?
end

def entry(type, database, user, address = nil)
Expand Down Expand Up @@ -163,7 +173,7 @@ def split_entries
def marshall_entries
return if @access_entries.empty?

@access_entries.each { |entry| @entries.push(PgHbaFileEntry.create(**entry)) }
@access_entries.each_with_index { |entry, index| @entries.push(PgHbaFileEntry.create(**entry, position: index)) }

@entries
end
Expand All @@ -172,7 +182,7 @@ def marshall_entries
class PgHbaFileEntry
include PostgreSQL::Cookbook::Utils

attr_reader :type, :comment
attr_reader :type, :comment, :position

ENTRY_FIELD_FORMAT = {
type: 8,
Expand All @@ -185,7 +195,7 @@ class PgHbaFileEntry
}.freeze
private_constant :ENTRY_FIELD_FORMAT

def initialize(type:, database:, user:, auth_method:, auth_options: nil, comment: nil)
def initialize(type:, database:, user:, auth_method:, auth_options: nil, comment: nil, position: nil)
@type = type
@type.freeze

Expand All @@ -194,6 +204,7 @@ def initialize(type:, database:, user:, auth_method:, auth_options: nil, comment
@auth_method = auth_method
@auth_options = PgHbaFileEntryAuthOptions.new(auth_options) if auth_options && !auth_options.empty?
self.comment = comment
@position = position
end

def to_s
Expand Down Expand Up @@ -254,7 +265,7 @@ class PgHbaFileEntryLocal < PgHbaFileEntry
MATCH_FIELDS = %i(type database user).freeze
private_constant :ENTRY_FIELDS, :MATCH_FIELDS

def initialize(type:, database:, user:, auth_method:, auth_options: nil, comment: nil)
def initialize(type:, database:, user:, auth_method:, auth_options: nil, comment: nil, position: nil)
raise PgHbaInvalidEntryType, "Invalid entry type #{properties.first}" unless type.eql?('local')

super
Expand All @@ -272,10 +283,10 @@ class PgHbaFileEntryHost < PgHbaFileEntry
MATCH_FIELDS = %i(type database user address).freeze
private_constant :ENTRY_FIELDS, :MATCH_FIELDS

def initialize(type:, database:, user:, address:, auth_method:, auth_options: nil, comment: nil)
def initialize(type:, database:, user:, address:, auth_method:, auth_options: nil, comment: nil, position: nil)
raise PgHbaInvalidEntryType unless %w(host hostssl hostnossl hostgssenc hostnogssenc).include?(type)

super(type: type, database: database, user: user, auth_method: auth_method, auth_options: auth_options, comment: comment)
super(type: type, database: database, user: user, auth_method: auth_method, auth_options: auth_options, comment: comment, position: position)
@address = address
end

Expand Down
8 changes: 6 additions & 2 deletions resources/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@

send(p, entry.send(p).to_s)
end
position entry.position if new_resource.position
end

action_class do
Expand All @@ -92,19 +93,22 @@
entry = PostgreSQL::Cookbook::AccessHelpers::PgHba::PgHbaFileEntry.create(**resource_properties)
config_resource.variables[:pg_hba].add(entry, new_resource.position)
else
entry.update(auth_method: new_resource.auth_method, auth_options: new_resource.auth_options, comment: new_resource.comment)
# entry.update(auth_method: new_resource.auth_method, auth_options: new_resource.auth_options, comment: new_resource.comment)
run_action(:update)
end
end
end

action :update do
converge_if_changed(:auth_method, :auth_options, :comment) do
converge_if_changed(:auth_method, :auth_options, :comment, :position) do
config_resource_init
entry = config_resource.variables[:pg_hba].entry(new_resource.type, new_resource.database, new_resource.user, new_resource.address)

raise Chef::Exceptions::CurrentValueDoesNotExist, "Cannot update access entry for '#{new_resource.name}' as it does not exist" if nil_or_empty?(entry)

entry.update(auth_method: new_resource.auth_method, auth_options: new_resource.auth_options, comment: new_resource.comment)

converge_if_changed(:position) { config_resource.variables[:pg_hba].move(entry, new_resource.position) }
end
end

Expand Down
1 change: 1 addition & 0 deletions test/cookbooks/test/recipes/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
user 'sous_chef'
auth_method 'md5'
address '127.0.0.1/32'
position 5

notifies :restart, 'postgresql_service[postgresql]', :delayed
end
Expand Down

0 comments on commit 54a40ee

Please sign in to comment.