Skip to content
This repository was archived by the owner on Feb 19, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions lib/puppet/functions/packagecloud/build_base_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# Author: Joe Damato
# Module Name: packagecloud
#
# Copyright 2014-2015, Computology, LLC
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "uri"

# ---- original file header ----
#
# @summary
# Summarise what the function does here
#
Puppet::Functions.create_function(:'packagecloud::build_base_url') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end


def default_impl(*args)

read_token = args[0]
server_address = args[1]

uri = URI(server_address)
uri.user = read_token
uri.password = ''

uri.to_s

end
end
155 changes: 155 additions & 0 deletions lib/puppet/functions/packagecloud/get_read_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# Author: Joe Damato
# Module Name: packagecloud
#
# Copyright 2014-2015, Computology, LLC
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "uri"
require 'net/http'
require "net/https"

module Packagecloud
class API
attr_reader :name

def initialize(name, master_token, server_address, os, dist, hostname)
@name = name
@master_token = master_token
@os = os
@dist = dist
@hostname = hostname
@base_url = "https://packagecloud.io/install/repositories/"

if !server_address.nil?
@base_url = URI.join(server_address, "/install/repositories/").to_s
end

@endpoint_params = {
:os => os,
:dist => dist,
:name => hostname
}
end

def repo_name
@name.gsub('/', '_')
end

def rpm_base_url
@rpm_base_url ||= master_rpm_base_url.dup.tap do |uri|
uri.user = read_token
end
end

def master_rpm_base_url
@master_rpm_base_url ||= URI(get(uri_for("rpm_base_url"), @endpoint_params).body.chomp)
end

def read_token
@read_token ||= post(uri_for("tokens.text"), @endpoint_params).body.chomp
end

def uri_for(resource)
URI.join(@base_url, "#{@name}/#{resource}").tap do |uri|
uri.user = @master_token
end
end

def get(uri, params)
uri.query = URI.respond_to?(:encode_www_form) ? URI.encode_www_form(params) : params.to_param
request = Net::HTTP::Get.new(uri.request_uri)

if uri.user
request.basic_auth uri.user.to_s, uri.password.to_s
end

http(uri.host, uri.port, request)
end

def post(uri, params)
request = Net::HTTP::Post.new(uri.request_uri)
request.form_data = params

if uri.user
request.basic_auth uri.user.to_s, uri.password.to_s
end

http(uri.scheme, uri.host, uri.port, request)
end

def http(scheme, host, port, request)
http = Net::HTTP.new(host, port)
if scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.use_ssl = true
store = OpenSSL::X509::Store.new
store.set_default_paths
http.cert_store = store
end

case res = http.start { |http| http.request(request) }
when Net::HTTPSuccess, Net::HTTPRedirection
res
else
res.error!
end
end
end
end

# ---- original file header ----
#
# @summary
# Summarise what the function does here
#
Puppet::Functions.create_function(:'packagecloud::get_read_token') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end


def default_impl(*args)

repo = args[0]
master_token = args[1]
server_address = args[2]

os = lookupvar('::operatingsystem').downcase
dist = lookupvar('::operatingsystemrelease')
hostname = lookupvar('::fqdn')

Packagecloud::API.new(repo, master_token, server_address, os, dist, hostname).read_token

end
end
41 changes: 41 additions & 0 deletions spec/functions/packagecloud_build_base_url_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'packagecloud::build_base_url' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################

end
41 changes: 41 additions & 0 deletions spec/functions/packagecloud_get_read_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'packagecloud::get_read_token' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################

end