1
- from typing import Optional , Dict , Any
1
+ from typing import Optional , Dict , Any , List , Union
2
+ from datetime import datetime
2
3
3
4
from dynatrace .dynatrace_object import DynatraceObject
4
5
from dynatrace .http_client import HttpClient
5
6
from dynatrace .pagination import PaginatedList
7
+ from dynatrace .utils import int64_to_datetime
6
8
7
9
8
10
class SettingService :
9
11
ENDPOINT = "/api/v2/settings/objects"
10
12
11
13
def __init__ (self , http_client : HttpClient ):
12
14
self .__http_client = http_client
13
-
14
- def list_objects (self ,schema_id : Optional [str ] = None ,
15
- scope : Optional [str ] = None ,external_ids : Optional [str ] = None ,
16
- fields : Optional [str ] = None ,
17
- filter :Optional [str ] = None , sort :Optional [str ] = None , page_size :Optional [str ] = None ) -> PaginatedList ["DynatraceObject" ]:
15
+
16
+ def list_objects (
17
+ self ,
18
+ schema_id : Optional [str ] = None ,
19
+ scope : Optional [str ] = None ,
20
+ external_ids : Optional [str ] = None ,
21
+ fields : Optional [str ] = None ,
22
+ filter : Optional [str ] = None ,
23
+ sort : Optional [str ] = None ,
24
+ page_size : Optional [str ] = None ,
25
+ ) -> PaginatedList ["SettingsObject" ]:
18
26
"""Lists settings
19
27
20
28
:return: a list of settings with details
@@ -28,64 +36,151 @@ def list_objects(self,schema_id: Optional[str] = None,
28
36
"sort" : sort ,
29
37
"pageSize" : page_size ,
30
38
}
31
- return PaginatedList (Settings , self .__http_client , target_url = self .ENDPOINT , list_item = "items" , target_params = params )
32
-
33
- def create_object (self ,external_id ,object_id ,schema_id ,schema_version ,scope , value ,validate_only ):
34
- """Creates a new settings object
39
+ return PaginatedList (
40
+ SettingsObject ,
41
+ self .__http_client ,
42
+ target_url = self .ENDPOINT ,
43
+ list_item = "items" ,
44
+ target_params = params ,
45
+ )
35
46
36
- :param external_id: External identifier for the object being created
37
- :param object_id: The ID of the settings object that should be replaced. Only applicable if an external identifier
38
- :param object_id: the ID of the object
39
- :param schema_id: The schema on which the object is based
40
- :param schema_version: The version of the schema on which the object is based.
41
- :param scope The scope that the object targets. For more details, please see Dynatrace Documentation.
42
- :param value The value of the setting.
43
- :return: a Settings object
44
- """
45
- params = {
46
- "validate_only" : validate_only ,
47
- }
48
- body = [ {
49
- "externalId" : external_id ,
50
- "objectId" : object_id ,
51
- "schemaId" : schema_id ,
52
- "schemaVersion" : schema_version ,
53
- "scope" : scope ,
54
- "value" : value
47
+ def create_object (
48
+ self ,
49
+ validate_only : Optional [bool ] = False ,
50
+ body : Union [
51
+ Optional [List ["SettingsObjectCreate" ]], Optional ["SettingsObjectCreate" ]
52
+ ] = [],
53
+ ):
54
+ """
55
+ Creates a new settings object or validates the provided settigns object
56
+
57
+ :param validate_only: If true, the request runs only validation of the submitted settings objects, without saving them
58
+ :param body: The JSON body of the request. Contains the settings objects
59
+ """
60
+ query_params = {"validateOnly" : validate_only }
55
61
56
- }]
57
-
58
- response = self .__http_client .make_request (self .ENDPOINT ,params = body , method = "POST" ,query_params = params ).json ()
62
+ if isinstance (body , SettingsObjectCreate ):
63
+ body = [body ]
64
+
65
+ body = [o .json () for o in body ]
66
+
67
+ response = self .__http_client .make_request (
68
+ self .ENDPOINT , params = body , method = "POST" , query_params = query_params
69
+ ).json ()
59
70
return response
60
-
61
-
71
+
62
72
def get_object (self , object_id : str ):
63
73
"""Gets parameters of specified settings object
64
74
65
75
:param object_id: the ID of the object
66
76
:return: a Settings object
67
77
"""
68
- response = self .__http_client .make_request (f"{ self .ENDPOINT } /{ object_id } " ).json ()
69
- return Settings (raw_element = response )
78
+ response = self .__http_client .make_request (
79
+ f"{ self .ENDPOINT } /{ object_id } "
80
+ ).json ()
81
+ return SettingsObject (raw_element = response )
70
82
71
- def update_object (self , object_id : str , value ):
83
+ def update_object (
84
+ self , object_id : str , value : Optional ["SettingsObjectCreate" ] = None
85
+ ):
72
86
"""Updates an existing settings object
73
-
74
- :param object_id: the ID of the object
75
87
88
+ :param object_id: the ID of the object
89
+ :param value: the JSON body of the request. Contains updated parameters of the settings object.
76
90
"""
77
- return self .__http_client .make_request (path = f"{ self .ENDPOINT } /{ object_id } " , params = value , method = "PUT" )
91
+ return self .__http_client .make_request (
92
+ f"{ self .ENDPOINT } /{ object_id } " , params = value .json (), method = "PUT"
93
+ )
78
94
79
- def delete_object (self , object_id : str ):
95
+ def delete_object (self , object_id : str , update_token : Optional [ str ] = None ):
80
96
"""Deletes the specified object
81
97
82
98
:param object_id: the ID of the object
99
+ :param update_token: The update token of the object. You can use it to detect simultaneous modifications by different users
83
100
:return: HTTP response
84
101
"""
85
- return self .__http_client .make_request (path = f"{ self .ENDPOINT } /{ object_id } " , method = "DELETE" )
102
+ query_params = {"updateToken" : update_token }
103
+ return self .__http_client .make_request (
104
+ f"{ self .ENDPOINT } /{ object_id } " ,
105
+ method = "DELETE" ,
106
+ query_params = query_params ,
107
+ ).json ()
108
+
109
+
110
+ class ModificationInfo (DynatraceObject ):
111
+ def _create_from_raw_data (self , raw_element : Dict [str , Any ]):
112
+ self .deleteable : bool = raw_element .get ("deleteable" )
113
+ self .first : bool = raw_element .get ("first" )
114
+ self .modifiable : bool = raw_element .get ("modifiable" )
115
+ self .modifiable_paths : List [str ] = raw_element .get ("modifiablePaths" , [])
116
+ self .movable : bool = raw_element .get ("movable" )
117
+ self .non_modifiable_paths : List [str ] = raw_element .get ("nonModifiablePaths" , [])
118
+
86
119
87
- class Settings (DynatraceObject ):
120
+ class SettingsObject (DynatraceObject ):
88
121
def _create_from_raw_data (self , raw_element : Dict [str , Any ]):
89
122
# Mandatory
90
123
self .objectId : str = raw_element ["objectId" ]
91
- self .value : str = raw_element ["value" ]
124
+ self .value : dict = raw_element ["value" ]
125
+ # Optional
126
+ self .author : str = raw_element .get ("author" )
127
+ self .created : datetime = (
128
+ int64_to_datetime (int (raw_element .get ("created" )))
129
+ if raw_element .get ("created" )
130
+ else None
131
+ )
132
+ self .created_by : str = raw_element .get ("createdBy" )
133
+ self .external_id : str = raw_element .get ("externalId" )
134
+ self .modification_info : ModificationInfo = (
135
+ ModificationInfo (
136
+ self ._http_client , self ._headers , raw_element .get ("modificationInfo" )
137
+ )
138
+ if raw_element .get ("modificationInfo" )
139
+ else None
140
+ )
141
+ self .modified : datetime = (
142
+ int64_to_datetime (int (raw_element .get ("modified" )))
143
+ if raw_element .get ("modified" )
144
+ else None
145
+ )
146
+ self .modified_by : str = raw_element .get ("modifiedBy" )
147
+ self .schema_id : str = raw_element .get ("schemaId" )
148
+ self .schema_version : str = raw_element .get ("schemaVersion" )
149
+ self .scope : str = raw_element .get ("scope" )
150
+ self .search_summary : str = raw_element .get ("searchSummary" )
151
+ self .summary : str = raw_element .get ("summary" )
152
+ self .update_token : str = raw_element .get ("updateToken" )
153
+
154
+
155
+ class SettingsObjectCreate :
156
+ def __init__ (
157
+ self ,
158
+ schema_id : str ,
159
+ value : dict ,
160
+ scope : str ,
161
+ external_id : Optional [str ] = None ,
162
+ insert_after : Optional [str ] = None ,
163
+ object_id : Optional [str ] = None ,
164
+ schema_version : Optional [str ] = None ,
165
+ ):
166
+ self .schema_id = schema_id
167
+ self .value = value
168
+ self .scope = scope
169
+ self .external_id = external_id
170
+ self .insert_after = insert_after
171
+ self .object_id = object_id
172
+ self .schema_version = schema_version
173
+
174
+ def json (self ) -> dict :
175
+ body = {"schemaId" : self .schema_id , "value" : self .value , "scope" : self .scope }
176
+
177
+ if self .external_id :
178
+ body ["externalId" ] = self .external_id
179
+ if self .insert_after :
180
+ body ["insertAfter" ] = self .insert_after
181
+ if self .object_id :
182
+ body ["objectId" ] = self .object_id
183
+ if self .schema_version :
184
+ body ["schemaVersion" ] = self .schema_version
185
+
186
+ return body
0 commit comments