Skip to content

Commit

Permalink
Change behavior, add collector when not assigned on announce
Browse files Browse the repository at this point in the history
  • Loading branch information
joente committed May 30, 2023
1 parent 92bd30b commit db76fd5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion pylibagent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,35 @@ async def announce(self, asset_name: Optional[str] = None,
async with ClientSession(headers=self._headers) as session:
async with session.get(
url,
params={'field': 'name'},
params={'fields': 'name', 'collectors': 'key'},
ssl=self.verify_ssl) as r:
if r.status != 200:
msg = await r.text()
raise Exception(f'{msg} (error code: {r.status})')

resp = await r.json()
name = resp["name"]
collectors = resp["collectors"]

for collector in collectors:
if collector['key'] == self.key:
break
else:
# The collector is not assigned yet
url = _join(
self.api_uri,
f'asset/{self.asset_id}/collector/{self.key}')
try:
async with ClientSession(headers=self._headers) as session:
async with session.post(url, ssl=self.verify_ssl) as r:
if r.status != 204:
msg = await r.text()
raise Exception(
f'{msg} (error code: {r.status})')
except Exception as e:
msg = str(e) or type(e).__name__
logging.error(f'failed to assign collector: {msg}')

logging.info(f'announced agent {name} (Id: {self.asset_id})')
return

Expand Down
2 changes: 1 addition & 1 deletion pylibagent/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.9'
__version__ = '0.2.0'

0 comments on commit db76fd5

Please sign in to comment.