diff --git a/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java b/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java index b8b5f697..2b17466d 100644 --- a/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java +++ b/src/main/java/io/seqera/tower/cli/commands/credentials/AddCmd.java @@ -21,6 +21,7 @@ import io.seqera.tower.cli.commands.credentials.add.AddAgentCmd; import io.seqera.tower.cli.commands.credentials.add.AddAwsCmd; import io.seqera.tower.cli.commands.credentials.add.AddAzureCmd; +import io.seqera.tower.cli.commands.credentials.add.AddAzureReposCmd; import io.seqera.tower.cli.commands.credentials.add.AddBitbucketCmd; import io.seqera.tower.cli.commands.credentials.add.AddCodeCommitCmd; import io.seqera.tower.cli.commands.credentials.add.AddContainerRegistryCmd; @@ -36,24 +37,21 @@ import java.io.IOException; -@Command( - name = "add", - description = "Add new workspace credentials.", - subcommands = { - AddAwsCmd.class, - AddCodeCommitCmd.class, - AddGoogleCmd.class, - AddGithubCmd.class, - AddGitlabCmd.class, - AddGiteaCmd.class, - AddBitbucketCmd.class, - AddSshCmd.class, - AddK8sCmd.class, - AddAzureCmd.class, - AddAgentCmd.class, - AddContainerRegistryCmd.class - } -) +@Command(name = "add", description = "Add new workspace credentials.", subcommands = { + AddAwsCmd.class, + AddCodeCommitCmd.class, + AddGoogleCmd.class, + AddGithubCmd.class, + AddGitlabCmd.class, + AddGiteaCmd.class, + AddBitbucketCmd.class, + AddAzureReposCmd.class, + AddSshCmd.class, + AddK8sCmd.class, + AddAzureCmd.class, + AddAgentCmd.class, + AddContainerRegistryCmd.class +}) public class AddCmd extends AbstractCredentialsCmd { @Override protected Response exec() throws ApiException, IOException { diff --git a/src/main/java/io/seqera/tower/cli/commands/credentials/add/AddAzureReposCmd.java b/src/main/java/io/seqera/tower/cli/commands/credentials/add/AddAzureReposCmd.java new file mode 100644 index 00000000..ca8efe38 --- /dev/null +++ b/src/main/java/io/seqera/tower/cli/commands/credentials/add/AddAzureReposCmd.java @@ -0,0 +1,35 @@ +/* + * Copyright 2021-2023, Seqera. + * + * 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 io.seqera.tower.cli.commands.credentials.add; + +import io.seqera.tower.cli.commands.credentials.providers.AzureReposProvider; +import io.seqera.tower.cli.commands.credentials.providers.CredentialsProvider; +import picocli.CommandLine.Command; +import picocli.CommandLine.Mixin; + +@Command(name = "azurerepos", description = "Add new Azure Repos workspace credentials.") +public class AddAzureReposCmd extends AbstractAddCmd { + + @Mixin + AzureReposProvider provider; + + @Override + protected CredentialsProvider getProvider() { + return provider; + } +} \ No newline at end of file diff --git a/src/main/java/io/seqera/tower/cli/commands/credentials/providers/AzureReposProvider.java b/src/main/java/io/seqera/tower/cli/commands/credentials/providers/AzureReposProvider.java new file mode 100644 index 00000000..97e25335 --- /dev/null +++ b/src/main/java/io/seqera/tower/cli/commands/credentials/providers/AzureReposProvider.java @@ -0,0 +1,49 @@ +/* + * Copyright 2021-2023, Seqera. + * + * 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 io.seqera.tower.cli.commands.credentials.providers; + +import io.seqera.tower.model.Credentials.ProviderEnum; +import io.seqera.tower.model.AzureReposSecurityKeys; +import picocli.CommandLine.Option; + +import java.io.IOException; + +public class AzureReposProvider extends AbstractGitProvider { + + @Option(names = { "-u", "--username" }, description = "Azure Repos username.", required = true) + public String userName; + + @Option(names = { "-p", + "--password" }, description = "Azure Repos account password or access token (recommended).", arity = "0..1", interactive = true, required = true) + public String password; + + @Option(names = { "-t", + "--token" }, description = "Azure Repos account access token.", arity = "0..1", required = true, interactive = true) + public String accessToken; + + public AzureReposProvider() { + super(ProviderEnum.AZUREREPOS); + } + + @Override + public AzureReposSecurityKeys securityKeys() throws IOException { + return new AzureReposSecurityKeys() + .username(userName) + .token(accessToken); + } +} diff --git a/src/test/java/io/seqera/tower/cli/credentials/providers/AzureReposProviderTest.java b/src/test/java/io/seqera/tower/cli/credentials/providers/AzureReposProviderTest.java new file mode 100644 index 00000000..da915bd0 --- /dev/null +++ b/src/test/java/io/seqera/tower/cli/credentials/providers/AzureReposProviderTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2021-2023, Seqera. + * + * 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 io.seqera.tower.cli.credentials.providers; + +import io.seqera.tower.cli.BaseCmdTest; +import io.seqera.tower.cli.commands.enums.OutputType; +import io.seqera.tower.cli.responses.CredentialsAdded; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockserver.client.MockServerClient; +import org.mockserver.model.MediaType; + +import static io.seqera.tower.cli.commands.AbstractApiCmd.USER_WORKSPACE_NAME; +import static org.mockserver.matchers.Times.exactly; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; +import static org.mockserver.model.JsonBody.json; + +class AzureReposProviderTest extends BaseCmdTest { + + @ParameterizedTest + @EnumSource(OutputType.class) + void testAdd(OutputType format, MockServerClient mock) { + + mock.when( + request() + .withMethod("POST") + .withPath("/credentials") + .withBody(json( + "{\"credentials\":{\"keys\":{\"username\":\"user@example.com\",\"token\":\"azure_repos_token\"},\"name\":\"azurerepos\",\"provider\":\"azurerepos\"}}")), + exactly(1)).respond( + response().withStatusCode(200).withBody("{\"credentialsId\":\"1cz5A8cuBkB5iJliCwJCFU\"}") + .withContentType(MediaType.APPLICATION_JSON)); + + ExecOut out = exec(format, mock, "credentials", "add", "azurerepos", "-n", "azurerepos", "-u", + "user@example.com", "-t", "azure_repos_token"); + assertOutput(format, out, + new CredentialsAdded("AZUREREPOS", "1cz5A8cuBkB5iJliCwJCFU", "azurerepos", USER_WORKSPACE_NAME)); + } + +} \ No newline at end of file