8000 Merge pull request #52305 from terminalmage/cp.cache_dest · perlchild/salt@1bf25a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bf25a1

Browse files
authored
Merge pull request saltstack#52305 from terminalmage/cp.cache_dest
fileclient: Add function to return the expected cache location
2 parents 287ee16 + a86f6de commit 1bf25a1

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

salt/fileclient.py

Lines chan 10000 ged: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,29 @@ def is_cached(self, path, saltenv='base', cachedir=None):
341341

342342
return ''
343343

344+
def cache_dest(self, url, saltenv='base', cachedir=None):
345+
'''
346+
Return the expected cache location for the specified URL and
347+
environment.
348+
'''
349+
proto = urlparse(url).scheme
350+
351+
if proto == '':
352+
# Local file path
353+
return url
354+
355+
if proto == 'salt':
356+
url, senv = salt.utils.url.parse(url)
357+
if senv:
358+
saltenv = senv
359+
return salt.utils.path.join(
360+
self.opts['cachedir'],
361+
'files',
362+
saltenv,
363+
url.lstrip('|/'))
364+
365+
return self._extrn_path(url, saltenv, cachedir=cachedir)
366+
344367
def list_states(self, saltenv):
345368
'''
346369
Return a list of all available sls modules on the master for a given

salt/modules/cp.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,29 @@ def cache_file(path, saltenv='base', source_hash=None):
525525
return result
526526

527527

528+
def cache_dest(url, saltenv='base'):
529+
'''
530+
.. versionadded:: Neon
531+
532+
Returns the expected cache path for the file, if cached using
533+
:py:func:`cp.cache_file <salt.modules.cp.cache_file>`.
534+
535+
.. note::
536+
This only returns the _expected_ path, it does not tell you if the URL
537+
is really cached. To check if the URL is cached, use
538+
:py:func:`cp.is_cached <salt.modules.cp.is_cached>` instead.
539+
540+
CLI Examples:
541+
542+
.. code-block:: bash
543+
544+
salt '*' cp.cache_dest https://foo.com/bar.rpm
545+
salt '*' cp.cache_dest salt://my/file
546+
salt '*' cp.cache_dest salt://my/file saltenv=dev
547+
'''
548+
return _client().cache_dest(url, saltenv)
549+
550+
528551
def cache_files(paths, saltenv='base'):
529552
'''
530553
Used to gather many files from the Master, the gathered files will be

tests/unit/test_fileclient.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,51 @@ def test_cache_file_with_alternate_cachedir_and_relative_path(self):
411411
log.debug('cache_loc = %s', cache_loc)
412412
log.debug('content = %s', content)
413413
self.assertTrue(saltenv in content)
414+
415+
def test_cache_dest(self):
416+
'''
417+
Tests functionality for cache_dest
418+
'''
419+
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
420+
patched_opts.update(self.MOCKED_OPTS)
421+
422+
relpath = 'foo.com/bar.txt'
423+
cachedir = self.minion_opts['cachedir']
424+
425+
def _external(saltenv='base'):
426+
return salt.utils.path.join(
427+
patched_opts['cachedir'],
428+
8000 'extrn_files',
429+
saltenv,
430+
relpath)
431+
432+
def _salt(saltenv='base'):
433+
return salt.utils.path.join(
434+
patched_opts['cachedir'],
435+
'files',
436+
saltenv,
437+
relpath)
438+
439+
def _check(ret, expected):
440+
assert ret == expected, '{0} != {1}'.format(ret, expected)
441+
442+
with patch.dict(fileclient.__opts__, patched_opts):
443+
client = fileclient.get_file_client(
444+
fileclient.__opts__, pillar=False)
445+
446+
_check(client.cache_dest('https://' + relpath),
447+
_external())
448+
449+
_check(client.cache_dest('https://' + relpath, 'dev'),
450+
685C _external('dev'))
451+
452+
_check(client.cache_dest('salt://' + relpath),
453+
_salt())
454+
455+
_check(client.cache_dest('salt://' + relpath, 'dev'),
456+
_salt('dev'))
457+
458+
_check(client.cache_dest('salt://' + relpath + '?saltenv=dev'),
459+
_salt('dev'))
460+
461+
_check('/foo/bar', '/foo/bar')

0 commit comments

Comments
 (0)
0