11#!/usr/bin/env python
22"""SingleStoreDB Cloud Organization."""
3+ import datetime
34from typing import Dict
45from typing import List
56from typing import Optional
67from typing import Union
78
9+ from ..exceptions import ManagementError
810from .manager import Manager
911from .utils import vars_to_str
1012
@@ -21,6 +23,91 @@ def stringify(x: Union[str, List[str]]) -> str:
2123 return x
2224
2325
26+ class Secret (object ):
27+ """
28+ SingleStoreDB secrets definition.
29+
30+ This object is not directly instantiated. It is used in results
31+ of API calls on the :class:`Organization`. See :meth:`Organization.get_secret`.
32+ """
33+
34+ def __init__ (
35+ self ,
36+ id : str ,
37+ name : str ,
38+ value : str ,
39+ created_by : str ,
40+ created_at : Union [str , datetime .datetime ],
41+ last_updated_by : str ,
42+ last_updated_at : Union [str , datetime .datetime ],
43+ deleted_by : Optional [str ] = None ,
44+ deleted_at : Optional [Union [str , datetime .datetime ]] = None ,
45+ ):
46+ # UUID of the secret
47+ self .id = id
48+
49+ # Name of the secret
50+ self .name = name
51+
52+ # Value of the secret
53+ self .value = value
54+
55+ # User who created the secret
56+ self .created_by = created_by
57+
58+ # Time when the secret was created
59+ self .created_at = created_at
60+
61+ # UUID of the user who last updated the secret
62+ self .last_updated_by = last_updated_by
63+
64+ # Time when the secret was last updated
65+ self .last_updated_at = last_updated_at
66+
67+ # UUID of the user who deleted the secret
68+ self .deleted_by = deleted_by
69+
70+ # Time when the secret was deleted
71+ self .deleted_at = deleted_at
72+
73+ @classmethod
74+ def from_dict (cls , obj : Dict [str , str ]) -> 'Secret' :
75+ """
76+ Construct a Secret from a dictionary of values.
77+
78+ Parameters
79+ ----------
80+ obj : dict
81+ Dictionary of values
82+
83+ Returns
84+ -------
85+ :class:`Secret`
86+
87+ """
88+ out = cls (
89+ id = obj ['secretID' ],
90+ name = obj ['name' ],
91+ value = obj ['value' ],
92+ created_by = obj ['createdBy' ],
93+ created_at = obj ['createdAt' ],
94+ last_updated_by = obj ['lastUpdatedBy' ],
95+ last_updated_at = obj ['lastUpdatedAt' ],
96+ deleted_by = obj .get ('deletedBy' ),
97+ deleted_at = obj .get ('deletedAt' ),
98+ )
99+
100+ return out
101+
102+ def __str__ (self ) -> str :
103+ """Return string representation."""
104+ return vars_to_str (self )
105+
106+ def __repr__ (self ) -> str :
107+ """Return string representation."""
108+ return str (self )
109+
110+
24111class Organization (object ):
25112 """
26113 Organization in SingleStoreDB Cloud portal.
@@ -55,6 +142,22 @@ def __repr__(self) -> str:
55142 """Return string representation."""
56143 return str (self )
57144
145+ def get_secret (self , name : str ) -> Secret :
146+ if self ._manager is None :
147+ raise ManagementError (msg = 'Organization not initialized' )
148+
149+ res = self ._manager ._get ('secrets' , params = dict (name = name ))
150+
151+ secrets = [Secret .from_dict (item ) for item in res .json ()['secrets' ]]
152+
153+ if len (secrets ) == 0 :
154+ raise ManagementError (msg = f'Secret { name } not found' )
155+
156+ if len (secrets ) > 1 :
157+ raise ManagementError (msg = f'Multiple secrets found for { name } ' )
158+
159+ return secrets [0 ]
160+
58161 @classmethod
59162 def from_dict (
60163 cls ,
0 commit comments