Class: LdsCfPlugin::HttpService

Inherits:
CF::CLI
  • Object
show all
Defined in:
lib/lds-cf-plugin/http_service.rb

Instance Method Summary collapse

Instance Method Details

#create_http_serviceObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lds-cf-plugin/http_service.rb', line 16

def create_http_service
  offerings = client.services
  offerings.keep_if { |s| s.label == 'http-service' && s.provider.start_with?('ICS') && s.active }
  service = offerings.first

  if !service
    fail "Cannot find Http service on #{client.target}"
  end
  
  if service.version != "0.1"
    fail "Your lds-cf-plugin version is out of date.  To update execute `gem update lds-cf-plugin`"
  end

  rest_client = CFoundry::RestClient.new(service.url, client.token)
  rest_client.trace = client.trace

  instance_name = input[:name]
  if !instance_name
    instance_name = ask("Name")
  end

  plan = service.service_plans.first

  url = input[:url]
  while !url || !is_valid_url(url)[0]
    success, result = is_valid_url(url);
    if url && !success
      line c("The url you provided #{b("'#{url}'")} is not valid.", :bad)
      line c("Error: #{result}", :bad) unless result.nil?
    end
    url = ask("URL")
  end
  
  parsedUrl = URI.parse(url)
  
  prompt_credentials = parsedUrl.user.nil? 
  if !input[:url]
    prompt_credentials = ask("Provide Credentials?", :default => false) if parsedUrl.user.nil? && input[:username].nil?
  else
    prompt_credentials = false
  end
  
  username = input[:username]
  if !username && prompt_credentials
    username = ask("Username")
  end

  password = input[:password]
  if !password && prompt_credentials
    password = ask("Password", :echo => "*", :forget => true, :default => "")
  end

  # Register service with gateway

  payload = {
      :space_guid => client.current_space.guid,
      :name => instance_name,
      :url => url,
  }
  if parsedUrl.user.nil?
    payload[:username] = username
    payload[:password] = password
  end
  options = {
      :payload => JSON.generate(payload)
  }
  request, response = rest_client.request("POST", "/register", options)
  if response[:status].to_i != 200
    fail "Error registering Http service with gateway:\n#{response[:body]}"
  end


  # Create service instance
  instance = client.managed_service_instance
  instance.name = instance_name

  instance.service_plan = plan
  instance.space = client.current_space

  with_progress("Creating service #{c(instance.name, :name)}") do
    instance.create!
  end

  instance
end

#is_valid_url(url) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lds-cf-plugin/http_service.rb', line 102

def is_valid_url(url)
  begin
    parsedUrl = URI.parse(url)
    unless(parsedUrl.scheme == "https" || parsedUrl.scheme == "http")
      return false, "Scheme '#{parsedUrl.scheme}' is invalid."
    end
  rescue => e
    return false, e.message
  end
  return true, nil
end

#preconditionObject



7
8
9
# File 'lib/lds-cf-plugin/http_service.rb', line 7

def precondition
  check_target
end