Class: PI::Cli::Command::Services

Inherits:
Base show all
Includes:
Interactive, PI::Cli::ChooseHelper
Defined in:
lib/cli/commands/services.rb

Instance Method Summary collapse

Methods included from PI::Cli::ChooseHelper

#check_status, #choose_app, #choose_project

Methods inherited from Base

#auth_token, #client, #initialize, #target_url

Constructor Details

This class inherits a constructor from PI::Cli::Command::Base

Instance Method Details

#app_service(appname = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cli/commands/services.rb', line 27

def app_service(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  services = client.app_service(appname)
  return display JSON.pretty_generate(services) if @options[:json]
  return display "No Services" if services.nil? || services.empty?
  services.sort! {|a, b| a[:name] <=> b[:name] }
  services_table = table do |t|
    t.headings = 'Name', 'Version', 'Type'
    services.each do |s|
      t << [s[:name], s[:version] ,s[:service_type]]
    end
  end
  display services_table
end

#bind_service(appname = nil) ⇒ Object



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
# File 'lib/cli/commands/services.rb', line 47

def bind_service(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  app  = client.app_get_byname(appname)
  err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)      

  services = client.usable_services(appname)
  err "No usable services!" if services.nil? || services.empty?
  
  choices = Array.new
  services.each do |s|
    choices << s[:name]
  end 
       
  manifest = ask "Select services, indexed list?", :choices => choices, :indexed => true
  manifest = manifest.to_a
  display "Selected services: ",false
  display "#{manifest}"  
  display "Binding service: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.bind_service(appname,manifest)
  result = check_status(app[:name], "bindservice")
  Thread.kill(t)
  if result[:code] == 200
    display "OK".green   
  else
    err result[:text]   
  end
end

#unbind_service(appname = nil) ⇒ Object



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
124
125
126
127
128
129
130
131
# File 'lib/cli/commands/services.rb', line 89

def unbind_service(appname=nil)
   unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  app  = client.app_get_byname(appname)
  err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)      
  services = client.app_service(appname)
  err "No binded services!" if services.nil? || services.empty?
  
  useservice = nil
  choose do |menu|
    display "=============Services============"
    menu.prompt = "Select Service: "
    menu.select_by = :index_or_name
    services.each do |service|
      menu.choice("#{service[:name]}") { useservice = service }
    end
  end
  display "Selected Service: ",false
  display "#{useservice[:name]}"  
  manifest = [useservice[:name]] 
  display "Unbinding service: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.unbind_service(appname,manifest)
  result = check_status(app[:name], "unbindservice")
  Thread.kill(t)
  if result[:code] == 200
    display "OK".green   
  else
    err result[:text]   
  end
end

#usable_services(appname = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cli/commands/services.rb', line 7

def usable_services(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  services = client.usable_services(appname)
  return display JSON.pretty_generate(services) if @options[:json]
  return display "No Services" if services.nil? || services.empty?
  services.sort! {|a, b| a[:name] <=> b[:name] }
  services_table = table do |t|
    t.headings = 'Name', 'Version', 'Type'
    services.each do |s|
      t << [s[:name], s[:version] ,s[:service_type]]
    end
  end
  display services_table
end