15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
17
#
18
- import logging , json
19
- import nuvolaris .config as cfg
18
+ import json
19
+ import logging
20
+
20
21
import nuvolaris .couchdb as cdb
21
22
import nuvolaris .couchdb_util as couchdb_util
22
- import nuvolaris .user_config as user_config
23
23
import nuvolaris .util as util
24
- import nuvolaris .mongodb as mdb
25
- from nuvolaris .user_metadata import UserMetadata
26
24
from nuvolaris .nuvolaris_metadata import NuvolarisMetadata
27
-
25
+ from nuvolaris .user_metadata import UserMetadata
26
+ import nuvolaris .bcrypt_util as bu
28
27
USER_META_DBN = "users_metadata"
29
28
29
+
30
30
def _add_metadata (db , metadata ):
31
31
"""
32
32
Add a new Openwhisk User metadata entry
33
33
"""
34
34
res = util .check (db .wait_db_ready (60 ), "wait_db_ready" , True )
35
- return util .check (cdb .update_templated_doc (db , USER_META_DBN , "user_metadata.json" , metadata ), f"add_metadata { metadata ['login' ]} " , res )
36
-
37
- def save_user_metadata (user_metadata :UserMetadata ):
35
+ return util .check (cdb .update_templated_doc (db , USER_META_DBN , "user_metadata.json" , metadata ),
36
+ f"add_metadata { metadata ['login' ]} " , res )
37
+
38
+
39
+ def save_user_metadata (user_metadata : UserMetadata ):
38
40
"""
39
41
Add a generic user metadata into the internal CouchDB
40
42
"""
@@ -48,7 +50,8 @@ def save_user_metadata(user_metadata:UserMetadata):
48
50
logging .error (f"failed to store Nuvolaris metadata for { metadata ['login' ]} . Cause: { e } " )
49
51
return None
50
52
51
- def save_nuvolaris_metadata (nuvolaris_metadata :NuvolarisMetadata ):
53
+
54
+ def save_nuvolaris_metadata (nuvolaris_metadata : NuvolarisMetadata ):
52
55
"""
53
56
Add nuvolaris user metadata into the internal CouchDB
54
57
"""
@@ -60,69 +63,72 @@ def save_nuvolaris_metadata(nuvolaris_metadata:NuvolarisMetadata):
60
63
return _add_metadata (db , metadata )
61
64
except Exception as e :
62
65
logging .error (f"failed to store Nuvolaris metadata for { metadata ['login' ]} . Cause: { e } " )
63
- return None
66
+ return None
67
+
64
68
65
69
def delete_user_metadata (login ):
66
70
logging .info (f"removing Nuvolaris metadata for user { login } " )
67
71
68
72
try :
69
73
db = couchdb_util .CouchDB ()
70
- selector = {"selector" :{"login" : {"$eq" : login }}}
74
+ selector = {"selector" : {"login" : {"$eq" : login }}}
71
75
response = db .find_doc (USER_META_DBN , json .dumps (selector ))
72
76
73
- if (response ['docs' ]):
74
- docs = list (response ['docs' ])
75
- if (len (docs ) > 0 ):
76
- doc = docs [0 ]
77
- logging .info (f"removing user metadata documents { doc ['_id' ]} " )
78
- return db .delete_doc (USER_META_DBN ,doc ['_id' ])
79
-
77
+ if (response ['docs' ]):
78
+ docs = list (response ['docs' ])
79
+ if (len (docs ) > 0 ):
80
+ doc = docs [0 ]
81
+ logging .info (f"removing user metadata documents { doc ['_id' ]} " )
82
+ return db .delete_doc (USER_META_DBN , doc ['_id' ])
83
+
80
84
logging .warn (f"Nuvolaris metadata for user { login } not found!" )
81
85
return None
82
86
except Exception as e :
83
87
logging .error (f"failed to remove Nuvolaris metadata for user { login } . Reason: { e } " )
84
88
return None
85
89
90
+
86
91
def update_user_metadata_password (login , password ):
87
92
logging .info (f"updating Nuvolaris password for user { login } " )
88
93
89
94
try :
90
95
db = couchdb_util .CouchDB ()
91
- selector = {"selector" :{"login" : {"$eq" : login }}}
96
+ selector = {"selector" : {"login" : {"$eq" : login }}}
92
97
response = db .find_doc (USER_META_DBN , json .dumps (selector ))
93
98
94
- if (response ['docs' ]):
95
- docs = list (response ['docs' ])
96
- if (len (docs ) > 0 ):
97
- doc = docs [0 ]
98
- logging .info (f"updating user credentials { doc ['_id' ]} " )
99
- doc ['password' ]= password
100
- return db .update_doc (USER_META_DBN ,doc )
101
-
99
+ if (response ['docs' ]):
100
+ docs = list (response ['docs' ])
101
+ if (len (docs ) > 0 ):
102
+ doc = docs [0 ]
103
+ logging .info (f"updating user credentials { doc ['_id' ]} " )
104
+ doc ['password' ] = bu . hash_password ( password )
105
+ return db .update_doc (USER_META_DBN , doc )
106
+
102
107
logging .warn (f"Nuvolaris metadata for user { login } not found!" )
103
108
return None
104
109
except Exception as e :
105
110
logging .error (f"failed to update credentials for quota Nuvolaris user { login } . Reason: { e } " )
106
- return None
111
+ return None
112
+
107
113
108
- def update_user_metadata_quota (login , quota :list ):
114
+ def update_user_metadata_quota (login , quota : list ):
109
115
logging .info (f"updating Nuvolaris quota for user { login } " )
110
116
111
117
try :
112
118
db = couchdb_util .CouchDB ()
113
- selector = {"selector" :{"login" : {"$eq" : login }}}
119
+ selector = {"selector" : {"login" : {"$eq" : login }}}
114
120
response = db .find_doc (USER_META_DBN , json .dumps (selector ))
115
121
116
- if (response ['docs' ]):
117
- docs = list (response ['docs' ])
118
- if (len (docs ) > 0 ):
119
- doc = docs [0 ]
120
- logging .info (f"updating user quota { doc ['_id' ]} " )
121
- doc ['quota' ]= quota
122
- return db .update_doc (USER_META_DBN ,doc )
123
-
122
+ if (response ['docs' ]):
123
+ docs = list (response ['docs' ])
124
+ if (len (docs ) > 0 ):
125
+ doc = docs [0 ]
126
+ logging .info (f"updating user quota { doc ['_id' ]} " )
127
+ doc ['quota' ] = quota
128
+ return db .update_doc (USER_META_DBN , doc )
129
+
124
130
logging .warn (f"Nuvolaris metadata for user { login } not found!" )
125
131
return None
126
132
except Exception as e :
127
133
logging .error (f"failed to update quota for Nuvolaris user { login } . Reason: { e } " )
128
- return None
134
+ return None
0 commit comments