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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 
     | 
    
      # File 'lib/lds-cf-plugin/oracle_service.rb', line 18
def create_oracle_service
  offerings = client.services
  offerings.keep_if { |s| s.label == 'oracle-service' && s.provider == 'ICS' }
  service = offerings.first
  if !service
    fail "Cannot find Oracle 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
  plans = service.service_plans
  
  selected_plan = nil
  plans.each do |plan|
    selcted_plan = plan if plan.name == input[:plan] 
  end unless input[:plan].nil?
  
  existing_plan = nil?
  plans.each do | plan |
    existing_plan = plan if plan.name == "Existing"
  end
  
  plan_type = nil
  plan_type = "Existing" unless existing_plan.nil?
  
  if(plans.length > 1 && !existing_plan.nil? && selected_plan.nil?)
    choices = ["Existing", "Create New"]
    options = {
      :choices => choices,
      :default => "Existing",
      :allow_other => false
    }
    plan_type = ask("What type of Oracle Service", options)
  end
  if plan_type == "Existing"
    selected_plan = existing_plan
    
    service = input[:service]
    if !service
      service = ask("Oracle service (database name/alias)")
    end
  
    schema = input[:schema]
    if !schema
      schema = ask("Oracle schema (user)")
    end
  
    password = input[:password]
    if !password
      password = ask("Oracle schema password", :echo => "*", :forget => true)
    end
    
      
    payload = {
        :space_guid => client.current_space.guid,
        :name => instance_name,
        :service => service,
        :schema => schema,
        :password => password
    }
    options = {
        :payload => JSON.generate(payload)
    }
    request, response = rest_client.request("POST", "/register", options)
    if response[:status].to_i != 200
      fail "Error registering Oracle service with gateway:\n#{response[:body]}"
    end
  else
    plans.delete(existing_plan) unless existing_plan.nil?
    options = {
      :choices => plans,
      :display => proc { |choice|
        "#{choice.name}: #{choice.description}"
      },
      :allow_other => false
    }
    selected_plan = ask("What type of Schema do you want", options)
  end
    instance = client.managed_service_instance
  instance.name = instance_name
  instance.service_plan = selected_plan
  instance.space = client.current_space
  with_progress("Creating service #{c(instance.name, :name)}") do
    instance.create!
  end
  instance
end
     |