Skip to content

Commit

Permalink
Merge pull request #2054 from ramkishor-ch/issue_2019
Browse files Browse the repository at this point in the history
Sub features and Example were missing in slcli cdn edit, slcli cdn origin-add
  • Loading branch information
allmightyspiff authored Jul 27, 2023
2 parents 882132b + e00e486 commit 28002af
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 59 deletions.
21 changes: 13 additions & 8 deletions SoftLayer/CLI/cdn/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,25 @@
type=click.Choice(['1', '0']),
help="Respect headers. The value 1 is On and 0 is Off."
)
@click.option('--cache', '-c', multiple=True, type=str,
@click.option('--cache', '-c', type=str,
help="Cache key optimization. These are the valid options to choose: 'include-all', 'ignore-all', "
"'include-specified', 'ignore-specified'. If you select 'include-specified' or 'ignore-specified' "
"please add a description too using again --cache, "
"e.g --cache=include-specified --cache=description."
"please add to option --cache-description.\n"
" e.g --cache=include-specified --cache-description=description."
)
@click.option('--cache-description', '-C', type=str,
help="In cache option, if you select 'include-specified' or 'ignore-specified', "
"please add a description too using this option.\n"
"e.g --cache include-specified --cache-description description."
)
@click.option('--performance-configuration', '-p',
type=click.Choice(['General web delivery', 'Large file optimization', 'Video on demand optimization']),
help="Optimize for, General web delivery', 'Large file optimization', 'Video on demand optimization', "
"the Dynamic content acceleration option is not added because this has a special configuration."
)
@environment.pass_env
def cli(env, identifier, header, http_port, https_port, origin, respect_headers, cache, performance_configuration):
def cli(env, identifier, header, http_port, https_port, origin, respect_headers, cache,
cache_description, performance_configuration):
"""Edit a CDN Account.
Note: You can use the hostname or uniqueId as IDENTIFIER.
Expand All @@ -53,15 +59,14 @@ def cli(env, identifier, header, http_port, https_port, origin, respect_headers,
cdn_id = helpers.resolve_id(manager.resolve_ids, identifier, 'CDN')

cache_result = {}
if cache:
if cache or cache_description:
if len(cache) > 1:
cache_result['cacheKeyQueryRule'] = cache[0]
cache_result['description'] = cache[1]
cache_result['cacheKeyQueryRule'] = cache
else:
cache_result['cacheKeyQueryRule'] = cache[0]

cdn_result = manager.edit(cdn_id, header=header, http_port=http_port, https_port=https_port, origin=origin,
respect_headers=respect_headers, cache=cache_result,
respect_headers=respect_headers, cache=cache_result, cache_description=cache_description,
performance_configuration=performance_configuration)

table = formatting.KeyValueTable(['name', 'value'])
Expand Down
40 changes: 30 additions & 10 deletions SoftLayer/CLI/cdn/origin_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,34 @@
@click.option('--bucket-name', '-b',
type=click.STRING,
help="The name of the available resource [required if --origin-type=storage]")
@click.option('--port', '-p',
@click.option('--http-port', '-p',
type=click.INT,
help="The http port number.",
default=80,
show_default=True)
help="The http port number. [http or https is required]")
@click.option('--https-port', '-s',
type=click.INT,
help="The https port number. [http or https is required]"
)
@click.option('--protocol', '-P',
type=click.STRING,
help="The protocol used by the origin.",
default='http',
show_default=True)
@click.option('--optimize-for', '-o',
type=click.Choice(['web', 'video', 'file']),
type=click.Choice(['web', 'video', 'file', 'dynamic']),
help="Performance configuration",
default='web',
show_default=True)
@click.option('--dynamic-path', '-d',
help="The path that Akamai edge servers periodically fetch the test object from."
"example = /detection-test-object.html")
@click.option('--compression', '-i',
help="Enable or disable compression of JPEG images for requests over certain network conditions.",
default='true',
show_default=True)
@click.option('--prefetching', '-g',
help="Enable or disable the embedded object prefetching feature.",
default='true',
show_default=True)
@click.option('--extensions', '-e',
type=click.STRING,
help="File extensions that can be stored in the CDN, example: 'jpg, png, pdf'")
Expand All @@ -50,7 +63,9 @@
show_default=True)
@environment.pass_env
def cli(env, unique_id, origin, path, origin_type, header,
bucket_name, port, protocol, optimize_for, extensions, cache_query):
bucket_name, http_port, https_port, protocol, optimize_for,
dynamic_path, compression, prefetching,
extensions, cache_query):
"""Create an origin path for an existing CDN mapping.
For more information see the following documentation: \n
Expand All @@ -62,10 +77,12 @@ def cli(env, unique_id, origin, path, origin_type, header,
if origin_type == 'storage' and not bucket_name:
raise exceptions.ArgumentError('[-b | --bucket-name] is required when [-t | --origin-type] is "storage"')

result = manager.add_origin(unique_id, origin, path, origin_type=origin_type,
header=header, port=port, protocol=protocol,
result = manager.add_origin(unique_id, origin, path, dynamic_path, origin_type=origin_type,
header=header, http_port=http_port, https_port=https_port, protocol=protocol,
bucket_name=bucket_name, file_extensions=extensions,
optimize_for=optimize_for, cache_query=cache_query)
optimize_for=optimize_for,
compression=compression, prefetching=prefetching,
cache_query=cache_query)

table = formatting.Table(['Item', 'Value'])
table.align['Item'] = 'r'
Expand All @@ -78,8 +95,11 @@ def cli(env, unique_id, origin, path, origin_type, header,

table.add_row(['Origin', result['origin']])
table.add_row(['Origin Type', result['originType']])
table.add_row(['Header', result['header']])
table.add_row(['Path', result['path']])
table.add_row(['Port', result['httpPort']])
table.add_row(['Http Port', result['httpPort']])
table.add_row(['Https Port', result['httpsPort']])
table.add_row(['Cache Key Rule', result['cacheKeyQueryRule']])
table.add_row(['Configuration', result['performanceConfiguration']])
table.add_row(['Status', result['status']])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@

createOriginPath = [
{
"header": "test.example.com",
"httpPort": 80,
"httpsPort": 81,
"mappingUniqueId": "993419389425697",
"origin": "10.10.10.1",
"originType": "HOST_SERVER",
"header": "test.example.com",
"path": "/example",
"status": "RUNNING",
"bucketName": "test-bucket",
'fileExtension': 'jpg',
"performanceConfiguration": "General web delivery"
"performanceConfiguration": "Dynamic content acceleration",
"dynamicContentAcceleration": {
"detectionPath": "/abc.html",
"prefetchEnabled": True,
"mobileImageCompressionEnabled": True
},
"cacheKeyQueryRule": "include-all"
}
]

Expand Down
53 changes: 35 additions & 18 deletions SoftLayer/managers/cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,35 @@ def get_origins(self, unique_id, **kwargs):

return self.cdn_path.listOriginPath(unique_id, **kwargs)

def add_origin(self, unique_id, origin, path, origin_type="server", header=None,
port=80, protocol='http', bucket_name=None, file_extensions=None,
optimize_for="web", cache_query="include all"):
def add_origin(self, unique_id, origin, path, dynamic_path, origin_type="server", header=None,
http_port=80, https_port=None, protocol='http', bucket_name=None, file_extensions=None,
optimize_for="web", compression=None, prefetching=None,
cache_query="include all"):
"""Creates an origin path for an existing CDN.
:param str unique_id: The unique ID associated with the CDN.
:param str path: relative path to the domain provided, e.g. "/articles/video"
:param str dynamic_path: The path that Akamai edge servers periodically fetch the test object from.
example = /detection-test-object.html
:param str origin: ip address or hostname if origin_type=server, API endpoint for
your S3 object storage if origin_type=storage
:param str origin_type: it can be 'server' or 'storage' types.
:param str header: the edge server uses the host header to communicate with the origin.
It defaults to hostname. (optional)
:param int port: the http port number (default: 80)
:param int http_port: the http port number (default: 80)
:param int https_port: the https port number
:param str protocol: the protocol of the origin (default: HTTP)
:param str bucket_name: name of the available resource
:param str file_extensions: file extensions that can be stored in the CDN, e.g. "jpg,png"
:param str optimize_for: performance configuration, available options: web, video, and file where:
- 'web' = 'General web delivery'
- 'video' = 'Video on demand optimization'
- 'file' = 'Large file optimization'
- 'web' = 'General web delivery'
- 'video' = 'Video on demand optimization'
- 'file' = 'Large file optimization'
- 'dynamic' = 'Dynamic content acceleration'
:param bool compression: Enable or disable compression of JPEG images for requests over
certain network conditions.
:param bool prefetching: Enable or disable the embedded object prefetching feature.
:param str cache_query: rules with the following formats: 'include-all', 'ignore-all',
'include: space separated query-names',
'ignore: space separated query-names'.'
Expand All @@ -94,20 +102,29 @@ def add_origin(self, unique_id, origin, path, origin_type="server", header=None,
performance_config = {
'web': 'General web delivery',
'video': 'Video on demand optimization',
'file': 'Large file optimization'
'file': 'Large file optimization',
"dynamic": "Dynamic content acceleration"
}

new_origin = {
'uniqueId': unique_id,
'path': path,
'origin': origin,
'originType': types.get(origin_type),
'httpPort': port,
'httpPort': http_port,
'httpsPort': https_port,
'protocol': protocol.upper(),
'performanceConfiguration': performance_config.get(optimize_for, 'General web delivery'),
'cacheKeyQueryRule': cache_query
'performanceConfiguration': performance_config.get(optimize_for),
'cacheKeyQueryRule': cache_query,
}

if optimize_for == 'dynamic':
new_origin['dynamicContentAcceleration'] = {
'detectionPath': "/" + str(dynamic_path),
'prefetchEnabled': bool(prefetching),
'mobileImageCompressionEnabled': bool(compression)
}

if header:
new_origin['header'] = header

Expand Down Expand Up @@ -175,7 +192,7 @@ def end_date(self):
return self._end_date

def edit(self, identifier, header=None, http_port=None, https_port=None, origin=None,
respect_headers=None, cache=None, performance_configuration=None):
respect_headers=None, cache=None, cache_description=None, performance_configuration=None):
"""Edit the cdn object.
:param string identifier: The CDN identifier.
Expand Down Expand Up @@ -223,12 +240,12 @@ def edit(self, identifier, header=None, http_port=None, https_port=None, origin=
if respect_headers:
config['respectHeaders'] = respect_headers

if cache:
if cache or cache_description:
if 'include-specified' in cache['cacheKeyQueryRule']:
cache_key_rule = self.get_cache_key_query_rule('include', cache)
cache_key_rule = self.get_cache_key_query_rule('include', cache_description)
config['cacheKeyQueryRule'] = cache_key_rule
elif 'ignore-specified' in cache['cacheKeyQueryRule']:
cache_key_rule = self.get_cache_key_query_rule('ignore', cache)
cache_key_rule = self.get_cache_key_query_rule('ignore', cache_description)
config['cacheKeyQueryRule'] = cache_key_rule
else:
config['cacheKeyQueryRule'] = cache['cacheKeyQueryRule']
Expand All @@ -254,18 +271,18 @@ def _get_ids_from_hostname(self, hostname):
return result

@staticmethod
def get_cache_key_query_rule(cache_type, cache):
def get_cache_key_query_rule(cache_type, cache_description):
"""Get the cdn object detail.
:param string cache_type: Cache type.
:param cache: Cache description.
:return: string value.
"""
if 'description' not in cache:
if cache_description is None:
raise SoftLayer.SoftLayerError('Please add a description to be able to update the'
' cache.')
cache_result = '%s: %s' % (cache_type, cache['description'])
cache_result = '%s: %s' % (cache_type, cache_description)

return cache_result

Expand Down
26 changes: 25 additions & 1 deletion tests/CLI/modules/cdn_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,30 @@ def test_add_origin_server(self):

self.assert_no_fail(result)

def test_add_origin_server_dynamic(self):
result = self.run_command(
['cdn', 'origin-add', '-t', 'server', '-H=test.example.com', '-s', 81, '-o', 'dynamic', '-c=include-all',
'-P', 'HTTPS', '-d', 'abc.html', '-g', True, '-i', True, '1234', '10.10.10.1', '/example/videos2', ])

self.assert_no_fail(result)

def test_add_origin_storage(self):
result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-H=test.example.com',
'-p', 80, '-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])

self.assert_no_fail(result)

def test_add_origin_storage_dynamic(self):
result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-H=test.example.com',
'-s', 81, '-o', 'dynamic', '-c=include-all', '1234', '10.10.10.1',
'/example/videos2', '-g', True, '-i', True])

self.assert_no_fail(result)

def test_add_origin_without_storage(self):
result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-H=test.example.com', '-p', 80,
'-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])
'-P', 'HTTPS', '-o', 'web', '-c=include-all',
'1234', '10.10.10.1', '/example/videos2'])

self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.ArgumentError)
Expand All @@ -86,6 +101,15 @@ def test_add_origin_storage_with_file_extensions(self):

self.assert_no_fail(result)

def test_add_origin_storage_with_file_extensions_dynamic(self):
result = self.run_command(
['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-e', 'jpg', '-H=test.example.com', '-s', 81,
'-P', 'HTTPS', '-o', 'dynamic', '-d', 'abc.html', '-g', True, '-i', True,
'-c=include-all', '1234', '10.10.10.1', '/example/videos2',
])

self.assert_no_fail(result)

def test_remove_origin(self):
result = self.run_command(['cdn', 'origin-remove', '1234',
'/example1'])
Expand Down
Loading

0 comments on commit 28002af

Please sign in to comment.