Class: VMC::Service

Inherits:
CLI
  • Object
show all
Defined in:
lib/vmc/cli/service.rb

Instance Method Summary collapse

Methods inherited from CLI

#check_logged_in, #check_target, #client, client, client=, #client_target, #color_enabled?, #default_action, #ensure_config_dir, #err, #execute, #fail, #force?, #invalidate_client, #log_error, #name_list, #no_v2, #one_of, #precondition, #quiet?, #remove_target_info, #sane_target_url, #save_target_info, #save_targets, #set_target, #table, #target_file, #target_info, #targets_info, #tokens_file, #user_colors, #v2?, #verbose?

Methods included from Spacing

#indented, #justify, #line, #lines, #quiet?, #spaced, #start_line, #tabular, #text_width, #trim_escapes

Methods included from Interactive

#ask, #handler, #input_state, #list_choices, #prompt, #show_default

Instance Method Details

#bind_serviceObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/vmc/cli/service.rb', line 201

def bind_service
  app = input[:app]
  instance = input[:instance, app]

  with_progress(
      "Binding #{c(instance.name, :name)} to #{c(app.name, :name)}") do |s|
    if app.binds?(instance)
      s.skip do
        err "App #{b(app.name)} already binds #{b(instance.name)}."
      end
    else
      app.bind(instance)
    end
  end
end

#create_serviceObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/vmc/cli/service.rb', line 123

def create_service
  services = client.services

  if input[:provider]
    services.reject! { |s| s.provider != input[:provider] }
  end

  if input[:version]
    services.reject! { |s| s.version != input[:version] }
  elsif !v2?
    services.reject!(&:deprecated?)
  end

  if v2? && plan = input.given(:plan)
    services.reject! do |s|
      if plan.is_a?(String)
        s.service_plans.none? { |p| p.name == plan.upcase }
      else
        s.service_plans.include? plan
      end
    end
  end

  until services.size < 2
    # cast to Array since it might be given as a Service with #invoke
    services = Array(input[:service, services.sort_by(&:label)])
    input.forget(:service)
  end

  if services.empty?
    fail "Cannot find services matching the given criteria."
  end

  service = services.first

  instance = client.service_instance
  instance.name = input[:name, service]

  if v2?
    instance.service_plan = input[:plan, service.service_plans]
    instance.space = client.current_space
  else
    instance.type = service.type
    instance.vendor = service.label
    instance.version = service.version
    instance.tier = "free"
  end

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

  if app = input[:app]
    invoke :bind_service, :instance => instance, :app => app
  end

  instance
end

#delete_serviceObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/vmc/cli/service.rb', line 266

def delete_service
  if input[:all]
    return unless input[:really, "ALL SERVICES", :bad]

    client.service_instances.each do |i|
      invoke :delete_service, :instance => i, :really => true
    end

    return
  end

  instance = input[:instance]

  return unless input[:really, instance.name, :name]

  bindings = []

  if v2?
    bindings = instance.service_bindings

    unless bindings.empty? || !input[:unbind, bindings.collect(&:app)]
      bindings.each do |b|
        invoke :unbind_service, :instance => instance, :app => b.app
      end

      bindings = []
    end
  end

  with_progress("Deleting #{c(instance.name, :name)}") do |s|
    if bindings.empty?
      instance.delete!
    else
      s.skip do
        apps = bindings.collect(&:app).collect { |a| b(a.name) }
        err "Service instance is bound to #{human_list(apps)}."
      end
    end
  end
end

#serviceObject



80
81
82
# File 'lib/vmc/cli/service.rb', line 80

def service
  display_service_instance(input[:instance])
end

#servicesObject



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
# File 'lib/vmc/cli/service.rb', line 20

def services
  msg =
    if space = input[:space]
      "Getting services in #{c(space.name, :name)}"
    else
      "Getting services"
    end

  instances =
    with_progress(msg) do
      client.service_instances(:depth => 2)
    end

  line unless quiet?

  if instances.empty? and !quiet?
    line "No services."
    return
  end

  instances.reject! do |i|
    !instance_matches(i, input)
  end

  if input[:full]
    spaced(instances) do |i|
      display_service_instance(i)
    end
  else
    table(
      ["name", "service", "version", v2? && "plan", v2? && "bound apps"],
      instances.collect { |i|
        if v2?
          plan = i.service_plan
          service = plan.service

          label = service.label
          version = service.version
          apps = name_list(i.service_bindings.collect(&:app))
        else
          label = i.vendor
          version = i.version
        end

        [ c(i.name, :name),
          label,
          version,
          v2? && plan.name,
          apps
        ]
      })
  end
end

#unbind_serviceObject



232
233
234
235
236
237
238
239
240
# File 'lib/vmc/cli/service.rb', line 232

def unbind_service
  app = input[:app]
  instance = input[:instance, app.services]

  with_progress(
      "Unbinding #{c(instance.name, :name)} from #{c(app.name, :name)}") do
    app.unbind(instance)
  end
end