-
Notifications
You must be signed in to change notification settings - Fork 16
/
NetworkDomainTearDown.java
203 lines (183 loc) · 8.23 KB
/
NetworkDomainTearDown.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* 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.examples.dimensiondata.cloudcontrol;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.jclouds.ContextBuilder;
import org.jclouds.dimensiondata.cloudcontrol.DimensionDataCloudControlApi;
import org.jclouds.dimensiondata.cloudcontrol.domain.FirewallRule;
import org.jclouds.dimensiondata.cloudcontrol.domain.NatRule;
import org.jclouds.dimensiondata.cloudcontrol.domain.NetworkDomain;
import org.jclouds.dimensiondata.cloudcontrol.domain.PublicIpBlock;
import org.jclouds.dimensiondata.cloudcontrol.domain.Server;
import org.jclouds.dimensiondata.cloudcontrol.domain.State;
import org.jclouds.dimensiondata.cloudcontrol.domain.Vlan;
import org.jclouds.dimensiondata.cloudcontrol.options.DatacenterIdListFilters;
import org.jclouds.logging.Logger;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.rest.ApiContext;
/**
* This example shows how a Network Domain and all of it's associated assets are removed.
* Takes 4 Program Arguments:
* <ul>
* <li>Endpoint URL</li>
* <li>Usernamme</li>
* <li>Password</li>
* <li>Network Domain Id</li>
* </ul>
*/
public class NetworkDomainTearDown
{
private static final Logger logger = Logger.CONSOLE;
public static void main(String[] args)
{
String provider = "dimensiondata-cloudcontrol";
String endpoint = args[0];
String username = args[1];
String password = args[2];
String networkDomainId = args[3];
try (ApiContext<DimensionDataCloudControlApi> ctx = ContextBuilder.newBuilder(provider)
.endpoint(endpoint)
.credentials(username, password)
.modules(ImmutableSet.of(new SLF4JLoggingModule()))
.build())
{
DimensionDataCloudControlApi api = ctx.getApi();
logger.info("Deleting resources for network domain %s", networkDomainId);
NetworkDomain networkDomain = api.getNetworkApi().getNetworkDomain(networkDomainId);
if (networkDomain == null)
{
logger.info("Network Domain with Id %s is not found", networkDomainId);
return;
}
if (networkDomain.state() != State.NORMAL)
{
logger.info("Network Domain with Id %s is not in a NORMAL state, cannot delete", networkDomain.id());
return;
}
String datacenterId = networkDomain.datacenterId();
removePublicIpBlocks(networkDomainId, api);
deleteNatRules(networkDomainId, api);
deleteFirewallRules(networkDomainId, api);
deleteServers(api, datacenterId);
ImmutableList<Server> servers = api.getServerApi().listServers().concat().toList();
if (!servers.isEmpty())
{
logger.info("Could not delete all Servers. Servers not deleted:");
for (Server server : servers)
{
logger.info("Id %s, Name %s, State, %s", server.id(), server.name(), server.state());
}
return;
}
deleteVlans(api, networkDomain);
deleteNetworkDomain(networkDomainId, api);
}
}
private static void removePublicIpBlocks(String networkDomainId, DimensionDataCloudControlApi api)
{
for (PublicIpBlock publicIpBlock : api.getNetworkApi().listPublicIPv4AddressBlocks(networkDomainId).concat().toList())
{
logger.info("Deleting PublicIpBlock with Id %s", publicIpBlock.id());
api.getNetworkApi().removePublicIpBlock(publicIpBlock.id());
}
}
private static void deleteFirewallRules(String networkDomainId, DimensionDataCloudControlApi api)
{
for (FirewallRule firewallRule : api.getNetworkApi().listFirewallRules(networkDomainId).concat().toList())
{
if (firewallRule.ruleType().equals("CLIENT_RULE"))
{
logger.info("Deleting FirewallRule with Id %s", firewallRule.id());
api.getNetworkApi().deleteFirewallRule(firewallRule.id());
}
}
}
private static void deleteNatRules(String networkDomainId, DimensionDataCloudControlApi api)
{
for (NatRule natRule : api.getNetworkApi().listNatRules(networkDomainId).concat().toList())
{
logger.info("Deleting NatRule with Id %s", natRule.id());
api.getNetworkApi().deleteNatRule(natRule.id());
}
}
private static void deleteNetworkDomain(String networkDomainId, DimensionDataCloudControlApi api)
{
logger.info("Deleting Network Domain with Id %s", networkDomainId);
api.getNetworkApi().deleteNetworkDomain(networkDomainId);
api.getNetworkApi().networkDomainDeletedPredicate().apply(networkDomainId);
}
private static void deleteVlans(DimensionDataCloudControlApi api, NetworkDomain networkDomain)
{
for (Vlan vlan : api.getNetworkApi().listVlans(networkDomain.id()).concat().toList())
{
try
{
if (vlan.state() != State.NORMAL)
{
logger.info("Vlan with Id %s is not in a NORMAL state, cannot delete", vlan.id());
continue;
}
logger.info("Deleting Vlan with Id %s", vlan.id());
api.getNetworkApi().deleteVlan(vlan.id());
api.getNetworkApi().vlanDeletedPredicate().apply(vlan.id());
}
catch (Exception e)
{
logger.error("Unable to delete Vlan with Id %s due to: %s", vlan.id(), e.getMessage());
}
}
}
private static void deleteServers(DimensionDataCloudControlApi api, String datacenterId)
{
for (Server server : api.getServerApi().listServers(DatacenterIdListFilters.Builder.datacenterId(datacenterId)))
{
try
{
if (server.state() == State.FAILED_ADD)
{
logger.info("Server with Id %s is in a FAILED_ADD state, running the clean server operation.", server.id());
api.getServerApi().cleanServer(server.id());
api.getServerApi().serverDeletedPredicate().apply(server.id());
if (api.getServerApi().getServer(server.id()) != null)
{
logger.info("Failed to clean Server with Id %s", server.id());
continue;
}
}
if (server.state() != State.NORMAL)
{
logger.info("Server with Id %s is not in a NORMAL state, current state %s - cannot delete", server.id(), server.state());
continue;
}
if (server.started())
{
logger.info("Shutting down Server with Id %s", server.id());
api.getServerApi().shutdownServer(server.id());
api.getServerApi().serverStoppedPredicate().apply(server.id());
}
logger.info("Deleting Server with Id %s", server.id());
api.getServerApi().deleteServer(server.id());
api.getServerApi().serverDeletedPredicate().apply(server.id());
}
catch (Exception e)
{
logger.error("Unable to Delete Server with Id %s due to: %s", server.id(), e.getMessage());
}
}
}
}