com.huawei.openstack4j.openstack.compute.internal.ComputeFloatingIPServiceImpl Maven / Gradle / Ivy
/*******************************************************************************
* Copyright 2016 ContainX and OpenStack4j
*
* Licensed 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 com.huawei.openstack4j.openstack.compute.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import com.huawei.openstack4j.api.compute.ComputeFloatingIPService;
import com.huawei.openstack4j.core.transport.ExecutionOptions;
import com.huawei.openstack4j.core.transport.propagation.PropagateOnStatus;
import com.huawei.openstack4j.model.common.ActionResponse;
import com.huawei.openstack4j.model.compute.FloatingIP;
import com.huawei.openstack4j.model.compute.Server;
import com.huawei.openstack4j.openstack.common.MapEntity;
import com.huawei.openstack4j.openstack.compute.domain.NovaFloatingIP;
import com.huawei.openstack4j.openstack.compute.domain.NovaFloatingIPPools;
import com.huawei.openstack4j.openstack.compute.domain.NovaFloatingIP.NovaFloatingIPs;
import com.huawei.openstack4j.openstack.compute.domain.actions.FloatingIpActions;
import com.huawei.openstack4j.openstack.compute.functions.ToActionResponseFunction;
/**
* OpenStack Floating-IP API Implementation
*
* @author Nathan Anderson
*/
public class ComputeFloatingIPServiceImpl extends BaseComputeServices implements ComputeFloatingIPService {
/**
* {@inheritDoc}
*/
@Override
public List list() {
return get(NovaFloatingIPs.class, uri("/os-floating-ips")).execute().getList();
}
/**
* {@inheritDoc}
*/
@Override
public List getPoolNames() {
return get(NovaFloatingIPPools.class, uri("/os-floating-ip-pools")).execute().getList();
}
/**
* {@inheritDoc}
*/
@Override
public FloatingIP allocateIP(String pool) {
return post(NovaFloatingIP.class, uri("/os-floating-ips")).entity(MapEntity.create("pool", pool))
.execute(ExecutionOptions.create(PropagateOnStatus.on(404)));
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse deallocateIP(String id) {
checkNotNull(id);
return ToActionResponseFunction.INSTANCE.apply(
delete(Void.class, uri("/os-floating-ips/%s", id)).executeWithResponse()
);
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse addFloatingIP(Server server, String fixedIpAddress, String ipAddress) {
checkNotNull(server);
checkNotNull(ipAddress);
return invokeAction(server.getId(), FloatingIpActions.Add.create(ipAddress, fixedIpAddress));
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse addFloatingIP(Server server, String ipAddress) {
return addFloatingIP(server, null, ipAddress);
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse removeFloatingIP(Server server, String ipAddress) {
checkNotNull(server);
checkNotNull(ipAddress);
return invokeAction(server.getId(), FloatingIpActions.Remove.create(ipAddress));
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse addFloatingIP(String serverId, String fixedIpAddress, String ipAddress) {
checkNotNull(serverId);
checkNotNull(ipAddress);
return invokeAction(serverId, FloatingIpActions.Add.create(ipAddress, fixedIpAddress));
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse addFloatingIP(String serverId, String ipAddress) {
return addFloatingIP(serverId, null, ipAddress);
}
/**
* {@inheritDoc}
*/
@Override
public ActionResponse removeFloatingIP(String serverId, String ipAddress) {
checkNotNull(serverId);
checkNotNull(ipAddress);
return invokeAction(serverId, FloatingIpActions.Remove.create(ipAddress));
}
}