use of com.vmware.vim25.VmConfigSpec in project photon-model by vmware.
the class InstanceClient method populateCloudConfig.
/**
* Puts the cloud-config user data in the OVF environment
*
* @param spec
* @param currentProps
*/
private boolean populateCloudConfig(VirtualMachineConfigSpec spec, ArrayOfVAppPropertyInfo currentProps) {
if (this.bootDisk == null) {
return false;
}
boolean customizationsApplied = false;
int nextKey = 1;
if (currentProps != null) {
nextKey = currentProps.getVAppPropertyInfo().stream().mapToInt(VAppPropertyInfo::getKey).max().orElse(1);
nextKey++;
}
VmConfigSpec configSpec = new VmConfigSpec();
configSpec.getOvfEnvironmentTransport().add(OvfDeployer.TRANSPORT_ISO);
String cloudConfig = getFileItemByPath(this.bootDisk, CLOUD_CONFIG_PROPERTY_USER_DATA);
if (cloudConfig != null) {
VAppPropertySpec propertySpec = new VAppPropertySpec();
VAppPropertyInfo userDataInfo = null;
if (currentProps != null) {
userDataInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(CLOUD_CONFIG_PROPERTY_USER_DATA)).findFirst().orElse(null);
if (userDataInfo == null) {
// try coreOS key
userDataInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(COREOS_CLOUD_CONFIG_PROPERTY_USER_DATA)).findFirst().orElse(null);
if (userDataInfo != null) {
VAppPropertyInfo coreosEncoding = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(COREOS_CLOUD_CONFIG_PROPERTY_USER_DATA_ENCODING)).findFirst().orElse(null);
if (coreosEncoding != null) {
VAppPropertySpec pSpec = new VAppPropertySpec();
coreosEncoding.setValue(CLOUD_CONFIG_BASE64_ENCODING);
pSpec.setOperation(ArrayUpdateOperation.EDIT);
pSpec.setInfo(coreosEncoding);
configSpec.getProperty().add(pSpec);
}
}
}
}
if (userDataInfo != null) {
propertySpec.setOperation(ArrayUpdateOperation.EDIT);
} else {
userDataInfo = new VAppPropertyInfo();
userDataInfo.setId(CLOUD_CONFIG_PROPERTY_USER_DATA);
userDataInfo.setType("string");
userDataInfo.setKey(nextKey++);
propertySpec.setOperation(ArrayUpdateOperation.ADD);
}
String encodedUserData = Base64.getEncoder().encodeToString(cloudConfig.getBytes());
userDataInfo.setValue(encodedUserData);
propertySpec.setInfo(userDataInfo);
configSpec.getProperty().add(propertySpec);
customizationsApplied = true;
}
String publicKeys = getFileItemByPath(this.bootDisk, CLOUD_CONFIG_PROPERTY_PUBLIC_KEYS);
if (publicKeys != null) {
VAppPropertySpec propertySpec = new VAppPropertySpec();
VAppPropertyInfo sshKeyInfo = null;
if (currentProps != null) {
sshKeyInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(CLOUD_CONFIG_PROPERTY_PUBLIC_KEYS)).findFirst().orElse(null);
}
if (sshKeyInfo != null) {
propertySpec.setOperation(ArrayUpdateOperation.EDIT);
} else {
sshKeyInfo = new VAppPropertyInfo();
sshKeyInfo.setType("string");
sshKeyInfo.setId(CLOUD_CONFIG_PROPERTY_PUBLIC_KEYS);
sshKeyInfo.setKey(nextKey++);
propertySpec.setOperation(ArrayUpdateOperation.ADD);
}
sshKeyInfo.setValue(publicKeys);
propertySpec.setInfo(sshKeyInfo);
configSpec.getProperty().add(propertySpec);
customizationsApplied = true;
}
String hostname = getFileItemByPath(this.bootDisk, CLOUD_CONFIG_PROPERTY_HOSTNAME);
if (hostname != null) {
VAppPropertySpec propertySpec = new VAppPropertySpec();
VAppPropertyInfo hostInfo = null;
if (currentProps != null) {
hostInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(CLOUD_CONFIG_PROPERTY_HOSTNAME)).findFirst().orElse(null);
if (hostInfo == null) {
// try coreOS key
hostInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(COREOS_CLOUD_CONFIG_PROPERTY_HOSTNAME)).findFirst().orElse(null);
}
}
if (hostInfo != null) {
propertySpec.setOperation(ArrayUpdateOperation.EDIT);
} else {
hostInfo = new VAppPropertyInfo();
hostInfo.setId(CLOUD_CONFIG_PROPERTY_USER_DATA);
hostInfo.setType("string");
hostInfo.setKey(nextKey++);
propertySpec.setOperation(ArrayUpdateOperation.ADD);
}
hostInfo.setValue(hostname);
propertySpec.setInfo(hostInfo);
configSpec.getProperty().add(propertySpec);
customizationsApplied = true;
}
if (customizationsApplied) {
spec.setVAppConfig(configSpec);
}
return customizationsApplied;
}
Aggregations