Adds AdvertiseWinrmLoginPolicy
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/ccce23f1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/ccce23f1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/ccce23f1 Branch: refs/heads/master Commit: ccce23f1d1bd267e8fe669e65927a4c5393058e4 Parents: e98c45a Author: Aled Sage <[email protected]> Authored: Sat Jun 13 17:40:18 2015 +0100 Committer: Aled Sage <[email protected]> Committed: Sat Jun 13 23:11:11 2015 +0100 ---------------------------------------------------------------------- .../policy/os/AdvertiseWinrmLoginPolicy.java | 82 ++++++++++++++++++++ .../os/AdvertiseWinrmLoginPolicyTest.java | 51 ++++++++++++ 2 files changed, 133 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ccce23f1/locations/jclouds/src/main/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicy.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicy.java b/locations/jclouds/src/main/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicy.java new file mode 100644 index 0000000..983a5ff --- /dev/null +++ b/locations/jclouds/src/main/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicy.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 brooklyn.policy.os; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import brooklyn.entity.Entity; +import brooklyn.entity.basic.AbstractEntity; +import brooklyn.entity.basic.EntityLocal; +import brooklyn.event.AttributeSensor; +import brooklyn.event.SensorEvent; +import brooklyn.event.SensorEventListener; +import brooklyn.event.basic.Sensors; +import brooklyn.location.Location; +import brooklyn.location.basic.WinRmMachineLocation; +import brooklyn.policy.basic.AbstractPolicy; + +import com.google.common.annotations.Beta; + +/** + * When attached to an entity, this will monitor for when an {@link WinRmMachineLocation} is added to that entity + * (e.g. when a VM has been provisioned for it). + * + * The policy will then add a sensor that advertises the Administrator login details. + * + * A preferred mechanism would be for an external key-management tool to provide access to the credentials. + */ +@Beta +public class AdvertiseWinrmLoginPolicy extends AbstractPolicy implements SensorEventListener<Location> { + + // TODO Would like support user-creation over WinRM + + private static final Logger LOG = LoggerFactory.getLogger(AdvertiseWinrmLoginPolicy.class); + + public static final AttributeSensor<String> VM_USER_CREDENTIALS = Sensors.newStringSensor( + "vm.user.credentials", + "The \"<user> : <password> @ <hostname>:<port>\""); + + public void setEntity(EntityLocal entity) { + super.setEntity(entity); + subscribe(entity, AbstractEntity.LOCATION_ADDED, this); + } + + @Override + public void onEvent(SensorEvent<Location> event) { + final Entity entity = event.getSource(); + final Location loc = event.getValue(); + if (loc instanceof WinRmMachineLocation) { + advertiseUserAsync(entity, (WinRmMachineLocation)loc); + } + } + + protected void advertiseUserAsync(final Entity entity, final WinRmMachineLocation machine) { + String user = machine.getUser(); + String hostname = machine.getHostname(); + int port = machine.config().get(WinRmMachineLocation.WINRM_PORT); + String password = machine.config().get(WinRmMachineLocation.PASSWORD); + + String creds = user + " : " + password + " @ " +hostname + ":" + port; + + LOG.info("Advertising user "+user+" @ "+hostname+":"+port); + + ((EntityLocal)entity).setAttribute(VM_USER_CREDENTIALS, creds); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ccce23f1/locations/jclouds/src/test/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicyTest.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicyTest.java b/locations/jclouds/src/test/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicyTest.java new file mode 100644 index 0000000..2bc43c7 --- /dev/null +++ b/locations/jclouds/src/test/java/brooklyn/policy/os/AdvertiseWinrmLoginPolicyTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 brooklyn.policy.os; + +import org.testng.annotations.Test; + +import brooklyn.entity.BrooklynAppUnitTestSupport; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.location.LocationSpec; +import brooklyn.location.basic.WinRmMachineLocation; +import brooklyn.policy.PolicySpec; +import brooklyn.test.EntityTestUtils; +import brooklyn.test.entity.TestEntity; + +import com.google.common.collect.ImmutableList; + +public class AdvertiseWinrmLoginPolicyTest extends BrooklynAppUnitTestSupport { + + @Test + public void testAdvertisesMachineLoginDetails() throws Exception { + TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) + .policy(PolicySpec.create(AdvertiseWinrmLoginPolicy.class))); + + WinRmMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(WinRmMachineLocation.class) + .configure("address", "1.2.3.4") + .configure("user", "myuser") + .configure("port", 5678) + .configure("password", "mypassword")); + app.start(ImmutableList.of(machine)); + + String expected = "myuser : mypassword @ 1.2.3.4:5678"; + + EntityTestUtils.assertAttributeEqualsEventually(entity, AdvertiseWinrmLoginPolicy.VM_USER_CREDENTIALS, expected); + } +}
