2 require 'puppet_x/bodeco/util'
4 Puppet::Functions.create_function(:'archive::artifactory_checksum') do
5 # @summary A function that returns the checksum value of an artifact stored in Artifactory
6 # @param url The URL of the artifact.
7 # @param checksum_type The checksum type.
8 # Note the function will raise an error if you ask for sha256 but your artifactory instance doesn't have the sha256 value calculated.
9 # @return [String] Returns the checksum.
10 dispatch :artifactory_checksum do
11 param 'Stdlib::HTTPUrl', :url
12 optional_param "Enum['sha1','sha256','md5']", :checksum_type
16 def artifactory_checksum(url, checksum_type = 'sha1')
17 uri = URI(url.sub('/artifactory/', '/artifactory/api/storage/'))
19 response = PuppetX::Bodeco::Util.content(uri)
20 content = JSON.parse(response)
22 checksum = content['checksums'] && content['checksums'][checksum_type]
23 raise("Could not parse #{checksum_type} from url: #{uri}\nresponse: #{response.body}") unless checksum =~ %r{\b[0-9a-f]{5,64}\b}