Skip to content

Commit

Permalink
Merge pull request saltstack-formulas#217 from alxwr/remove-db-or-user
Browse files Browse the repository at this point in the history
Remove DB and/or user
  • Loading branch information
alxwr authored Apr 24, 2019
2 parents 49e4a81 + 39915a4 commit 52ed6ab
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
22 changes: 17 additions & 5 deletions mysql/database.sls
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,31 @@ include:
{% for database_obj in salt['pillar.get']('mysql:database', []) %}
{% set state_id = 'mysql_db_' ~ loop.index0 %}
{% set database = database_obj.get('name') if database_obj is mapping else database_obj %}
{% if not database_obj %}{# in case database_obj == [] #}
{% continue %}
{% elif database_obj is mapping %}
{% set database = database_obj.get('name') %}
{% set present = database_obj.get('present', True) %}
{% else %}
{% set database = database_obj %}
{% set present = True %}
{% endif %}
{{ state_id }}:
{%- if present %}
mysql_database.present:
{% if database_obj is mapping %}
- character_set: {{ database_obj.get('character_set', '') }}
- collate: {{ database_obj.get('collate', '') }}
{% endif %}
{% else %}
mysql_database.absent:
{% endif %}
- name: {{ database }}
- connection_host: '{{ mysql_host }}'
- connection_user: '{{ mysql_salt_user }}'
{% if mysql_salt_pass %}
- connection_pass: '{{ mysql_salt_pass }}'
{% endif %}
{% if database_obj is mapping %}
- character_set: {{ database_obj.get('character_set', '') }}
- collate: {{ database_obj.get('collate', '') }}
{% endif %}
- connection_charset: utf8
{% if salt['pillar.get'](['mysql', 'schema', database, 'load']|join(':'), False) %}
Expand Down
34 changes: 20 additions & 14 deletions mysql/user.sls
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,28 @@ include:
{% set state_id = 'mysql_user_' ~ name ~ '_' ~ host%}
{{ state_id }}:
mysql_user.present:
- name: {{ name }}
- host: '{{ host }}'
{%- if user['password_hash'] is defined %}
- password_hash: '{{ user['password_hash'] }}'
{%- elif user['password'] is defined and user['password'] != None %}
- password: '{{ user['password'] }}'
{%- if user.get('present', True) %}
mysql_user.present:
- name: {{ name }}
- host: '{{ host }}'
{%- if user['password_hash'] is defined %}
- password_hash: '{{ user['password_hash'] }}'
{%- elif user['password'] is defined and user['password'] != None %}
- password: '{{ user['password'] }}'
{%- else %}
- allow_passwordless: True
{%- endif %}
{%- else %}
- allow_passwordless: True
mysql_user.absent:
- name: {{ name }}
- host: '{{ host }}'
{%- endif %}
- connection_host: '{{ mysql_host }}'
- connection_user: '{{ mysql_salt_user }}'
{% if mysql_salt_pass %}
- connection_pass: '{{ mysql_salt_pass }}'
{% endif %}
- connection_charset: utf8
- connection_host: '{{ mysql_host }}'
- connection_user: '{{ mysql_salt_user }}'
{%- if mysql_salt_pass %}
- connection_pass: '{{ mysql_salt_pass }}'
{%- endif %}
- connection_charset: utf8
{%- if 'grants' in user %}
{{ state_id ~ '_grants' }}:
Expand Down
9 changes: 9 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ mysql:
- name: bar
character_set: utf8
collate: utf8_general_ci
# Delete DB
- name: obsolete_db
present: False
schema:
foo:
load: True
Expand Down Expand Up @@ -143,6 +146,12 @@ mysql:
- database: foo
grants: ['select', 'insert', 'update']

# Remove a user
obsoleteuser:
host: localhost
# defaults to True
present: False

# Override any names defined in map.jinja
# serverpkg: mysql-server
# clientpkg: mysql-client
Expand Down

0 comments on commit 52ed6ab

Please sign in to comment.