Skip to content

Commit ad0ecfc

Browse files
committed
Migrate mysql operations & tests to use explicit fact imports.
1 parent a7147b4 commit ad0ecfc

32 files changed

+157
-132
lines changed

pyinfra/operations/mysql.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
import six
1717

1818
from pyinfra.api import MaskString, operation, OperationError, StringCommand
19-
from pyinfra.facts.mysql import make_execute_mysql_command, make_mysql_command
19+
from pyinfra.facts.mysql import (
20+
make_execute_mysql_command,
21+
make_mysql_command,
22+
MysqlDatabases,
23+
MysqlUserGrants,
24+
MysqlUsers,
25+
)
2026

2127

2228
@operation(is_idempotent=False)
@@ -137,8 +143,12 @@ def user(
137143
if require_subject:
138144
raise OperationError('Cannot set `require_subject` if `require` is not "X509"')
139145

140-
current_users = host.fact.mysql_users(
141-
mysql_user, mysql_password, mysql_host, mysql_port,
146+
current_users = host.get_fact(
147+
MysqlUsers,
148+
mysql_user=mysql_user,
149+
mysql_password=mysql_password,
150+
mysql_host=mysql_host,
151+
mysql_port=mysql_port,
142152
)
143153

144154
user_host = '{0}@{1}'.format(user, user_hostname)
@@ -325,9 +335,12 @@ def database(
325335
)
326336
'''
327337

328-
current_databases = host.fact.mysql_databases(
329-
mysql_user, mysql_password,
330-
mysql_host, mysql_port,
338+
current_databases = host.get_fact(
339+
MysqlDatabases,
340+
mysql_user=mysql_user,
341+
mysql_password=mysql_password,
342+
mysql_host=mysql_host,
343+
mysql_port=mysql_port,
331344
)
332345

333346
is_present = database in current_databases
@@ -439,10 +452,14 @@ def privileges(
439452
).format(database, table))
440453

441454
database_table = '{0}.{1}'.format(database, table)
442-
user_grants = host.fact.mysql_user_grants(
443-
user, user_hostname,
444-
mysql_user, mysql_password,
445-
mysql_host, mysql_port,
455+
user_grants = host.get_fact(
456+
MysqlUserGrants,
457+
user=user,
458+
user_hostname=user_hostname,
459+
mysql_user=mysql_user,
460+
mysql_password=mysql_password,
461+
mysql_host=mysql_host,
462+
mysql_port=mysql_port,
446463
)
447464

448465
existing_privileges = []

tests/operations/mysql.database/add.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"charset": "UTF-8"
66
},
77
"facts": {
8-
"mysql_databases": []
8+
"mysql.MysqlDatabases": {
9+
"mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {}
10+
}
911
},
1012
"commands": [
1113
"mysql -Be 'CREATE DATABASE somedb COLLATE utf8_bin CHARSET UTF-8'"

tests/operations/mysql.database/add_noop.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"args": ["somedb"],
33
"facts": {
4-
"mysql_databases": {
5-
"somedb": {}
4+
"mysql.MysqlDatabases": {
5+
"mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
6+
"somedb": {}
7+
}
68
}
79
},
810
"commands": [],

tests/operations/mysql.database/add_privileges.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"user_privileges": "ALL"
66
},
77
"facts": {
8-
"mysql_databases": [],
9-
"mysql_user_grants": {
10-
"someuser": {
8+
"mysql.MysqlDatabases": {
9+
"mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {}
10+
},
11+
"mysql.MysqlUserGrants": {
12+
"user=someuser, user_hostname=localhost, mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
1113
"localhost": {}
1214
}
1315
}

tests/operations/mysql.database/delete.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"present": false
55
},
66
"facts": {
7-
"mysql_databases": [
8-
"somedb"
9-
]
7+
"mysql.MysqlDatabases": {
8+
"mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
9+
"somedb": {}
10+
}
11+
}
1012
},
1113
"commands": [
1214
"mysql -Be 'DROP DATABASE somedb'"

tests/operations/mysql.database/delete_noop.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"present": false
55
},
66
"facts": {
7-
"mysql_databases": {}
7+
"mysql.MysqlDatabases": {
8+
"mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {}
9+
}
810
},
911
"commands": [],
1012
"noop_description": "mysql database somedb does not exist"

tests/operations/mysql.privileges/add.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
22
"args": ["someuser", ["SELECT", "INSERT", "DELETE"]],
33
"facts": {
4-
"mysql_user_grants": {
5-
"someuser": {
6-
"localhost": {
7-
"*.*": {
8-
"privileges": ["DELETE"]
9-
}
4+
"mysql.MysqlUserGrants": {
5+
"user=someuser, user_hostname=localhost, mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
6+
"*.*": {
7+
"privileges": ["DELETE"]
108
}
119
}
1210
}

tests/operations/mysql.privileges/add_database.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
"database": "mydb"
55
},
66
"facts": {
7-
"mysql_user_grants": {
8-
"someuser": {
9-
"localhost": {
10-
"*.*": {
11-
"privileges": []
12-
}
7+
"mysql.MysqlUserGrants": {
8+
"user=someuser, user_hostname=localhost, mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
9+
"*.*": {
10+
"privileges": []
1311
}
1412
}
1513
}

tests/operations/mysql.privileges/add_existing.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
22
"args": ["someuser", ["SELECT", "INSERT"]],
33
"facts": {
4-
"mysql_user_grants": {
5-
"someuser": {
6-
"localhost": {
7-
"*.*": {
8-
"privileges": ["SELECT", "INSERT"]
9-
}
4+
"mysql.MysqlUserGrants": {
5+
"user=someuser, user_hostname=localhost, mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
6+
"*.*": {
7+
"privileges": ["SELECT", "INSERT"]
108
}
119
}
1210
}

tests/operations/mysql.privileges/add_table.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
"table": "mytable"
66
},
77
"facts": {
8-
"mysql_user_grants": {
9-
"someuser": {
10-
"localhost": {
11-
"*.*": {
12-
"privileges": []
13-
}
8+
"mysql.MysqlUserGrants": {
9+
"user=someuser, user_hostname=localhost, mysql_user=None, mysql_password=None, mysql_host=None, mysql_port=None": {
10+
"*.*": {
11+
"privileges": []
1412
}
1513
}
1614
}

0 commit comments

Comments
 (0)