141dd4fe509dd4d2f9514dc84638aeeb008335a6
[so.git] /
1 /*-
2  * Copyright (C) 2018 Bell Canada. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.so.heatbridge.openstack.factory;
17
18 import com.google.common.base.Preconditions;
19 import org.onap.so.heatbridge.openstack.api.OpenstackAccess;
20 import org.onap.so.heatbridge.openstack.api.OpenstackClient;
21 import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
22 import org.onap.so.heatbridge.openstack.api.OpenstackV2ClientImpl;
23 import org.onap.so.heatbridge.openstack.api.OpenstackV3ClientImpl;
24 import org.openstack4j.api.OSClient.OSClientV2;
25 import org.openstack4j.api.OSClient.OSClientV3;
26 import org.openstack4j.api.exceptions.AuthenticationException;
27 import org.openstack4j.openstack.OSFactory;
28 import org.openstack4j.model.common.Identifier;
29
30 public class OpenstackClientFactoryImpl implements OpenstackClientFactory {
31
32     @Override
33     public OpenstackClient createOpenstackV3Client(OpenstackAccess osAccess) throws OpenstackClientException {
34         Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v3 Auth: endpoint not set.");
35         Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v3 Auth: username not set.");
36         Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v3 Auth: password not set.");
37         Preconditions.checkNotNull(osAccess.getDomainNameIdentifier(), "Keystone-v3 Auth: domain not set.");
38         Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v3 Auth: region not set.");
39         Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v3 Auth: tenant-id not set.");
40
41         OSClientV3 client;
42         try {
43             client = OSFactory.builderV3()
44                 .endpoint(osAccess.getUrl())
45                 .credentials(osAccess.getUser(), osAccess.getPassword(), osAccess.getDomainNameIdentifier())
46                 .scopeToProject(Identifier.byId(osAccess.getTenantId()))
47                 .authenticate()
48                 .useRegion(osAccess.getRegion());
49             return new OpenstackV3ClientImpl(client);
50         } catch (AuthenticationException exception) {
51             throw new OpenstackClientException("Failed to authenticate with Keystone-v3: " + osAccess.getUrl(), exception);
52         }
53     }
54
55     @Override
56     public OpenstackClient createOpenstackV2Client(OpenstackAccess osAccess) throws OpenstackClientException {
57         Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v2 Auth: endpoint not set.");
58         Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v2 Auth: username not set.");
59         Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v2 Auth: password not set.");
60         Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v2 Auth: tenant-id not set.");
61         Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v2 Auth: region not set.");
62
63         OSClientV2 client;
64         try {
65             client = OSFactory.builderV2()
66                 .endpoint(osAccess.getUrl())
67                 .credentials(osAccess.getUser(), osAccess.getPassword())
68                 .tenantId(osAccess.getTenantId())
69                 .authenticate()
70                 .useRegion(osAccess.getRegion());
71             return new OpenstackV2ClientImpl(client);
72         } catch (AuthenticationException exception) {
73             throw new OpenstackClientException("Failed to authenticate with Keystone-v2.0: " + osAccess.getUrl(),
74                 exception);
75         }
76     }
77 }