def setUp(self): self.alias = os.environ["ALIAS"] self.key = os.environ["KEY"] self.secret = os.environ["SECRET"] self.time = str(int(time.mktime(time.gmtime()))) self.max = MaxCDN(self.alias, self.key, self.secret)
def purge_maxcdn_zone(zone_name): try: account, zone_id = get_maxcdn_zone_id(zone_name) except ValueError: return get_maxcdn_zone_id(zone_name) api = MaxCDN(CREDENTIALS['maxcdn'][account]['alias'], CREDENTIALS['maxcdn'][account]['consumer_key'], CREDENTIALS['maxcdn'][account]['consumer_secret']) return api.purge(zone_id)
def setUp(self): self.alias = "test_alias" self.key = "test_key" self.secret = "test_secret" self.server = "rws.example.com" self.maxcdn = MaxCDN(self.alias, self.key, self.secret, server=self.server)
def __init__(self, alias, key, secret, **kwargs): """ Establish a connection to MaxCDN using the given alias, key and secret. Additional parameters accepted by the MaxCDN API client can be given in **kwargs and will be passed through unmodified. :param str alias: The alias for the MaxCDN API :param str key: The key for the MaxCDN API :param str secret: The secret for the MaxCDN API """ self._api = MaxCDN(alias, key, secret, **kwargs)
def fetch_maxcdn_zones(): data = dict() data['maxcdn'] = dict() for account in CREDENTIALS['maxcdn']: data['maxcdn'][account] = dict() api = MaxCDN(CREDENTIALS['maxcdn'][account]['alias'], CREDENTIALS['maxcdn'][account]['consumer_key'], CREDENTIALS['maxcdn'][account]['consumer_secret']) # page_size=100 because default value is 50 zones = api.get("/zones/pull.json?page_size=100") for zone in zones["data"]["pullzones"]: zone_name = zone['name'] zone_id = zone['id'] data['maxcdn'][account][zone_name] = zone_id return data
cdn_consumer_key = sys.argv[3] cdn_consumer_secret = sys.argv[4] cdn_zone_id = sys.argv[5] dist_path = os.path.join(PROJECT_PATH, 'dist') releases = get_paths_list(dist_path, '/releases/%s/' % version) legacy_versioned_js = get_paths_list(os.path.join(dist_path, 'js'), '/js/%s/' % version) legacy_versioned_css = get_paths_list(os.path.join(dist_path, 'css'), '/css/%s/' % version) legacy_versioned_themes = get_paths_list(os.path.join(dist_path, 'themes'), '/themes/%s/' % version) legacy_schemas = [ '/schemas/%s/json-schema.json' % version, '/schemas/%s/xml-schema.xsd' % version ] paths = releases paths += legacy_versioned_js + legacy_versioned_css paths += legacy_versioned_themes + legacy_schemas print "Invalidate following files:" print paths pieces = list(split(paths, 200)) for piece in pieces: api = MaxCDN(cdn_alias, cdn_consumer_key, cdn_consumer_secret) api.purge(cdn_zone_id, piece)
import os from maxcdn import MaxCDN alias = os.environ["ALIAS"] key = os.environ["KEY"] secret = os.environ["SECRET"] maxcdn = MaxCDN(alias, key, secret) def get_logs(): maxcdn.get("v3/reporing/logs.json") def get_users(): maxcdn.get("users.json") def get_account(): maxcdn.get("account.json") def get_pullzones(): maxcdn.get("zones/pull.json") if __name__ == '__main__': import timeit for f in ['get_logs', 'get_users', 'get_account', 'get_pullzones']: t = timeit.Timer(f + "()", setup="from __main__ import " + f) print("%-20s %5.0fms" % (f + ":", (t.timeit(number=1) * 1000)))
#!/usr/bin/python ''' An actual working MaxCDN python script to pull raw log information from MaxCDNs API this is a slightly altered version from https://www.maxcdn.com/one/tutorial/trivial-way-to-manage-maxcdn-account-with-python/ A list of their APIs https://docs.maxcdn.com/ ''' from maxcdn import MaxCDN # You will have to replace the following with the API information api = MaxCDN("alias", "key", "secret") print "Hit [ENTER] to get it" while raw_input() != "exit": def fetch(dfrom, dto, option, value, zid): if option != "": option = "&" + option + "=" + value if zid != "": zid = "&zone_id=" + zid # Needed to add zid to this variable Original did not have it listed data = api.get('/v3/reporting/logs.json?start=' + dfrom + '&end=' + dto + option + zid) records = data['records'] lines = len(records) for i in range(0, lines): print "\nZone ID: " print records[i]['zone_id'] print "Source IP: " print records[i]['client_ip'] # Added the Time and Status
import logging from django.conf import settings log = logging.getLogger(__name__) CDN_SERVICE = getattr(settings, 'CDN_SERVICE', None) CDN_USERNAME = getattr(settings, 'CDN_USERNAME', None) CDN_KEY = getattr(settings, 'CDN_KEY', None) CDN_SECET = getattr(settings, 'CDN_SECET', None) CDN_ID = getattr(settings, 'CDN_ID', None) if CDN_USERNAME and CDN_KEY and CDN_SECET and CDN_ID and CDN_SERVICE == 'maxcdn': from maxcdn import MaxCDN api = MaxCDN(CDN_USERNAME, CDN_KEY, CDN_SECET) def purge(files): return api.purge(CDN_ID, files) else: def purge(files): log.error("CDN not configured, can't purge files")
except: zoneid = None if not "ALIAS" in env or not "KEY" in env or not "SECRET" in env: print(dedent("""\ Usage: purge.py zoneid Add credentials to your environment like so: $ export ALIAS=<alias> $ export KEY=<key> $ export SECRET=<secret> """)) exit(1) maxcdn = MaxCDN(env["ALIAS"], env["KEY"], env["SECRET"]) if zoneid is None: zones = maxcdn.get("/zones/pull.json") for zone in zones["data"]["pullzones"]: print("Purging zone: %s (%s)" % ( zone["name"], zone["id"])) pp.pprint(maxcdn.purge(zone["id"])) else: print("Purging zone: %s" % (zoneid)) res = maxcdn.purge(zoneid) try: if res["code"] == 200: print("SUCCESS!") else:
zoneid = sys.argv[1] with open(sys.argv[2]) as f: config = json.load(f) if not all(k in config for k in ["key", "secret"]): print("Error: secretsfile does not contain key and/or secret!", file=sys.stderr) sys.exit(1) MAXCDN_ALIAS = "macports" MAXCDN_KEY = config['key'] MAXCDN_SECRET = config['secret'] # Initialize MaxCDN API maxcdn = MaxCDN(MAXCDN_ALIAS, MAXCDN_KEY, MAXCDN_SECRET) # Purge requested zone res = maxcdn.purge(zoneid) if not 'code' in res: print("Error: Unexpected response:", file=sys.stderr) pp.pprint(res, file=sys.stderr) sys.exit(1) elif res['code'] == 200: print("Zone {} purged.".format(zoneid)) else: print("Purging of zone {} failed with code: " + res['code'], file=sys.stderr) sys.exit(1)
companyalias = "<my_company_name_here>" # zone_type would be either pull or push - pull indicates ==> pull zones zone_type = "pull" # newly created "Purge-Cache" Application has been created # with permissions to be able to work with MaxCDN via API. # Below are the consumer_key and consumer_secret for that. consumer_key = "<consumer_key_here>" consumer_secret = "<secret_key_here>" #zones = [] # pull zones list.. ie., comma seperated pull zone numbers # pull zone ids list. zones = [123456,234567] api = MaxCDN(companyalias, consumer_key, consumer_secret) for zone_id in zones: print("Purging zone: %s" % (zone_id)) res = api.purge(zone_id) try: if res["code"] == 200: print("SUCCESS!") else: print("Failed with code: " + res["code"]) exit(res["code"]) except KeyError: print("Something went terribly wrong!") pp.pprint(res) exit(1)
from maxcdn import MaxCDN api = MaxCDN('windsmobi', '', '') # Purge All Cache api.delete('/zones/pull.json/470828/cache') print('MaxCDN cache purged')