Skip to content

Commit 7a5ca29

Browse files
committed
feat: Combine multiple grants into one while checking state
1 parent 9af99b9 commit 7a5ca29

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

lib/puppet/provider/mysql_grant/mysql.rb

+29-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
commands mysql_raw: 'mysql'
88

99
def self.instances
10-
instances = []
10+
instance_configs = {}
1111
users.map do |user|
1212
user_string = cmd_user(user)
1313
query = "SHOW GRANTS FOR #{user_string};"
@@ -54,16 +54,32 @@ def self.instances
5454
# fix double backslash that MySQL prints, so resources match
5555
table.gsub!('\\\\', '\\')
5656
# We need to return an array of instances so capture these
57-
instances << new(
58-
name: "#{user}@#{host}/#{table}",
59-
ensure: :present,
60-
privileges: stripped_privileges.sort,
61-
table: table,
62-
user: "#{user}@#{host}",
63-
options: options,
64-
)
57+
name = "#{user}@#{host}/#{table}"
58+
if instance_configs.key?(name)
59+
instance_config = instance_configs[name]
60+
stripped_privileges.concat instance_config.privileges
61+
options.concat instance_config.options
62+
end
63+
64+
instance_configs[name] = {
65+
:privileges => stripped_privileges.uniq.sort,
66+
:table => table,
67+
:user => "#{user}@#{host}",
68+
:options => options.uniq,
69+
}
6570
end
6671
end
72+
instances = []
73+
instance_configs.map do |name, config|
74+
instances << new(
75+
name: name,
76+
ensure: :present,
77+
privileges: config[:privileges],
78+
table: config[:table],
79+
user: config[:user],
80+
options: config[:options],
81+
)
82+
end
6783
instances
6884
end
6985

@@ -90,10 +106,10 @@ def grant(user, table, privileges, options)
90106
def create
91107
grant(@resource[:user], @resource[:table], @resource[:privileges], @resource[:options])
92108

93-
@property_hash[:ensure] = :present
94-
@property_hash[:table] = @resource[:table]
95-
@property_hash[:user] = @resource[:user]
96-
@property_hash[:options] = @resource[:options] if @resource[:options]
109+
@property_hash[:ensure] = :present
110+
@property_hash[:table] = @resource[:table]
111+
@property_hash[:user] = @resource[:user]
112+
@property_hash[:options] = @resource[:options] if @resource[:options]
97113
@property_hash[:privileges] = @resource[:privileges]
98114

99115
exists? ? (return true) : (return false)

0 commit comments

Comments
 (0)