Search in sources :

Example 96 with Connection

use of org.jboss.tools.openshift.core.connection.Connection 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 97 with Connection

use of org.jboss.tools.openshift.core.connection.Connection in project jbosstools-openshift by jbosstools.

the class ApplicationSourceTreeItems method loadImageStreams.

private Collection<IApplicationSource> loadImageStreams(IProject project, Connection conn) {
    final Collection<IImageStream> streams = conn.getResources(ResourceKind.IMAGE_STREAM, project.getNamespaceName());
    try {
        if (StringUtils.isNotBlank(conn.getClusterNamespace())) {
            Collection<IImageStream> commonStreams = conn.getResources(ResourceKind.IMAGE_STREAM, (String) conn.getClusterNamespace());
            commonStreams.stream().filter(s -> !streams.contains(s)).forEach(s -> streams.add(s));
        }
    } catch (OpenShiftException e) {
        OpenShiftUIActivator.log(IStatus.ERROR, e.getLocalizedMessage(), e);
    }
    Collection<IApplicationSource> sources = new ArrayList<>();
    for (IImageStream is : streams) {
        List<ITagReference> tags = is.getTags().stream().filter(t -> t.isAnnotatedWith(OpenShiftAPIAnnotations.TAGS) && ArrayUtils.contains(t.getAnnotation(OpenShiftAPIAnnotations.TAGS).split(","), BUILDER_TAG)).collect(Collectors.toList());
        if (!tags.isEmpty()) {
            tags.forEach(t -> sources.add(new ImageStreamApplicationSource(is, t)));
        }
    }
    return sources;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) IImageStream(com.openshift.restclient.model.IImageStream) OpenShiftException(com.openshift.restclient.OpenShiftException) TemplateApplicationSource(org.jboss.tools.openshift.internal.ui.wizard.newapp.fromtemplate.TemplateApplicationSource) ObservableTreeItem(org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem) IProject(com.openshift.restclient.model.IProject) ArrayList(java.util.ArrayList) IStatus(org.eclipse.core.runtime.IStatus) IModelFactory(org.jboss.tools.openshift.internal.ui.treeitem.IModelFactory) OpenShiftUIActivator(org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator) ResourceKind(com.openshift.restclient.ResourceKind) ITagReference(com.openshift.restclient.model.image.ITagReference) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Connection(org.jboss.tools.openshift.core.connection.Connection) List(java.util.List) ICommonAttributes(org.jboss.tools.openshift.core.ICommonAttributes) OpenShiftAPIAnnotations(org.jboss.tools.openshift.core.OpenShiftAPIAnnotations) CapabilityVisitor(com.openshift.restclient.capability.CapabilityVisitor) ConnectionsRegistryUtil(org.jboss.tools.openshift.core.connection.ConnectionsRegistryUtil) IProjectTemplateList(com.openshift.restclient.capability.resources.IProjectTemplateList) ITemplate(com.openshift.restclient.model.template.ITemplate) ImageStreamApplicationSource(org.jboss.tools.openshift.internal.ui.wizard.newapp.fromimage.ImageStreamApplicationSource) Collections(java.util.Collections) ArrayUtils(org.apache.commons.lang.ArrayUtils) ITagReference(com.openshift.restclient.model.image.ITagReference) OpenShiftException(com.openshift.restclient.OpenShiftException) ArrayList(java.util.ArrayList) IImageStream(com.openshift.restclient.model.IImageStream) ImageStreamApplicationSource(org.jboss.tools.openshift.internal.ui.wizard.newapp.fromimage.ImageStreamApplicationSource)

Example 98 with Connection

use of org.jboss.tools.openshift.core.connection.Connection in project jbosstools-openshift by jbosstools.

the class ApplicationSourceFromImageModel method lookupImageMetadata.

@Override
protected IDockerImageMetadata lookupImageMetadata() {
    if (source == null) {
        return null;
    }
    try {
        Connection conn = ConnectionsRegistryUtil.getConnectionFor(getProject());
        IResource istag = conn.getResource(ResourceKind.IMAGE_STREAM_TAG, source.getNamespace(), source.getName());
        return new ImageStreamTagMetaData(istag.toJson(true));
    } catch (Exception e) {
        OpenShiftUIActivator.getDefault().getLogger().logError(NLS.bind("Unable to retrieve imagestream tag for {0}", getImageName()), e);
    }
    return null;
}
Also used : ImageStreamTagMetaData(org.jboss.tools.openshift.internal.core.docker.ImageStreamTagMetaData) Connection(org.jboss.tools.openshift.core.connection.Connection) IResource(com.openshift.restclient.model.IResource) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 99 with Connection

use of org.jboss.tools.openshift.core.connection.Connection in project jbosstools-openshift by jbosstools.

the class DeployImageJobTest method givenAnImageStreamTo.

private IImageStream givenAnImageStreamTo(String namespace, DockerImageURI uri) {
    IImageStream is = mock(IImageStream.class);
    when(is.getNamespaceName()).thenReturn(namespace);
    when(is.getName()).thenReturn(IMAGE_STREAM_NAME);
    when(is.getDockerImageRepository()).thenReturn(uri);
    List<IResource> streams = Arrays.asList(is);
    Connection conn = mock(Connection.class);
    when(conn.getResources(anyString(), eq(namespace))).thenReturn(streams);
    when(conn.getClusterNamespace()).thenReturn(ICommonAttributes.COMMON_NAMESPACE);
    when(parameters.getConnection()).thenReturn(conn);
    return is;
}
Also used : ResourceMocks.createConnection(org.jboss.tools.openshift.test.util.ResourceMocks.createConnection) Connection(org.jboss.tools.openshift.core.connection.Connection) IImageStream(com.openshift.restclient.model.IImageStream) IResource(com.openshift.restclient.model.IResource)

Example 100 with Connection

use of org.jboss.tools.openshift.core.connection.Connection in project jbosstools-openshift by jbosstools.

the class CDKOpenshiftUtility method createOpenshiftConnection.

public IConnection createOpenshiftConnection(ServiceManagerEnvironment env, ConnectionsRegistry registry) {
    // Create the connection
    String soughtHost = env.openshiftHost + ":" + env.openshiftPort;
    ConnectionsFactoryTracker connectionsFactory = new ConnectionsFactoryTracker();
    connectionsFactory.open();
    IConnectionFactory factory = connectionsFactory.getById(IConnectionsFactory.CONNECTIONFACTORY_OPENSHIFT_ID);
    IConnection con = factory.create(soughtHost);
    // Set some defaults
    String authScheme = env.getAuthorizationScheme();
    String username = env.getUsername();
    String password = env.getPassword();
    if (authScheme != null && !authScheme.isEmpty()) {
        authScheme = new String("" + authScheme.charAt(0)).toUpperCase() + authScheme.substring(1);
    }
    ((Connection) con).setAuthScheme(authScheme);
    ((Connection) con).setUsername(username);
    if (password != null) {
        ((Connection) con).setPassword(password);
    }
    ((Connection) con).setRememberPassword(true);
    String ocLoc = env.get(ServiceManagerEnvironmentLoader.OC_LOCATION_KEY);
    if (ocLoc != null) {
        ((Connection) con).setExtendedProperty(ICommonAttributes.OC_LOCATION_KEY, ocLoc);
        ((Connection) con).setExtendedProperty(ICommonAttributes.OC_OVERRIDE_KEY, true);
    }
    updateOpenshiftConnection(env, con, false);
    if (registry != null)
        registry.add(con);
    return con;
}
Also used : Connection(org.jboss.tools.openshift.core.connection.Connection) IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) IConnectionFactory(org.jboss.tools.openshift.common.core.connection.IConnectionFactory) ConnectionsFactoryTracker(org.jboss.tools.openshift.common.core.connection.ConnectionsFactoryTracker)

Aggregations

Connection (org.jboss.tools.openshift.core.connection.Connection)110 Test (org.junit.Test)48 IConnection (org.jboss.tools.openshift.common.core.connection.IConnection)30 IResource (com.openshift.restclient.model.IResource)27 IProject (com.openshift.restclient.model.IProject)18 IService (com.openshift.restclient.model.IService)11 IStatus (org.eclipse.core.runtime.IStatus)11 IDeploymentConfig (com.openshift.restclient.model.IDeploymentConfig)9 Collection (java.util.Collection)8 List (java.util.List)8 ISelection (org.eclipse.jface.viewers.ISelection)8 IPod (com.openshift.restclient.model.IPod)7 ArrayList (java.util.ArrayList)7 CoreException (org.eclipse.core.runtime.CoreException)7 OpenShiftException (com.openshift.restclient.OpenShiftException)6 ResourceKind (com.openshift.restclient.ResourceKind)6 IRoute (com.openshift.restclient.model.route.IRoute)6 MultiValidator (org.eclipse.core.databinding.validation.MultiValidator)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 Status (org.eclipse.core.runtime.Status)6