Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit ac0baf8

Browse files
committed
Refactored some logic to make it easier to maintain
1 parent ce9b976 commit ac0baf8

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

src/main/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorUtils.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private static void createAppForCit(CertificateIssuingTemplate cit, String appNa
150150
Application application = new Application();
151151

152152
// Obtaining the owners from the user list
153-
List<Application.OwnerIdsAndType> ownersList = CloudConnectorUtils.resolveOwners(usersList, apiKey, cloud);
153+
List<Application.OwnerIdsAndType> ownersList = CloudConnectorUtils.resolveUsersToCloudOwners(usersList, apiKey, cloud);
154154

155155
Map<String, String> citAliasIdMap = new HashMap<>();
156156
citAliasIdMap.put(cit.name(), cit.id());
@@ -189,24 +189,21 @@ private static void addCitToApp(CertificateIssuingTemplate cit, Application appl
189189
application.internalFqDns(null);
190190

191191
// Updating the owners list
192-
List<Application.OwnerIdsAndType> ownersList = CloudConnectorUtils.resolveOwners(usersList, apiKey, cloud);
192+
List<Application.OwnerIdsAndType> ownersList = CloudConnectorUtils.resolveUsersToCloudOwners(usersList, apiKey, cloud);
193193
application.ownerIdsAndTypes(ownersList);
194194

195195
cloud.updateApplication(application, appId, apiKey);
196196
}
197197
}
198198

199-
private static List<Application.OwnerIdsAndType> resolveOwners(String[] usersList, String apiKey, Cloud cloud) {
199+
private static List<Application.OwnerIdsAndType> resolveUsersToCloudOwners(String[] usersList, String apiKey, Cloud cloud) {
200200
List<Application.OwnerIdsAndType> ownersList = new ArrayList<>();
201201

202202
if (usersList == null) {
203203
// When no user is provided on the list, adds the current one as owner
204204
UserDetails userDetails = cloud.authorize(apiKey);
205-
String userId = userDetails.user().id();
206-
Application.OwnerIdsAndType currentOwner = new Application.OwnerIdsAndType();
207-
currentOwner.ownerId(userId);
208-
currentOwner.ownerType(CloudConstants.OWNER_TYPE_USER);
209-
ownersList.add(currentOwner);
205+
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_USER, userDetails.user().id());
206+
ownersList.add(owner);
210207
}
211208
else {
212209
// Resolving the usernames list
@@ -216,20 +213,16 @@ private static List<Application.OwnerIdsAndType> resolveOwners(String[] usersLis
216213
UserResponse response = cloud.retrieveUser(username, apiKey);
217214
// If the name matches a user, create the entry
218215
if (response != null) {
219-
Application.OwnerIdsAndType owner = new Application.OwnerIdsAndType();
220-
owner.ownerId(response.users().get(0).id());
221-
owner.ownerType(CloudConstants.OWNER_TYPE_USER);
216+
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_USER, response.users().get(0).id());
222217
ownersList.add(owner);
223-
}else{
218+
} else {
224219
if (tResponse == null) {
225220
tResponse = cloud.retrieveTeams(apiKey);
226221
}
227222
if (tResponse != null) {
228223
for (Team t : tResponse.teams()) {
229224
if (t.name().equals(username)) {
230-
Application.OwnerIdsAndType owner = new Application.OwnerIdsAndType();
231-
owner.ownerId(t.id());
232-
owner.ownerType(CloudConstants.OWNER_TYPE_TEAM);
225+
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_TEAM, t.id());
233226
ownersList.add(owner);
234227
break;
235228
}
@@ -242,8 +235,27 @@ private static List<Application.OwnerIdsAndType> resolveOwners(String[] usersLis
242235
return ownersList;
243236
}
244237

238+
public static Application.OwnerIdsAndType createOwner(String type, String id) {
239+
Application.OwnerIdsAndType owner = new Application.OwnerIdsAndType();
240+
owner.ownerType(type);
241+
owner.ownerId(id);
242+
return owner;
243+
}
244+
245245
public static CloudPolicy getCloudPolicy( String policyName, String apiKey, Cloud cloud) throws VCertException{
246246
CloudPolicy cloudPolicy = new CloudPolicy();
247+
248+
String[] usersList = resolveCloudOwnersNames(policyName, apiKey, cloud);
249+
cloudPolicy.owners(usersList);
250+
251+
CertificateIssuingTemplate cit = getPolicy(policyName, apiKey, cloud);
252+
cloudPolicy.certificateIssuingTemplate( cit );
253+
cloudPolicy.caInfo(getCAInfo( cit, apiKey, cloud ));
254+
255+
return cloudPolicy;
256+
}
257+
258+
private static String[] resolveCloudOwnersNames(String policyName, String apiKey, Cloud cloud) throws VCertException {
247259
CloudZone zone = new CloudZone(policyName);
248260

249261
Application app = cloud.applicationByName(zone.appName(), apiKey);
@@ -256,7 +268,7 @@ public static CloudPolicy getCloudPolicy( String policyName, String apiKey, Clou
256268
if (owner.ownerType().equals(CloudConstants.OWNER_TYPE_USER)) {
257269
User user = cloud.retrieveUserById(owner.ownerId(), apiKey);
258270
usersList.add(user.username());
259-
}else if (owner.ownerType().equals(CloudConstants.OWNER_TYPE_TEAM)) {
271+
} else if (owner.ownerType().equals(CloudConstants.OWNER_TYPE_TEAM)) {
260272
if (tResponse == null){
261273
// This validation caches the teams list, so we don't have to call
262274
// the teams' endpoint multiple times when iterating owners of type TEAM
@@ -272,14 +284,9 @@ public static CloudPolicy getCloudPolicy( String policyName, String apiKey, Clou
272284
}
273285
}
274286
}
275-
cloudPolicy.owners(usersList.toArray(new String[0]));
276287

277-
CertificateIssuingTemplate cit = getPolicy(policyName, apiKey, cloud);
278-
cloudPolicy.certificateIssuingTemplate( cit );
279-
cloudPolicy.caInfo(getCAInfo( cit, apiKey, cloud ));
280-
281-
return cloudPolicy;
282-
}
288+
return usersList.toArray(new String[0]);
289+
}
283290

284291
private static CertificateIssuingTemplate getPolicy(String policyName, String apiKey, Cloud cloud) throws VCertException {
285292

0 commit comments

Comments
 (0)