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

Commit 9ef9fb4

Browse files
authored
ReadZoneConfiguration() refactoring (#26)
* Updated the way readZoneConfiguration method works for CloudConnector. Instead of getting all the projects and iterating them, now it directly calls an api method to get the zone filtered by Id. This update only works when the zone id is passed as argument. When the project_name\zone_name is passed as argument, the method still retrieves all projects and iterates over them to match the project name and zone name.
1 parent 40ba06a commit 9ef9fb4

4 files changed

Lines changed: 59 additions & 28 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.venafi.vcert.sdk.connectors.cloud.domain.Project;
2121
import com.venafi.vcert.sdk.connectors.cloud.domain.ProjectZone;
2222
import com.venafi.vcert.sdk.connectors.cloud.domain.Projects;
23+
import com.venafi.vcert.sdk.connectors.cloud.domain.TagProjectZone;
2324
import com.venafi.vcert.sdk.connectors.cloud.domain.UserDetails;
2425
import com.venafi.vcert.sdk.utils.FeignUtils;
2526

@@ -44,6 +45,10 @@ public interface Cloud {
4445
@RequestLine("GET /devopsprojects/{projectId}?zoneDetails=true")
4546
Project projectById(@Param("projectId") String projectId, @Param("apiKey") String apiKey);
4647

48+
@Headers("tppl-api-key: {apiKey}")
49+
@RequestLine("GET /zones/tag/{tag}")
50+
TagProjectZone zoneByTag(@Param("tag") String tag, @Param("apiKey") String apiKey);
51+
4752
@Headers("tppl-api-key: {apiKey}")
4853
@RequestLine("GET /certificateissuingtemplates/{certificateIssuingTemplateId}")
4954
CertificateIssuingTemplate certificateIssuingTemplateById(

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
import com.venafi.vcert.sdk.connectors.Connector;
3737
import com.venafi.vcert.sdk.connectors.Policy;
3838
import com.venafi.vcert.sdk.connectors.ZoneConfiguration;
39+
import com.venafi.vcert.sdk.connectors.cloud.domain.CertificateIssuingTemplate;
3940
import com.venafi.vcert.sdk.connectors.cloud.domain.Project;
4041
import com.venafi.vcert.sdk.connectors.cloud.domain.ProjectZone;
4142
import com.venafi.vcert.sdk.connectors.cloud.domain.Projects;
43+
import com.venafi.vcert.sdk.connectors.cloud.domain.TagProjectZone;
4244
import com.venafi.vcert.sdk.connectors.cloud.domain.UserDetails;
4345
import com.venafi.vcert.sdk.endpoint.Authentication;
4446
import com.venafi.vcert.sdk.endpoint.ConnectorType;
@@ -105,32 +107,34 @@ public void authenticate(Authentication auth) throws VCertException {
105107

106108
@Override
107109
public ZoneConfiguration readZoneConfiguration(String zone) throws VCertException {
108-
ProjectZone projectZone = null;
109110
String[] zoneIdentifiers = parseZoneIdentifiers(zone);
110-
111-
Projects projects = cloud.projects(auth.apiKey());
112-
if (projects.projects().isEmpty()) {
113-
throw new VCertException("No projects present.");
114-
}
111+
CertificateIssuingTemplate cit = null;
112+
String zoneId = null;
115113

116114
if (zoneIdentifiers[0] != null) {
117-
// Find zone by ID
118-
String zoneId = zoneIdentifiers[0];
119-
120-
for (Project project : projects.projects()) {
121-
for (ProjectZone projZone : project.zones()) {
122-
if (zoneId.equals(projZone.id())) {
123-
projectZone = projZone;
124-
break;
125-
}
126-
}
115+
// Find zone by tag
116+
String zoneTag = zoneIdentifiers[0];
117+
TagProjectZone tpz = cloud.zoneByTag(zone, auth.apiKey());
118+
if (tpz == null) {
119+
throw new VCertException(format("No zone with Id '%s'.", zoneTag));
127120
}
128121

129-
if (projectZone == null) {
130-
throw new VCertException(format("No zone with ID '%s'.", zoneId));
122+
zoneId = tpz.id();
123+
cit = cloud.certificateIssuingTemplateById(tpz.certificateIssuingTemplateId(), auth.apiKey());
124+
125+
if (cit == null){
126+
throw new VCertException(format("Certificate issue template not found. Id provided = [%s] ",
127+
tpz.certificateIssuingTemplateId()));
131128
}
129+
132130
} else {
133131
// Find zone by project name and zone name
132+
ProjectZone projectZone = null;
133+
Projects projects = cloud.projects(auth.apiKey());
134+
if (projects.projects().isEmpty()) {
135+
throw new VCertException("No projects present.");
136+
}
137+
134138
String projectName = zoneIdentifiers[1];
135139
String zoneName = zoneIdentifiers[2];
136140

@@ -149,15 +153,18 @@ public ZoneConfiguration readZoneConfiguration(String zone) throws VCertExceptio
149153
throw new VCertException(
150154
format("No zone with name '%s' in '%s' project.", zoneName, projectName));
151155
}
152-
}
153156

154-
if (projectZone.cit() == null) {
155-
throw new VCertException(format("No certificate issuing template ID for '%s' zone.", zone));
157+
zoneId = projectZone.id();
158+
cit = projectZone.cit();
159+
160+
if (cit == null) {
161+
throw new VCertException(format("No certificate issuing template ID for '%s' zone.", zone));
162+
}
156163
}
157164

158-
ZoneConfiguration zoneConfig = projectZone.cit().toZoneConfig();
159-
zoneConfig.policy(projectZone.cit().toPolicy());
160-
zoneConfig.zoneId(projectZone.id());
165+
ZoneConfiguration zoneConfig = cit.toZoneConfig();
166+
zoneConfig.policy(cit.toPolicy());
167+
zoneConfig.zoneId(zoneId);
161168

162169
return zoneConfig;
163170
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.venafi.vcert.sdk.connectors.cloud.domain;
2+
3+
import java.time.OffsetDateTime;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@Data
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class TagProjectZone {
13+
private String id;
14+
private String companyId;
15+
private String devopsProjectId;
16+
private String name;
17+
private String certificateIssuingTemplateId;
18+
private OffsetDateTime creationDate;
19+
}

src/test/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorAT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void generateRequest() throws VCertException, IOException {
8585
}
8686

8787
@Test
88-
void requestCertificate() throws VCertException, SocketException, UnknownHostException {
88+
void requestCertificate() throws VCertException, UnknownHostException {
8989
String zoneName = System.getenv("CLOUDZONE");
9090
ZoneConfiguration zoneConfiguration = classUnderTest.readZoneConfiguration(zoneName);
9191
CertificateRequest certificateRequest = new CertificateRequest()
@@ -102,7 +102,7 @@ void requestCertificate() throws VCertException, SocketException, UnknownHostExc
102102
}
103103

104104
@Test
105-
void retrieveCertificate() throws VCertException, SocketException, UnknownHostException {
105+
void retrieveCertificate() throws VCertException, UnknownHostException {
106106
String zoneName = System.getenv("CLOUDZONE");
107107
ZoneConfiguration zoneConfiguration = classUnderTest.readZoneConfiguration(zoneName);
108108
CertificateRequest certificateRequest = new CertificateRequest()
@@ -134,8 +134,8 @@ void revokeCertificate() throws VCertException {
134134
}
135135

136136
@Test
137-
void renewCertificate() throws VCertException, UnknownHostException, SocketException,
138-
CertificateException, NoSuchAlgorithmException {
137+
void renewCertificate() throws VCertException, UnknownHostException,
138+
CertificateException {
139139
String zoneName = System.getenv("CLOUDZONE");
140140
String commonName = TestUtils.randomCN();
141141
ZoneConfiguration zoneConfiguration = classUnderTest.readZoneConfiguration(zoneName);

0 commit comments

Comments
 (0)