1919use Symfony \Component \Console \Input \InputArgument ;
2020use Symfony \Component \Console \Input \InputInterface ;
2121use Symfony \Component \Console \Input \InputOption ;
22+ use Tightenco \Collect \Support \Arr ;
23+ use Tightenco \Collect \Support \Collection ;
2224use Ymir \Cli \Command \AbstractCommand ;
2325use Ymir \Cli \Command \Network \CreateNetworkCommand ;
2426use Ymir \Cli \Console \ConsoleOutput ;
@@ -49,6 +51,28 @@ protected function configure()
4951 ->addOption ('type ' , null , InputOption::VALUE_REQUIRED , 'The database server type to create on the cloud provider ' );
5052 }
5153
54+ /**
55+ * Determine the network to create the database on.
56+ */
57+ protected function determineNetwork (string $ question , InputInterface $ input , ConsoleOutput $ output ): int
58+ {
59+ $ networks = $ this ->apiClient ->getTeamNetworks ($ this ->cliConfiguration ->getActiveTeamId ())->whereNotIn ('status ' , ['deleting ' , 'failed ' ]);
60+
61+ if ($ networks ->isEmpty () && !$ output ->confirm ('Your team doesn \'t have any provisioned networks to create the database server on. Would you like to create one first? <fg=default>(Answering "<comment>no</comment>" will cancel the command.)</> ' )) {
62+ throw new CommandCancelledException ();
63+ }
64+
65+ if ($ networks ->isEmpty ()) {
66+ $ this ->retryApi (function () use ($ output ) {
67+ $ this ->invoke ($ output , CreateNetworkCommand::NAME );
68+ }, 'Do you want to try creating a network again? ' , $ output );
69+
70+ return (int ) Arr::get ($ this ->apiClient ->getTeamNetworks ($ this ->cliConfiguration ->getActiveTeamId ())->last (), 'id ' );
71+ }
72+
73+ return parent ::determineNetwork ($ question , $ input , $ output );
74+ }
75+
5276 /**
5377 * {@inheritdoc}
5478 */
@@ -60,7 +84,7 @@ protected function perform(InputInterface $input, ConsoleOutput $output)
6084 $ name = $ output ->askSlug ('What is the name of the database server ' );
6185 }
6286
63- $ network = $ this ->determineNetwork ($ input , $ output );
87+ $ network = $ this ->apiClient -> getNetwork ( $ this -> determineNetwork (' On what network should the database server be created? ' , $ input , $ output) );
6488 $ type = $ this ->determineType ($ network , $ input , $ output );
6589 $ storage = $ this ->determineStorage ($ input , $ output );
6690 $ public = $ this ->determinePublic ($ input , $ output );
@@ -75,37 +99,6 @@ protected function perform(InputInterface $input, ConsoleOutput $output)
7599 $ output ->infoWithDelayWarning ('Database server created ' );
76100 }
77101
78- /**
79- * Determine the network to create the database on.
80- */
81- private function determineNetwork (InputInterface $ input , ConsoleOutput $ output ): array
82- {
83- $ networkIdOrName = $ this ->getStringOption ($ input , 'network ' , true );
84- $ networks = $ this ->apiClient ->getTeamNetworks ($ this ->cliConfiguration ->getActiveTeamId ())->whereNotIn ('status ' , ['deleting ' , 'failed ' ]);
85-
86- if ($ networks ->isEmpty () && !$ output ->confirm ('Your team doesn \'t have any provisioned networks to create the database server on. Would you like to create one first? <fg=default>(Answering "<comment>no</comment>" will cancel the command.)</> ' )) {
87- throw new CommandCancelledException ();
88- }
89-
90- if ($ networks ->isEmpty ()) {
91- $ this ->retryApi (function () use ($ output ) {
92- $ this ->invoke ($ output , CreateNetworkCommand::NAME );
93- }, 'Do you want to try creating a network again? ' , $ output );
94-
95- return $ this ->apiClient ->getTeamNetworks ($ this ->cliConfiguration ->getActiveTeamId ())->last ();
96- } elseif (!$ networks ->isEmpty () && empty ($ networkIdOrName )) {
97- $ networkIdOrName = $ output ->choiceWithResourceDetails ('On what network should the database server be created? ' , $ networks );
98- }
99-
100- $ network = $ networks ->firstWhere ('id ' , $ networkIdOrName ) ?? $ networks ->firstWhere ('name ' , $ networkIdOrName );
101-
102- if (!is_array ($ network )) {
103- throw new RuntimeException (sprintf ('Unable to find a network with "%s" as the ID or name ' , $ networkIdOrName ));
104- }
105-
106- return $ network ;
107- }
108-
109102 /**
110103 * Determine whether the database should be publicly accessible or not.
111104 */
@@ -142,7 +135,7 @@ private function determineStorage(InputInterface $input, ConsoleOutput $output):
142135 /**
143136 * Determine the database server type to create.
144137 */
145- private function determineType (array $ network , InputInterface $ input , ConsoleOutput $ output ): string
138+ private function determineType (Collection $ network , InputInterface $ input , ConsoleOutput $ output ): string
146139 {
147140 if (!isset ($ network ['provider ' ]['id ' ])) {
148141 throw new RuntimeException ('The Ymir API failed to return information on the cloud provider ' );
0 commit comments