Add puppet/archive module
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / lib / puppet / functions / archive / artifactory_checksum.rb
1 require 'json'
2 require 'puppet_x/bodeco/util'
3
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
13     return_type 'String'
14   end
15
16   def artifactory_checksum(url, checksum_type = 'sha1')
17     uri = URI(url.sub('/artifactory/', '/artifactory/api/storage/'))
18
19     response = PuppetX::Bodeco::Util.content(uri)
20     content = JSON.parse(response)
21
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}
24     checksum
25   end
26 end