Add type as param for cloud_init config

This commit is contained in:
Brian Cain 2020-06-10 15:38:18 -07:00
parent 98efa215c9
commit 7b95826dd7
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
5 changed files with 70 additions and 4 deletions

View File

@ -14,11 +14,19 @@ module VagrantPlugins
"text/x-include-once-url", "text/x-include-url", "text/x-include-once-url", "text/x-include-url",
"text/x-shellscript"].map(&:freeze).freeze "text/x-shellscript"].map(&:freeze).freeze
DEFAULT_CONFIG_TYPE = :user_data
# @note This value is for internal use only # @note This value is for internal use only
# #
# @return [String] # @return [String]
attr_reader :id attr_reader :id
# The 'type' of data being stored. If not defined,
# will default to :user_data
#
# @return [Symbol]
attr_accessor :type
# @return [String] # @return [String]
attr_accessor :content_type attr_accessor :content_type
@ -28,9 +36,11 @@ module VagrantPlugins
# @return [String] # @return [String]
attr_accessor :inline attr_accessor :inline
def initialize() def initialize(type=nil)
@logger = Log4r::Logger.new("vagrant::config::vm::cloud_init") @logger = Log4r::Logger.new("vagrant::config::vm::cloud_init")
@type = type if type
@content_type = UNSET_VALUE @content_type = UNSET_VALUE
@path = UNSET_VALUE @path = UNSET_VALUE
@inline = UNSET_VALUE @inline = UNSET_VALUE
@ -40,6 +50,12 @@ module VagrantPlugins
end end
def finalize! def finalize!
if !@type
@type = DEFAULT_CONFIG_TYPE
else
@type = @type.to_sym
end
@content_type = nil if @content_type == UNSET_VALUE @content_type = nil if @content_type == UNSET_VALUE
@path = nil if @path == UNSET_VALUE @path = nil if @path == UNSET_VALUE
@inline = nil if @inline == UNSET_VALUE @inline = nil if @inline == UNSET_VALUE
@ -49,6 +65,13 @@ module VagrantPlugins
def validate(machine) def validate(machine)
errors = _detected_errors errors = _detected_errors
if @type && @type != DEFAULT_CONFIG_TYPE
errors << I18n.t("vagrant.cloud_init.incorrect_type_set",
type: @type,
machine: machine.name,
default_type: DEFAULT_CONFIG_TYPE)
end
if !@content_type if !@content_type
errors << I18n.t("vagrant.cloud_init.content_type_not_set", errors << I18n.t("vagrant.cloud_init.content_type_not_set",
machine: machine.name, machine: machine.name,

View File

@ -473,10 +473,13 @@ module VagrantPlugins
# Stores config options for cloud_init # Stores config options for cloud_init
# #
# @param [Symbol] type
# @param [Hash] options # @param [Hash] options
# @param [Block] block # @param [Block] block
def cloud_init(**options, &block) def cloud_init(type=nil, **options, &block)
cloud_init_config = VagrantConfigCloudInit.new() type = type.to_sym if type
cloud_init_config = VagrantConfigCloudInit.new(type)
if block_given? if block_given?
block.call(cloud_init_config, VagrantConfigCloudInit) block.call(cloud_init_config, VagrantConfigCloudInit)

View File

@ -2240,6 +2240,9 @@ en:
incorrect_path_type: |- incorrect_path_type: |-
The path '%{path}' given for guest '%{machine}' is of type '%{type}'. The path '%{path}' given for guest '%{machine}' is of type '%{type}'.
Config option 'path' must be a String. Config option 'path' must be a String.
incorrect_type_set: |-
The type '%{type}' defined for a cloud_init config with guest '%{machine}'
is not a valid type. Currently, Vagrant only supports type: '%{default_type}'.
path_and_inline_set: |- path_and_inline_set: |-
Guest '%{machine}' defines both a 'path' and 'inline' for its cloud_init config, Guest '%{machine}' defines both a 'path' and 'inline' for its cloud_init config,
however only one config option should be defined for cloud_init. however only one config option should be defined for cloud_init.

View File

@ -5,7 +5,7 @@ require Vagrant.source_root.join("plugins/kernel_v2/config/cloud_init")
describe VagrantPlugins::Kernel_V2::VagrantConfigCloudInit do describe VagrantPlugins::Kernel_V2::VagrantConfigCloudInit do
include_context "unit" include_context "unit"
subject { described_class.new } subject { described_class.new(:user_data) }
let(:provider) { double("provider") } let(:provider) { double("provider") }
let(:machine) { double("machine", name: "rspec", provider: provider, let(:machine) { double("machine", name: "rspec", provider: provider,
@ -46,6 +46,22 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigCloudInit do
subject.finalize! subject.finalize!
expect(subject.content_type).to eq("text/cloud-config") expect(subject.content_type).to eq("text/cloud-config")
end end
context "with no type set" do
let(:type_subject) { described_class.new }
before do
type_subject.content_type = "text/cloud-config"
type_subject.inline = <<-CONFIG
package_update: true
CONFIG
end
it "defaults to a type" do
type_subject.finalize!
expect(type_subject.type).to eq(:user_data)
end
end
end end
context "with an invalid option set" do context "with an invalid option set" do

View File

@ -41,3 +41,24 @@ option.
Examples of how to define these options can be found in the Examples of how to define these options can be found in the
[usage documentation](/docs/cloud-init/configuration). [usage documentation](/docs/cloud-init/configuration).
### cloud_init Type
When defining a config for cloud_init, you can optionally define a `type` for the config:
```ruby
config.vm.cloud_init :user_data, content_type: "text/cloud-config", path: "config.cfg"
config.vm.cloud_init :user_data do |cloud_init|
cloud_init.content_type = "text/cloud-config"
cloud_init.path = "config.cfg"
end
```
However, this is not a requirement. Leaving off `type` will default to `:user_data`.
- `type` (Symbol) - This is an optional config that defines the type of
cloud-init config. Currently, the only supported `type` is `:user_data`. If a type
is not defined, it will default to `:user_data`.