Skip to content

Commit dc1861b

Browse files
authored
Merge pull request #129 from vutny/manage-postgres-with-client
[REFACTORING] Add `postgres.manage` state to provision DB objects
2 parents 62c54f0 + 74ddea8 commit dc1861b

File tree

4 files changed

+94
-79
lines changed

4 files changed

+94
-79
lines changed

README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ various DB objects in the cluster.
2323
-------------------
2424

2525
Installs the PostgreSQL client binaries and libraries.
26-
Allows to create such DB objects as: users, tablespaces, databases, schemas and
26+
27+
``postgres.manage``
28+
-------------------
29+
30+
Creates such DB objects as: users, tablespaces, databases, schemas and
2731
extensions. See ``pillar.example`` file for details.
2832

2933
``postgres.python``

postgres/client.sls

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{%- from "postgres/map.jinja" import postgres with context -%}
2-
{%- from "postgres/macros.jinja" import format_state with context -%}
32
43
{%- set pkgs = [] %}
54
{%- for pkg in (postgres.pkg_client, postgres.pkg_libpq_dev) %}
65
{%- if pkg %}
76
{%- do pkgs.append(pkg) %}
87
{%- endif %}
9-
{%- endfor -%}
8+
{%- endfor %}
109
11-
{%- if postgres.use_upstream_repo -%}
10+
{%- if postgres.use_upstream_repo %}
1211
1312
include:
1413
- postgres.upstream
@@ -46,78 +45,3 @@ postgresql-client-libs:
4645
{%- endfor %}
4746
4847
{%- endif %}
49-
50-
# Ensure that Salt is able to use postgres modules
51-
# after installing client binaries
52-
53-
postgres-reload-modules:
54-
test.nop:
55-
- reload_modules: True
56-
57-
# User states
58-
59-
{%- for name, user in postgres.users|dictsort() %}
60-
61-
{{ format_state(name, 'postgres_user', user) }}
62-
- require:
63-
- pkg: postgresql-client-libs
64-
65-
{%- endfor %}
66-
67-
# Tablespace states
68-
69-
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
70-
71-
{{ format_state(name, 'postgres_tablespace', tblspace) }}
72-
- require:
73-
- pkg: postgresql-client-libs
74-
{%- if 'owner' in tblspace %}
75-
- postgres_user: postgres_user-{{ tblspace.owner }}
76-
{%- endif %}
77-
78-
{%- endfor %}
79-
80-
# Database states
81-
82-
{%- for name, db in postgres.databases|dictsort() %}
83-
84-
{{ format_state(name, 'postgres_database', db) }}
85-
- require:
86-
- pkg: postgresql-client-libs
87-
{%- if 'owner' in db %}
88-
- postgres_user: postgres_user-{{ db.owner }}
89-
{%- endif %}
90-
{%- if 'tablespace' in db %}
91-
- postgres_tablespace: postgres_tablespace-{{ db.tablespace }}
92-
{%- endif %}
93-
94-
{%- endfor %}
95-
96-
# Schema states
97-
98-
{%- for name, schema in postgres.schemas|dictsort() %}
99-
100-
{{ format_state(name, 'postgres_schema', schema) }}
101-
- require:
102-
- pkg: postgresql-client-libs
103-
{%- if 'owner' in schema %}
104-
- postgres_user: postgres_user-{{ schema.owner }}
105-
{%- endif %}
106-
107-
{%- endfor %}
108-
109-
# Extension states
110-
111-
{%- for name, extension in postgres.extensions|dictsort() %}
112-
113-
{{ format_state(name, 'postgres_extension', extension) }}
114-
- require:
115-
- pkg: postgresql-client-libs
116-
{%- if 'maintenance_db' in extension %}
117-
- postgres_database: postgres_database-{{ extension.maintenance_db }}
118-
{%- endif %}
119-
{%- if 'schema' in extension %}
120-
- postgres_schema: postgres_schema-{{ extension.schema }}
121-
{%- endif %}
122-
123-
{%- endfor %}

postgres/init.sls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
include:
22
- postgres.server
33
- postgres.client
4+
- postgres.manage

postgres/manage.sls

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{%- from "postgres/map.jinja" import postgres with context -%}
2+
{%- from "postgres/macros.jinja" import format_state with context -%}
3+
4+
{%- if not salt.get('postgres.user_create') %}
5+
6+
# Salt states for managing PostgreSQL is not available,
7+
# need to provision client binaries first
8+
9+
include:
10+
- postgres.client
11+
12+
{%- endif %}
13+
14+
# Ensure that Salt is able to use postgres modules
15+
16+
postgres-reload-modules:
17+
test.nop:
18+
- reload_modules: True
19+
20+
# User states
21+
22+
{%- for name, user in postgres.users|dictsort() %}
23+
24+
{{ format_state(name, 'postgres_user', user) }}
25+
- require:
26+
- test: postgres-reload-modules
27+
28+
{%- endfor %}
29+
30+
# Tablespace states
31+
32+
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
33+
34+
{{ format_state(name, 'postgres_tablespace', tblspace) }}
35+
- require:
36+
- test: postgres-reload-modules
37+
{%- if 'owner' in tblspace %}
38+
- postgres_user: postgres_user-{{ tblspace.owner }}
39+
{%- endif %}
40+
41+
{%- endfor %}
42+
43+
# Database states
44+
45+
{%- for name, db in postgres.databases|dictsort() %}
46+
47+
{{ format_state(name, 'postgres_database', db) }}
48+
- require:
49+
- test: postgres-reload-modules
50+
{%- if 'owner' in db %}
51+
- postgres_user: postgres_user-{{ db.owner }}
52+
{%- endif %}
53+
{%- if 'tablespace' in db %}
54+
- postgres_tablespace: postgres_tablespace-{{ db.tablespace }}
55+
{%- endif %}
56+
57+
{%- endfor %}
58+
59+
# Schema states
60+
61+
{%- for name, schema in postgres.schemas|dictsort() %}
62+
63+
{{ format_state(name, 'postgres_schema', schema) }}
64+
- require:
65+
- test: postgres-reload-modules
66+
{%- if 'owner' in schema %}
67+
- postgres_user: postgres_user-{{ schema.owner }}
68+
{%- endif %}
69+
70+
{%- endfor %}
71+
72+
# Extension states
73+
74+
{%- for name, extension in postgres.extensions|dictsort() %}
75+
76+
{{ format_state(name, 'postgres_extension', extension) }}
77+
- require:
78+
- test: postgres-reload-modules
79+
{%- if 'maintenance_db' in extension %}
80+
- postgres_database: postgres_database-{{ extension.maintenance_db }}
81+
{%- endif %}
82+
{%- if 'schema' in extension %}
83+
- postgres_schema: postgres_schema-{{ extension.schema }}
84+
{%- endif %}
85+
86+
{%- endfor %}

0 commit comments

Comments
 (0)