Search in sources :

Example 1 with IAMGroup

use of org.apache.cloudstack.iam.api.IAMGroup 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 2 with IAMGroup

use of org.apache.cloudstack.iam.api.IAMGroup 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 3 with IAMGroup

use of org.apache.cloudstack.iam.api.IAMGroup in project cloudstack by apache.

the class IAMServiceImpl method listIAMGroups.

@Override
public Pair<List<IAMGroup>, Integer> listIAMGroups(Long iamGroupId, String iamGroupName, String path, Long startIndex, Long pageSize) {
    if (iamGroupId != null) {
        IAMGroup group = _aclGroupDao.findById(iamGroupId);
        if (group == null) {
            throw new InvalidParameterValueException("Unable to find acl group by id " + iamGroupId);
        }
    }
    Filter searchFilter = new Filter(IAMGroupVO.class, "id", true, startIndex, pageSize);
    SearchBuilder<IAMGroupVO> sb = _aclGroupDao.createSearchBuilder();
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("path", sb.entity().getPath(), SearchCriteria.Op.LIKE);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    SearchCriteria<IAMGroupVO> sc = sb.create();
    if (iamGroupName != null) {
        sc.setParameters("name", iamGroupName);
    }
    if (iamGroupId != null) {
        sc.setParameters("id", iamGroupId);
    }
    sc.setParameters("path", path + "%");
    Pair<List<IAMGroupVO>, Integer> groups = _aclGroupDao.searchAndCount(sc, searchFilter);
    return new Pair<List<IAMGroup>, Integer>(new ArrayList<IAMGroup>(groups.first()), groups.second());
}
Also used : IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

Example 4 with IAMGroup

use of org.apache.cloudstack.iam.api.IAMGroup in project cloudstack by apache.

the class IAMServiceUnitTest method createAclGroupTest.

@Test(expected = InvalidParameterValueException.class)
public void createAclGroupTest() {
    IAMGroup group = _iamService.createIAMGroup("group1", "my first group", "/root/mydomain");
    assertNotNull("Acl group 'group1' failed to create ", group);
    IAMGroupVO group2 = new IAMGroupVO("group1", "my second group");
    when(_aclGroupDao.findByName(eq("/root/mydomain"), eq("group1"))).thenReturn(group2);
    IAMGroup group3 = _iamService.createIAMGroup("group1", "my first group", "/root/mydomain");
}
Also used : IAMGroupVO(org.apache.cloudstack.iam.server.IAMGroupVO) IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) Test(org.junit.Test)

Example 5 with IAMGroup

use of org.apache.cloudstack.iam.api.IAMGroup in project cloudstack by apache.

the class IAMServiceImpl method deleteIAMGroup.

@DB
@Override
public boolean deleteIAMGroup(final Long iamGroupId) {
    // get the Acl Group entity
    final IAMGroup grp = _aclGroupDao.findById(iamGroupId);
    if (grp == null) {
        throw new InvalidParameterValueException("Unable to find acl group: " + iamGroupId + "; failed to delete acl group.");
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // remove this group related entry in acl_group_policy_map
            List<IAMGroupPolicyMapVO> groupPolicyMap = _aclGroupPolicyMapDao.listByGroupId(grp.getId());
            if (groupPolicyMap != null) {
                for (IAMGroupPolicyMapVO gr : groupPolicyMap) {
                    _aclGroupPolicyMapDao.remove(gr.getId());
                }
            }
            // remove this group related entry in acl_group_account table
            List<IAMGroupAccountMapVO> groupAcctMap = _aclGroupAccountMapDao.listByGroupId(grp.getId());
            if (groupAcctMap != null) {
                for (IAMGroupAccountMapVO grpAcct : groupAcctMap) {
                    _aclGroupAccountMapDao.remove(grpAcct.getId());
                }
            }
            // remove this group from acl_group table
            _aclGroupDao.remove(iamGroupId);
        }
    });
    invalidateIAMCache();
    return true;
}
Also used : IAMGroup(org.apache.cloudstack.iam.api.IAMGroup) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ArrayList(java.util.ArrayList) List(java.util.List) DB(com.cloud.utils.db.DB)

Aggregations

IAMGroup (org.apache.cloudstack.iam.api.IAMGroup)26 ArrayList (java.util.ArrayList)13 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)9 IAMGroupResponse (org.apache.cloudstack.api.response.iam.IAMGroupResponse)9 List (java.util.List)7 DB (com.cloud.utils.db.DB)6 ServerApiException (org.apache.cloudstack.api.ServerApiException)6 IAMPolicy (org.apache.cloudstack.iam.api.IAMPolicy)6 Pair (com.cloud.utils.Pair)5 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)5 TransactionStatus (com.cloud.utils.db.TransactionStatus)5 IAMGroupVO (org.apache.cloudstack.iam.server.IAMGroupVO)5 Test (org.junit.Test)5 Domain (com.cloud.domain.Domain)3 Account (com.cloud.user.Account)2 AccountVO (com.cloud.user.AccountVO)2 SSHKeyPair (com.cloud.user.SSHKeyPair)1 Filter (com.cloud.utils.db.Filter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1