use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateOrRetireHelper.
/**
* Helper method to paginate through resources to be deleted.
*/
private void disassociateOrRetireHelper(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.deletionNextPageLink == null) {
logFine(() -> String.format("Finished %s of compute states for Azure", ctx.request.preserveMissing ? "retiring" : "deletion"));
ctx.subStage = next;
handleSubStage(ctx);
return;
}
CompletionHandler completionHandler = (o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
ctx.deletionNextPageLink = queryTask.results.nextPageLink;
List<Operation> operations = new ArrayList<>();
for (Object s : queryTask.results.documents.values()) {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
String vmId = computeState.id;
// present in Azure but have older timestamp in local repository.
if (ctx.vmIds.contains(vmId) || ctx.regionIds.contains(vmId)) {
continue;
}
if (ctx.request.preserveMissing) {
logFine(() -> String.format("Retiring compute state %s", computeState.documentSelfLink));
ComputeState cs = new ComputeState();
cs.powerState = PowerState.OFF;
cs.lifecycleState = LifecycleState.RETIRED;
operations.add(Operation.createPatch(this, computeState.documentSelfLink).setBody(cs));
} else {
// Deleting the localResourceState is done by disassociating the endpointLink from the
// localResourceState. If the localResourceState isn't associated with any other
// endpointLink, we issue a delete then
Operation dOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, computeState);
if (dOp != null) {
dOp.sendWith(getHost());
logFine(() -> String.format("Deleting compute state %s", computeState.documentSelfLink));
}
if (computeState.diskLinks != null && !computeState.diskLinks.isEmpty()) {
computeState.diskLinks.forEach(dl -> {
sendRequest(Operation.createGet(this, dl).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving " + "diskState: %s", ex.getMessage()));
} else {
DiskState diskState = op.getBody(DiskState.class);
Operation diskOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, diskState);
if (diskOp != null) {
diskOp.sendWith(getHost());
logFine(() -> String.format("Deleting disk state %s of machine %s", dl, computeState.documentSelfLink));
}
}
}));
});
}
if (computeState.networkInterfaceLinks != null && !computeState.networkInterfaceLinks.isEmpty()) {
computeState.networkInterfaceLinks.forEach(nil -> {
sendRequest(Operation.createGet(this, nil).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving NetworkInterface state: %s", ex.getMessage()));
} else {
NetworkInterfaceState networkInterfaceState = op.getBody(NetworkInterfaceState.class);
Operation nicOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, networkInterfaceState);
if (nicOp != null) {
nicOp.sendWith(getHost());
logFine(() -> String.format("Deleting NetworkInterface state %s of machine %s", nil, computeState.documentSelfLink));
}
}
}));
});
}
}
}
if (operations.size() == 0) {
logFine(() -> String.format("No compute/disk states to %s", ctx.request.preserveMissing ? "retire" : "delete"));
disassociateOrRetireHelper(ctx, next);
return;
}
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null) {
// We don't want to fail the whole data collection if some of the
// operation fails.
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
disassociateOrRetireHelper(ctx, next);
}).sendWith(this);
};
logFine(() -> String.format("Querying page [%s] for resources to be %s", ctx.deletionNextPageLink, ctx.request.preserveMissing ? "retire" : "delete"));
sendRequest(Operation.createGet(createInventoryUri(this.getHost(), ctx.deletionNextPageLink)).setCompletion(completionHandler));
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class TestVSphereProvisionWithStaticIpTask method createNic.
private String createNic(SubnetState subnet) throws Throwable {
NetworkInterfaceState nic = new NetworkInterfaceState();
nic.name = "nic for " + subnet.networkLink;
nic.networkLink = subnet.networkLink;
nic.subnetLink = subnet.documentSelfLink;
if (this.nicDescription != null) {
this.nicDescription = TestUtils.doPost(this.host, this.nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(this.host, NetworkInterfaceDescriptionService.FACTORY_LINK));
nic.networkInterfaceDescriptionLink = this.nicDescription.documentSelfLink;
}
nic = TestUtils.doPost(this.host, nic, NetworkInterfaceState.class, UriUtils.buildUri(this.host, NetworkInterfaceService.FACTORY_LINK));
return nic.documentSelfLink;
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class ModelUtils method createNetworkInterface.
public static NetworkInterfaceState createNetworkInterface(BaseModelTest test, String name, SubnetState subnetState, NetworkState networkState) throws Throwable {
NetworkInterfaceState nis = new NetworkInterfaceState();
nis.name = name;
nis.subnetLink = subnetState.documentSelfLink;
nis.networkLink = networkState.documentSelfLink;
NetworkInterfaceState networkInterfaceState = test.postServiceSynchronously(NetworkInterfaceService.FACTORY_LINK, nis, NetworkInterfaceState.class);
return networkInterfaceState;
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class ModelUtils method createCompute.
public static ComputeService.ComputeStateWithDescription createCompute(BaseModelTest test, ComputeDescriptionService.ComputeDescription cd) throws Throwable {
ComputeService.ComputeState cs = new ComputeService.ComputeStateWithDescription();
cs.id = UUID.randomUUID().toString();
cs.name = cd.name;
cs.descriptionLink = cd.documentSelfLink;
cs.resourcePoolLink = null;
cs.address = "10.0.0.1";
cs.primaryMAC = "01:23:45:67:89:ab";
cs.powerState = ComputeService.PowerState.ON;
cs.adapterManagementReference = URI.create("https://esxhost-01:443/sdk");
cs.diskLinks = new ArrayList<>();
cs.diskLinks.add(createDiskState(test, cs.name).documentSelfLink);
cs.networkInterfaceLinks = new ArrayList<>();
EndpointState endpointState = createEndpoint(test);
NetworkState networkState = createNetwork(test, endpointState);
SubnetState subnetState = createSubnet(test, networkState, endpointState);
SubnetRangeState subnetRangeState = createSubnetRange(test, subnetState, "12.12.12.2", "12.12.12.120");
NetworkInterfaceState networkInterfaceState1 = createNetworkInterface(test, "nic-1", subnetState, networkState);
IPAddressState ipAddressState1 = createIpAddress(test, "12.12.12.2", IPAddressState.IPAddressStatus.ALLOCATED, subnetRangeState, networkInterfaceState1.documentSelfLink);
NetworkInterfaceState networkInterfaceState2 = createNetworkInterface(test, "nic-2", subnetState, networkState);
IPAddressState ipAddressState2 = createIpAddress(test, "12.12.12.3", IPAddressState.IPAddressStatus.ALLOCATED, subnetRangeState, networkInterfaceState2.documentSelfLink);
NetworkInterfaceState networkInterfaceState3 = createNetworkInterface(test, "nic-3", subnetState, networkState);
IPAddressState ipAddressState3 = createIpAddress(test, "12.12.12.4", IPAddressState.IPAddressStatus.ALLOCATED, subnetRangeState, networkInterfaceState3.documentSelfLink);
cs.networkInterfaceLinks.add(networkInterfaceState1.documentSelfLink);
cs.networkInterfaceLinks.add(networkInterfaceState2.documentSelfLink);
cs.networkInterfaceLinks.add(networkInterfaceState3.documentSelfLink);
cs.customProperties = new HashMap<>();
cs.customProperties.put(TEST_DESC_PROPERTY_NAME, TEST_DESC_PROPERTY_VALUE);
cs.tenantLinks = new ArrayList<>();
cs.tenantLinks.add("http://tenant");
ComputeService.ComputeState returnState = test.postServiceSynchronously(ComputeService.FACTORY_LINK, cs, ComputeService.ComputeState.class);
return ComputeService.ComputeStateWithDescription.create(cd, returnState);
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class AzureInstanceService method updateNicStates.
/**
* Update {@code computeState.nicState[i].address} with Azure NICs' private IP.
*/
private DeferredResult<AzureInstanceContext> updateNicStates(AzureInstanceContext ctx) {
if (ctx.nics == null || ctx.nics.isEmpty()) {
// Do nothing.
return DeferredResult.completed(ctx);
}
List<DeferredResult<Void>> updateNICsDR = new ArrayList<>(ctx.nics.size());
for (AzureNicContext nicCtx : ctx.nics) {
if (nicCtx.nic == null) {
continue;
}
final NetworkInterfaceState nicStateToUpdate = new NetworkInterfaceState();
nicStateToUpdate.id = nicCtx.nic.id();
nicStateToUpdate.documentSelfLink = nicCtx.nicStateWithDesc.documentSelfLink;
if (nicCtx.nic.ipConfigurations() != null && !nicCtx.nic.ipConfigurations().isEmpty()) {
nicStateToUpdate.address = nicCtx.nic.ipConfigurations().get(0).privateIPAddress();
}
Operation updateNicOp = Operation.createPatch(ctx.service, nicStateToUpdate.documentSelfLink).setBody(nicStateToUpdate);
DeferredResult<Void> updateNicDR = ctx.service.sendWithDeferredResult(updateNicOp).thenAccept(ignored -> logFine(() -> String.format("Updating NIC state [%s] with Private IP [%s]: SUCCESS", nicCtx.nic.name(), nicStateToUpdate.address)));
updateNICsDR.add(updateNicDR);
}
return DeferredResult.allOf(updateNICsDR).thenApply(ignored -> ctx);
}
Aggregations