Search in sources :

Example 26 with IRoute

use of com.openshift.restclient.model.route.IRoute in project jbosstools-openshift by jbosstools.

the class RouteTimeout method getRoute.

private IRoute getRoute(IResource resource, Connection connection, IProgressMonitor monitor) {
    SubMonitor routeMonitor = SubMonitor.convert(monitor);
    routeMonitor.beginTask("Determine route to set the haproxy timeout for...", 2);
    if (routeMonitor.isCanceled()) {
        return null;
    }
    List<IService> services = connection.getResources(ResourceKind.SERVICE, resource.getNamespaceName());
    Collection<IService> matchingServices = ResourceUtils.getServicesFor(resource, services);
    routeMonitor.worked(1);
    if (routeMonitor.isCanceled()) {
        return null;
    }
    List<IRoute> routes = connection.getResources(ResourceKind.ROUTE, resource.getNamespaceName());
    // TODO: support multiple matching routes, for now only get first
    Optional<IRoute> matchingRoute = matchingServices.stream().flatMap(service -> ResourceUtils.getRoutesFor(service, routes).stream()).findFirst();
    routeMonitor.worked(1);
    routeMonitor.done();
    return matchingRoute.orElse(null);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) IResource(com.openshift.restclient.model.IResource) ResourceKind(com.openshift.restclient.ResourceKind) NLS(org.eclipse.osgi.util.NLS) SubMonitor(org.eclipse.core.runtime.SubMonitor) Collection(java.util.Collection) OpenShiftServerUtils(org.jboss.tools.openshift.core.server.OpenShiftServerUtils) ResourceUtils(org.jboss.tools.openshift.internal.core.util.ResourceUtils) CoreException(org.eclipse.core.runtime.CoreException) OpenShiftCoreActivator(org.jboss.tools.openshift.internal.core.OpenShiftCoreActivator) Connection(org.jboss.tools.openshift.core.connection.Connection) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) List(java.util.List) OpenShiftAPIAnnotations(org.jboss.tools.openshift.core.OpenShiftAPIAnnotations) IRoute(com.openshift.restclient.model.route.IRoute) Optional(java.util.Optional) IService(com.openshift.restclient.model.IService) SubMonitor(org.eclipse.core.runtime.SubMonitor) IRoute(com.openshift.restclient.model.route.IRoute) IService(com.openshift.restclient.model.IService)

Example 27 with IRoute

use of com.openshift.restclient.model.route.IRoute in project jbosstools-openshift by jbosstools.

the class OpenShiftDebugMode method resetRouteTimeout.

private void resetRouteTimeout(IResource resource, Connection connection, IProgressMonitor monitor) throws CoreException {
    monitor.subTask("Clearing/restoring route timeout after debugging...");
    IRoute route = new RouteTimeout(resource, connection).reset(context, monitor);
    if (route != null) {
        safeSend(route, connection, monitor);
    }
}
Also used : IRoute(com.openshift.restclient.model.route.IRoute)

Example 28 with IRoute

use of com.openshift.restclient.model.route.IRoute in project jbosstools-openshift by jbosstools.

the class ServerAdapterHandler method getOpenShiftServer.

/**
 * Finds the OpenShift server corresponding to the selection or prompts the
 * user to create one.
 *
 * @param resource
 *            the selected OpenShift {@link IResource}
 *
 * @return the matching OpenShift {@link IServer} or <code>null</code> if
 *         none was found or user cancelled the creation operation.
 */
private IServer getOpenShiftServer(final IResource resource) {
    if (resource == null) {
        return null;
    }
    IResource source = null;
    IRoute route = null;
    if (resource instanceof IService) {
        source = (IService) resource;
    } else if (resource instanceof IRoute) {
        route = (IRoute) resource;
        final IRoute localRoute = route;
        source = (IService) route.getProject().getResources(ResourceKind.SERVICE).stream().filter(s -> ResourceUtils.areRelated(localRoute, (IService) s)).findFirst().orElseGet(() -> null);
    } else if (resource instanceof IReplicationController) {
        source = resource;
    } else if (resource instanceof IPod) {
        final Collection<IService> services = ResourceUtils.getServicesFor((IPod) resource, resource.getProject().getResources(ResourceKind.SERVICE));
        if (!services.isEmpty()) {
            source = services.iterator().next();
        } else {
            source = ResourceUtils.getDeploymentConfigOrReplicationControllerFor((IPod) resource);
        }
    }
    if (source != null) {
        final Connection connection = ConnectionsRegistryUtil.safeGetConnectionFor(source);
        return openOrCreateServerAdapter(source, route, connection);
    }
    return null;
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) ServerSettingsWizard(org.jboss.tools.openshift.internal.ui.server.ServerSettingsWizard) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ResourceUtils(org.jboss.tools.openshift.internal.core.util.ResourceUtils) OpenShiftResourceUniqueId(org.jboss.tools.openshift.core.util.OpenShiftResourceUniqueId) CoreException(org.eclipse.core.runtime.CoreException) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) IPod(com.openshift.restclient.model.IPod) UIUtils(org.jboss.tools.openshift.internal.common.ui.utils.UIUtils) PartInitException(org.eclipse.ui.PartInitException) CommonNavigator(org.eclipse.ui.navigator.CommonNavigator) OpenShiftUIActivator(org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator) IService(com.openshift.restclient.model.IService) IResource(com.openshift.restclient.model.IResource) ResourceKind(com.openshift.restclient.ResourceKind) NLS(org.eclipse.osgi.util.NLS) Collection(java.util.Collection) IServer(org.eclipse.wst.server.core.IServer) OpenShiftServerUtils(org.jboss.tools.openshift.core.server.OpenShiftServerUtils) ExecutionException(org.eclipse.core.commands.ExecutionException) Display(org.eclipse.swt.widgets.Display) Connection(org.jboss.tools.openshift.core.connection.Connection) WizardUtils(org.jboss.tools.common.ui.WizardUtils) IRoute(com.openshift.restclient.model.route.IRoute) ConnectionsRegistryUtil(org.jboss.tools.openshift.core.connection.ConnectionsRegistryUtil) ISelection(org.eclipse.jface.viewers.ISelection) AbstractHandler(org.eclipse.core.commands.AbstractHandler) IServerWorkingCopy(org.eclipse.wst.server.core.IServerWorkingCopy) IReplicationController(com.openshift.restclient.model.IReplicationController) Connection(org.jboss.tools.openshift.core.connection.Connection) IRoute(com.openshift.restclient.model.route.IRoute) Collection(java.util.Collection) IResource(com.openshift.restclient.model.IResource) IService(com.openshift.restclient.model.IService) IReplicationController(com.openshift.restclient.model.IReplicationController) IPod(com.openshift.restclient.model.IPod)

Example 29 with IRoute

use of com.openshift.restclient.model.route.IRoute in project jbosstools-openshift by jbosstools.

the class NewApplicationWizard method performFinish.

@Override
public boolean performFinish() {
    final IResourcesModelJob createJob = isTemplateFlow() ? fromTemplateModel.createFinishJob() : fromImageModel.createFinishJob();
    createJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            IStatus status = event.getResult();
            if (JobUtils.isOk(status) || JobUtils.isWarning(status)) {
                Display.getDefault().syncExec(createJob.getSummaryRunnable(getShell()));
                OpenShiftUIUtils.showOpenShiftExplorer();
                if (model.getEclipseProject() != null) {
                    // No need to import the project from git, it's already here
                    return;
                }
                Collection<IResource> resources = createJob.getResources();
                final Map<IProject, Collection<IBuildConfig>> projectsAndBuildConfigs = getBuildConfigs(resources);
                if (projectsAndBuildConfigs.isEmpty()) {
                    return;
                }
                Connection connection = model.getConnection();
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        ImportApplicationWizard wizard = new ImportApplicationWizard(projectsAndBuildConfigs);
                        wizard.addImportJobChangeListener(new JobChangeAdapter() {

                            @Override
                            public void done(IJobChangeEvent event) {
                                IService service = getService(resources);
                                List<org.eclipse.core.resources.IProject> importedProjects = wizard.getImportJob().getImportedProjects();
                                if (service != null && importedProjects.size() == 1) {
                                    Display.getDefault().asyncExec(new Runnable() {

                                        @Override
                                        public void run() {
                                            if (MessageDialog.openQuestion(getShell(), "Create server adapter", NLS.bind("Would you like to create a server adapter for the imported {0} project?", importedProjects.get(0).getName()))) {
                                                createServerAdapter(importedProjects.get(0), connection, service, getRoute(resources));
                                            }
                                        }
                                    });
                                }
                            }
                        });
                        new WizardDialog(getShell(), wizard).open();
                    }
                });
            }
        }

        protected Map<IProject, Collection<IBuildConfig>> getBuildConfigs(Collection<IResource> resources) {
            Map<IProject, Collection<IBuildConfig>> projects = new LinkedHashMap<>();
            for (IResource resource : resources) {
                if (resource instanceof IBuildConfig) {
                    IBuildConfig buildConfig = (IBuildConfig) resource;
                    if (StringUtils.isNotBlank(buildConfig.getSourceURI())) {
                        IProject p = buildConfig.getProject();
                        Collection<IBuildConfig> buildConfigs = projects.get(p);
                        if (buildConfigs == null) {
                            buildConfigs = new LinkedHashSet<>();
                            projects.put(p, buildConfigs);
                        }
                        buildConfigs.add(buildConfig);
                    }
                }
            }
            return projects;
        }

        protected IService getService(Collection<IResource> resources) {
            IResource service = getResourceOfType(resources, IService.class);
            return service == null ? null : (IService) service;
        }

        protected IRoute getRoute(Collection<IResource> resources) {
            IResource route = getResourceOfType(resources, IRoute.class);
            return route == null ? null : (IRoute) route;
        }

        private IResource getResourceOfType(Collection<IResource> resources, Class<? extends IResource> type) {
            for (IResource resource : resources) {
                if (type.isInstance(resource)) {
                    return resource;
                }
            }
            return null;
        }

        protected void createServerAdapter(org.eclipse.core.resources.IProject project, Connection connection, IService service, IRoute route) {
            try {
                IServerWorkingCopy server = OpenShiftServerUtils.create(OpenShiftResourceUniqueId.get(service));
                ServerSettingsWizardPageModel serverModel = new ServerSettingsWizardPageModel(service, route, project, connection, server, OCBinary.getInstance().getStatus(connection, new NullProgressMonitor()), RSyncValidator.get().getStatus());
                serverModel.loadResources();
                serverModel.updateServer();
                server.setAttribute(OpenShiftServerUtils.SERVER_START_ON_CREATION, false);
                serverModel.saveServer(null);
            } catch (CoreException ce) {
                OpenShiftUIActivator.getDefault().getLogger().logError("Error occured while creating a server adapter", ce);
                return;
            }
        }
    });
    boolean success = false;
    try {
        Job job = new JobChainBuilder(createJob.getJob()).runWhenSuccessfullyDone(new RefreshResourcesJob(createJob, true)).build();
        IStatus status = runInWizard(job, createJob.getDelegatingProgressMonitor(), getContainer());
        success = isFailed(status);
    } catch (InvocationTargetException | InterruptedException e) {
        OpenShiftUIActivator.getDefault().getLogger().logError(e);
        success = false;
    } finally {
        UsageStats.getInstance().newV3Application(model.getConnection().getHost(), success);
    }
    return success;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) ImportApplicationWizard(org.jboss.tools.openshift.internal.ui.wizard.importapp.ImportApplicationWizard) IRoute(com.openshift.restclient.model.route.IRoute) RefreshResourcesJob(org.jboss.tools.openshift.internal.ui.job.RefreshResourcesJob) IBuildConfig(com.openshift.restclient.model.IBuildConfig) List(java.util.List) IResourcesModelJob(org.jboss.tools.openshift.internal.ui.job.IResourcesModelJob) RefreshResourcesJob(org.jboss.tools.openshift.internal.ui.job.RefreshResourcesJob) Job(org.eclipse.core.runtime.jobs.Job) IService(com.openshift.restclient.model.IService) JobChainBuilder(org.jboss.tools.openshift.internal.common.core.job.JobChainBuilder) Connection(org.jboss.tools.openshift.core.connection.Connection) ServerSettingsWizardPageModel(org.jboss.tools.openshift.internal.ui.server.ServerSettingsWizardPageModel) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IProject(com.openshift.restclient.model.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IResourcesModelJob(org.jboss.tools.openshift.internal.ui.job.IResourcesModelJob) CoreException(org.eclipse.core.runtime.CoreException) IServerWorkingCopy(org.eclipse.wst.server.core.IServerWorkingCopy) Collection(java.util.Collection) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IResource(com.openshift.restclient.model.IResource)

Example 30 with IRoute

use of com.openshift.restclient.model.route.IRoute in project jbosstools-openshift by jbosstools.

the class SelectedRoutePreference method getKey.

String getKey(List<IRoute> routes) {
    // sort to remove the dependency on order
    Set<String> set = new TreeSet<>();
    for (IRoute route : routes) {
        String key = getKey(route);
        if (key != null)
            set.add(key);
    }
    StringBuilder sb = new StringBuilder();
    for (String key : set) {
        if (sb.length() > 0)
            sb.append(KEY_SEPARATOR);
        sb.append(key);
    }
    return sb.toString();
}
Also used : TreeSet(java.util.TreeSet) IRoute(com.openshift.restclient.model.route.IRoute)

Aggregations

IRoute (com.openshift.restclient.model.route.IRoute)34 Test (org.junit.Test)15 IService (com.openshift.restclient.model.IService)10 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 List (java.util.List)6 ResourceKind (com.openshift.restclient.ResourceKind)5 Connection (org.jboss.tools.openshift.core.connection.Connection)5 IProject (com.openshift.restclient.model.IProject)4 IResource (com.openshift.restclient.model.IResource)4 Collection (java.util.Collection)4 CoreException (org.eclipse.core.runtime.CoreException)4 NLS (org.eclipse.osgi.util.NLS)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 AbstractHandler (org.eclipse.core.commands.AbstractHandler)3 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 IStatus (org.eclipse.core.runtime.IStatus)3 ISelection (org.eclipse.jface.viewers.ISelection)3