Search in sources :

Example 1 with AccessKeyType

use of com.microsoft.azure.management.containerregistry.AccessKeyType in project azure-tools-for-java by Microsoft.

the class ContainerRegistryMvpModel method createImageSettingWithRegistry.

/**
 * Get Registry Credential.
 */
public PrivateRegistryImageSetting createImageSettingWithRegistry(@NotNull final Registry registry) throws Exception {
    if (!registry.adminUserEnabled()) {
        throw new Exception(ADMIN_USER_NOT_ENABLED);
    }
    final RegistryCredentials credentials = registry.getCredentials();
    if (credentials == null) {
        throw new Exception(CANNOT_GET_CREDENTIAL);
    }
    String username = credentials.username();
    final Map<AccessKeyType, String> passwords = credentials.accessKeys();
    if (Utils.isEmptyString(username) || passwords == null || !passwords.containsKey(AccessKeyType.PRIMARY)) {
        throw new Exception(CANNOT_GET_CREDENTIAL);
    }
    return new PrivateRegistryImageSetting(registry.loginServerUrl(), username, passwords.get(AccessKeyType.PRIMARY), IMAGE_TAG, null);
}
Also used : AccessKeyType(com.microsoft.azure.management.containerregistry.AccessKeyType) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) RegistryCredentials(com.microsoft.azure.management.containerregistry.RegistryCredentials)

Example 2 with AccessKeyType

use of com.microsoft.azure.management.containerregistry.AccessKeyType in project azure-tools-for-java by Microsoft.

the class ContainerRegistryPropertyViewPresenter method getProperty.

private ContainerRegistryProperty getProperty(Registry registry, String sid) {
    String userName = "";
    String password = "";
    String password2 = "";
    if (registry.adminUserEnabled()) {
        RegistryCredentials credentials = registry.getCredentials();
        userName = credentials.username();
        Map<AccessKeyType, String> passwords = credentials.accessKeys();
        password = passwords.get(AccessKeyType.PRIMARY) == null ? "" : passwords.get(AccessKeyType.PRIMARY);
        password2 = passwords.get(AccessKeyType.SECONDARY) == null ? "" : passwords.get(AccessKeyType.SECONDARY);
    }
    return new ContainerRegistryProperty(registry.id(), registry.name(), registry.type(), registry.resourceGroupName(), registry.regionName(), sid, registry.loginServerUrl(), registry.adminUserEnabled(), userName, password, password2);
}
Also used : AccessKeyType(com.microsoft.azure.management.containerregistry.AccessKeyType) ContainerRegistryProperty(com.microsoft.azuretools.core.mvp.ui.containerregistry.ContainerRegistryProperty) RegistryCredentials(com.microsoft.azure.management.containerregistry.RegistryCredentials)

Example 3 with AccessKeyType

use of com.microsoft.azure.management.containerregistry.AccessKeyType in project azure-tools-for-java by Microsoft.

the class ContainerRegistryMvpModelTest method testCreateImageSettingWithRegistryWhenPasswordIsEmpty.

@Test(expected = Exception.class)
public void testCreateImageSettingWithRegistryWhenPasswordIsEmpty() throws Exception {
    when(registryMock1.adminUserEnabled()).thenReturn(true);
    RegistryCredentials credentials = mock(RegistryCredentials.class);
    when(registryMock1.getCredentials()).thenReturn(credentials);
    Map<AccessKeyType, String> passwords = new HashMap<>();
    when(credentials.accessKeys()).thenReturn(passwords);
    containerRegistryMvpModel.createImageSettingWithRegistry(registryMock1);
}
Also used : AccessKeyType(com.microsoft.azure.management.containerregistry.AccessKeyType) HashMap(java.util.HashMap) Mockito.anyString(org.mockito.Mockito.anyString) RegistryCredentials(com.microsoft.azure.management.containerregistry.RegistryCredentials) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with AccessKeyType

use of com.microsoft.azure.management.containerregistry.AccessKeyType in project azure-tools-for-java by Microsoft.

the class ContainerRegistryMvpModelTest method testCreateImageSettingWithRegistry.

@Test
public void testCreateImageSettingWithRegistry() throws Exception {
    when(registryMock1.adminUserEnabled()).thenReturn(true);
    RegistryCredentials credentials = mock(RegistryCredentials.class);
    when(registryMock1.getCredentials()).thenReturn(credentials);
    when(registryMock1.loginServerUrl()).thenReturn("url");
    when(credentials.username()).thenReturn("Alice");
    Map<AccessKeyType, String> passwords = new HashMap<>();
    passwords.put(AccessKeyType.PRIMARY, "primaryKey");
    when(credentials.accessKeys()).thenReturn(passwords);
    PrivateRegistryImageSetting setting = containerRegistryMvpModel.createImageSettingWithRegistry(registryMock1);
    assertEquals(setting.getUsername(), "Alice");
    assertEquals(setting.getPassword(), "primaryKey");
    assertEquals(setting.getServerUrl(), "url");
    assertEquals(setting.getImageNameWithTag(), "image:tag");
}
Also used : AccessKeyType(com.microsoft.azure.management.containerregistry.AccessKeyType) PrivateRegistryImageSetting(com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting) HashMap(java.util.HashMap) Mockito.anyString(org.mockito.Mockito.anyString) RegistryCredentials(com.microsoft.azure.management.containerregistry.RegistryCredentials) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

AccessKeyType (com.microsoft.azure.management.containerregistry.AccessKeyType)4 RegistryCredentials (com.microsoft.azure.management.containerregistry.RegistryCredentials)4 PrivateRegistryImageSetting (com.microsoft.azuretools.core.mvp.model.webapp.PrivateRegistryImageSetting)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Mockito.anyString (org.mockito.Mockito.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ContainerRegistryProperty (com.microsoft.azuretools.core.mvp.ui.containerregistry.ContainerRegistryProperty)1