@@ -55,12 +55,6 @@ def get_app(app_ref)
5555 { }
5656 end
5757
58- def get_app_stats ( app_ref )
59- capture ( "get" , "app" , app_ref . app_name ,
60- "--project" , app_ref . project_name ,
61- "-o" , "stats" )
62- end
63-
6458 def get_all_builds
6559 output = capture ( "get" , "builds" , "-A" , "-o" , "json" )
6660 return [ ] if output . nil? || output . empty?
@@ -89,26 +83,61 @@ def get_builds(app_ref)
8983 [ ]
9084 end
9185
92- def edit_app ( app_ref )
93- exec_passthrough ( "edit" , "app" , app_ref . app_name ,
94- "--project" , app_ref . project_name )
86+ def get_all_services ( project : nil )
87+ service_types = %w[ keyvaluestore postgres mysql opensearch ]
88+ all_services = [ ]
89+
90+ service_types . each do |type |
91+ services = get_services_by_type ( type , project : project )
92+ services . each { |s | s [ "_type" ] = type }
93+ all_services . concat ( services )
94+ end
95+
96+ all_services
9597 end
9698
97- def create_app ( project , app_name , git_url :, git_revision :, size : "mini" )
98- run ( "create" , "app" , app_name ,
99- "--project" , project ,
100- "--git-url" , git_url ,
101- "--git-revision" , git_revision ,
102- "--size" , size )
99+ def get_services_by_type ( type , project : nil )
100+ args = [ "get" , type ]
101+ if project
102+ args += [ "--project" , project ]
103+ else
104+ args << "-A"
105+ end
106+ args += [ "-o" , "json" ]
107+
108+ output = capture ( *args )
109+ return [ ] if output . nil? || output . empty?
110+
111+ data = JSON . parse ( output )
112+ data . is_a? ( Array ) ? data : ( data [ "items" ] || [ ] )
113+ rescue JSON ::ParserError
114+ [ ]
115+ rescue Deploio ::NctlError
116+ [ ]
103117 end
104118
105- def delete_app ( app_ref )
106- run ( "delete" , "app" , app_ref . app_name ,
107- "--project" , app_ref . project_name )
119+ def get_service ( type , name , project :)
120+ output = capture ( "get" , type , name , "--project" , project , "-o" , "json" )
121+ return nil if output . nil? || output . empty?
122+
123+ JSON . parse ( output )
124+ rescue JSON ::ParserError
125+ nil
126+ rescue Deploio ::NctlError
127+ nil
108128 end
109129
110- def create_project ( project_name )
111- run ( "create" , "project" , project_name )
130+ def get_service_connection_string ( type , name , project :)
131+ case type
132+ when "postgres" , "mysql"
133+ capture ( "get" , type , name , "--project" , project , "--print-connection-string" ) . strip
134+ when "keyvaluestore"
135+ build_keyvaluestore_connection_string ( name , project )
136+ when "opensearch"
137+ build_opensearch_connection_string ( name , project )
138+ end
139+ rescue Deploio ::NctlError
140+ nil
112141 end
113142
114143 def get_projects
@@ -269,5 +298,31 @@ def check_nctl_version
269298 raise Deploio ::NctlError ,
270299 "nctl version #{ version } is too old. Need #{ REQUIRED_VERSION } +. Run: brew upgrade nctl"
271300 end
301+
302+ def build_keyvaluestore_connection_string ( name , project )
303+ data = get_service ( "keyvaluestore" , name , project : project )
304+ return nil unless data
305+
306+ at_provider = data . dig ( "status" , "atProvider" ) || { }
307+ fqdn = at_provider [ "fqdn" ]
308+ return nil if fqdn . nil? || fqdn . empty?
309+
310+ token = capture ( "get" , "keyvaluestore" , name , "--project" , project , "--print-token" ) . strip
311+ "rediss://:#{ token } @#{ fqdn } :6379"
312+ end
313+
314+ def build_opensearch_connection_string ( name , project )
315+ data = get_service ( "opensearch" , name , project : project )
316+ return nil unless data
317+
318+ at_provider = data . dig ( "status" , "atProvider" ) || { }
319+ hosts = at_provider [ "hosts" ] || [ ]
320+ return nil if hosts . empty?
321+
322+ user = capture ( "get" , "opensearch" , name , "--project" , project , "--print-user" ) . strip
323+ password = capture ( "get" , "opensearch" , name , "--project" , project , "--print-password" ) . strip
324+ host = hosts . first
325+ "https://#{ user } :#{ password } @#{ host } "
326+ end
272327 end
273328end
0 commit comments