diff --git a/src/main/java/com/thindeck/steps/LoadBalancer.java b/src/main/java/com/thindeck/steps/LoadBalancer.java new file mode 100644 index 00000000..6c49743f --- /dev/null +++ b/src/main/java/com/thindeck/steps/LoadBalancer.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2014, Thindeck.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the thindeck.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.thindeck.steps; + +import com.jcabi.aspects.Immutable; + +/** + * Load balancer representation. + * + * @author Carlos Miranda (miranda.cma@gmail.com) + * @version $Id$ + * @since 0.3 + * @todo #293 Let's implement a class Nginx that implements this interface. It + * should be able to connect via SSH to a server that has Nginx installed. At + * the minimum, we should be update the Nginx configuration of the server it's + * connected to. This class will be used primarily for the UpdateLB step. See + * https://github.com/yegor256/thindeck/issues/303 for more details. + */ +@Immutable +public interface LoadBalancer { + + /** + * Update load balancer configuration with the given mapping. + * @param host The host name indicated by requests + * @param hport Port corresponding to the host name + * @param server Server name to redirect requests to + * @param sport Server port to redirect requests to + * @checkstyle ParameterNumber (3 lines) + */ + void update( + final String host, final int hport, final String server, final int sport + ); + +} diff --git a/src/main/java/com/thindeck/steps/UpdateLB.java b/src/main/java/com/thindeck/steps/UpdateLB.java new file mode 100644 index 00000000..219765fd --- /dev/null +++ b/src/main/java/com/thindeck/steps/UpdateLB.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2014, Thindeck.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the thindeck.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.thindeck.steps; + +import com.jcabi.aspects.Immutable; +import com.thindeck.api.Context; +import com.thindeck.api.Step; +import java.io.IOException; + +/** + * Update Load Balancer. + * + * @author Carlos Miranda (miranda.cma@gmail.com) + * @version $Id$ + * @since 0.3 + */ +@Immutable +public final class UpdateLB implements Step { + + /** + * Public ctor. + * @param bal The load balancer + */ + @SuppressWarnings("PMD.UnusedFormalParameter") + public UpdateLB(final LoadBalancer bal) { + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public String name() { + return "update-lb"; + } + + @Override + public void exec(final Context ctx) throws IOException { + throw new UnsupportedOperationException("exec: Not yet implemented"); + } + + @Override + public void commit(final Context ctx) throws IOException { + // nothing to commit + } + + @Override + public void rollback(final Context ctx) throws IOException { + // nothing to rollback + } + +} diff --git a/src/test/java/com/thindeck/steps/UpdateLBTest.java b/src/test/java/com/thindeck/steps/UpdateLBTest.java new file mode 100644 index 00000000..9bd6286c --- /dev/null +++ b/src/test/java/com/thindeck/steps/UpdateLBTest.java @@ -0,0 +1,102 @@ +/** + * Copyright (c) 2014, Thindeck.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the thindeck.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.thindeck.steps; + +import com.thindeck.api.Context; +import com.thindeck.api.Step; +import com.thindeck.api.mock.MkContext; +import java.io.IOException; +import org.junit.Test; +import org.mockito.Mockito; +import org.xembly.Directives; + +/** + * Test case for {@link UpdateLB}. + * + * @author Carlos Miranda (miranda.cma@gmail.com) + * @version $Id$ + * @since 0.3 + */ +public final class UpdateLBTest { + + /** + * FindTanks can update load balancer configuration from memo. + * @throws IOException If fails + * @todo #293 The test is currently ignored for two reasons: + * 1. The schema memo.xsd does not yet define the domain element. The test + * will fail because the XML validation will throw an exception. + * 2. UpdateLB is not yet implemented, which means the verify statements + * will fail even if the Memo XML is valid. + * Let's address these issues and enable this test. See Github issue + * https://github.com/yegor256/thindeck/issues/308 for further details. + */ + @Test + @org.junit.Ignore + public void updatesLoadBalancerNginxConfig() throws IOException { + final LoadBalancer balancer = Mockito.mock(LoadBalancer.class); + final Step step = new UpdateLB(balancer); + final Context ctx = new MkContext(); + // @checkstyle MagicNumber (6 lines) + final String domain = "www.example.com"; + final int firstport = 80; + final int secondport = 443; + final String tank = "tank.thindeck.com"; + final int firstout = 32667; + final int secondout = 32668; + // @checkstyle MultipleStringLiterals (30 lines) + // @checkstyle LineLength (13 lines) + ctx.memo().update( + new Directives() + .xpath("/memo") + .addIf("domains") + .addIf("domain") + .add("host").set(domain).up() + .addIf("ports") + .add("port").set(String.valueOf(firstport)).up() + .add("port").set(String.valueOf(secondport)).up() + .up().up().up() + .addIf("containers") + .addIf("container").attr("type", "green") + .add("cid").set("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789").up() + .add("ports") + .add("in").set(String.valueOf(firstport)).up() + .add("out").set(String.valueOf(firstout)).up() + .add("in").set(String.valueOf(secondport)).up() + .add("out").set(String.valueOf(secondout)).up() + .up() + .add("dir").set("/fake/dir").up() + .add("tank").set(tank) + ); + step.exec(ctx); + Mockito.verify(balancer).update(domain, firstport, tank, firstout); + Mockito.verify(balancer).update(domain, secondport, tank, secondout); + } + +}