-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from redhataccess/ap_CCS-4538_cache_clear_pr
CCS-4538: Provide an endpoint in Git2Pantheon that would handle cache…
- Loading branch information
Showing
14 changed files
with
308 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.