Skip to content

Commit 18cbb16

Browse files
Merge pull request #1502 from FernandoOjeda/ft/cdn_edit_option
Add the cdn edit option.
2 parents 706845b + 835c5af commit 18cbb16

File tree

7 files changed

+265
-1
lines changed

7 files changed

+265
-1
lines changed

SoftLayer/CLI/cdn/edit.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""Edit a CDN Account."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import formatting
9+
from SoftLayer.CLI import helpers
10+
11+
12+
@click.command()
13+
@click.argument('identifier')
14+
@click.option('--header', '-H',
15+
type=click.STRING,
16+
help="Host header."
17+
)
18+
@click.option('--http-port', '-t',
19+
type=click.INT,
20+
help="HTTP port."
21+
)
22+
@click.option('--origin', '-o',
23+
type=click.STRING,
24+
help="Origin server address."
25+
)
26+
@click.option('--respect-headers', '-r',
27+
type=click.Choice(['1', '0']),
28+
help="Respect headers. The value 1 is On and 0 is Off."
29+
)
30+
@click.option('--cache', '-c', multiple=True, type=str,
31+
help="Cache key optimization. These are the valid options to choose: 'include-all', 'ignore-all', "
32+
"'include-specified', 'ignore-specified'. If you select 'include-specified' or 'ignore-specified' "
33+
"please add a description too using again --cache, "
34+
"e.g --cache=include-specified --cache=description."
35+
)
36+
@click.option('--performance-configuration', '-p',
37+
type=click.Choice(['General web delivery', 'Large file optimization', 'Video on demand optimization']),
38+
help="Optimize for, General web delivery', 'Large file optimization', 'Video on demand optimization', "
39+
"the Dynamic content acceleration option is not added because this has a special configuration."
40+
)
41+
@environment.pass_env
42+
def cli(env, identifier, header, http_port, origin, respect_headers, cache, performance_configuration):
43+
"""Edit a CDN Account.
44+
45+
Note: You can use the hostname or uniqueId as IDENTIFIER.
46+
"""
47+
48+
manager = SoftLayer.CDNManager(env.client)
49+
cdn_id = helpers.resolve_id(manager.resolve_ids, identifier, 'CDN')
50+
51+
cache_result = {}
52+
if cache:
53+
if len(cache) > 1:
54+
cache_result['cacheKeyQueryRule'] = cache[0]
55+
cache_result['description'] = cache[1]
56+
else:
57+
cache_result['cacheKeyQueryRule'] = cache[0]
58+
59+
cdn_result = manager.edit(cdn_id, header=header, http_port=http_port, origin=origin,
60+
respect_headers=respect_headers, cache=cache_result,
61+
performance_configuration=performance_configuration)
62+
63+
table = formatting.KeyValueTable(['name', 'value'])
64+
table.align['name'] = 'r'
65+
table.align['value'] = 'l'
66+
67+
for cdn in cdn_result:
68+
table.add_row(['Create Date', cdn.get('createDate')])
69+
table.add_row(['Header', cdn.get('header')])
70+
table.add_row(['Http Port', cdn.get('httpPort')])
71+
table.add_row(['Origin Type', cdn.get('originType')])
72+
table.add_row(['Performance Configuration', cdn.get('performanceConfiguration')])
73+
table.add_row(['Protocol', cdn.get('protocol')])
74+
table.add_row(['Respect Headers', cdn.get('respectHeaders')])
75+
table.add_row(['Unique Id', cdn.get('uniqueId')])
76+
table.add_row(['Vendor Name', cdn.get('vendorName')])
77+
table.add_row(['Cache key optimization', cdn.get('cacheKeyQueryRule')])
78+
table.add_row(['cname', cdn.get('cname')])
79+
table.add_row(['Origin server address', cdn.get('originHost')])
80+
81+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
('cdn', 'SoftLayer.CLI.cdn'),
6464
('cdn:detail', 'SoftLayer.CLI.cdn.detail:cli'),
65+
('cdn:edit', 'SoftLayer.CLI.cdn.edit:cli'),
6566
('cdn:list', 'SoftLayer.CLI.cdn.list:cli'),
6667
('cdn:origin-add', 'SoftLayer.CLI.cdn.origin_add:cli'),
6768
('cdn:origin-list', 'SoftLayer.CLI.cdn.origin_list:cli'),

SoftLayer/fixtures/SoftLayer_Network_CdnMarketplace_Configuration_Mapping.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
listDomainMappings = [
22
{
3+
"cacheKeyQueryRule": "include-all",
34
"cname": "cdnakauuiet7s6u6.cdnedge.bluemix.net",
5+
"createDate": "2020-09-29T15:19:01-06:00",
46
"domain": "test.example.com",
57
"header": "test.example.com",
68
"httpPort": 80,
@@ -17,6 +19,7 @@
1719
listDomainMappingByUniqueId = [
1820
{
1921
"cname": "cdnakauuiet7s6u6.cdnedge.bluemix.net",
22+
"performanceConfiguration": "Large file optimization",
2023
"domain": "test.example.com",
2124
"header": "test.example.com",
2225
"httpPort": 80,
@@ -29,3 +32,21 @@
2932
"vendorName": "akamai"
3033
}
3134
]
35+
36+
updateDomainMapping = [
37+
{
38+
"createDate": "2021-02-09T19:32:29-06:00",
39+
"originType": "HOST_SERVER",
40+
"path": "/*",
41+
"performanceConfiguration": "Large file optimization",
42+
"protocol": "HTTP",
43+
"respectHeaders": True,
44+
"uniqueId": "424406419091111",
45+
"vendorName": "akamai",
46+
"header": "www.test.com",
47+
"httpPort": 83,
48+
"cname": "cdn.test.cloud",
49+
"originHost": "1.1.1.1",
50+
"cacheKeyQueryRule": "include: test"
51+
}
52+
]

SoftLayer/managers/cdn.py

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
66
:license: MIT, see LICENSE for more details.
77
"""
8-
8+
import SoftLayer
99
from SoftLayer import utils
1010

1111

12+
# pylint: disable=no-self-use,too-many-lines,too-many-instance-attributes
13+
14+
1215
class CDNManager(utils.IdentifierMixin, object):
1316
"""Manage Content Delivery Networks in the account.
1417
@@ -27,6 +30,7 @@ def __init__(self, client):
2730
self.cdn_path = self.client['SoftLayer_Network_CdnMarketplace_Configuration_Mapping_Path']
2831
self.cdn_metrics = self.client['Network_CdnMarketplace_Metrics']
2932
self.cdn_purge = self.client['SoftLayer_Network_CdnMarketplace_Configuration_Cache_Purge']
33+
self.resolvers = [self._get_ids_from_hostname]
3034

3135
def list_cdn(self, **kwargs):
3236
"""Lists Content Delivery Networks for the active user.
@@ -170,3 +174,90 @@ def start_data(self):
170174
def end_date(self):
171175
"""Retrieve the cdn usage metric end date."""
172176
return self._end_date
177+
178+
def edit(self, identifier, header=None, http_port=None, origin=None,
179+
respect_headers=None, cache=None, performance_configuration=None):
180+
"""Edit the cdn object.
181+
182+
:param string identifier: The CDN identifier.
183+
:param header: The cdn Host header.
184+
:param http_port: The cdn HTTP port.
185+
:param origin: The cdn Origin server address.
186+
:param respect_headers: The cdn Respect headers.
187+
:param cache: The cdn Cache key optimization.
188+
:param performance_configuration: The cdn performance configuration.
189+
190+
:returns: SoftLayer_Container_Network_CdnMarketplace_Configuration_Mapping[].
191+
"""
192+
cdn_instance_detail = self.get_cdn(str(identifier))
193+
194+
config = {
195+
'uniqueId': cdn_instance_detail.get('uniqueId'),
196+
'originType': cdn_instance_detail.get('originType'),
197+
'protocol': cdn_instance_detail.get('protocol'),
198+
'path': cdn_instance_detail.get('path'),
199+
'vendorName': cdn_instance_detail.get('vendorName'),
200+
'cname': cdn_instance_detail.get('cname'),
201+
'domain': cdn_instance_detail.get('domain'),
202+
'httpPort': cdn_instance_detail.get('httpPort'),
203+
'origin': cdn_instance_detail.get('originHost'),
204+
'header': cdn_instance_detail.get('header')
205+
}
206+
207+
if header:
208+
config['header'] = header
209+
210+
if http_port:
211+
config['httpPort'] = http_port
212+
213+
if origin:
214+
config['origin'] = origin
215+
216+
if respect_headers:
217+
config['respectHeaders'] = respect_headers
218+
219+
if cache:
220+
if 'include-specified' in cache['cacheKeyQueryRule']:
221+
cache_key_rule = self.get_cache_key_query_rule('include', cache)
222+
config['cacheKeyQueryRule'] = cache_key_rule
223+
elif 'ignore-specified' in cache['cacheKeyQueryRule']:
224+
cache_key_rule = self.get_cache_key_query_rule('ignore', cache)
225+
config['cacheKeyQueryRule'] = cache_key_rule
226+
else:
227+
config['cacheKeyQueryRule'] = cache['cacheKeyQueryRule']
228+
229+
if performance_configuration:
230+
config['performanceConfiguration'] = performance_configuration
231+
232+
return self.cdn_configuration.updateDomainMapping(config)
233+
234+
def _get_ids_from_hostname(self, hostname):
235+
"""Get the cdn object detail.
236+
237+
:param string hostname: The CDN identifier.
238+
:returns: SoftLayer_Container_Network_CdnMarketplace_Configuration_Mapping[].
239+
"""
240+
result = []
241+
cdn_list = self.cdn_configuration.listDomainMappings()
242+
for cdn in cdn_list:
243+
if cdn.get('domain', '').lower() == hostname.lower():
244+
result.append(cdn.get('uniqueId'))
245+
break
246+
247+
return result
248+
249+
@staticmethod
250+
def get_cache_key_query_rule(cache_type, cache):
251+
"""Get the cdn object detail.
252+
253+
:param string cache_type: Cache type.
254+
:param cache: Cache description.
255+
256+
:return: string value.
257+
"""
258+
if 'description' not in cache:
259+
raise SoftLayer.SoftLayerError('Please add a description to be able to update the'
260+
' cache.')
261+
cache_result = '%s: %s' % (cache_type, cache['description'])
262+
263+
return cache_result

docs/cli/cdn.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ Interacting with CDN
2727
.. click:: SoftLayer.CLI.cdn.purge:cli
2828
:prog: cdn purge
2929
:show-nested:
30+
31+
.. click:: SoftLayer.CLI.cdn.edit:cli
32+
:prog: cdn edit
33+
:show-nested:

tests/CLI/modules/cdn_tests.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,34 @@ def test_remove_origin(self):
9595

9696
self.assert_no_fail(result)
9797
self.assertEqual(result.output, "Origin with path /example1 has been deleted\n")
98+
99+
def test_edit_header(self):
100+
result = self.run_command(['cdn', 'edit', 'test.example.com', '--header=www.test.com'])
101+
self.assert_no_fail(result)
102+
header_result = json.loads(result.output)
103+
self.assertEqual('www.test.com', header_result['Header'])
104+
105+
def test_edit_http_port(self):
106+
result = self.run_command(['cdn', 'edit', 'test.example.com', '--http-port=83'])
107+
self.assert_no_fail(result)
108+
header_result = json.loads(result.output)
109+
self.assertEqual(83, header_result['Http Port'])
110+
111+
def test_edit_respect_headers(self):
112+
result = self.run_command(['cdn', 'edit', 'test.example.com', '--respect-headers=1'])
113+
self.assert_no_fail(result)
114+
header_result = json.loads(result.output)
115+
self.assertEqual(True, header_result['Respect Headers'])
116+
117+
def test_edit_cache(self):
118+
result = self.run_command(['cdn', 'edit', 'test.example.com', '--cache', 'include-specified',
119+
'--cache', 'test'])
120+
self.assert_no_fail(result)
121+
header_result = json.loads(result.output)
122+
self.assertEqual('include: test', header_result['Cache key optimization'])
123+
124+
def test_edit_cache_by_uniqueId(self):
125+
result = self.run_command(['cdn', 'edit', '9934111111111', '--cache', 'include-specified', '--cache', 'test'])
126+
self.assert_no_fail(result)
127+
header_result = json.loads(result.output)
128+
self.assertEqual('include: test', header_result['Cache key optimization'])

tests/managers/cdn_tests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77

8+
from SoftLayer import fixtures
89
from SoftLayer.managers import cdn
910
from SoftLayer import testing
1011

@@ -102,3 +103,37 @@ def test_purge_content(self):
102103
self.assert_called_with('SoftLayer_Network_CdnMarketplace_Configuration_Cache_Purge',
103104
'createPurge',
104105
args=args)
106+
107+
def test_cdn_edit(self):
108+
identifier = '9934111111111'
109+
header = 'www.test.com'
110+
result = self.cdn_client.edit(identifier, header=header)
111+
112+
self.assertEqual(fixtures.SoftLayer_Network_CdnMarketplace_Configuration_Mapping.
113+
updateDomainMapping, result)
114+
115+
self.assert_called_with(
116+
'SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
117+
'updateDomainMapping',
118+
args=({
119+
'uniqueId': '9934111111111',
120+
'originType': 'HOST_SERVER',
121+
'protocol': 'HTTP',
122+
'path': '/',
123+
'vendorName': 'akamai',
124+
'cname': 'cdnakauuiet7s6u6.cdnedge.bluemix.net',
125+
'domain': 'test.example.com',
126+
'httpPort': 80,
127+
'header': 'www.test.com',
128+
'origin': '1.1.1.1'
129+
},)
130+
)
131+
132+
def test_cdn_instance_by_hostname(self):
133+
hostname = 'test.example.com'
134+
result = self.cdn_client._get_ids_from_hostname(hostname)
135+
expected_result = fixtures.SoftLayer_Network_CdnMarketplace_Configuration_Mapping.listDomainMappings
136+
self.assertEqual(expected_result[0]['uniqueId'], result[0])
137+
self.assert_called_with(
138+
'SoftLayer_Network_CdnMarketplace_Configuration_Mapping',
139+
'listDomainMappings',)

0 commit comments

Comments
 (0)