Skip to content

Commit

Permalink
Merge pull request #77 from Evolane/settings
Browse files Browse the repository at this point in the history
adde support for v2 api Settings
  • Loading branch information
Dynatrace-James-Kitson committed Jan 5, 2024
2 parents a2b54d3 + a9b6ae3 commit 68b8acd
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions dynatrace/environment_v2/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ def _create_from_raw_data(self, raw_element: Dict[str, Any]):

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

91 changes: 91 additions & 0 deletions dynatrace/environment_v2/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from typing import Optional, Dict, Any

from dynatrace.dynatrace_object import DynatraceObject
from dynatrace.http_client import HttpClient
from dynatrace.pagination import PaginatedList


class SettingService:
ENDPOINT = "/api/v2/settings/objects"

def __init__(self, http_client: HttpClient):
self.__http_client = http_client

def list_objects(self,schema_id: Optional[str] = None,
scope: Optional[str] = None,external_ids: Optional[str] = None,
fields: Optional[str] = None,
filter:Optional[str] = None, sort:Optional[str] = None, page_size:Optional[str] = None) -> PaginatedList["DynatraceObject"]:
"""Lists settings
:return: a list of settings with details
"""
params = {
"schemaIds": schema_id,
"scope": scope,
"fields": fields,
"externalIds": external_ids,
"filter": filter,
"sort": sort,
"pageSize": page_size,
}
return PaginatedList(Settings, self.__http_client, target_url=self.ENDPOINT, list_item="items", target_params=params)

def create_object(self,external_id,object_id,schema_id,schema_version,scope, value,validate_only):
"""Creates a new settings object
:param external_id: External identifier for the object being created
:param object_id: The ID of the settings object that should be replaced. Only applicable if an external identifier
:param object_id: the ID of the object
:param schema_id: The schema on which the object is based
:param schema_version: The version of the schema on which the object is based.
:param scope The scope that the object targets. For more details, please see Dynatrace Documentation.
:param value The value of the setting.
:return: a Settings object
"""
params = {
"validate_only": validate_only,
}
body =[ {
"externalId" : external_id,
"objectId": object_id,
"schemaId": schema_id,
"schemaVersion": schema_version,
"scope": scope,
"value" : value

}]

response = self.__http_client.make_request(self.ENDPOINT,params=body, method="POST",query_params=params).json()
return response


def get_object(self, object_id: str):
"""Gets parameters of specified settings object
:param object_id: the ID of the object
:return: a Settings object
"""
response = self.__http_client.make_request(f"{self.ENDPOINT}/{object_id}").json()
return Settings(raw_element=response)

def update_object(self, object_id: str, value):
"""Updates an existing settings object
:param object_id: the ID of the object
"""
return self.__http_client.make_request(path=f"{self.ENDPOINT}/{object_id}", params=value, method="PUT")

def delete_object(self, object_id: str):
"""Deletes the specified object
:param object_id: the ID of the object
:return: HTTP response
"""
return self.__http_client.make_request(path=f"{self.ENDPOINT}/{object_id}", method="DELETE")

class Settings(DynatraceObject):
def _create_from_raw_data(self, raw_element: Dict[str, Any]):
# Mandatory
self.objectId: str = raw_element["objectId"]
self.value: str = raw_element["value"]
3 changes: 3 additions & 0 deletions dynatrace/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
from dynatrace.environment_v2.problems import ProblemService
from dynatrace.environment_v2.service_level_objectives import SloService
from dynatrace.environment_v2.logs import LogService
from dynatrace.environment_v2.settings import SettingService

from dynatrace.http_client import HttpClient


Expand Down Expand Up @@ -118,6 +120,7 @@ def __init__(
self.oneagents_config_environment: OneAgentEnvironmentWideConfigService = OneAgentEnvironmentWideConfigService(self.__http_client)
self.oneagents_config_host: OneAgentOnAHostConfigService = OneAgentOnAHostConfigService(self.__http_client)
self.oneagents_config_hostgroup: OneAgentInAHostGroupService = OneAgentInAHostGroupService(self.__http_client)
self.settings : SettingService = SettingService(self.__http_client)
self.plugins: PluginService = PluginService(self.__http_client)
self.problems: ProblemService = ProblemService(self.__http_client)
self.slos: SloService = SloService(self.__http_client)
Expand Down
37 changes: 37 additions & 0 deletions test/environment_v2/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from datetime import datetime

import dynatrace.environment_v2.settings as st
from dynatrace import Dynatrace
from dynatrace.pagination import PaginatedList
payload = {"additionalInformation": [],
"contactDetails":[{"email": 'unittest@contoso.com', "integrationType": "EMAIL"}],
"description": 'unittest',
"identifier":'unittest',
"links": [],
"name": 'unittest',
"responsibilities": {"development": False,
"infrastructure": False,
"lineOfBusiness": True,
"operations": False,
"security": False},
"supplementaryIdentifiers": [] }
def test_list_objects(dt: Dynatrace):
settings = dt.settings.list_objects(schema_id="builtin:ownership.teams")
assert isinstance(settings, PaginatedList)
assert len(list(settings)) == 1
assert all(isinstance(s, st.Settings) for s in settings)

def test_get_object(dt: Dynatrace):
setting = dt.settings.get_object(object_id="vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJGVjN2UyNTdhLWM5MTktM2YzMC05NWFiLTliMzNkMmQwZGRkY77vVN4V2t6t")
assert isinstance(setting, st.Settings)

def test_post_object(dt: Dynatrace):

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)
assert response[0].get("code") == 200
assert response[0].get("code") is not None

def test_put_object(dt: Dynatrace):
payload["identifier"] = "unittestupdate"
response = dt.settings.update_object("unittest",payload)
print(response)
31 changes: 31 additions & 0 deletions test/mock_data/GET_api_v2_settings_objects_804a7afa1d2a354.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"items": [

{
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJGVjN2UyNTdhLWM5MTktM2YzMC05NWFiLTliMzNkMmQwZGRkY77vVN4V2t6t",
"value": {
"name": "user",
"description": "user",
"identifier": "user",
"supplementaryIdentifiers": [],
"responsibilities": {
"development": false,
"security": false,
"operations": false,
"infrastructure": true,
"lineOfBusiness": false
},
"contactDetails": [
{
"integrationType": "EMAIL",
"email": "test@contoso.com"
}
],
"links": [],
"additionalInformation": []
}
}
],
"totalCount": 1,
"pageSize": 500
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJGVjN2UyNTdhLWM5MTktM2YzMC05NWFiLTliMzNkMmQwZGRkY77vVN4V2t6t",
"value": {
"name": "user",
"description": "user",
"identifier": "user",
"supplementaryIdentifiers": [],
"responsibilities": {
"development": false,
"security": false,
"operations": false,
"infrastructure": true,
"lineOfBusiness": false
},
"contactDetails": [
{
"integrationType": "EMAIL",
"email": "test@contoso.com"
}
],
"links": [],
"additionalInformation": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"code": 200,
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJDVhNjk1MzViLWQ3NDYtMzVmYi05MDdjLTM2ODNkMmMyNWQzOb7vVN4V2t6t"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 200,
"objectId": "vu9U3hXa3q0AAAABABdidWlsdGluOm93bmVyc2hpcC50ZWFtcwAGdGVuYW50AAZ0ZW5hbnQAJDVhNjk1MzViLWQ3NDYtMzVmYi05MDdjLTM2ODNkMmMyNWQzOb7vVN4V2t6t"
}

0 comments on commit 68b8acd

Please sign in to comment.