Introduce CI/CD variables collection class
This commit is contained in:
parent
26607a1690
commit
87637693ac
3 changed files with 131 additions and 10 deletions
|
@ -100,19 +100,17 @@ class KubernetesService < DeploymentService
|
|||
def predefined_variables
|
||||
config = YAML.dump(kubeconfig)
|
||||
|
||||
variables = [
|
||||
{ key: 'KUBE_URL', value: api_url, public: true },
|
||||
{ key: 'KUBE_TOKEN', value: token, public: false },
|
||||
{ key: 'KUBE_NAMESPACE', value: actual_namespace, public: true },
|
||||
{ key: 'KUBECONFIG', value: config, public: false, file: true }
|
||||
]
|
||||
variables = Gitlab::Ci::Variables::Collection.new.tap do |collection|
|
||||
collection.append(key: 'KUBE_URL', value: api_url, public: true)
|
||||
collection.append(key: 'KUBE_TOKEN', value: token, public: false)
|
||||
collection.append(key: 'KUBE_NAMESPACE', value: actual_namespace, public: true)
|
||||
collection.append(key: 'KUBECONFIG', value: config, public: false, file: true)
|
||||
|
||||
if ca_pem.present?
|
||||
variables << { key: 'KUBE_CA_PEM', value: ca_pem, public: true }
|
||||
variables << { key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true }
|
||||
collection.append(key: 'KUBE_CA_PEM', value: ca_pem, public: true)
|
||||
collection.append(key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true)
|
||||
end
|
||||
|
||||
variables
|
||||
variables.to_hash
|
||||
end
|
||||
|
||||
# Constructs a list of terminals from the reactive cache
|
||||
|
|
60
lib/gitlab/ci/variables/collection.rb
Normal file
60
lib/gitlab/ci/variables/collection.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
module Gitlab
|
||||
module Ci
|
||||
module Variables
|
||||
class Collection
|
||||
include Enumerable
|
||||
|
||||
Variable = Struct.new(:key, :value, :public, :file)
|
||||
|
||||
def initialize(variables = [])
|
||||
@variables = []
|
||||
|
||||
variables.each { |variable| append(variable) }
|
||||
end
|
||||
|
||||
def append(resource)
|
||||
@variables.append(fabricate(resource))
|
||||
end
|
||||
|
||||
def each
|
||||
@variables.each { |variable| yield variable }
|
||||
end
|
||||
|
||||
def +(other)
|
||||
self.class.new.tap do |collection|
|
||||
self.each { |variable| collection.append(variable) }
|
||||
other.each { |variable| collection.append(variable) }
|
||||
end
|
||||
end
|
||||
|
||||
def to_h
|
||||
self.map do |variable|
|
||||
variable.to_h.reject do |key, value|
|
||||
key == :file && value == false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :to_hash, :to_h
|
||||
|
||||
private
|
||||
|
||||
def fabricate(resource)
|
||||
case resource
|
||||
when Hash
|
||||
Variable.new(resource.fetch(:key),
|
||||
resource.fetch(:value),
|
||||
resource.fetch(:public, false),
|
||||
resource.fetch(:file, false))
|
||||
when ::Ci::Variable
|
||||
Variable.new(resource.key, resource.value, false, false)
|
||||
when Collection::Variable
|
||||
resource.dup
|
||||
else
|
||||
raise ArgumentError, 'Unknown CI/CD variable resource!'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
63
spec/lib/gitlab/ci/variables/collection_spec.rb
Normal file
63
spec/lib/gitlab/ci/variables/collection_spec.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Gitlab::Ci::Variables::Collection do
|
||||
describe '.new' do
|
||||
it 'can be initialized with an array' do
|
||||
variable = { key: 'SOME_VAR', value: 'Some Value'}
|
||||
collection = described_class.new([variable])
|
||||
|
||||
expect(collection.first.to_h).to include variable
|
||||
end
|
||||
|
||||
it 'can be initialized without an argument' do
|
||||
expect(subject).to be_none
|
||||
end
|
||||
end
|
||||
|
||||
describe '#append' do
|
||||
it 'appends a hash' do
|
||||
subject.append(key: 'VARIABLE', value: 'something')
|
||||
|
||||
expect(subject).to be_one
|
||||
end
|
||||
|
||||
it 'appends a Ci::Variable' do
|
||||
subject.append(build(:ci_variable))
|
||||
|
||||
expect(subject).to be_one
|
||||
end
|
||||
|
||||
it 'appends an internal resource' do
|
||||
collection = described_class.new([{ key: 'TEST', value: 1 }])
|
||||
|
||||
subject.append(collection.first)
|
||||
|
||||
expect(subject).to be_one
|
||||
end
|
||||
end
|
||||
|
||||
describe '#+' do
|
||||
it 'makes it possible to combine with an array' do
|
||||
collection = described_class.new([{ key: 'TEST', value: 1 }])
|
||||
variables = [{ key: 'TEST', value: 'something'}]
|
||||
|
||||
expect((collection + variables).count).to eq 2
|
||||
end
|
||||
|
||||
it 'makes it possible to combine with another collection' do
|
||||
collection = described_class.new([{ key: 'TEST', value: 1 }])
|
||||
other = described_class.new([{ key: 'TEST', value: 2 }])
|
||||
|
||||
expect((collection + other).count).to eq 2
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_hash' do
|
||||
it 'creates a hash / value mapping' do
|
||||
collection = described_class.new([{ key: 'TEST', value: 1 }])
|
||||
|
||||
expect(collection.to_hash)
|
||||
.to eq [{ key: 'TEST', value: 1, public: false }]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue