mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
modules p*: use f-strings (#10974)
* modules p*: use f-strings * add changelog frag
This commit is contained in:
@@ -115,18 +115,18 @@ class PagerDutyUser(object):
|
||||
except PDClientError as e:
|
||||
if e.response.status_code == 400:
|
||||
self._module.fail_json(
|
||||
msg="Failed to add %s due to invalid argument" % (pd_name))
|
||||
msg=f"Failed to add {pd_name} due to invalid argument")
|
||||
if e.response.status_code == 401:
|
||||
self._module.fail_json(msg="Failed to add %s due to invalid API key" % (pd_name))
|
||||
self._module.fail_json(msg=f"Failed to add {pd_name} due to invalid API key")
|
||||
if e.response.status_code == 402:
|
||||
self._module.fail_json(
|
||||
msg="Failed to add %s due to inability to perform the action within the API token" % (pd_name))
|
||||
msg=f"Failed to add {pd_name} due to inability to perform the action within the API token")
|
||||
if e.response.status_code == 403:
|
||||
self._module.fail_json(
|
||||
msg="Failed to add %s due to inability to review the requested resource within the API token" % (pd_name))
|
||||
msg=f"Failed to add {pd_name} due to inability to review the requested resource within the API token")
|
||||
if e.response.status_code == 429:
|
||||
self._module.fail_json(
|
||||
msg="Failed to add %s due to reaching the limit of making requests" % (pd_name))
|
||||
msg=f"Failed to add {pd_name} due to reaching the limit of making requests")
|
||||
|
||||
# delete a user account from PD
|
||||
def delete_user(self, pd_user_id, pd_name):
|
||||
@@ -137,17 +137,17 @@ class PagerDutyUser(object):
|
||||
except PDClientError as e:
|
||||
if e.response.status_code == 404:
|
||||
self._module.fail_json(
|
||||
msg="Failed to remove %s as user was not found" % (pd_name))
|
||||
msg=f"Failed to remove {pd_name} as user was not found")
|
||||
if e.response.status_code == 403:
|
||||
self._module.fail_json(
|
||||
msg="Failed to remove %s due to inability to review the requested resource within the API token" % (pd_name))
|
||||
msg=f"Failed to remove {pd_name} due to inability to review the requested resource within the API token")
|
||||
if e.response.status_code == 401:
|
||||
# print out the list of incidents
|
||||
pd_incidents = self.get_incidents_assigned_to_user(pd_user_id)
|
||||
self._module.fail_json(msg="Failed to remove %s as user has assigned incidents %s" % (pd_name, pd_incidents))
|
||||
self._module.fail_json(msg=f"Failed to remove {pd_name} as user has assigned incidents {pd_incidents}")
|
||||
if e.response.status_code == 429:
|
||||
self._module.fail_json(
|
||||
msg="Failed to remove %s due to reaching the limit of making requests" % (pd_name))
|
||||
msg=f"Failed to remove {pd_name} due to reaching the limit of making requests")
|
||||
|
||||
# get incidents assigned to a user
|
||||
def get_incidents_assigned_to_user(self, pd_user_id):
|
||||
@@ -169,7 +169,7 @@ class PagerDutyUser(object):
|
||||
team_info = self._apisession.find('teams', team, attribute='name')
|
||||
if team_info is not None:
|
||||
try:
|
||||
updated_team = self._apisession.rput('/teams/' + team_info['id'] + '/users/' + pd_user_id, json={
|
||||
updated_team = self._apisession.rput(f"/teams/{team_info['id']}/users/{pd_user_id}", json={
|
||||
'role': pd_role
|
||||
})
|
||||
except PDClientError:
|
||||
@@ -216,7 +216,7 @@ def main():
|
||||
try:
|
||||
session = APISession(access_token)
|
||||
except PDClientError as e:
|
||||
module.fail_json(msg="Failed to authenticate with PagerDuty: %s" % e)
|
||||
module.fail_json(msg=f"Failed to authenticate with PagerDuty: {e}")
|
||||
|
||||
user = PagerDutyUser(module, session)
|
||||
|
||||
@@ -227,14 +227,14 @@ def main():
|
||||
# remove user
|
||||
if not module.check_mode:
|
||||
user.delete_user(user_exists, pd_user)
|
||||
module.exit_json(changed=True, result="Successfully deleted user %s" % pd_user)
|
||||
module.exit_json(changed=True, result=f"Successfully deleted user {pd_user}")
|
||||
else:
|
||||
module.exit_json(changed=False, result="User %s already exists." % pd_user)
|
||||
module.exit_json(changed=False, result=f"User {pd_user} already exists.")
|
||||
|
||||
# in case that the user does not exist
|
||||
else:
|
||||
if state == "absent":
|
||||
module.exit_json(changed=False, result="User %s was not found." % pd_user)
|
||||
module.exit_json(changed=False, result=f"User {pd_user} was not found.")
|
||||
|
||||
else:
|
||||
# add user, adds user with the default notification rule and contact info (email)
|
||||
@@ -244,7 +244,7 @@ def main():
|
||||
pd_user_id = user.does_user_exist(pd_email)
|
||||
# add a user to the team/s
|
||||
user.add_user_to_teams(pd_user_id, pd_teams, pd_role)
|
||||
module.exit_json(changed=True, result="Successfully created & added user %s to team %s" % (pd_user, pd_teams))
|
||||
module.exit_json(changed=True, result=f"Successfully created & added user {pd_user} to team {pd_teams}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user