Search in sources :

Example 1 with MsoTenant

use of org.onap.so.openstack.beans.MsoTenant in project so by onap.

the class MsoKeystoneUtils method queryTenant.

/**
 * Query for a tenant by ID in the given cloud. If the tenant exists, return an MsoTenant object. If not, return
 * null.
 * <p>
 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin requests go to the centralized
 * identity service in DCP. However, if some artifact must exist in each local LCP instance as well, then it will be
 * needed to access the correct region.
 * <p>
 *
 * @param tenantId The Openstack ID of the tenant to query
 * @param cloudSiteId The cloud identifier (may be a region) in which to query the tenant.
 * @return the tenant properties of the queried tenant, or null if not found
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 */
public MsoTenant queryTenant(String tenantId, String cloudSiteId) throws MsoException {
    // Obtain the cloud site information where we will query the tenant
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSite);
    // Check if the tenant exists and return its Tenant Id
    try {
        Tenant tenant = findTenantById(keystoneAdminClient, tenantId);
        if (tenant == null) {
            return null;
        }
        Map<String, String> metadata = new HashMap<>();
        if (cloudSite.getIdentityService().getTenantMetadata()) {
            OpenStackRequest<Metadata> request = keystoneAdminClient.tenants().showMetadata(tenant.getId());
            Metadata tenantMetadata = executeAndRecordOpenstackRequest(request);
            if (tenantMetadata != null) {
                metadata = tenantMetadata.getMetadata();
            }
        }
        return new MsoTenant(tenant.getId(), tenant.getName(), metadata);
    } catch (OpenStackBaseException e) {
        // Convert Keystone OpenStackResponseException to MsoOpenstackException
        throw keystoneErrorToMsoException(e, "QueryTenant");
    } catch (RuntimeException e) {
        // Catch-all
        throw runtimeExceptionToMsoException(e, "QueryTenant");
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoTenant(org.onap.so.openstack.beans.MsoTenant) Tenant(com.woorea.openstack.keystone.model.Tenant) Keystone(com.woorea.openstack.keystone.Keystone) HashMap(java.util.HashMap) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Metadata(com.woorea.openstack.keystone.model.Metadata) MsoTenant(org.onap.so.openstack.beans.MsoTenant)

Example 2 with MsoTenant

use of org.onap.so.openstack.beans.MsoTenant in project so by onap.

the class MsoKeystoneUtils method queryTenantByName.

/**
 * Query for a tenant with the specified name in the given cloud. If the tenant exists, return an MsoTenant object.
 * If not, return null. This query is useful if the client knows it has the tenant name, skipping an initial lookup
 * by ID that would always fail.
 * <p>
 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin requests go to the centralized
 * identity service in DCP. However, if some artifact must exist in each local LCP instance as well, then it will be
 * needed to access the correct region.
 * <p>
 *
 * @param tenantName The name of the tenant to query
 * @param cloudSiteId The cloud identifier (may be a region) in which to query the tenant.
 * @return the tenant properties of the queried tenant, or null if not found
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 */
public MsoTenant queryTenantByName(String tenantName, String cloudSiteId) throws MsoException {
    // Obtain the cloud site information where we will query the tenant
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSite);
    try {
        Tenant tenant = findTenantByName(keystoneAdminClient, tenantName);
        if (tenant == null) {
            return null;
        }
        Map<String, String> metadata = new HashMap<>();
        if (cloudSite.getIdentityService().getTenantMetadata()) {
            OpenStackRequest<Metadata> request = keystoneAdminClient.tenants().showMetadata(tenant.getId());
            Metadata tenantMetadata = executeAndRecordOpenstackRequest(request);
            if (tenantMetadata != null) {
                metadata = tenantMetadata.getMetadata();
            }
        }
        return new MsoTenant(tenant.getId(), tenant.getName(), metadata);
    } catch (OpenStackBaseException e) {
        // Convert Keystone OpenStackResponseException to MsoOpenstackException
        throw keystoneErrorToMsoException(e, "QueryTenantName");
    } catch (RuntimeException e) {
        // Catch-all
        throw runtimeExceptionToMsoException(e, "QueryTenantName");
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoTenant(org.onap.so.openstack.beans.MsoTenant) Tenant(com.woorea.openstack.keystone.model.Tenant) Keystone(com.woorea.openstack.keystone.Keystone) HashMap(java.util.HashMap) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Metadata(com.woorea.openstack.keystone.model.Metadata) MsoTenant(org.onap.so.openstack.beans.MsoTenant)

Example 3 with MsoTenant

use of org.onap.so.openstack.beans.MsoTenant in project so by onap.

the class MsoKeystoneUtilsTest method queryTenantByNameTest.

@Test
public void queryTenantByNameTest() throws Exception {
    StubOpenStack.mockOpenStackGetTenantByName(wireMockServer, "tenant");
    MsoTenant msoTenant = msoKeystoneUtils.queryTenantByName("tenant", "MTN13");
    Assert.assertEquals("testingTenantName", msoTenant.getTenantName());
}
Also used : MsoTenant(org.onap.so.openstack.beans.MsoTenant) Test(org.junit.Test) BaseTest(org.onap.so.BaseTest)

Example 4 with MsoTenant

use of org.onap.so.openstack.beans.MsoTenant in project so by onap.

the class MsoKeystoneUtilsTest method queryTenantTest.

@Test
public void queryTenantTest() throws Exception {
    StubOpenStack.mockOpenStackGetTenantById(wireMockServer, "tenantId");
    MsoTenant msoTenant = msoKeystoneUtils.queryTenant("tenantId", "MTN13");
    Assert.assertEquals("testingTenantName", msoTenant.getTenantName());
}
Also used : MsoTenant(org.onap.so.openstack.beans.MsoTenant) Test(org.junit.Test) BaseTest(org.onap.so.BaseTest)

Aggregations

MsoTenant (org.onap.so.openstack.beans.MsoTenant)4 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)2 Keystone (com.woorea.openstack.keystone.Keystone)2 Metadata (com.woorea.openstack.keystone.model.Metadata)2 Tenant (com.woorea.openstack.keystone.model.Tenant)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 BaseTest (org.onap.so.BaseTest)2 CloudSite (org.onap.so.db.catalog.beans.CloudSite)2 MsoCloudSiteNotFound (org.onap.so.openstack.exceptions.MsoCloudSiteNotFound)2