Skip to content

Commit

Permalink
merge func with remove-user.sls saltstack-formulas#119
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain303 committed May 19, 2016
1 parent b4199e9 commit 0dc28d0
Showing 1 changed file with 65 additions and 9 deletions.
74 changes: 65 additions & 9 deletions mysql/user.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# vim: set ft=yaml:
{#-
# vim: set ft=jinja:
This state handle creation and deletion of mysql's user.
-#}
{#-
===== FETCH DATA =====
-#}
{% from "mysql/defaults.yaml" import rawmap with context %}
{%- set mysql = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:server:lookup')) %}
{%- set mysql_root_user = salt['pillar.get']('mysql:server:root_user', 'root') %}
Expand All @@ -9,19 +15,45 @@

{% set user_states = [] %}
{% set user_hosts = [] %}

{#-
===== MACRO DEFINITION =====
-#}
{# this macro is the salt statement to remove a user #}
{% macro mysql_user_remove(name, host, where) %}
{% set state_id = 'mysql_user_remove_' ~ name ~ '_' ~ host %}
{{ state_id }}:
# {{ where }}
mysql_user.absent:
- name: {{ name }}
- host: '{{ host }}'
- connection_host: '{{ mysql_host }}'
- connection_user: '{{ mysql_salt_user }}'
{% if mysql_salt_pass %}
- connection_pass: '{{ mysql_salt_pass }}'
{% endif %}
- connection_charset: utf8
{% endmacro %}
{#-
===== MAIN OUTPUT=====
-#}
include:
- mysql.python

{#-
===== LOOP OVER DATA : users =====
-#}
{% for name, user in salt['pillar.get']('mysql:user', {}).items() %}

{#-
>> select multiples host for the same user host: or hosts: in pillar =====
-#}
{% set user_host = salt['pillar.get']('mysql:user:%s:host'|format(name)) %}
{% if user_host != '' %}
{% set user_hosts = [user_host] %}
{% else %}
{% set user_hosts = salt['pillar.get']('mysql:user:%s:hosts'|format(name)) %}
{% endif %}

{#-
>> fetch something about mine, used to overwrite user_hosts at the end
-#}
{% if not user_hosts %}
{% set mine_target = salt['pillar.get']('mysql:user:%s:mine_hosts:target'|format(name)) %}
{% set mine_function = salt['pillar.get']('mysql:user:%s:mine_hosts:function'|format(name)) %}
Expand All @@ -31,10 +63,16 @@ include:
{% set user_hosts = salt['mine.get'](mine_target, mine_function, mine_expression_form).values() %}
{% endif %}
{% endif %}

{#-
===== INNER LOOP OVER DATA : host -> fecthed above single or multiple =====
-#}
{% for host in user_hosts %}
{# Don't create absent user, see remove-user.sls #}
{% if user.absent is not defined or not user.absent %}
{% if user.absent is defined and user.absent %}
{{ mysql_user_remove(name, host, 'top') }}
{% else %}
{#-
CREATE USER
-#}
{% set state_id = 'mysql_user_' ~ name ~ '_' ~ host%}
{{ state_id }}:
mysql_user.present:
Expand Down Expand Up @@ -93,9 +131,27 @@ include:
- mysql_user: {{ state_id }}
{% endfor %}
{% endif %}
{# endif user.absent #}

{# END user.absent #}
{% endif %}

{% do user_states.append(state_id) %}
{#-
=============== END FOR host
-#}
{% endfor %}

{#-
extra remove user with multiples host see #119 for user.hosts_absent (list)
must be in user loop not in host loop.
-#}
{% set user_hosts_absent = salt['pillar.get']('mysql:user:%s:hosts_absent'|format(name)) %}
{% if user_hosts_absent != '' %}
{% for h in user_hosts_absent %}
{{ mysql_user_remove(name, h, 'end') }}
{% endfor %}
{% endif %}
{#-
=============== END FOR user
-#}
{% endfor %}

0 comments on commit 0dc28d0

Please sign in to comment.