Skip to content

Commit

Permalink
Merge pull request #62 from wp-media/MathieuLamiot/issue49
Browse files Browse the repository at this point in the history
Provide an endpoint to automate the update of the IP list for WP-Rocket
  • Loading branch information
MathieuLamiot authored Dec 28, 2023
2 parents 999443a + 0df6c82 commit d02dc58
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
10 changes: 10 additions & 0 deletions sources/TechTeamBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sources.listeners.SlackInteractionListener import SlackInteractionListener
from sources.listeners.SlackCommandListener import SlackCommandListener
from sources.listeners.GithubWebhookListener import GithubWebhookListener
from sources.listeners.SupportListener import SupportListener
import sources.utils.Constants as cst


Expand Down Expand Up @@ -69,6 +70,14 @@ def __setup_github_webhook_endpoint(self):
github_webhook_endpoint = GithubWebhookListener()
self.add_endpoint("/github/webhook", endpoint_name='github_webhook', handler=github_webhook_endpoint, methods=['POST'])

def __setup_support_enpoints(self):
"""
Creates the endpoints for the Support team
"""
support_listener = SupportListener()
self.add_endpoint("/support/wprocket-ips", endpoint_name='support_wprocket_ips',
handler=support_listener.get_wprocket_ips, methods=['GET'])

def __load_config(self):
with open(Path(__file__).parent.parent / "config" / "app.json", encoding='utf-8') as file_app_config:
self.__app_config = json.load(file_app_config)
Expand All @@ -82,6 +91,7 @@ def setup(self):
self.__setup_slack_interaction_endpoint()
self.__setup_slack_command_endpoint()
self.__setup_github_webhook_endpoint()
self.__setup_support_enpoints()

def run(self, **kwargs):
self.app.run(port=self.__app_config['port'], **kwargs)
11 changes: 9 additions & 2 deletions sources/handlers/ServerListHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def __init__(self):
self.slack_message_factory = SlackMessageFactory()
self.ovh_api_factory = OvhApiFactory()

def send_wp_rocket_ips(self, app_context, slack_user):
def generate_wp_rocket_ips(self, app_context):
"""
List all IPs used for WP Rocket and sends it in a Slack DM
Generates a text list of all IPs used by WP Rocket
"""
text = "List of IPs used for WP Rocket:\n\n"

Expand Down Expand Up @@ -72,4 +72,11 @@ def send_wp_rocket_ips(self, app_context, slack_user):
# https://gitlab.one.com/systems/chef-repo/-/blob/master/roles/onecom-global-firewall-macros.json#L173
text += "46.30.212.64\n46.30.212.65\n46.30.212.66\n46.30.212.67\n46.30.212.68\n46.30.212.69\n46.30.211.85\n"

return text

def send_wp_rocket_ips_to_slack(self, app_context, slack_user):
"""
List all IPs used for WP Rocket and sends it in a Slack DM
"""
text = self.generate_wp_rocket_ips(app_context)
self.slack_message_factory.post_message(app_context, slack_user, text)
2 changes: 1 addition & 1 deletion sources/handlers/SlackCommandHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def wp_rocket_ips_command_callback(self, payload_json):

current_app.logger.info("wp_rocket_ips_command_callback: Starting processing thread...")
thread = Thread(
target=self.server_list_handler.send_wp_rocket_ips, kwargs={
target=self.server_list_handler.send_wp_rocket_ips_to_slack, kwargs={
"app_context": current_app.app_context(), "slack_user": initiator})
thread.start()

Expand Down
25 changes: 25 additions & 0 deletions sources/listeners/SupportListener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
This module defines the endpoint handler (called listener) for the Support team endpoints.
"""

from flask import current_app
from sources.handlers.ServerListHandler import ServerListHandler


class SupportListener():
"""
Class to define the support endpoints handler. It is callable and called when the right url is used.
"""

def __init__(self):
"""
The listener instanciates the handlers it will pass the request to so that it is processed.
"""
self.server_list_handler = ServerListHandler()

def get_wprocket_ips(self):
"""
Method generating the list of IPs used by WP Rocket and returning it in a list as a string.
"""
response_payload = self.server_list_handler.generate_wp_rocket_ips(app_context=current_app.app_context())
return response_payload, 200

0 comments on commit d02dc58

Please sign in to comment.