lib/vagrant-rackspace/config.rb in vagrant-rackspace-0.1.3 vs lib/vagrant-rackspace/config.rb in vagrant-rackspace-0.1.4

- old
+ new

@@ -79,10 +79,19 @@ # and so on, and enables you to manage the disk configuration. # # This defaults to MANUAL attr_accessor :disk_config + # Cloud Networks to attach to the server + # + # @return [Array] + attr_accessor :networks + + # Default Rackspace Cloud Network IDs + SERVICE_NET_ID = '11111111-1111-1111-1111-111111111111' + PUBLIC_NET_ID = '00000000-0000-0000-0000-000000000000' + def initialize @api_key = UNSET_VALUE @rackspace_region = UNSET_VALUE @rackspace_compute_url = UNSET_VALUE @rackspace_auth_url = UNSET_VALUE @@ -91,10 +100,11 @@ @public_key_path = UNSET_VALUE @rackconnect = UNSET_VALUE @server_name = UNSET_VALUE @username = UNSET_VALUE @disk_config = UNSET_VALUE + @networks = [] end def finalize! @api_key = nil if @api_key == UNSET_VALUE @rackspace_region = nil if @rackspace_region == UNSET_VALUE @@ -104,10 +114,11 @@ @image = /Ubuntu/ if @image == UNSET_VALUE @rackconnect = nil if @rackconnect == UNSET_VALUE @server_name = nil if @server_name == UNSET_VALUE @username = nil if @username == UNSET_VALUE @disk_config = nil if @disk_config == UNSET_VALUE + @networks = nil if @networks.empty? if @public_key_path == UNSET_VALUE @public_key_path = Vagrant.source_root.join("keys/vagrant.pub") end end @@ -120,16 +131,43 @@ else @rackspace_auth_url end end + def network(net_id, options={}) + # Eventually, this should accept options for network configuration, + # primarily the IP address, but at the time of writing these + # options are unsupported by Cloud Networks. + options = {:attached => true}.merge(options) + + # Add the default Public and ServiceNet networks + if @networks.empty? + @networks = [PUBLIC_NET_ID, SERVICE_NET_ID] + end + + net_id = SERVICE_NET_ID if net_id == :service_net + + if options[:attached] + @networks << net_id unless @networks.include? net_id + else + @networks.delete net_id + end + end + def validate(machine) errors = [] errors << I18n.t("vagrant_rackspace.config.api_key_required") if !@api_key errors << I18n.t("vagrant_rackspace.config.username_required") if !@username + { + :rackspace_compute_url => @rackspace_compute_url, + :rackspace_auth_url => @rackspace_auth_url + }.each_pair do |key, value| + errors << I18n.t("vagrant_rackspace.config.invalid_uri", :key => key, :uri => value) unless value.nil? || valid_uri?(value) + end + public_key_path = File.expand_path(@public_key_path, machine.env.root_path) if !File.file?(public_key_path) errors << I18n.t("vagrant_rackspace.config.public_key_not_found") end @@ -138,9 +176,16 @@ private def lon_region? rackspace_region && rackspace_region != UNSET_VALUE && rackspace_region.to_sym == :lon + end + + private + + def valid_uri? value + uri = URI.parse value + uri.kind_of?(URI::HTTP) end end end end