Skip to content

Commit 52ed6ab

Browse files
authored
Merge pull request #217 from alxwr/remove-db-or-user
Remove DB and/or user
2 parents 49e4a81 + 39915a4 commit 52ed6ab

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

mysql/database.sls

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,31 @@ include:
1313
1414
{% for database_obj in salt['pillar.get']('mysql:database', []) %}
1515
{% set state_id = 'mysql_db_' ~ loop.index0 %}
16-
{% set database = database_obj.get('name') if database_obj is mapping else database_obj %}
16+
{% if not database_obj %}{# in case database_obj == [] #}
17+
{% continue %}
18+
{% elif database_obj is mapping %}
19+
{% set database = database_obj.get('name') %}
20+
{% set present = database_obj.get('present', True) %}
21+
{% else %}
22+
{% set database = database_obj %}
23+
{% set present = True %}
24+
{% endif %}
1725
{{ state_id }}:
26+
{%- if present %}
1827
mysql_database.present:
28+
{% if database_obj is mapping %}
29+
- character_set: {{ database_obj.get('character_set', '') }}
30+
- collate: {{ database_obj.get('collate', '') }}
31+
{% endif %}
32+
{% else %}
33+
mysql_database.absent:
34+
{% endif %}
1935
- name: {{ database }}
2036
- connection_host: '{{ mysql_host }}'
2137
- connection_user: '{{ mysql_salt_user }}'
2238
{% if mysql_salt_pass %}
2339
- connection_pass: '{{ mysql_salt_pass }}'
2440
{% endif %}
25-
{% if database_obj is mapping %}
26-
- character_set: {{ database_obj.get('character_set', '') }}
27-
- collate: {{ database_obj.get('collate', '') }}
28-
{% endif %}
2941
- connection_charset: utf8
3042
3143
{% if salt['pillar.get'](['mysql', 'schema', database, 'load']|join(':'), False) %}

mysql/user.sls

+20-14
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,28 @@ include:
3434
3535
{% set state_id = 'mysql_user_' ~ name ~ '_' ~ host%}
3636
{{ state_id }}:
37-
mysql_user.present:
38-
- name: {{ name }}
39-
- host: '{{ host }}'
40-
{%- if user['password_hash'] is defined %}
41-
- password_hash: '{{ user['password_hash'] }}'
42-
{%- elif user['password'] is defined and user['password'] != None %}
43-
- password: '{{ user['password'] }}'
37+
{%- if user.get('present', True) %}
38+
mysql_user.present:
39+
- name: {{ name }}
40+
- host: '{{ host }}'
41+
{%- if user['password_hash'] is defined %}
42+
- password_hash: '{{ user['password_hash'] }}'
43+
{%- elif user['password'] is defined and user['password'] != None %}
44+
- password: '{{ user['password'] }}'
45+
{%- else %}
46+
- allow_passwordless: True
47+
{%- endif %}
4448
{%- else %}
45-
- allow_passwordless: True
49+
mysql_user.absent:
50+
- name: {{ name }}
51+
- host: '{{ host }}'
4652
{%- endif %}
47-
- connection_host: '{{ mysql_host }}'
48-
- connection_user: '{{ mysql_salt_user }}'
49-
{% if mysql_salt_pass %}
50-
- connection_pass: '{{ mysql_salt_pass }}'
51-
{% endif %}
52-
- connection_charset: utf8
53+
- connection_host: '{{ mysql_host }}'
54+
- connection_user: '{{ mysql_salt_user }}'
55+
{%- if mysql_salt_pass %}
56+
- connection_pass: '{{ mysql_salt_pass }}'
57+
{%- endif %}
58+
- connection_charset: utf8
5359
5460
{%- if 'grants' in user %}
5561
{{ state_id ~ '_grants' }}:

pillar.example

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ mysql:
7272
- name: bar
7373
character_set: utf8
7474
collate: utf8_general_ci
75+
# Delete DB
76+
- name: obsolete_db
77+
present: False
7578
schema:
7679
foo:
7780
load: True
@@ -143,6 +146,12 @@ mysql:
143146
- database: foo
144147
grants: ['select', 'insert', 'update']
145148

149+
# Remove a user
150+
obsoleteuser:
151+
host: localhost
152+
# defaults to True
153+
present: False
154+
146155
# Override any names defined in map.jinja
147156
# serverpkg: mysql-server
148157
# clientpkg: mysql-client

0 commit comments

Comments
 (0)