-
Notifications
You must be signed in to change notification settings - Fork 2
CCS-4538: Provide an endpoint in Git2Pantheon that would handle cache clearing for both Drupal and Akamai #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aprajshekhar
wants to merge
6
commits into
redhataccess:master
Choose a base branch
from
aprajshekhar:ap_CCS-4538_cache_clear_pr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39d5b5b
CCS-4538: Provide an endpoint in Git2Pantheon that would handle cache…
aprajshekhar f8ccc4c
CCS-4538: Provide an endpoint in Git2Pantheon that would handle cache…
aprajshekhar 753ff9e
CCS-4538: Provide an endpoint in Git2Pantheon that would handle cache…
aprajshekhar 8ede372
Update setup.py
rednitish 8cf2cec
Update setup.py
rednitish 928ae56
Due to a bug underlying framework, JSON body wasn't getting created c…
rednitish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| Clears the drupal and akamai cache | ||
| --- | ||
| tags: | ||
| - Clears the drupal and akamai cache. | ||
|
|
||
| parameters: | ||
| - in: body | ||
| name: body | ||
| description: 'The structure containing list of URLs of modules and assemblies whose cache has to be cleared' | ||
| required: true | ||
| schema: | ||
| $ref: '#/definitions/CacheClearData' | ||
|
|
||
| reponses: | ||
| 200: | ||
| description: 'The status of upload corresponding to the key' | ||
| schema: | ||
| $ref: '#/definitions/UploaderKey' | ||
| 400: | ||
| description: 'Invalid content error' | ||
| schema: | ||
| $ref: '#/definitions/Error' | ||
| 500: | ||
| description: 'Internal server error' | ||
| schema: | ||
| $ref: '#/definitions/Error' | ||
|
|
||
|
|
||
| definitions: | ||
| CacheClearData: | ||
| type: object | ||
| properties: | ||
| assemblies: | ||
| type: array | ||
| items: | ||
| type: string | ||
| modules: | ||
| type: array | ||
| items: | ||
| type: string | ||
| Error: | ||
| type: object | ||
| properties: | ||
| code: | ||
| type: string | ||
| description: 'HTTP status code of the error' | ||
| message: | ||
| type: string | ||
| description: 'Error message' | ||
| details: | ||
| type: string | ||
| description: 'Error details' | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import requests, logging, json | ||
| from ..client import RestClient | ||
| from akamai.edgegrid import EdgeGridAuth | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AkamaiCachePurgeClient: | ||
| def __init__(self,host, client_token, client_secret, access_token): | ||
| self.session: requests.Session = requests.Session() | ||
| self.session.auth = EdgeGridAuth(client_token=client_token, client_secret=client_secret, | ||
| access_token=access_token) | ||
| self.host = host | ||
| self.akamai_rest_client = RestClient(auth_session=self.session, verbose=True, | ||
| base_url=self.host) | ||
|
|
||
| def purge(self, purge_obj, action='delete'): | ||
| logger.info('Adding %s request to the queue for %s' % (action, json.dumps(purge_obj))) | ||
| return self.akamai_rest_client.post('/ccu/v3/delete/url', json.dumps(purge_obj)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| from urllib import parse | ||
|
|
||
| import json | ||
| import logging | ||
| import requests | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class RestClient: | ||
| def __init__(self, auth_session, verbose, base_url): | ||
| self.auth_session: requests.Session = auth_session | ||
| self.verbose = verbose | ||
| self.base_url = base_url | ||
| self.errors = self.init_errors_dict() | ||
|
|
||
| def join_path(self, path): | ||
| return parse.urljoin(self.base_url, path) | ||
|
|
||
| def get(self, endpoint, params=None): | ||
| response = self.auth_session.get(self.join_path(endpoint), params=params) | ||
| self.process_response(endpoint, response) | ||
| return response.json() | ||
|
|
||
| def log_verbose(self, endpoint, response): | ||
| if self.verbose: | ||
| logger.info( | ||
| 'status=' + str(response.status_code) + ' for endpoint=' + endpoint + | ||
| ' with content type=' + response.headers['content-type'] | ||
| ) | ||
| logger.info("response body=" + json.dumps(response.json(), indent=2)) | ||
|
|
||
| def post(self, endpoint, body, params=None): | ||
| headers = {"content-type": "application/json"} | ||
| response = self.auth_session.post(self.join_path(endpoint), data=body, headers=headers, params=params) | ||
| self.process_response(endpoint, response) | ||
| return response.json() | ||
|
|
||
| def process_response(self, endpoint, response): | ||
| self.check_error(response, endpoint) | ||
| self.log_verbose(endpoint, response) | ||
|
|
||
| @staticmethod | ||
| def init_errors_dict(): | ||
| return { | ||
| 404: "Call to {URI} failed with a 404 result\n with details: {details}\n", | ||
| 403: "Call to {URI} failed with a 403 result\n with details: {details}\n", | ||
| 401: "Call to {URI} failed with a 401 result\n with details: {details}\n", | ||
| 400: "Call to {URI} failed with a 400 result\n with details: {details}\n" | ||
| } | ||
|
|
||
| def check_error(self, response, endpoint): | ||
| if not 200 >= response.status_code >= 400: | ||
| return | ||
| message = self.errors.get(response.status_code) | ||
| if message: | ||
| raise Exception(message.format(URI=self.join_path(endpoint), details=response.json())) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from decorest import RestClient, GET, on, header, POST, body | ||
|
|
||
|
|
||
| class DrupalClient(RestClient): | ||
| def __init__(self, *args, **kwargs): | ||
| super(DrupalClient, self).__init__(*args, **kwargs) | ||
|
|
||
| @GET('/api/cache_clear/topic/{guid}') | ||
| @header('accept', 'application/json') | ||
| def purge_cache_module(self, guid): | ||
| """Purge the drupal cache for module""" | ||
|
|
||
| @GET('/api/cache_clear/guide/{guid}') | ||
| @header('accept', 'application/json') | ||
| def purge_cache_assembly(self, guid): | ||
| """Purge the drupal cache for module""" | ||
|
|
||
| @POST('/api/cache_clear/topic') | ||
| @header('content-type', 'application/json') | ||
| @header('accept', 'application/json') | ||
| @body('ids') | ||
| def purge_cache_module_bulk(self, ids): | ||
| """Purge the drupal cache for module""" | ||
|
|
||
| @GET('/api/cache_clear/guide') | ||
| @header('content-type', 'application/json') | ||
| @header('accept', 'application/json') | ||
| @body('ids') | ||
| def purge_cache_assembly_bulk(self, ids): | ||
| """Purge the drupal cache for module""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.