Search in sources :

Example 31 with FabricService

use of io.fabric8.api.FabricService in project fabric8 by jboss-fuse.

the class ProfileServiceImpl method deleteProfile.

@Override
public void deleteProfile(FabricService fabricService, String versionId, String profileId, boolean force) {
    assertValid();
    Profile profile = getRequiredProfile(versionId, profileId);
    LOGGER.info("deleteProfile: {}", profile);
    // TODO: what about child profiles ?
    Container[] containers = fabricService != null ? fabricService.getAssociatedContainers(versionId, profileId) : new Container[0];
    if (containers.length == 0) {
        profileRegistry.get().deleteProfile(versionId, profileId);
    } else if (force) {
        for (Container container : containers) {
            container.removeProfiles(profileId);
        }
        profileRegistry.get().deleteProfile(versionId, profileId);
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("Cannot delete profile:").append(profileId).append(".");
        sb.append("Profile has assigned ").append(containers.length).append(" container(s):");
        for (Container c : containers) {
            sb.append(" ").append(c.getId());
        }
        sb.append(". Use force option to also remove the profile from the containers.");
        throw new FabricException(sb.toString());
    }
    // lets remove any pending requirements on this profile
    FabricRequirements requirements = fabricService != null ? fabricService.getRequirements() : null;
    if (requirements != null && requirements.removeProfileRequirements(profileId)) {
        try {
            fabricService.setRequirements(requirements);
        } catch (IOException e) {
            throw new FabricException("Failed to update requirements after deleting profile " + profileId + ". " + e, e);
        }
    }
}
Also used : Container(io.fabric8.api.Container) FabricException(io.fabric8.api.FabricException) FabricRequirements(io.fabric8.api.FabricRequirements) IOException(java.io.IOException) Profile(io.fabric8.api.Profile)

Example 32 with FabricService

use of io.fabric8.api.FabricService in project fabric8 by jboss-fuse.

the class BeanUtils method convertContainerToMap.

public static Map<String, Object> convertContainerToMap(FabricService fabricService, Container container, List<String> fields) {
    Map<String, Object> answer = new TreeMap<String, Object>();
    for (String field : fields) {
        if (field.equalsIgnoreCase("profiles") || field.equalsIgnoreCase("profileIds")) {
            answer.put(field, Ids.getIds(container.getProfiles()));
        } else if (field.equalsIgnoreCase("childrenIds") || field.equalsIgnoreCase("children")) {
            answer.put(field, Ids.getIds(container.getChildren()));
        } else if (field.equalsIgnoreCase("parent") || field.equalsIgnoreCase("parentId")) {
            answer.put(field, Ids.getId(container.getParent()));
        } else if (field.equalsIgnoreCase("version") || field.equalsIgnoreCase("versionId")) {
            answer.put(field, container.getVersionId());
        } else if (field.equalsIgnoreCase("overlayProfile")) {
            Profile overlayProfile = container.getOverlayProfile();
            Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService, overlayProfile);
            answer.put(field, convertProfileToMap(fabricService, effectiveProfile, getFields(Profile.class)));
        } else {
            addProperty(container, field, answer);
        }
    }
    return answer;
}
Also used : TreeMap(java.util.TreeMap) Profile(io.fabric8.api.Profile)

Example 33 with FabricService

use of io.fabric8.api.FabricService in project fabric8 by jboss-fuse.

the class BeanUtils method getFields.

public static List<String> getFields(Class clazz) {
    List<String> answer = new ArrayList<String>();
    try {
        for (PropertyDescriptor desc : PropertyUtils.getPropertyDescriptors(clazz)) {
            if (desc.getReadMethod() != null) {
                answer.add(desc.getName());
            }
        }
    } catch (Exception e) {
        throw new FabricException("Failed to get property descriptors for " + clazz.toString(), e);
    }
    // few tweaks to maintain compatibility with existing views for now...
    if (clazz.getSimpleName().equals("Container")) {
        answer.add("parentId");
        answer.add("versionId");
        answer.add("profileIds");
        answer.add("childrenIds");
        answer.remove("fabricService");
    } else if (clazz.getSimpleName().equals("Profile")) {
        answer.add("id");
        answer.add("parentIds");
        answer.add("childIds");
        answer.add("containerCount");
        answer.add("containers");
        answer.add("fileConfigurations");
    } else if (clazz.getSimpleName().equals("Version")) {
        answer.add("id");
        answer.add("defaultVersion");
    }
    return answer;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Example 34 with FabricService

use of io.fabric8.api.FabricService in project fabric8 by jboss-fuse.

the class FabricManager method doGetProfile.

Map<String, Object> doGetProfile(Version version, String profileId, List<String> fields, boolean mandatory) {
    Profile profile;
    if (mandatory) {
        profile = version.getRequiredProfile(profileId);
    } else {
        profile = version.getProfile(profileId);
    }
    if (profile == null) {
        return null;
    }
    Map<String, Object> answer = BeanUtils.convertProfileToMap(fabricService, profile, fields);
    String iconURLField = "iconURL";
    if (fields.contains(iconURLField) && !profile.isOverlay()) {
        // TODO this could move to Profile.getIconURL() but that would require
        // introducing profileService into ProfileImpl and the ProfileBuilder stuff
        String restApi = restApiUrl();
        if (restApi != null && restApi.length() > 0) {
            // turn REST into relative URI so it works with docker containers etc (avoids local ports etc)
            try {
                URL url = new URL(restApi);
                restApi = url.getPath();
            } catch (MalformedURLException e) {
            // Ignore
            }
            String icon = getIconURL(version, version.getId(), profile, profileId, restApi);
            answer.put(iconURLField, icon);
        }
    }
    return answer;
}
Also used : MalformedURLException(java.net.MalformedURLException) Profile(io.fabric8.api.Profile) URL(java.net.URL)

Example 35 with FabricService

use of io.fabric8.api.FabricService in project fabric8 by jboss-fuse.

the class FabricManager method getProfileFeatures.

@Override
public Map<String, Object> getProfileFeatures(String versionId, String profileId) {
    Profile profile = profileService.getVersion(versionId).getRequiredProfile(profileId);
    Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService, profileService.getOverlayProfile(profile));
    Map<String, Boolean> isParentFeature = new HashMap<String, Boolean>();
    for (String feature : profile.getFeatures()) {
        isParentFeature.put(feature, Boolean.FALSE);
    }
    for (String feature : effectiveProfile.getFeatures()) {
        if (isParentFeature.get(feature) == null) {
            isParentFeature.put(feature, Boolean.TRUE);
        }
    }
    Map<String, Object> rc = new HashMap<String, Object>();
    List<Map<String, Object>> featureDefs = new ArrayList<Map<String, Object>>();
    for (Map.Entry<String, Boolean> featureEntry : isParentFeature.entrySet()) {
        Map<String, Object> featureDef = new HashMap<String, Object>();
        featureDef.put("id", featureEntry.getKey());
        featureDef.put("isParentFeature", featureEntry.getValue());
        featureDefs.add(featureDef);
    }
    rc.put("featureDefinitions", featureDefs);
    List<Map<String, Object>> repositoryDefs = new ArrayList<Map<String, Object>>();
    for (String repo : effectiveProfile.getRepositories()) {
        Map<String, Object> repoDef = new HashMap<String, Object>();
        repoDef.put("id", repo);
        Closeable closeable = null;
        try {
            URL url = new URL(repo);
            InputStream os = url.openStream();
            closeable = os;
            InputStream is = new BufferedInputStream(url.openStream());
            closeable = is;
            char[] buffer = new char[8192];
            StringBuilder data = new StringBuilder();
            Reader in = new InputStreamReader(is, "UTF-8");
            closeable = in;
            for (; ; ) {
                int stat = in.read(buffer, 0, buffer.length);
                if (stat < 0) {
                    break;
                }
                data.append(buffer, 0, stat);
            }
            repoDef.put("data", data.toString());
        } catch (Throwable t) {
            repoDef.put("error", t.getMessage());
        } finally {
            try {
                if (closeable != null) {
                    closeable.close();
                }
            } catch (Throwable t) {
            // whatevs, I tried
            }
        }
        repositoryDefs.add(repoDef);
    }
    rc.put("repositoryDefinitions", repositoryDefs);
    return rc;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Profile(io.fabric8.api.Profile) URL(java.net.URL) BufferedInputStream(java.io.BufferedInputStream) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Aggregations

FabricService (io.fabric8.api.FabricService)80 Container (io.fabric8.api.Container)76 Test (org.junit.Test)52 Profile (io.fabric8.api.Profile)50 BundleContext (org.osgi.framework.BundleContext)29 Version (io.fabric8.api.Version)24 ArrayList (java.util.ArrayList)21 ProfileService (io.fabric8.api.ProfileService)19 HashMap (java.util.HashMap)16 CuratorFramework (org.apache.curator.framework.CuratorFramework)16 HashSet (java.util.HashSet)14 Map (java.util.Map)13 FabricException (io.fabric8.api.FabricException)12 IOException (java.io.IOException)12 ContainerProxy (io.fabric8.itests.paxexam.support.ContainerProxy)11 Ignore (org.junit.Ignore)10 File (java.io.File)9 LinkedList (java.util.LinkedList)9 Path (javax.ws.rs.Path)8 FabricRequirements (io.fabric8.api.FabricRequirements)7