Skip to content

Commit

Permalink
making host_tz dynamic based on actual server instead of Default UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedsajid committed Dec 13, 2021
1 parent 7e564e6 commit 21cdfc2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bonfire/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ def run(host,

username = gl_api.username

# We definitely have credentials - can query for host timezone, if not set
# already
# If host_tz is set to default utc, try to retrieve timezone from the server
# which might be UTC but it doesn't hurt
if gl_api.host_tz == 'utc':
try:
gl_api.host_tz = gl_api.host_timezone()
except:
print("Unable to retrieve timezone from server\nUsing default timezone: " + gl_api.host_tz)

# Check if the query should be retrieved from the configuration
if query[0] == ":":
section_name = "query" + query
Expand Down
24 changes: 24 additions & 0 deletions bonfire/graylog_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,34 @@ def search(self, query, fetch_all=False):
result.query_object = query
return result

def node_info(self):
url = "system/cluster/nodes"
return self.get(url=url)

def cluster_info(self):
url = "cluster"
return self.get(url=url)

def user_info(self, username):
url = "users/" + username
return self.get(url=url)

def host_timezone(self):
# Need to retreive server timezoneinfo to be passed to SearchQuery
# Will be using master node's timezone and assuming
# that's used across the cluster

# First getting all the nodes to determine master
masternode = None
for nodes in self.node_info()["nodes"]:
if nodes["is_master"] == True:
masternode = nodes["node_id"]
# There can only be one
break

# timezone of master node
return self.cluster_info()[masternode]["timezone"]

def streams(self):
url = "streams"
return self.get(url=url)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ def test_userinfo():
assert result == expected


@httpretty.activate
def test_nodeinfo():
httpretty.register_uri(httpretty.GET, "http://dummyhost:80/system/cluster/nodes",
body='{"nodes":[{"cluster_id": "testcluster", "node_id": "node1", "is_master": true}], "total": 1}',
content_type="application/json")

# More of some dummy tests now
g = api.GraylogAPI("dummyhost", 80, '/', "dummy", password="dummy")

result = g.node_info()
expected = {"nodes":[{"cluster_id": "testcluster", "node_id": "node1", "is_master": True}], "total": 1}
assert result == expected


@httpretty.activate
def test_clusterinfo():
httpretty.register_uri(httpretty.GET, "http://dummyhost:80/cluster",
body='{"nodeid":{"facility": "graylog-server", "codename": "Noir"}}',
content_type="application/json")

# More of some dummy tests now
g = api.GraylogAPI("dummyhost", 80, '/', "dummy", password="dummy")

result = g.cluster_info()
expected = {"nodeid":{"facility": "graylog-server", "codename": "Noir"}}
assert result == expected


@httpretty.activate
def test_streams():
httpretty.register_uri(httpretty.GET, "http://dummyhost:80/streams",
Expand Down

0 comments on commit 21cdfc2

Please sign in to comment.