Skip to content

Commit af4a659

Browse files
authored
fix: service copy without default template (#1558)
### Motivation When copying a service without providing a template we try to fallback to the "<TaskName>/default" template. But our fallback logic is not working correctly and causing an NPE. ### Modification We now only accept a single service when copying. If no default template exists and no template is provided we respond with an message indicating the missing template. ### Result The `service <Service> copy` Command is working again. ##### Other context Fixes #1550
1 parent d88453f commit af4a659

3 files changed

Lines changed: 24 additions & 39 deletions

File tree

node/src/main/java/eu/cloudnetservice/node/command/sub/ServiceCommand.java

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
import eu.cloudnetservice.common.column.RowedFormatter;
2323
import eu.cloudnetservice.common.language.I18n;
2424
import eu.cloudnetservice.common.resource.ResourceFormatter;
25-
import eu.cloudnetservice.common.tuple.Tuple2;
2625
import eu.cloudnetservice.common.util.WildcardUtil;
2726
import eu.cloudnetservice.driver.channel.ChannelMessageSender;
2827
import eu.cloudnetservice.driver.event.EventListener;
2928
import eu.cloudnetservice.driver.event.EventManager;
3029
import eu.cloudnetservice.driver.event.events.service.CloudServiceLogEntryEvent;
3130
import eu.cloudnetservice.driver.provider.CloudServiceProvider;
32-
import eu.cloudnetservice.driver.provider.SpecificCloudServiceProvider;
3331
import eu.cloudnetservice.driver.service.ServiceDeployment;
3432
import eu.cloudnetservice.driver.service.ServiceInfoSnapshot;
3533
import eu.cloudnetservice.driver.service.ServiceRemoteInclusion;
@@ -51,7 +49,6 @@
5149
import java.util.Objects;
5250
import java.util.Set;
5351
import java.util.regex.Pattern;
54-
import java.util.stream.Collectors;
5552
import java.util.stream.Stream;
5653
import lombok.NonNull;
5754
import org.incendo.cloud.annotation.specifier.Greedy;
@@ -203,44 +200,32 @@ public void copyService(
203200
@Nullable @Flag("includes") @Quoted String includes,
204201
@Flag("case-sensitive") boolean caseSensitive
205202
) {
206-
// associate all services with a template
207-
Collection<Tuple2<SpecificCloudServiceProvider, ServiceTemplate>> targets = services.stream()
208-
.map(service -> {
209-
if (template != null) {
210-
return new Tuple2<>(service.provider(), template);
211-
} else {
212-
// find a matching template
213-
return service.configuration().templates().stream()
214-
.filter(st -> st.prefix().equalsIgnoreCase(service.serviceId().taskName()))
215-
.filter(st -> st.name().equalsIgnoreCase("default"))
216-
.map(st -> new Tuple2<>(service.provider(), st))
217-
.findFirst()
218-
.orElse(null);
219-
}
220-
})
221-
.collect(Collectors.toSet());
222-
// check if we found a result
223-
if (targets.isEmpty()) {
224-
source.sendMessage(I18n.trans("command-service-copy-no-default-template"));
225-
return;
203+
var service = services.iterator().next();
204+
var serviceProvider = service.provider();
205+
if (template == null) {
206+
template = serviceProvider.installedTemplates().stream()
207+
.filter(st -> st.prefix().equalsIgnoreCase(service.serviceId().taskName()))
208+
.filter(st -> st.name().equalsIgnoreCase("default"))
209+
.findFirst()
210+
.orElse(null);
211+
212+
if (template == null) {
213+
source.sendMessage(I18n.trans("command-service-copy-no-default-template", service.serviceId().name()));
214+
return;
215+
}
226216
}
217+
227218
// split on a semicolon and try to fix the patterns the user entered
228219
var parsedExcludes = parseDeploymentPatterns(excludes, caseSensitive);
229220
var parsedIncludes = parseDeploymentPatterns(includes, caseSensitive);
230-
for (var target : targets) {
231-
target.first().addServiceDeployment(ServiceDeployment.builder()
232-
.template(target.second())
233-
.excludes(parsedExcludes)
234-
.includes(parsedIncludes)
235-
.withDefaultExclusions()
236-
.build());
237-
target.first().removeAndExecuteDeployments();
238-
// send a message for each service we did copy the template of
239-
//noinspection ConstantConditions
240-
source.sendMessage(I18n.trans("command-service-copy-success",
241-
target.first().serviceInfo().name(),
242-
target.second().toString()));
243-
}
221+
serviceProvider.addServiceDeployment(ServiceDeployment.builder()
222+
.template(template)
223+
.excludes(parsedExcludes)
224+
.includes(parsedIncludes)
225+
.withDefaultExclusions()
226+
.build());
227+
serviceProvider.removeAndExecuteDeployments();
228+
source.sendMessage(I18n.trans("command-service-copy-success", service.serviceId().name(), template));
244229
}
245230

246231
@Command("service|ser <name> delete|del")

node/src/main/resources/lang/de_DE.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ command-cluster-start-sync=Die Cluster Synchronisation wird gestartet
175175
# Command Service
176176
#
177177
command-service-description=Verwaltet alle Services in dem Cluster
178-
command-service-copy-no-default-template=Der von Dir angegebene Service hat keine default Template. Benutze "copy {0$name$} template\=storage\:prefix/name" um ein Template anzugeben, in das du kopieren möchtest
178+
command-service-copy-no-default-template=Der angegebene Service hat kein default Template. Um ein Template anzugeben kann "service {0$name$} copy --template storage\:prefix/name" verwendet werden
179179
command-service-copy-success=Der Service {0$name$} wurde erfolgreich in das Template {1$template$} kopiert
180180
command-service-add-deployment-success=Das Deployment {0$deployment$} wurde erfolgreich zu der Warteschlange hinzugefügt
181181
command-service-deploy-deployment-success=Die Ressourcen des Services wurden erfolgreich deployed

node/src/main/resources/lang/en_US.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ command-cluster-start-sync=The cluster sync starts now
175175
# Command Service
176176
#
177177
command-service-description=Manages all services in the cluster
178-
command-service-copy-no-default-template=The service you provided does not have a default template, use "copy {0$name$} template=storage:prefix/name" to provide a template you would like to copy to
178+
command-service-copy-no-default-template=The provided service does not have a default template. To specify a template "service {0$name$} copy --template storage:prefix/name" can be used
179179
command-service-copy-success=The service {0$name$} was successfully copied to the template {1$template$}
180180
command-service-add-deployment-success=The deployment {0$deployment$} was successfully added to the waiting deployments
181181
command-service-deploy-deployment-success=The resources of the service were successfully deployed

0 commit comments

Comments
 (0)