Skip to content

Commit

Permalink
Merge pull request #186 from KohlsTechnology/issue-161
Browse files Browse the repository at this point in the history
Add config option for image project id
  • Loading branch information
erjohnso authored Apr 3, 2018
2 parents 1287d31 + 41c020d commit f870d7c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ This provider exposes quite a few provider-specific configuration options:
* `image_family` - Specify an "image family" to pull the latest image from. For example: `centos-7`
will pull the most recent CentOS 7 image. For more info, refer to
[Google Image documentation](https://cloud.google.com/compute/docs/images#image_families).
* `image_project_id` - The ID of the GCP project to search for the `image` or `image_family`.
* `instance_group` - Unmanaged instance group to add the machine to. If one
doesn't exist it will be created.
* `instance_ready_timeout` - The number of seconds to wait for the instance
Expand Down
6 changes: 4 additions & 2 deletions lib/vagrant-google/action/run_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
zone_config = env[:machine].provider_config.get_zone_config(zone)
image = zone_config.image
image_family = zone_config.image_family
image_project_id = zone_config.image_project_id
instance_group = zone_config.instance_group
name = zone_config.name
machine_type = zone_config.machine_type
Expand Down Expand Up @@ -76,6 +77,7 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
env[:ui].info(" -- Disk name: #{disk_name}")
env[:ui].info(" -- Image: #{image}")
env[:ui].info(" -- Image family: #{image_family}")
env[:ui].info(" -- Image Project: #{image_project_id}") if image_project_id
env[:ui].info(" -- Instance Group: #{instance_group}")
env[:ui].info(" -- Zone: #{zone}") if zone
env[:ui].info(" -- Network: #{network}") if network
Expand All @@ -94,11 +96,11 @@ def call(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
env[:ui].info(" -- Scopes: #{service_accounts}")

# Munge image configs
image = env[:google_compute].images.get(image).self_link
image = env[:google_compute].images.get(image, image_project_id).self_link

# If image_family is set, get the latest image image from the family.
unless image_family.nil?
image = env[:google_compute].images.get_from_family(image_family).self_link
image = env[:google_compute].images.get_from_family(image_family, image_project_id).self_link
end

# Munge network configs
Expand Down
9 changes: 9 additions & 0 deletions lib/vagrant-google/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Config < Vagrant.plugin("2", :config) # rubocop:disable Metrics/ClassLengt
# @return [String]
attr_accessor :image_family

# The name of the image_project_id
#
# @return [String]
attr_accessor :image_project_id

# The instance group name to put the instance in.
#
# @return [String]
Expand Down Expand Up @@ -166,6 +171,7 @@ def initialize(zone_specific=false)
@google_project_id = UNSET_VALUE
@image = UNSET_VALUE
@image_family = UNSET_VALUE
@image_project_id = UNSET_VALUE
@instance_group = UNSET_VALUE
@machine_type = UNSET_VALUE
@disk_size = UNSET_VALUE
Expand Down Expand Up @@ -271,6 +277,9 @@ def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedC
# Default image family is nil
@image_family = nil if @image_family == UNSET_VALUE

# Default image project is nil
@image_project_id = nil if @image_project_id == UNSET_VALUE

# Default instance group name is nil
@instance_group = nil if @instance_group == UNSET_VALUE

Expand Down
4 changes: 3 additions & 1 deletion test/unit/common/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

its("name") { should match "i-[0-9]{10}-[0-9a-f]{4}" }
its("image") { should be_nil }
its("image_family") { should be_nil }
its("image_project_id") { should be_nil }
its("instance_group") { should be_nil }
its("zone") { should == "us-central1-f" }
its("network") { should == "default" }
Expand All @@ -54,7 +56,7 @@
# simple boilerplate test, so I cut corners here. It just sets
# each of these attributes to "foo" in isolation, and reads the value
# and asserts the proper result comes back out.
[:name, :image, :zone, :instance_ready_timeout, :machine_type, :disk_size, :disk_name, :disk_type,
[:name, :image, :image_family, :image_project_id, :zone, :instance_ready_timeout, :machine_type, :disk_size, :disk_name, :disk_type,
:network, :network_project_id, :metadata, :labels, :can_ip_forward, :external_ip, :autodelete_disk].each do |attribute|

it "should not default #{attribute} if overridden" do
Expand Down

0 comments on commit f870d7c

Please sign in to comment.