This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
/
DeploymentParamsToXML.java
129 lines (114 loc) · 6 KB
/
DeploymentParamsToXML.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azurecompute.binders;
import static com.google.common.base.Throwables.propagate;
import static org.jclouds.azurecompute.domain.OSImage.Type.LINUX;
import org.jclouds.azurecompute.domain.DeploymentParams;
import org.jclouds.azurecompute.domain.OSImage;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import com.jamesmurty.utils.XMLBuilder;
public final class DeploymentParamsToXML implements Binder {
@Override
@SuppressWarnings("unchecked")
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
DeploymentParams params = DeploymentParams.class.cast(input);
try {
XMLBuilder builder = XMLBuilder.create("Deployment", "http://schemas.microsoft.com/windowsazure")
.e("Name").t(params.name()).up()
.e("DeploymentSlot").t("Production").up()
.e("Label").t(params.name()).up()
.e("RoleList")
.e("Role")
.e("RoleName").t(params.name()).up()
.e("RoleType").t("PersistentVMRole").up()
.e("ConfigurationSets");
if (params.os() == OSImage.Type.WINDOWS) {
XMLBuilder configBuilder = builder.e("ConfigurationSet"); // Windows
configBuilder.e("ConfigurationSetType").t("WindowsProvisioningConfiguration").up()
.e("ComputerName").t(params.name()).up()
.e("AdminPassword").t(params.password()).up()
.e("ResetPasswordOnFirstLogon").t("false").up()
.e("EnableAutomaticUpdate").t("false").up()
.e("DomainJoin")
.e("Credentials")
.e("Domain").t(params.name()).up()
.e("Username").t(params.username()).up()
.e("Password").t(params.password()).up()
.up() // Credentials
.e("JoinDomain").t(params.name()).up()
.up() // Domain Join
.e("StoredCertificateSettings").up()
.up(); // Windows ConfigurationSet
} else if (params.os() == OSImage.Type.LINUX) {
XMLBuilder configBuilder = builder.e("ConfigurationSet"); // Linux
configBuilder.e("ConfigurationSetType").t("LinuxProvisioningConfiguration").up()
.e("HostName").t(params.name()).up()
.e("UserName").t(params.username()).up()
.e("UserPassword").t(params.password()).up()
.e("DisableSshPasswordAuthentication").t("false").up()
.e("SSH")
.e("PublicKeys").up()
.e("KeyPairs").up()
.up(); // Linux ConfigurationSet
} else {
throw new IllegalArgumentException("Unrecognized os type " + params);
}
XMLBuilder configBuilder = builder.e("ConfigurationSet"); // Network
configBuilder.e("ConfigurationSetType").t("NetworkConfiguration").up();
XMLBuilder inputEndpoints = configBuilder.e("InputEndpoints");
for (DeploymentParams.ExternalEndpoint endpoint : params.externalEndpoints()) {
XMLBuilder inputBuilder = inputEndpoints.e("InputEndpoint");
inputBuilder.e("LocalPort").t(Integer.toString(endpoint.localPort())).up()
.e("Name").t(endpoint.name()).up()
.e("Port").t(Integer.toString(endpoint.port())).up()
.e("Protocol").t(endpoint.protocol().toLowerCase()).up()
.up(); //InputEndpoint
}
inputEndpoints.up();
//configBuilder.e("SubnetNames").up().up();
XMLBuilder subnetNames = configBuilder.e("SubnetNames");
for (String subnetName : params.subnetNames()) {
subnetNames.e("SubnetName").t(subnetName).up()
.up(); //subnetName
}
builder.up() //ConfigurationSets
// TODO No Disk should be specified for a Role if using a VMImage
.e("DataVirtualHardDisks").up()
.e("OSVirtualHardDisk")
.e("HostCaching").t("ReadWrite").up()
.e("MediaLink").t(params.mediaLink().toASCIIString()).up()
// TODO
/// If you are using a VM image, it must be specified as VMImageName for the role,
// not as SourceImageNamefor OSVirtualHardDisk.</Message></Error>]
.e("SourceImageName").t(params.sourceImageName()).up()
.e("OS").t(params.os() == LINUX ? "Linux" : "Windows").up()
.up() //OSVirtualHardDisk
.e("RoleSize").t(params.size().getText()).up()
.up() //Role
.up() //RoleList
.e("VirtualNetworkName").t(params.virtualNetworkName()).up();
if (params.reservedIPName() != null) {
builder.up().up().up().e("ReservedIPName").t(params.reservedIPName()).up();
}
// TODO: Undeprecate this method as forcing users to wrap a String in guava's ByteSource is not great.
return (R) request.toBuilder().payload(builder.asString()).build();
} catch (Exception e) {
throw propagate(e);
}
}
}