All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aeontronix.enhancedmule.tools.role.RoleGroup Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha4
Show newest version
/*
 * Copyright (c) Aeontronix 2023
 */

package com.aeontronix.enhancedmule.tools.role;

import com.aeontronix.commons.URLBuilder;
import com.aeontronix.commons.exception.UnexpectedException;
import com.aeontronix.enhancedmule.tools.anypoint.LegacyAnypointClient;
import com.aeontronix.enhancedmule.tools.anypoint.AnypointObject;
import com.aeontronix.enhancedmule.tools.anypoint.NotFoundException;
import com.aeontronix.enhancedmule.tools.anypoint.Organization;
import com.aeontronix.enhancedmule.tools.anypoint.provisioning.RoleDescriptor;
import com.aeontronix.enhancedmule.tools.util.HttpException;
import com.aeontronix.enhancedmule.tools.util.HttpHelper;
import com.aeontronix.enhancedmule.tools.util.JsonHelper;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.*;

public class RoleGroup extends AnypointObject {
    @JsonProperty("role_group_id")
    private String id;
    @JsonProperty
    private String name;
    @JsonProperty
    private String description;
    @JsonProperty
    private boolean editable;
    @JsonProperty("external_names")
    private Set externalNames;

    public RoleGroup(LegacyAnypointClient client) {
        super(client);
    }

    public RoleGroup(Organization parent) {
        super(parent);
    }

    public RoleGroup() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public Set getExternalNames() {
        return externalNames;
    }

    public void setExternalNames(Set externalNames) {
        this.externalNames = externalNames;
    }

    public RoleGroup update() throws HttpException {
        HashMap changes = new HashMap<>();
        if (editable) {
            changes.put("name", name);
            changes.put("description", description);
        }
        changes.put("external_names", externalNames);
        String json = httpHelper.httpPut(buildUrlStr(parent, id), changes);
        return jsonHelper.readJson(new RoleGroup(), json, parent);
    }

    public static RoleGroup findById(Organization organization, HttpHelper httpHelper, JsonHelper jsonHelper, String id) throws NotFoundException, HttpException {
        try {
            String buildUrl = buildUrlStr(organization, id);
            String json = httpHelper.httpGet(buildUrl);
            return jsonHelper.readJson(new RoleGroup(), json, organization);
        } catch (HttpException e) {
            if (e.getStatusCode() == 404) {
                throw new NotFoundException("Role with id " + id + " not found in org " + organization.getId());
            } else {
                throw e;
            }
        }
    }

    public RoleAssignmentList findRoleAssignments() throws HttpException, NotFoundException {
        try {
            return new RoleAssignmentList(this);
        } catch (HttpException e) {
            if (e.getStatusCode() == 404) {
                throw new NotFoundException(e.getMessage());
            } else {
                throw e;
            }
        }
    }

    public List assignRoles(Iterable roleAssignmentAdditions) throws HttpException {
        String json = httpHelper.httpPost(buildUrl(parent, id).path("roles").toString(), roleAssignmentAdditions);
        return jsonHelper.readJsonList(RoleAssignment.class, json, null);
    }

    public void deleteAllRoleAssignment() throws HttpException {
        try {
            deleteRoleAssignment(findRoleAssignments());
        } catch (NotFoundException e) {
            throw new UnexpectedException(e);
        }
    }

    public void deleteRoleAssignment(Iterable roleAssignments) throws HttpException {
        for (RoleAssignment roleAssignment : roleAssignments) {
            httpHelper.httpDelete(buildUrl(parent, getId()).path("roles").queryParam("roleId", roleAssignment.getId()).toString(),
                    Collections.singletonList(roleAssignment));
        }
    }

    private static String buildUrlStr(Organization organization, String id) {
        return buildUrl(organization, id).toString();
    }

    private static URLBuilder buildUrl(Organization organization, String id) {
        return new URLBuilder("/accounts/api/organizations/").path(organization.getId()).path("rolegroups").path(id);
    }

    public boolean same(RoleDescriptor roleDescriptor) {
        return Objects.equals(roleDescriptor.getDescription(),description) &&
                Objects.equals(roleDescriptor.getExternalNames(),externalNames);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy