Skip to content

Commit 19af2a7

Browse files
committed
minor code refactoring
1 parent 5f451ba commit 19af2a7

File tree

7 files changed

+25
-43
lines changed

7 files changed

+25
-43
lines changed

api/src/main/java/org/apache/cloudstack/userdata/UserDataManager.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@
2222

2323
import com.cloud.utils.component.Manager;
2424

25+
import java.io.IOException;
26+
2527
public interface UserDataManager extends Manager, Configurable {
2628
String VM_USERDATA_MAX_LENGTH_STRING = "vm.userdata.max.length";
2729
ConfigKey<Integer> VM_USERDATA_MAX_LENGTH = new ConfigKey<>("Advanced", Integer.class, VM_USERDATA_MAX_LENGTH_STRING, "32768",
2830
"Max length of vm userdata after base64 encoding. Default is 32768 and maximum is 1048576", true);
2931

3032
String concatenateUserData(String userdata1, String userdata2, String userdataProvider);
3133
String validateUserData(String userData, BaseCmd.HTTPMethod httpmethod);
32-
String validateAndGetUserDataForSystemVM(String userDataUuid);
34+
35+
/**
36+
* This method validates the user data uuid for system VMs and returns the user data
37+
* after compression and base64 encoding for the system VM to consume.
38+
*
39+
* @param userDataUuid
40+
* @return a String containing the user data after compression and base64 encoding
41+
* @throws IOException
42+
*/
43+
String validateAndGetUserDataForSystemVM(String userDataUuid) throws IOException;
3344
}

engine/userdata/src/main/java/org/apache/cloudstack/userdata/UserDataManagerImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.userdata;
1818

19+
import java.io.IOException;
1920
import java.io.UnsupportedEncodingException;
2021
import java.net.URLDecoder;
2122
import java.util.HashMap;
@@ -26,6 +27,7 @@
2627
import com.cloud.user.User;
2728
import com.cloud.user.UserDataVO;
2829
import com.cloud.user.dao.UserDataDao;
30+
import com.cloud.utils.compression.CompressionUtil;
2931
import org.apache.cloudstack.api.BaseCmd;
3032
import org.apache.cloudstack.framework.config.ConfigKey;
3133
import org.apache.commons.codec.binary.Base64;
@@ -128,7 +130,7 @@ public String validateUserData(String userData, BaseCmd.HTTPMethod httpmethod) {
128130
}
129131

130132
@Override
131-
public String validateAndGetUserDataForSystemVM(String userDataUuid) {
133+
public String validateAndGetUserDataForSystemVM(String userDataUuid) throws IOException {
132134
if (StringUtils.isBlank(userDataUuid)) {
133135
return null;
134136
}
@@ -137,7 +139,11 @@ public String validateAndGetUserDataForSystemVM(String userDataUuid) {
137139
return null;
138140
}
139141
if (userDataVo.getDomainId() == Domain.ROOT_DOMAIN && userDataVo.getAccountId() == User.UID_ADMIN) {
140-
return userDataVo.getUserData();
142+
// Decode base64 user data, compress it, then re-encode to reduce command line length
143+
String plainTextUserData = new String(java.util.Base64.getDecoder().decode(userDataVo.getUserData()));
144+
CompressionUtil compressionUtil = new CompressionUtil();
145+
byte[] compressedUserData = compressionUtil.compressString(plainTextUserData);
146+
return java.util.Base64.getEncoder().encodeToString(compressedUserData);
141147
}
142148
throw new CloudRuntimeException("User data can only be used by system VMs if it belongs to the ROOT domain and ADMIN account.");
143149
}

plugins/network-elements/elastic-loadbalancer/src/main/java/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.cloud.network.lb;
1818

1919
import java.util.ArrayList;
20-
import java.util.Base64;
2120
import java.util.Collections;
2221
import java.util.HashSet;
2322
import java.util.List;
@@ -31,7 +30,6 @@
3130
import javax.inject.Inject;
3231
import javax.naming.ConfigurationException;
3332

34-
import com.cloud.utils.compression.CompressionUtil;
3533
import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
3634
import org.apache.cloudstack.config.ApiServiceConfiguration;
3735
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
@@ -492,12 +490,7 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, Depl
492490
try {
493491
String userData = userDataManager.validateAndGetUserDataForSystemVM(userDataUuid);
494492
if (StringUtils.isNotBlank(userData)) {
495-
// Decode base64 user data, compress it, then re-encode to reduce command line length
496-
String plainTextUserData = new String(Base64.getDecoder().decode(userData));
497-
CompressionUtil compressionUtil = new CompressionUtil();
498-
byte[] compressedUserData = compressionUtil.compressString(plainTextUserData);
499-
String encodedUserData = Base64.getEncoder().encodeToString(compressedUserData);
500-
buf.append(" userdata=").append(encodedUserData);
493+
buf.append(" userdata=").append(userData);
501494
}
502495
} catch (Exception e) {
503496
logger.warn("Failed to load user data for the elastic lb vm, ignored", e);

plugins/network-elements/internal-loadbalancer/src/main/java/org/apache/cloudstack/network/lb/InternalLoadBalancerVMManagerImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import java.util.ArrayList;
2828
import java.util.Arrays;
29-
import java.util.Base64;
3029
import java.util.Iterator;
3130
import java.util.LinkedHashMap;
3231
import java.util.List;
@@ -37,7 +36,6 @@
3736

3837
import com.cloud.event.ActionEvent;
3938
import com.cloud.event.EventTypes;
40-
import com.cloud.utils.compression.CompressionUtil;
4139
import org.apache.cloudstack.context.CallContext;
4240
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
4341
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
@@ -257,12 +255,7 @@ public boolean finalizeVirtualMachineProfile(final VirtualMachineProfile profile
257255
try {
258256
String userData = userDataManager.validateAndGetUserDataForSystemVM(userDataUuid);
259257
if (StringUtils.isNotBlank(userData)) {
260-
// Decode base64 user data, compress it, then re-encode to reduce command line length
261-
String plainTextUserData = new String(Base64.getDecoder().decode(userData));
262-
CompressionUtil compressionUtil = new CompressionUtil();
263-
byte[] compressedUserData = compressionUtil.compressString(plainTextUserData);
264-
String encodedUserData = Base64.getEncoder().encodeToString(compressedUserData);
265-
buf.append(" userdata=").append(encodedUserData);
258+
buf.append(" userdata=").append(userData);
266259
}
267260
} catch (Exception e) {
268261
logger.warn("Failed to load user data for the internal lb vm, ignored", e);

server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.nio.charset.Charset;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22-
import java.util.Base64;
2322
import java.util.Date;
2423
import java.util.HashMap;
2524
import java.util.HashSet;
@@ -33,7 +32,6 @@
3332
import javax.inject.Inject;
3433
import javax.naming.ConfigurationException;
3534

36-
import com.cloud.utils.compression.CompressionUtil;
3735
import org.apache.cloudstack.agent.lb.IndirectAgentLB;
3836
import org.apache.cloudstack.ca.CAManager;
3937
import org.apache.cloudstack.consoleproxy.ConsoleAccessManager;
@@ -1278,12 +1276,7 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, Depl
12781276
try {
12791277
String userData = userDataManager.validateAndGetUserDataForSystemVM(userDataUuid);
12801278
if (StringUtils.isNotBlank(userData)) {
1281-
// Decode base64 user data, compress it, then re-encode to reduce command line length
1282-
String plainTextUserData = new String(Base64.getDecoder().decode(userData));
1283-
CompressionUtil compressionUtil = new CompressionUtil();
1284-
byte[] compressedUserData = compressionUtil.compressString(plainTextUserData);
1285-
String encodedUserData = Base64.getEncoder().encodeToString(compressedUserData);
1286-
buf.append(" userdata=").append(encodedUserData);
1279+
buf.append(" userdata=").append(userData);
12871280
}
12881281
} catch (Exception e) {
12891282
logger.warn("Failed to load user data for the cpvm, ignored", e);

server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.text.ParseException;
2929
import java.text.SimpleDateFormat;
3030
import java.util.ArrayList;
31-
import java.util.Base64;
3231
import java.util.Calendar;
3332
import java.util.Collections;
3433
import java.util.Date;
@@ -49,7 +48,6 @@
4948
import javax.inject.Inject;
5049
import javax.naming.ConfigurationException;
5150

52-
import com.cloud.utils.compression.CompressionUtil;
5351
import com.google.gson.JsonSyntaxException;
5452
import com.google.gson.reflect.TypeToken;
5553

@@ -2107,12 +2105,7 @@ public boolean finalizeVirtualMachineProfile(final VirtualMachineProfile profile
21072105
try {
21082106
String userData = userDataManager.validateAndGetUserDataForSystemVM(userDataUuid);
21092107
if (StringUtils.isNotBlank(userData)) {
2110-
// Decode base64 user data, compress it, then re-encode to reduce command line length
2111-
String plainTextUserData = new String(Base64.getDecoder().decode(userData));
2112-
CompressionUtil compressionUtil = new CompressionUtil();
2113-
byte[] compressedUserData = compressionUtil.compressString(plainTextUserData);
2114-
String encodedUserData = Base64.getEncoder().encodeToString(compressedUserData);
2115-
buf.append(" userdata=").append(encodedUserData);
2108+
buf.append(" userdata=").append(userData);
21162109
}
21172110
} catch (Exception e) {
21182111
logger.warn("Failed to load user data for the virtual router, ignored", e);

services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.net.URISyntaxException;
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
26-
import java.util.Base64;
2726
import java.util.Collections;
2827
import java.util.Date;
2928
import java.util.HashMap;
@@ -36,7 +35,6 @@
3635
import javax.inject.Inject;
3736
import javax.naming.ConfigurationException;
3837

39-
import com.cloud.utils.compression.CompressionUtil;
4038
import org.apache.cloudstack.agent.lb.IndirectAgentLB;
4139
import org.apache.cloudstack.ca.CAManager;
4240
import org.apache.cloudstack.context.CallContext;
@@ -1240,12 +1238,7 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, Depl
12401238
try {
12411239
String userData = userDataManager.validateAndGetUserDataForSystemVM(userDataUuid);
12421240
if (StringUtils.isNotBlank(userData)) {
1243-
// Decode base64 user data, compress it, then re-encode to reduce command line length
1244-
String plainTextUserData = new String(Base64.getDecoder().decode(userData));
1245-
CompressionUtil compressionUtil = new CompressionUtil();
1246-
byte[] compressedUserData = compressionUtil.compressString(plainTextUserData);
1247-
String encodedUserData = Base64.getEncoder().encodeToString(compressedUserData);
1248-
buf.append(" userdata=").append(encodedUserData);
1241+
buf.append(" userdata=").append(userData);
12491242
}
12501243
} catch (Exception e) {
12511244
logger.warn("Failed to load user data for the ssvm, ignored", e);

0 commit comments

Comments
 (0)