aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-openstack-adapters/src/main/java/org/onap/so/heatbridge/openstack/factory/OpenstackClientFactoryImpl.java
blob: 25b3f1dbebd62192620380c5ba9f1e5c18c71c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*-
 * Copyright (C) 2018 Bell Canada. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.so.heatbridge.openstack.factory;

import com.google.common.base.Preconditions;
import org.onap.so.heatbridge.openstack.api.OpenstackAccess;
import org.onap.so.heatbridge.openstack.api.OpenstackClient;
import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
import org.onap.so.heatbridge.openstack.api.OpenstackV2ClientImpl;
import org.onap.so.heatbridge.openstack.api.OpenstackV3ClientImpl;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.api.OSClient.OSClientV3;
import org.openstack4j.api.exceptions.AuthenticationException;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.model.common.Identifier;

public class OpenstackClientFactoryImpl implements OpenstackClientFactory {

    @Override
    public OpenstackClient createOpenstackV3Client(OpenstackAccess osAccess) throws OpenstackClientException {
        Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v3 Auth: endpoint not set.");
        Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v3 Auth: username not set.");
        Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v3 Auth: password not set.");
        Preconditions.checkNotNull(osAccess.getDomainNameIdentifier(), "Keystone-v3 Auth: domain not set.");
        Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v3 Auth: region not set.");
        Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v3 Auth: tenant-id not set.");

        OSClientV3 client;
        try {
            client = OSFactory.builderV3().endpoint(osAccess.getUrl())
                    .credentials(osAccess.getUser(), osAccess.getPassword(), osAccess.getDomainNameIdentifier())
                    .scopeToProject(Identifier.byId(osAccess.getTenantId())).authenticate()
                    .useRegion(osAccess.getRegion());
            return new OpenstackV3ClientImpl(client);
        } catch (AuthenticationException exception) {
            throw new OpenstackClientException("Failed to authenticate with Keystone-v3: " + osAccess.getUrl(),
                    exception);
        }
    }

    @Override
    public OpenstackClient createOpenstackV2Client(OpenstackAccess osAccess) throws OpenstackClientException {
        Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v2 Auth: endpoint not set.");
        Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v2 Auth: username not set.");
        Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v2 Auth: password not set.");
        Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v2 Auth: tenant-id not set.");
        Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v2 Auth: region not set.");

        OSClientV2 client;
        try {
            client = OSFactory.builderV2().endpoint(osAccess.getUrl())
                    .credentials(osAccess.getUser(), osAccess.getPassword()).tenantId(osAccess.getTenantId())
                    .authenticate().useRegion(osAccess.getRegion());
            return new OpenstackV2ClientImpl(client);
        } catch (AuthenticationException exception) {
            throw new OpenstackClientException("Failed to authenticate with Keystone-v2.0: " + osAccess.getUrl(),
                    exception);
        }
    }
}