Skip to content

Commit 68b8acd

Browse files
Merge pull request #77 from Evolane/settings
adde support for v2 api Settings
2 parents a2b54d3 + a9b6ae3 commit 68b8acd

8 files changed

+197
-0
lines changed

dynatrace/environment_v2/schemas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ def _create_from_raw_data(self, raw_element: Dict[str, Any]):
4949

5050
def to_json(self) -> Dict[str, Any]:
5151
return {"name": self.name, "id": self.id}
52+

dynatrace/environment_v2/settings.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from typing import Optional, Dict, Any
2+
3+
from dynatrace.dynatrace_object import DynatraceObject
4+
from dynatrace.http_client import HttpClient
5+
from dynatrace.pagination import PaginatedList
6+
7+
8+
class SettingService:
9+
ENDPOINT = "/api/v2/settings/objects"
10+
11+
def __init__(self, http_client: HttpClient):
12+
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"]:
18+
"""Lists settings
19+
20+
:return: a list of settings with details
21+
"""
22+
params = {
23+
"schemaIds": schema_id,
24+
"scope": scope,
25+
"fields": fields,
26+
"externalIds": external_ids,
27+
"filter": filter,
28+
"sort": sort,
29+
"pageSize": page_size,
30+
}
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
35+
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
55+
56+
}]
57+
58+
response = self.__http_client.make_request(self.ENDPOINT,params=body, method="POST",query_params=params).json()
59+
return response
60+
61+
62+
def get_object(self, object_id: str):
63+
"""Gets parameters of specified settings object
64+
65+
:param object_id: the ID of the object
66+
:return: a Settings object
67+
"""
68+
response = self.__http_client.make_request(f"{self.ENDPOINT}/{object_id}").json()
69+
return Settings(raw_element=response)
70+
71+
def update_object(self, object_id: str, value):
72+
"""Updates an existing settings object
73+
74+
:param object_id: the ID of the object
75+
76+
"""
77+
return self.__http_client.make_request(path=f"{self.ENDPOINT}/{object_id}", params=value, method="PUT")
78+
79+
def delete_object(self, object_id: str):
80+
"""Deletes the specified object
81+
82+
:param object_id: the ID of the object
83+
:return: HTTP response
84+
"""
85+
return self.__http_client.make_request(path=f"{self.ENDPOINT}/{object_id}", method="DELETE")
86+
87+
class Settings(DynatraceObject):
88+
def _create_from_raw_data(self, raw_element: Dict[str, Any]):
89+
# Mandatory
90+
self.objectId: str = raw_element["objectId"]
91+
self.value: str = raw_element["value"]

dynatrace/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
from dynatrace.environment_v2.problems import ProblemService
5757
from dynatrace.environment_v2.service_level_objectives import SloService
5858
from dynatrace.environment_v2.logs import LogService
59+
from dynatrace.environment_v2.settings import SettingService
60+
5961
from dynatrace.http_client import HttpClient
6062

6163

@@ -118,6 +120,7 @@ def __init__(
118120
self.oneagents_config_environment: OneAgentEnvironmentWideConfigService = OneAgentEnvironmentWideConfigService(self.__http_client)
119121
self.oneagents_config_host: OneAgentOnAHostConfigService = OneAgentOnAHostConfigService(self.__http_client)
120122
self.oneagents_config_hostgroup: OneAgentInAHostGroupService = OneAgentInAHostGroupService(self.__http_client)
123+
self.settings : SettingService = SettingService(self.__http_client)
121124
self.plugins: PluginService = PluginService(self.__http_client)
122125
self.problems: ProblemService = ProblemService(self.__http_client)
123126
self.slos: SloService = SloService(self.__http_client)

test/environment_v2/test_settings.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from datetime import datetime
2+
3+
import dynatrace.environment_v2.settings as st
4+
from dynatrace import Dynatrace
5+
from dynatrace.pagination import PaginatedList
6+
payload = {"additionalInformation": [],
7+
"contactDetails":[{"email": 'unittest@contoso.com', "integrationType": "EMAIL"}],
8+
"description": 'unittest',
9+
"identifier":'unittest',
10+
"links": [],
11+
"name": 'unittest',
12+
"responsibilities": {"development": False,
13+
"infrastructure": False,
14+
"lineOfBusiness": True,
15+
"operations": False,
16+
"security": False},
17+
"supplementaryIdentifiers": [] }
18+
def test_list_objects(dt: Dynatrace):
19+
settings = dt.settings.list_objects(schema_id="builtin:ownership.teams")
20+
assert isinstance(settings, PaginatedList)
21+
assert len(list(settings)) == 1
22+
assert all(isinstance(s, st.Settings) for s in settings)
23+
24+
def test_get_object(dt: Dynatrace):
25+
setting = dt.settings.get_object(object_id="vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJGVjN2UyNTdhLWM5MTktM2YzMC05NWFiLTliMzNkMmQwZGRkY77vVN4V2t6t")
26+
assert isinstance(setting, st.Settings)
27+
28+
def test_post_object(dt: Dynatrace):
29+
30+
response = dt.settings.create_object(external_id='unittest',object_id='unittest',schema_id="builtin:ownership.teams",schema_version="1.0.6",scope="environment", value=payload,validate_only=False)
31+
assert response[0].get("code") == 200
32+
assert response[0].get("code") is not None
33+
34+
def test_put_object(dt: Dynatrace):
35+
payload["identifier"] = "unittestupdate"
36+
response = dt.settings.update_object("unittest",payload)
37+
print(response)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"items": [
3+
4+
{
5+
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJGVjN2UyNTdhLWM5MTktM2YzMC05NWFiLTliMzNkMmQwZGRkY77vVN4V2t6t",
6+
"value": {
7+
"name": "user",
8+
"description": "user",
9+
"identifier": "user",
10+
"supplementaryIdentifiers": [],
11+
"responsibilities": {
12+
"development": false,
13+
"security": false,
14+
"operations": false,
15+
"infrastructure": true,
16+
"lineOfBusiness": false
17+
},
18+
"contactDetails": [
19+
{
20+
"integrationType": "EMAIL",
21+
"email": "test@contoso.com"
22+
}
23+
],
24+
"links": [],
25+
"additionalInformation": []
26+
}
27+
}
28+
],
29+
"totalCount": 1,
30+
"pageSize": 500
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJGVjN2UyNTdhLWM5MTktM2YzMC05NWFiLTliMzNkMmQwZGRkY77vVN4V2t6t",
3+
"value": {
4+
"name": "user",
5+
"description": "user",
6+
"identifier": "user",
7+
"supplementaryIdentifiers": [],
8+
"responsibilities": {
9+
"development": false,
10+
"security": false,
11+
"operations": false,
12+
"infrastructure": true,
13+
"lineOfBusiness": false
14+
},
15+
"contactDetails": [
16+
{
17+
"integrationType": "EMAIL",
18+
"email": "test@contoso.com"
19+
}
20+
],
21+
"links": [],
22+
"additionalInformation": []
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"code": 200,
4+
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJDVhNjk1MzViLWQ3NDYtMzVmYi05MDdjLTM2ODNkMmMyNWQzOb7vVN4V2t6t"
5+
}
6+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"code": 200,
3+
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJDVhNjk1MzViLWQ3NDYtMzVmYi05MDdjLTM2ODNkMmMyNWQzOb7vVN4V2t6t"
4+
}

0 commit comments

Comments
 (0)