package com.microsoft.azure.management.network.implementation;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.NetworkPeering;
import com.microsoft.azure.management.network.NetworkPeerings;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.IndependentChildrenImpl;
import rx.Completable;
import rx.Observable;
import rx.functions.Func1;
@LangDefinition
class NetworkPeeringsImpl
extends IndependentChildrenImpl<
NetworkPeering,
NetworkPeeringImpl,
VirtualNetworkPeeringInner,
VirtualNetworkPeeringsInner,
NetworkManager,
Network>
implements NetworkPeerings {
private final NetworkImpl network;
NetworkPeeringsImpl(final NetworkImpl parent) {
super(parent.manager().inner().virtualNetworkPeerings(), parent.manager());
this.network = parent;
}
@Override
public NetworkPeeringImpl define(String name) {
return wrapModel(name);
}
@Override
protected NetworkPeeringImpl wrapModel(String name) {
VirtualNetworkPeeringInner inner = new VirtualNetworkPeeringInner().withName(name);
return new NetworkPeeringImpl(inner, this.network);
}
@Override
protected NetworkPeeringImpl wrapModel(VirtualNetworkPeeringInner inner) {
return (inner != null) ? new NetworkPeeringImpl(inner, this.network) : null;
}
@Override
public Completable deleteByParentAsync(String groupName, String parentName, final String name) {
return this.manager().networks()
.getByResourceGroupAsync(groupName, parentName)
.flatMap(new Func1<Network, Observable<NetworkPeering>>() {
@Override
public Observable<NetworkPeering> call(Network localNetwork) {
if (localNetwork == null) {
return Observable.just(null);
} else {
String peeringId = localNetwork.id() + "/peerings/" + name;
return localNetwork.peerings().getByIdAsync(peeringId);
}
}
})
.flatMap(new Func1<NetworkPeering, Observable<NetworkPeering>>() {
@Override
public Observable<NetworkPeering> call(NetworkPeering localPeering) {
if (localPeering == null) {
return Observable.just(null);
} else if (!localPeering.isSameSubscription()) {
return Observable.just(localPeering);
} else {
return Observable.just(localPeering).concatWith(localPeering.getRemotePeeringAsync());
}
}
})
.flatMap(new Func1<NetworkPeering, Observable<Void>>() {
@Override
public Observable<Void> call(NetworkPeering peering) {
if (peering == null) {
return Observable.just(null);
} else {
String networkName = ResourceUtils.nameFromResourceId(peering.networkId());
return peering.manager().inner().virtualNetworkPeerings().deleteAsync(
peering.resourceGroupName(),
networkName,
peering.name());
}
}
})
.last()
.toCompletable();
}
@Override
public Observable<NetworkPeering> getByParentAsync(String resourceGroup, String parentName, String name) {
return this.inner().getAsync(resourceGroup, parentName, name).map(new Func1<VirtualNetworkPeeringInner, NetworkPeering>() {
@Override
public NetworkPeering call(VirtualNetworkPeeringInner inner) {
return wrapModel(inner);
}
});
}
@Override
public PagedList<NetworkPeering> listByParent(String resourceGroupName, String parentName) {
return wrapList(this.inner().list(resourceGroupName, parentName));
}
@Override
public PagedList<NetworkPeering> list() {
return this.wrapList(this.inner().list(this.network.resourceGroupName(), this.network.name()));
}
@Override
public Observable<NetworkPeering> listAsync() {
return this.wrapPageAsync(this.inner().listAsync(this.network.resourceGroupName(), this.network.name()));
}
@Override
public NetworkPeering getByRemoteNetwork(Network network) {
return (network != null) ? this.getByRemoteNetwork(network.id()) : null;
}
@Override
public NetworkPeering getByRemoteNetwork(String remoteNetworkResourceId) {
if (remoteNetworkResourceId != null) {
for (NetworkPeering peering : this.list()) {
if (peering.remoteNetworkId().equalsIgnoreCase(remoteNetworkResourceId)) {
return peering;
}
}
}
return null;
}
@Override
public Observable<NetworkPeering> getByRemoteNetworkAsync(Network network) {
if (network != null) {
return this.getByRemoteNetworkAsync(network.id());
} else {
return Observable.just(null);
}
}
@Override
public Observable<NetworkPeering> getByRemoteNetworkAsync(final String remoteNetworkResourceId) {
if (remoteNetworkResourceId == null) {
return Observable.just(null);
} else {
return this.listAsync().filter(new Func1<NetworkPeering, Boolean>() {
@Override
public Boolean call(NetworkPeering peering) {
if (peering == null) {
return false;
} else {
return remoteNetworkResourceId.equalsIgnoreCase(peering.remoteNetworkId());
}
}
});
}
}
}