Debian Codegraph @

ansible_collections/community/general/plugins/modules/heroku_collaborator.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2018, Ansible Project
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: heroku_collaborator
11 short_description: Add or delete app collaborators on Heroku
12 description:
13 - Manages collaborators for Heroku apps.
14 - If set to V(present) and heroku user is already collaborator, then do nothing.
15 - If set to V(present) and heroku user is not collaborator, then add user to app.
16 - If set to V(absent) and heroku user is collaborator, then delete user from app.
17 author:
18 - Marcel Arns (@marns93)
19 requirements:
20 - heroku3
21 extends_documentation_fragment:
22 - community.general._attributes
23 attributes:
24 check_mode:
25 support: full
26 diff_mode:
27 support: none
28 options:
29 api_key:
30 type: str
31 description:
32 - Heroku API key.
33 apps:
34 type: list
35 elements: str
36 description:
37 - List of Heroku App names.
38 required: true
39 suppress_invitation:
40 description:
41 - Suppress email invitation when creating collaborator.
42 type: bool
43 default: false
44 user:
45 type: str
46 description:
47 - User ID or e-mail.
48 required: true
49 state:
50 type: str
51 description:
52 - Create or remove the heroku collaborator.
53 choices: ["present", "absent"]
54 default: "present"
55 notes:
56 - E(HEROKU_API_KEY) and E(TF_VAR_HEROKU_API_KEY) environment variables can be used instead setting O(api_key).
57 - If you use C(check_mode), you can also pass the C(-v) flag to see affected apps in RV(ignore:msg), for example C(["heroku-example-app"]).
58 """
59
60 EXAMPLES = r"""
61 - name: Create a heroku collaborator
62 community.general.heroku_collaborator:
63 api_key: YOUR_API_KEY
64 user: [email protected]
65 apps: heroku-example-app
66 state: present
67
68 - name: An example of using the module in loop
69 community.general.heroku_collaborator:
70 api_key: YOUR_API_KEY
71 user: '{{ item.user }}'
72 apps: '{{ item.apps | default(apps) }}'
73 suppress_invitation: '{{ item.suppress_invitation | default(suppress_invitation) }}'
74 state: '{{ item.state | default("present") }}'
75 with_items:
76 - {user: '[email protected]'}
77 - {state: 'absent', user: '[email protected]', suppress_invitation: false}
78 - {user: '[email protected]', apps: ["heroku-example-app"]}
79 """
80
81 RETURN = """ # """
82
83 from ansible.module_utils.basic import AnsibleModule
84
85 from ansible_collections.community.general.plugins.module_utils._heroku import HerokuHelper
86
87
88 def add_or_delete_heroku_collaborator(module, client):
89 user = module.params["user"]
90 state = module.params["state"]
91 affected_apps = []
92 result_state = False
93
94 for app in module.params["apps"]:
95 if app not in client.apps():
96 module.fail_json(msg=f"App {app} does not exist")
97
98 heroku_app = client.apps()[app]
99
100 heroku_collaborator_list = [collaborator.user.email for collaborator in heroku_app.collaborators()]
101
102 if state == "absent" and user in heroku_collaborator_list:
103 if not module.check_mode:
104 heroku_app.remove_collaborator(user)
105 affected_apps += [app]
106 result_state = True
107 elif state == "present" and user not in heroku_collaborator_list:
108 if not module.check_mode:
109 heroku_app.add_collaborator(user_id_or_email=user, silent=module.params["suppress_invitation"])
110 affected_apps += [app]
111 result_state = True
112
113 return result_state, affected_apps
114
115
116 def main():
117 argument_spec = HerokuHelper.heroku_argument_spec()
118 argument_spec.update(
119 user=dict(required=True, type="str"),
120 apps=dict(required=True, type="list", elements="str"),
121 suppress_invitation=dict(default=False, type="bool"),
122 state=dict(default="present", type="str", choices=["present", "absent"]),
123 )
124 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
125
126 client = HerokuHelper(module).get_heroku_client()
127
128 has_changed, msg = add_or_delete_heroku_collaborator(module, client)
129 module.exit_json(changed=has_changed, msg=msg)
130
131
132 if __name__ == "__main__":
133 main()