Skip to content

Commit 08ca596

Browse files
Merge pull request #1086 from acamacho82/#882
#882 - Added table which shows the status of each url
2 parents 5c3313e + 9e1af1b commit 08ca596

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

SoftLayer/CLI/cdn/purge.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@
55

66
import SoftLayer
77
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import formatting
89

910

1011
@click.command()
1112
@click.argument('account_id')
1213
@click.argument('content_url', nargs=-1)
1314
@environment.pass_env
1415
def cli(env, account_id, content_url):
15-
"""Purge cached files from all edge nodes."""
16+
"""Purge cached files from all edge nodes.
17+
18+
Examples:
19+
slcli cdn purge 97794 http://example.com/cdn/file.txt
20+
slcli cdn purge 97794 http://example.com/cdn/file.txt https://dal01.example.softlayer.net/image.png
21+
"""
1622

1723
manager = SoftLayer.CDNManager(env.client)
18-
manager.purge_content(account_id, content_url)
24+
content_list = manager.purge_content(account_id, content_url)
25+
26+
table = formatting.Table(['url', 'status'])
27+
28+
for content in content_list:
29+
table.add_row([
30+
content['url'],
31+
content['statusCode']
32+
])
33+
34+
env.fout(table)

SoftLayer/fixtures/SoftLayer_Network_ContentDelivery_Account.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
loadContent = True
3434

35-
purgeContent = True
35+
purgeContent = [
36+
{'url': 'http://z/img/0z020.png', 'statusCode': 'SUCCESS'},
37+
{'url': 'http://y/img/0z010.png', 'statusCode': 'FAILED'},
38+
{'url': 'http://', 'statusCode': 'INVALID_URL'}
39+
]
3640

37-
purgeCache = True
41+
purgeCache = [{'url': 'http://example.com', 'statusCode': 'SUCCESS'}]

SoftLayer/managers/cdn.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,16 @@ def purge_content(self, account_id, urls):
129129
be purged.
130130
:param urls: a string or a list of strings representing the CDN URLs
131131
that should be purged.
132-
:returns: true if all purge requests were successfully submitted;
133-
otherwise, returns the first error encountered.
132+
:returns: a list of SoftLayer_Container_Network_ContentDelivery_PurgeService_Response objects
133+
which indicates if the purge for each url was SUCCESS, FAILED or INVALID_URL.
134134
"""
135135

136136
if isinstance(urls, six.string_types):
137137
urls = [urls]
138138

139+
content_list = []
139140
for i in range(0, len(urls), MAX_URLS_PER_PURGE):
140-
result = self.account.purgeCache(urls[i:i + MAX_URLS_PER_PURGE],
141-
id=account_id)
142-
if not result:
143-
return result
141+
content = self.account.purgeCache(urls[i:i + MAX_URLS_PER_PURGE], id=account_id)
142+
content_list.extend(content)
144143

145-
return True
144+
return content_list

tests/CLI/modules/cdn_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def test_load_content(self):
4949
def test_purge_content(self):
5050
result = self.run_command(['cdn', 'purge', '1234',
5151
'http://example.com'])
52-
52+
expected = [{"url": "http://example.com", "status": "SUCCESS"}]
5353
self.assert_no_fail(result)
54-
self.assertEqual(result.output, "")
54+
self.assertEqual(json.loads(result.output), expected)
5555

5656
def test_list_origins(self):
5757
result = self.run_command(['cdn', 'origin-list', '1234'])

tests/managers/cdn_tests.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77
import math
8+
import mock
89

910
from SoftLayer import fixtures
1011
from SoftLayer.managers import cdn
@@ -110,25 +111,30 @@ def test_purge_content(self):
110111
math.ceil(len(urls) / float(cdn.MAX_URLS_PER_PURGE)))
111112

112113
def test_purge_content_failure(self):
113-
urls = ['http://z/img/0x004.png',
114+
urls = ['http://',
114115
'http://y/img/0x002.png',
115116
'http://x/img/0x001.png']
116117

117-
mock = self.set_mock('SoftLayer_Network_ContentDelivery_Account',
118-
'purgeCache')
119-
mock.return_value = False
118+
contents = [
119+
{'url': urls[0], 'statusCode': 'INVALID_URL'},
120+
{'url': urls[1], 'statusCode': 'FAILED'},
121+
{'url': urls[2], 'statusCode': 'FAILED'}
122+
]
120123

121-
self.cdn_client.purge_content(12345, urls)
122-
calls = self.calls('SoftLayer_Network_ContentDelivery_Account',
123-
'purgeCache')
124-
self.assertEqual(len(calls),
125-
math.ceil(len(urls) / float(cdn.MAX_URLS_PER_PURGE)))
124+
self.cdn_client.account = mock.Mock()
125+
self.cdn_client.account.purgeCache.return_value = contents
126+
127+
result = self.cdn_client.purge_content(12345, urls)
128+
129+
self.assertEqual(contents, result)
126130

127131
def test_purge_content_single(self):
128132
url = 'http://geocities.com/Area51/Meteor/12345/under_construction.gif'
133+
self.cdn_client.account = mock.Mock()
134+
self.cdn_client.account.purgeCache.return_value = [{'url': url, 'statusCode': 'SUCCESS'}]
129135

130-
self.cdn_client.purge_content(12345, url)
131-
self.assert_called_with('SoftLayer_Network_ContentDelivery_Account',
132-
'purgeCache',
133-
args=([url],),
134-
identifier=12345)
136+
expected = [{'url': url, 'statusCode': 'SUCCESS'}]
137+
138+
result = self.cdn_client.purge_content(12345, url)
139+
140+
self.assertEqual(expected, result)

0 commit comments

Comments
 (0)