Search in sources :

Example 1 with IAMGroupResponse

use of org.apache.cloudstack.api.response.iam.IAMGroupResponse in project cloudstack by apache.

the class IAMApiServiceImpl method createIAMGroupResponse.

@Override
public IAMGroupResponse createIAMGroupResponse(IAMGroup group) {
    IAMGroupResponse response = new IAMGroupResponse();
    response.setId(group.getUuid());
    response.setName(group.getName());
    response.setDescription(group.getDescription());
    String domainPath = group.getPath();
    if (domainPath != null) {
        DomainVO domain = _domainDao.findDomainByPath(domainPath);
        if (domain != null) {
            response.setDomainId(domain.getUuid());
            response.setDomainName(domain.getName());
        }
    }
    long accountId = group.getAccountId();
    AccountVO owner = _accountDao.findById(accountId);
    if (owner != null) {
        response.setAccountName(owner.getAccountName());
    }
    // find all the members in this group
    List<Long> members = _iamSrv.listAccountsByGroup(group.getId());
    if (members != null && members.size() > 0) {
        for (Long member : members) {
            AccountVO mem = _accountDao.findById(member);
            if (mem != null) {
                response.addMemberAccount(mem.getAccountName());
            }
        }
    }
    // find all the policies attached to this group
    List<IAMPolicy> policies = _iamSrv.listIAMPoliciesByGroup(group.getId());
    if (policies != null && policies.size() > 0) {
        for (IAMPolicy policy : policies) {
            response.addPolicy(policy.getName());
        }
    }
    response.setObjectName("aclgroup");
    return response;
}
Also used : DomainVO(com.cloud.domain.DomainVO) IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) IAMGroupResponse(org.apache.cloudstack.api.response.iam.IAMGroupResponse) AccountVO(com.cloud.user.AccountVO)

Example 2 with IAMGroupResponse

use of org.apache.cloudstack.api.response.iam.IAMGroupResponse in project cloudstack by apache.

the class IAMApiServiceImpl method listIAMGroups.

@Override
public ListResponse<IAMGroupResponse> listIAMGroups(Long iamGroupId, String iamGroupName, Long domainId, Long startIndex, Long pageSize) {
    // acl check
    Account caller = CallContext.current().getCallingAccount();
    Domain domain = null;
    if (domainId != null) {
        domain = _domainDao.findById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
        }
        _accountMgr.checkAccess(caller, domain);
    } else {
        domain = _domainDao.findById(caller.getDomainId());
    }
    String domainPath = domain.getPath();
    // search for groups
    Pair<List<IAMGroup>, Integer> result = _iamSrv.listIAMGroups(iamGroupId, iamGroupName, domainPath, startIndex, pageSize);
    // generate group response
    ListResponse<IAMGroupResponse> response = new ListResponse<IAMGroupResponse>();
    List<IAMGroupResponse> groupResponses = new ArrayList<IAMGroupResponse>();
    for (IAMGroup group : result.first()) {
        IAMGroupResponse resp = createIAMGroupResponse(group);
        groupResponses.add(resp);
    }
    response.setResponses(groupResponses, result.second());
    return response;
}
Also used : Account(com.cloud.user.Account) ListResponse(org.apache.cloudstack.api.response.ListResponse) IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) IAMGroupResponse(org.apache.cloudstack.api.response.iam.IAMGroupResponse) List(java.util.List) ArrayList(java.util.ArrayList) Domain(com.cloud.domain.Domain)

Example 3 with IAMGroupResponse

use of org.apache.cloudstack.api.response.iam.IAMGroupResponse in project cloudstack by apache.

the class IAMApiServiceTest method addRemovePolicyToGroupTest.

@Test
public void addRemovePolicyToGroupTest() {
    IAMGroup group = new IAMGroupVO("group1", "tester group1");
    List<IAMGroup> groups = new ArrayList<IAMGroup>();
    groups.add(group);
    Long groupId = group.getId();
    List<Long> policyIds = new ArrayList<Long>();
    policyIds.add(100L);
    policyIds.add(200L);
    IAMPolicy policy1 = new IAMPolicyVO("policy1", "my first policy");
    IAMPolicy policy2 = new IAMPolicyVO("policy2", "my second policy");
    List<IAMPolicy> policies = new ArrayList<IAMPolicy>();
    policies.add(policy1);
    policies.add(policy2);
    when(_iamSrv.attachIAMPoliciesToGroup(policyIds, groupId)).thenReturn(group);
    when(_iamSrv.listIAMPoliciesByGroup(groupId)).thenReturn(policies);
    Pair<List<IAMGroup>, Integer> grpList = new Pair<List<IAMGroup>, Integer>(groups, 1);
    when(_iamSrv.listIAMGroups(null, "group1", callerDomainPath, 0L, 20L)).thenReturn(grpList);
    _aclSrv.attachIAMPoliciesToGroup(policyIds, groupId);
    ListResponse<IAMGroupResponse> grpResp = _aclSrv.listIAMGroups(null, "group1", callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", grpResp.getCount() == 1);
    IAMGroupResponse resp = grpResp.getResponses().get(0);
    Set<String> policyNames = resp.getPolicyList();
    assertEquals("There should be 2 policies in the group", 2, policyNames.size());
    assertTrue("policy1 should be assigned to the group", policyNames.contains("policy1"));
    assertTrue("policy2 should be assigned to the group", policyNames.contains("policy2"));
    // remove "policy2" from group1
    policyIds.remove(1);
    policies.remove(policy2);
    when(_iamSrv.removeIAMPoliciesFromGroup(policyIds, groupId)).thenReturn(group);
    _aclSrv.removeIAMPoliciesFromGroup(policyIds, groupId);
    grpResp = _aclSrv.listIAMGroups(null, "group1", callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", grpResp.getCount() == 1);
    resp = grpResp.getResponses().get(0);
    policyNames = resp.getPolicyList();
    assertEquals("There should be 1 policy attached to the group", 1, policyNames.size());
    assertFalse("policy2 should not belong to the group anymore", policyNames.contains("policy2"));
}
Also used : IAMGroupVO(org.apache.cloudstack.iam.server.IAMGroupVO) IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) IAMPolicyVO(org.apache.cloudstack.iam.server.IAMPolicyVO) ArrayList(java.util.ArrayList) IAMGroupResponse(org.apache.cloudstack.api.response.iam.IAMGroupResponse) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair) Test(org.junit.Test)

Example 4 with IAMGroupResponse

use of org.apache.cloudstack.api.response.iam.IAMGroupResponse in project cloudstack by apache.

the class RemoveAccountFromIAMGroupCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException {
    CallContext.current().setEventDetails("IAM group Id: " + getId());
    IAMGroup result = _iamApiSrv.removeAccountsFromGroup(accountIdList, id);
    if (result != null) {
        IAMGroupResponse response = _iamApiSrv.createIAMGroupResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove accounts from iam group");
    }
}
Also used : IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) ServerApiException(org.apache.cloudstack.api.ServerApiException) IAMGroupResponse(org.apache.cloudstack.api.response.iam.IAMGroupResponse)

Example 5 with IAMGroupResponse

use of org.apache.cloudstack.api.response.iam.IAMGroupResponse in project cloudstack by apache.

the class AddAccountToIAMGroupCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException {
    CallContext.current().setEventDetails("IAM group Id: " + getId());
    IAMGroup result = _iamApiSrv.addAccountsToGroup(accountIdList, id);
    if (result != null) {
        IAMGroupResponse response = _iamApiSrv.createIAMGroupResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add accounts to iam group");
    }
}
Also used : IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) ServerApiException(org.apache.cloudstack.api.ServerApiException) IAMGroupResponse(org.apache.cloudstack.api.response.iam.IAMGroupResponse)

Aggregations

IAMGroupResponse (org.apache.cloudstack.api.response.iam.IAMGroupResponse)10 IAMGroup (org.apache.cloudstack.iam.api.IAMGroup)9 ServerApiException (org.apache.cloudstack.api.ServerApiException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Pair (com.cloud.utils.Pair)3 IAMGroupVO (org.apache.cloudstack.iam.server.IAMGroupVO)3 Test (org.junit.Test)3 AccountVO (com.cloud.user.AccountVO)2 IAMPolicy (org.apache.cloudstack.iam.api.IAMPolicy)2 Domain (com.cloud.domain.Domain)1 DomainVO (com.cloud.domain.DomainVO)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 Account (com.cloud.user.Account)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1 IAMPolicyVO (org.apache.cloudstack.iam.server.IAMPolicyVO)1