Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jbide-24961: DON'T MERGE by now. #1647

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected IStatus doRun(final IProgressMonitor monitor) {
try {
// first, we need to tag the image with the OpenShift target
// project
this.dockerConnection.tagImage(imageName, tmpImageName);
this.dockerConnection.tagImage(imageName, tmpImageName); // username/fedora:my-tag & 172.30.1.1:5000/myproject/fedora:my-tag
// then we can push that image with the new name
this.dockerConnection.pushImage(tmpImageName, registryAccount, getPushProgressHandler(tmpImageName));
// FIXME: needs more fined tuned error handling once Neon.0 is no longer supported:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.jboss.tools.openshift.internal.common.core.job.AbstractDelegatingMonitorJob;
import org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator;
import org.jboss.tools.openshift.internal.ui.wizard.common.IResourceLabelsPageModel.Label;
import org.jboss.tools.openshift.internal.ui.wizard.importapp.ImportApplicationWizard;

import com.openshift.restclient.IClient;
import com.openshift.restclient.OpenShiftException;
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.capability.CapabilityVisitor;
import com.openshift.restclient.capability.resources.IClientCapability;
import com.openshift.restclient.capability.resources.IProjectTemplateProcessing;
import com.openshift.restclient.model.IBuildConfig;
import com.openshift.restclient.model.IProject;
import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.template.IParameter;
Expand All @@ -45,21 +51,23 @@ public class CreateApplicationFromTemplateJob extends AbstractDelegatingMonitorJ
private Collection<Label> labels;
private Collection<IParameter> parameters;
private Collection<IResource> resources;
private String buildConfigName;

public CreateApplicationFromTemplateJob(IProject project, ITemplate template) {
this(project, template, Collections.emptyList(), Collections.emptyList());
this(project, template, "", Collections.emptyList(),Collections.emptyList());
}

public CreateApplicationFromTemplateJob(IProject project, ITemplate template, Collection<IParameter> parameters,
Collection<Label> labels) {
public CreateApplicationFromTemplateJob(IProject project, ITemplate template, String buildConfigName, Collection<IParameter> parameters, Collection<Label> labels) {
super("Create Application From Template Job");
this.project = project;
this.template = template;
this.buildConfigName = buildConfigName;
this.labels = labels;
this.parameters = parameters;
}

@Override
@SuppressWarnings("serial")
@Override
protected IStatus doRun(IProgressMonitor monitor) {
template.updateParameterValues(parameters);
for (Label label : labels) {
Expand All @@ -70,28 +78,44 @@ protected IStatus doRun(IProgressMonitor monitor) {

@Override
public IStatus visit(IProjectTemplateProcessing capability) {

try {
ITemplate processed = capability.process(template);
Collection<IResource> existing = findExistingResources(project, processed);
if (!existing.isEmpty()) {
if(!existing.isEmpty()) {
return createErrorStatusForExistingResources(existing);
}
parameters = processed.getParameters().values();
resources = capability.apply(processed);
return handleResponse(resources);
} catch (OpenShiftException e) {
}catch(OpenShiftException e) {
String message = e.getMessage();
if (e.getStatus() != null) {
if(e.getStatus() != null) {
message = e.getStatus().getMessage();
}
return new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID, -1, message, e);
}
}

}, new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID,
"Template processing is unsupported for this client and server combination.", null));

},
new Status(IStatus.ERROR, OpenShiftUIActivator.PLUGIN_ID, "Template processing is unsupported for this client and server combination.", null));

// if (resources != null) {
// Collection<IResource> buildConfigs = resources.stream().filter(r -> ResourceKind.BUILD_CONFIG.equals(r.getKind())).collect(Collectors.toList());
// if (!buildConfigs.isEmpty()) {
// Optional<IResource> buildConfigToImport = buildConfigs.stream().filter(r -> this.buildConfigName.equals(r.getName())).findFirst();
// if (buildConfigToImport.isPresent()) {
// ArrayList<IBuildConfig> buildConfigList = new ArrayList<>();
// buildConfigList.add((IBuildConfig)buildConfigToImport.get());
// Map<IProject, Collection<IBuildConfig>> projectsAndBuildConfigs = new HashMap<>();
// projectsAndBuildConfigs.put(project, buildConfigList);
// ImportApplicationWizard wizard = new ImportApplicationWizard(projectsAndBuildConfigs);
// wizard.init(workbench, selection);
// }
// }
// }

return status;
}

Expand All @@ -109,7 +133,7 @@ public Collection<IResource> visit(IClientCapability capability) {
try {
IResource found = client.get(resource.getKind(), resource.getName(), project.getName());
existing.add(found);
} catch (OpenShiftException e) {
}catch(OpenShiftException e) {
//this is expected if the resource is not found
//@TODO change to NotFoundException of some kind
}
Expand All @@ -118,32 +142,33 @@ public Collection<IResource> visit(IClientCapability capability) {
}
}, new ArrayList<IResource>());
}

/**
* Get the list of parameters for this Job.
* The values
* will have changed if the parameter is generated and
* has been processed by the server.
* @return
*/
public Collection<IParameter> getParameters() {
public Collection<IParameter> getParameters(){
return parameters;
}



@Override
public Collection<IResource> getResources() {
public Collection<IResource> getResources(){
return resources;
}

private IStatus handleResponse(Collection<IResource> resources) {
int severity = IStatus.OK;
for (IResource resource : resources) {
if (resource.getKind() == ResourceKind.STATUS) {
if(resource.getKind() == ResourceKind.STATUS) {
severity = IStatus.WARNING;
break;
}
}
return new Status(severity, OpenShiftUIActivator.PLUGIN_ID, OK, "Resources created from template.", null);
return new Status(severity, OpenShiftUIActivator.PLUGIN_ID, OK,"Resources created from template.",null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jboss.tools.openshift.internal.ui.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.osgi.util.NLS;

import com.openshift.restclient.model.template.IParameter;

public class ResourceParametersUtils {

public static final String LABEL_UNKNOWN_PARAMETER = "(Unknown parameter {0})";

private static final Pattern PATTERN = Pattern.compile("\\$\\{[^}]+\\}");

private ResourceParametersUtils() {}

public static String replaceParametersInString(Map<String, IParameter> templateParameters, String str) {
if (str == null || str.length() == 0) {
return "";
}
if (templateParameters == null || templateParameters.isEmpty()) {
return str;
}
StringBuffer result = new StringBuffer();
Matcher m = PATTERN.matcher(str);
while (m.find()) {
String parameterVariable = m.group();
String parameterName = parameterVariable.substring(2, parameterVariable.length() - 1);
if (templateParameters.containsKey(parameterName)) {
m.appendReplacement(result, templateParameters.get(parameterName).getValue());
} else {
m.appendReplacement(result, NLS.bind(LABEL_UNKNOWN_PARAMETER, parameterName));
}
}
m.appendTail(result);
return result.toString();
}
}
Loading