Search in sources :

Example 26 with IService

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

the class ScaleDeploymentHandler method getDeploymentConfig.

private IDeploymentConfig getDeploymentConfig(IPod pod, IResourceWrapper<?, ?> wrapper) {
    IDeploymentConfig dc = null;
    if (!ResourceUtils.isBuildPod(pod)) {
        IServiceWrapper service = ResourceWrapperUtils.getServiceWrapperFor(wrapper, serviceWrapper -> ResourceUtils.areRelated(pod, (IService) serviceWrapper.getWrapped()));
        dc = getDeploymentConfig(service);
    }
    return dc;
}
Also used : IServiceWrapper(org.jboss.tools.openshift.internal.ui.models.IServiceWrapper) IDeploymentConfig(com.openshift.restclient.model.IDeploymentConfig) IService(com.openshift.restclient.model.IService)

Example 27 with IService

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

the class ScaleDeploymentHandler method getDeploymentConfig.

private IDeploymentConfig getDeploymentConfig(IReplicationController rc, IResourceWrapper<?, ?> wrapper) {
    IDeploymentConfig dc = null;
    IServiceWrapper service = ResourceWrapperUtils.getServiceWrapperFor(wrapper, serviceWrapper -> ResourceUtils.areRelated(rc, (IService) serviceWrapper.getWrapped()));
    if (service != null) {
        dc = ResourceUtils.getDeploymentConfigFor(rc, ResourceWrapperUtils.getResources(service.getResourcesOfKind(ResourceKind.DEPLOYMENT_CONFIG)));
    }
    return dc;
}
Also used : IServiceWrapper(org.jboss.tools.openshift.internal.ui.models.IServiceWrapper) IDeploymentConfig(com.openshift.restclient.model.IDeploymentConfig) IService(com.openshift.restclient.model.IService)

Example 28 with IService

use of com.openshift.restclient.model.IService 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 IService

use of com.openshift.restclient.model.IService 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 IService

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

the class ScaleDeploymentContributionItem method isRelevant.

private boolean isRelevant() {
    ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
    ISelection selection = service != null ? service.getSelection() : null;
    if (selection == null || selection.isEmpty()) {
        return false;
    }
    IRunningPodHolder podHolder = UIUtils.getFirstElement(selection, IRunningPodHolder.class);
    if (podHolder == null) {
        return false;
    }
    IOpenshiftUIElement<?, IOpenshiftUIElement<?, ?>> podUIElement = podHolder.getPodUIElement();
    if (!(podUIElement instanceof IResourceWrapper)) {
        return false;
    }
    IResourceWrapper<?, ?> podWrapper = (IResourceWrapper<?, ?>) podUIElement;
    if (ResourceWrapperUtils.getServiceWrapperFor(podWrapper, serviceWrapper -> ResourceUtils.areRelated((IPod) podWrapper.getWrapped(), (IService) serviceWrapper.getWrapped())) == null) {
        return false;
    }
    return true;
}
Also used : IOpenshiftUIElement(org.jboss.tools.openshift.internal.ui.models.IOpenshiftUIElement) ISelectionService(org.eclipse.ui.ISelectionService) OpenShiftImages(org.jboss.tools.openshift.internal.ui.OpenShiftImages) CompoundContributionItem(org.eclipse.ui.actions.CompoundContributionItem) PlatformUI(org.eclipse.ui.PlatformUI) ResourceWrapperUtils(org.jboss.tools.openshift.internal.ui.utils.ResourceWrapperUtils) ResourceUtils(org.jboss.tools.openshift.internal.core.util.ResourceUtils) CommandContributionItemParameter(org.eclipse.ui.menus.CommandContributionItemParameter) HashMap(java.util.HashMap) IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) IPod(com.openshift.restclient.model.IPod) UIUtils(org.jboss.tools.openshift.internal.common.ui.utils.UIUtils) MenuItem(org.eclipse.swt.widgets.MenuItem) IWorkbenchContribution(org.eclipse.ui.menus.IWorkbenchContribution) IRunningPodHolder(org.jboss.tools.openshift.internal.ui.models.IRunningPodHolder) Map(java.util.Map) SWT(org.eclipse.swt.SWT) IContributionItem(org.eclipse.jface.action.IContributionItem) ISelection(org.eclipse.jface.viewers.ISelection) IServiceLocator(org.eclipse.ui.services.IServiceLocator) Menu(org.eclipse.swt.widgets.Menu) CommandContributionItem(org.eclipse.ui.menus.CommandContributionItem) IOpenshiftUIElement(org.jboss.tools.openshift.internal.ui.models.IOpenshiftUIElement) IService(com.openshift.restclient.model.IService) IResourceWrapper(org.jboss.tools.openshift.internal.ui.models.IResourceWrapper) IRunningPodHolder(org.jboss.tools.openshift.internal.ui.models.IRunningPodHolder) ISelection(org.eclipse.jface.viewers.ISelection) ISelectionService(org.eclipse.ui.ISelectionService)

Aggregations

IService (com.openshift.restclient.model.IService)32 Test (org.junit.Test)15 Connection (org.jboss.tools.openshift.core.connection.Connection)10 IResource (com.openshift.restclient.model.IResource)9 IRoute (com.openshift.restclient.model.route.IRoute)8 IDeploymentConfig (com.openshift.restclient.model.IDeploymentConfig)6 IProject (com.openshift.restclient.model.IProject)5 ResourceKind (com.openshift.restclient.ResourceKind)4 IBuildConfig (com.openshift.restclient.model.IBuildConfig)4 IPod (com.openshift.restclient.model.IPod)4 IReplicationController (com.openshift.restclient.model.IReplicationController)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)3 CoreException (org.eclipse.core.runtime.CoreException)3 ResourceUtils (org.jboss.tools.openshift.internal.core.util.ResourceUtils)3 IClient (com.openshift.restclient.IClient)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2