|
| 1 | +package cloud.stackit.sdk.iaas.examples; |
| 2 | + |
| 3 | +import cloud.stackit.sdk.core.exception.ApiException; |
| 4 | +import cloud.stackit.sdk.iaas.v2alpha1api.api.IaasApi; |
| 5 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.AddVPCRoutingTablePayload; |
| 6 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.AddVPCStaticRoutePayload; |
| 7 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.AddVPCStaticRoutePayloadDestination; |
| 8 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.AddVPCStaticRoutePayloadNexthop; |
| 9 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.CreateVPCPayload; |
| 10 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.CreateVPCRegionPayload; |
| 11 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.DestinationCIDRv4; |
| 12 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.NetworkRangeIPv4Request; |
| 13 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.NexthopInternet; |
| 14 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.RegionalVPC; |
| 15 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.Route; |
| 16 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.VPC; |
| 17 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.VPCNetworkRange; |
| 18 | +import cloud.stackit.sdk.iaas.v2alpha1api.model.VPCRoutingTable; |
| 19 | +import cloud.stackit.sdk.resourcemanager.v0api.api.ResourceManagerApi; |
| 20 | +import cloud.stackit.sdk.resourcemanager.v0api.model.CreateProjectPayload; |
| 21 | +import cloud.stackit.sdk.resourcemanager.v0api.model.Member; |
| 22 | +import cloud.stackit.sdk.resourcemanager.v0api.model.Project; |
| 23 | +import cloud.stackit.sdk.resourcemanager.v0api.wait.ResourcemanagerWait; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +public class IaaSVPCExample { |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + /* |
| 36 | + * Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env |
| 37 | + * STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY |
| 38 | + * */ |
| 39 | + IaasApi iaasApi = new IaasApi(); |
| 40 | + ResourceManagerApi resourceManagerApi = new ResourceManagerApi(); |
| 41 | + |
| 42 | + String region = "eu01"; |
| 43 | + String parentContainerId = System.getenv("STACKIT_CONTAINER_ID"); |
| 44 | + String ownerEmail = System.getenv("STACKIT_OWNER_EMAIL"); |
| 45 | + |
| 46 | + final Resources r = new Resources(); |
| 47 | + |
| 48 | + Runnable cleanup = () -> { |
| 49 | + try { |
| 50 | + if (r.networkRange != null) { |
| 51 | + iaasApi.deleteVPCNetworkRange( |
| 52 | + r.project.getProjectId(), |
| 53 | + r.vpc.getId(), |
| 54 | + region, |
| 55 | + r.networkRange.getVPCNetworkRangeIPv4().getId() |
| 56 | + ); |
| 57 | + while (true) { |
| 58 | + try { |
| 59 | + String status = iaasApi.getVPCNetworkRange(r.project.getProjectId(), r.vpc.getId(), region, r.networkRange.getVPCNetworkRangeIPv4().getId()) |
| 60 | + .getVPCNetworkRangeIPv4().getStatus(); |
| 61 | + if ("FAILED".equals(status)) { |
| 62 | + throw new RuntimeException("network range deletion failed"); |
| 63 | + } |
| 64 | + TimeUnit.SECONDS.sleep(5); |
| 65 | + } catch (ApiException e) { |
| 66 | + if (e.getCode() == 404) { |
| 67 | + break; |
| 68 | + } |
| 69 | + throw e; |
| 70 | + } catch (InterruptedException e) { |
| 71 | + throw new RuntimeException(e); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + if (r.staticRoute != null) { |
| 76 | + iaasApi.deleteVPCStaticRoute( |
| 77 | + r.project.getProjectId(), |
| 78 | + r.vpc.getId(), |
| 79 | + region, |
| 80 | + r.vpcRoutingTable.getId(), |
| 81 | + r.staticRoute.getId() |
| 82 | + ); |
| 83 | + } |
| 84 | + if (r.vpcRoutingTable != null) { |
| 85 | + iaasApi.deleteVPCRoutingTable( |
| 86 | + r.project.getProjectId(), |
| 87 | + r.vpc.getId(), |
| 88 | + region, |
| 89 | + r.vpcRoutingTable.getId() |
| 90 | + ); |
| 91 | + } |
| 92 | + if (r.vpcRegion != null) { |
| 93 | + iaasApi.deleteVPCRegion( |
| 94 | + r.project.getProjectId(), |
| 95 | + r.vpc.getId(), |
| 96 | + region |
| 97 | + ); |
| 98 | + while (true) { |
| 99 | + try { |
| 100 | + String status = iaasApi.getVPCRegion(r.project.getProjectId(), r.vpc.getId(), region) |
| 101 | + .getStatus(); |
| 102 | + if ("FAILED".equals(status)) { |
| 103 | + throw new RuntimeException("region deletion failed"); |
| 104 | + } |
| 105 | + TimeUnit.SECONDS.sleep(5); |
| 106 | + } catch (ApiException e) { |
| 107 | + if (e.getCode() == 404 || e.getCode() == 400) { |
| 108 | + break; |
| 109 | + } |
| 110 | + } catch (InterruptedException e) { |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + if (r.vpc != null) { |
| 116 | + iaasApi.deleteVPC( |
| 117 | + r.project.getProjectId(), |
| 118 | + r.vpc.getId() |
| 119 | + ); |
| 120 | + } |
| 121 | + if (r.project != null) { |
| 122 | + resourceManagerApi.deleteProject(r.project.getProjectId().toString()); |
| 123 | + } |
| 124 | + } catch (ApiException e) { |
| 125 | + System.out.println("cleanup failed, there may be resource left over: " + e.getMessage()); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + try { |
| 130 | + // create project |
| 131 | + System.out.println("creating project..."); |
| 132 | + Set<Member> members = new HashSet<>(); |
| 133 | + members.add(new Member().subject(ownerEmail).role("owner")); |
| 134 | + Map<String, String> labels = new HashMap<>(); |
| 135 | + labels.put("enable-vpc", "true"); |
| 136 | + r.project = resourceManagerApi.createProject(new CreateProjectPayload() |
| 137 | + .containerParentId(parentContainerId) |
| 138 | + .name("vpc-example-project") |
| 139 | + .members(members) |
| 140 | + .labels(labels) |
| 141 | + ); |
| 142 | + ResourcemanagerWait.createProjectWaitHandler(resourceManagerApi, r.project.getContainerId()) |
| 143 | + .waitWithContextAsync() |
| 144 | + .get(); |
| 145 | + System.out.println("project created: " + r.project.getProjectId()); |
| 146 | + |
| 147 | + // create vpc |
| 148 | + System.out.println("creating vpc..."); |
| 149 | + r.vpc = iaasApi.createVPC(r.project.getProjectId(), new CreateVPCPayload() |
| 150 | + .name("example-vpc") |
| 151 | + .description("example-vpc") |
| 152 | + ); |
| 153 | + System.out.println("vpc created: " + r.vpc.getId()); |
| 154 | + |
| 155 | + // create region (this must happen before creating a routing table or network area) |
| 156 | + System.out.println("creating region..."); |
| 157 | + r.vpcRegion = iaasApi.createVPCRegion(r.project.getProjectId(), r.vpc.getId(), region, new CreateVPCRegionPayload()); |
| 158 | + while (true) { |
| 159 | + try { |
| 160 | + String status = iaasApi.getVPCRegion(r.project.getProjectId(), r.vpc.getId(), region).getStatus(); |
| 161 | + if ("FAILED".equals(status)) { |
| 162 | + throw new RuntimeException("region creation failed"); |
| 163 | + } |
| 164 | + if ("CREATED".equals(status)) { |
| 165 | + break; |
| 166 | + } |
| 167 | + TimeUnit.SECONDS.sleep(5); |
| 168 | + } catch (ApiException e) { |
| 169 | + if (e.getCode() == 404) { |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + System.out.println("region created"); |
| 175 | + |
| 176 | + // create routing table |
| 177 | + System.out.println("creating routing table..."); |
| 178 | + r.vpcRoutingTable = iaasApi.addVPCRoutingTable( |
| 179 | + r.project.getProjectId(), |
| 180 | + r.vpc.getId(), |
| 181 | + region, |
| 182 | + new AddVPCRoutingTablePayload() |
| 183 | + .name("example-routing-table") |
| 184 | + ); |
| 185 | + System.out.println("routing table created: " + r.vpcRoutingTable.getId()); |
| 186 | + |
| 187 | + // create a static route |
| 188 | + System.out.println("creating static route..."); |
| 189 | + r.staticRoute = iaasApi.addVPCStaticRoute( |
| 190 | + r.project.getProjectId(), |
| 191 | + r.vpc.getId(), |
| 192 | + region, |
| 193 | + r.vpcRoutingTable.getId(), |
| 194 | + new AddVPCStaticRoutePayload() |
| 195 | + .destination(new AddVPCStaticRoutePayloadDestination(new DestinationCIDRv4().cidrv4("0.0.0.0/0"))) |
| 196 | + .nexthop(new AddVPCStaticRoutePayloadNexthop(new NexthopInternet().type("internet"))) |
| 197 | + ); |
| 198 | + System.out.println("static route created: " + r.staticRoute.getId()); |
| 199 | + |
| 200 | + // create a network range |
| 201 | + System.out.println("creating network range..."); |
| 202 | + r.networkRange = iaasApi.createVPCNetworkRange( |
| 203 | + r.project.getProjectId(), |
| 204 | + r.vpc.getId(), |
| 205 | + region, |
| 206 | + new NetworkRangeIPv4Request() |
| 207 | + .ipVersion(NetworkRangeIPv4Request.IpVersionEnum.IPV4) |
| 208 | + .prefix("192.168.1.0/24") |
| 209 | + .description("example-network-range") |
| 210 | + .defaultPrefixLen(26L) |
| 211 | + .maxPrefixLen(29L) |
| 212 | + .minPrefixLen(16L) |
| 213 | + .addNameserversItem("10.0.0.1") |
| 214 | + .addNameserversItem("10.0.0.8") |
| 215 | + ); |
| 216 | + while (true) { |
| 217 | + try { |
| 218 | + String status = iaasApi.getVPCNetworkRange(r.project.getProjectId(), r.vpc.getId(), region, r.networkRange.getVPCNetworkRangeIPv4().getId()).getVPCNetworkRangeIPv4().getStatus(); |
| 219 | + if ("FAILED".equals(status)) { |
| 220 | + throw new RuntimeException("region creation failed"); |
| 221 | + } |
| 222 | + if ("CREATED".equals(status)) { |
| 223 | + break; |
| 224 | + } |
| 225 | + TimeUnit.SECONDS.sleep(5); |
| 226 | + } catch (ApiException e) { |
| 227 | + if (e.getCode() == 404) { |
| 228 | + break; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + System.out.println("network range created: " + r.networkRange.getVPCNetworkRangeIPv4().getId()); |
| 233 | + } catch (Exception e) { |
| 234 | + System.out.println("failed to create resources: " + e.getMessage()); |
| 235 | + e.printStackTrace(); |
| 236 | + } finally { |
| 237 | + cleanup.run(); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + private static class Resources { |
| 242 | + Project project = null; |
| 243 | + VPC vpc = null; |
| 244 | + RegionalVPC vpcRegion = null; |
| 245 | + VPCRoutingTable vpcRoutingTable = null; |
| 246 | + Route staticRoute = null; |
| 247 | + VPCNetworkRange networkRange = null; |
| 248 | + } |
| 249 | +} |
0 commit comments