Skip to content

Commit

Permalink
Make Public IP optional parameter for VM creation (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergiy Getlin <[email protected]>
  • Loading branch information
sergiygetlin and sergiygetlin authored May 9, 2022
1 parent cc54bd3 commit 1a56713
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.auto.value.AutoValue;
import com.google.gson.JsonObject;
import java.util.Optional;
import javax.annotation.Nullable;

/** Virtual machine creation request data. */
@AutoValue
Expand All @@ -23,6 +24,7 @@ public abstract class CreateVirtualMachineRequestData extends BaseComputeRequest
public abstract String subnetName();

/** Public IP address associated with the VM. */
@Nullable
public abstract PublicIpAddress publicIpAddress();

/** Disk associated with the VM. */
Expand Down Expand Up @@ -84,7 +86,9 @@ public JsonObject serialize() {
JsonObject requestData = super.serializeCommon();
requestData.addProperty("network", network().name());
requestData.addProperty("subnetName", subnetName());
requestData.addProperty("ip", publicIpAddress().ipAddress());
if (publicIpAddress() != null) {
requestData.addProperty("ip", publicIpAddress().ipAddress());
}
requestData.addProperty("disk", disk().name());
requestData.addProperty("image", image());
return requestData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,38 @@ public void serializeCreateVirtualMachine() {
createVirtualMachine.resourceUidCreation());
}

@Test
public void serializeCreateVirtualMachineWithoutPublicIp() {
CreateVirtualMachineRequestData createVirtualMachine =
CreateVirtualMachineRequestData.builder()
.setTenantId("my-tenant")
.setSubscriptionId("my-sub")
.setResourceGroupName("my-rg")
.setRegion(Region.US_EAST)
.setName("my-vm")
.setDisk(mockDisk())
.setNetwork(mockNetwork())
.setSubnetName("my-subnet")
.setImage("my-image")
.build();

assertEquals(ComputeManagerOperation.AZURE_CREATE_VM, createVirtualMachine.cloudOperation());
assertEquals(
"{\"tenantId\":\"my-tenant\",\"subscriptionId\":\"my-sub\",\"resourceGroupName\":\"my-rg\","
+ "\"name\":\"my-vm\",\"region\":\"eastus\","
+ "\"network\":\"my-network\",\"subnetName\":\"my-subnet\","
+ "\"disk\":\"my-disk\",\"image\":\"my-image\"}",
createVirtualMachine.serialize().toString());
assertEquals(
Optional.of(
new CloudResourceUid()
.azureVirtualMachine(
new AzureVirtualMachine()
.resourceGroup(azureResourceGroup(createVirtualMachine))
.vmName("my-vm"))),
createVirtualMachine.resourceUidCreation());
}

private static PublicIpAddress mockPublicIpAddress() {
PublicIpAddress mock = mock(PublicIpAddress.class);
when(mock.name()).thenReturn("my-ip");
Expand Down

0 comments on commit 1a56713

Please sign in to comment.