default to root/Administrator on image login user

This commit is contained in:
Adrian Cole 2011-11-16 14:02:00 +02:00
parent 85a0cdfa9c
commit 6392edfcee
2 changed files with 90 additions and 1 deletions

View File

@ -18,10 +18,14 @@
*/ */
package org.jclouds.compute.strategy.impl; package org.jclouds.compute.strategy.impl;
import static com.google.common.base.Preconditions.checkState;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
@ -41,6 +45,14 @@ public class ReturnCredentialsBoundToImage implements PopulateDefaultLoginCreden
@Override @Override
public Credentials execute(Object resourceToAuthenticate) { public Credentials execute(Object resourceToAuthenticate) {
checkState(resourceToAuthenticate instanceof Image, "this is only valid for images");
if (creds != null)
return creds; return creds;
Image image = Image.class.cast(resourceToAuthenticate);
if (image.getOperatingSystem() != null && OsFamily.WINDOWS.equals(image.getOperatingSystem().getFamily())) {
return new Credentials("Administrator", null);
} else {
return new Credentials("root", null);
}
} }
} }

View File

@ -0,0 +1,77 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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 org.jclouds.compute.strategy.impl;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.assertEquals;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.domain.Credentials;
import org.testng.annotations.Test;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "ReturnCredentialsBoundToImageTest")
public class ReturnCredentialsBoundToImageTest {
public void testDefaultIsToReturnConfiguredCredential() {
Image image = createMock(Image.class);
replay(image);
Credentials creds = new Credentials("ubuntu", "foo");
assertEquals(new ReturnCredentialsBoundToImage(creds).execute(image), creds);
verify(image);
}
public void testReturnAdministratorOnWindows() {
Image image = createMock(Image.class);
expect(image.getOperatingSystem()).andReturn(
OperatingSystem.builder().family(OsFamily.WINDOWS).description("foo").build()).atLeastOnce();
replay(image);
Credentials creds = new Credentials("Administrator", null);
assertEquals(new ReturnCredentialsBoundToImage(null).execute(image), creds);
verify(image);
}
public void testReturnRootWhenNotOnWindows() {
Image image = createMock(Image.class);
expect(image.getOperatingSystem()).andReturn(
OperatingSystem.builder().family(OsFamily.LINUX).description("foo").build()).atLeastOnce();
replay(image);
Credentials creds = new Credentials("root", null);
assertEquals(new ReturnCredentialsBoundToImage(null).execute(image), creds);
verify(image);
}
}