Commit 87637693 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Introduce CI/CD variables collection class

parent 26607a16
......@@ -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 }
]
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 }
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)
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
......
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
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment