From 31d33834ee9f4b603a13203bf3a3c84fe2f9fb56 Mon Sep 17 00:00:00 2001 From: Daisuke Miyamoto Date: Thu, 16 Feb 2017 16:10:36 +0900 Subject: [PATCH] Add Amazon ECR plugin --- build.gradle | 1 + deploy/jp.classmethod.aws.gradle | 4 ++ .../ecr/AmazonECRCreateRepositoryTask.java | 56 +++++++++++++++++ .../ecr/AmazonECRDeleteRepositoryTask.java | 56 +++++++++++++++++ .../AmazonECRGetAuthorizationTokenTask.java | 62 +++++++++++++++++++ .../aws/gradle/ecr/AmazonECRPlugin.java | 29 +++++++++ .../gradle/ecr/AmazonECRPluginExtension.java | 39 ++++++++++++ .../jp.classmethod.aws.ecr.properties | 17 +++++ 8 files changed, 264 insertions(+) create mode 100644 src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRCreateRepositoryTask.java create mode 100644 src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRDeleteRepositoryTask.java create mode 100644 src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRGetAuthorizationTokenTask.java create mode 100644 src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPlugin.java create mode 100644 src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPluginExtension.java create mode 100644 src/main/resources/META-INF/gradle-plugins/jp.classmethod.aws.ecr.properties diff --git a/build.gradle b/build.gradle index b4d2ae2a..7f8776fd 100644 --- a/build.gradle +++ b/build.gradle @@ -234,6 +234,7 @@ dependencies { compile "com.amazonaws:aws-java-sdk-iam:$awsJavaSdkVersion" compile "com.amazonaws:aws-java-sdk-sqs:$awsJavaSdkVersion" compile "com.amazonaws:aws-java-sdk-sns:$awsJavaSdkVersion" + compile "com.amazonaws:aws-java-sdk-ecr:$awsJavaSdkVersion" // tests testCompile "junit:junit:$junitVersion" diff --git a/deploy/jp.classmethod.aws.gradle b/deploy/jp.classmethod.aws.gradle index 5fc3406a..12c45b16 100644 --- a/deploy/jp.classmethod.aws.gradle +++ b/deploy/jp.classmethod.aws.gradle @@ -93,6 +93,10 @@ pluginBundle { id = 'jp.classmethod.aws.cloudformation' displayName = 'Gradle AWS CloudFormation plugin' } + awsECRPlugin { + id = 'jp.classmethod.aws.ecr' + displayName = 'Gradle Amazon ECR plugin' + } awsEC2Plugin { id = 'jp.classmethod.aws.ec2' displayName = 'Gradle Amazon EC2 plugin' diff --git a/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRCreateRepositoryTask.java b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRCreateRepositoryTask.java new file mode 100644 index 00000000..c4f952fc --- /dev/null +++ b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRCreateRepositoryTask.java @@ -0,0 +1,56 @@ +/* + * Copyright 2015-2016 the original author or authors. + * + * 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 jp.classmethod.aws.gradle.ecr; + +import lombok.Getter; +import lombok.Setter; + +import org.gradle.api.internal.ConventionTask; +import org.gradle.api.tasks.TaskAction; + +import com.amazonaws.services.ecr.AmazonECR; +import com.amazonaws.services.ecr.model.CreateRepositoryRequest; +import com.amazonaws.services.ecr.model.CreateRepositoryResult; +import com.amazonaws.services.ecr.model.Repository; +import com.google.common.base.MoreObjects; + +public class AmazonECRCreateRepositoryTask extends ConventionTask { + + @Getter + @Setter + private String repositoryName; + + @Getter + private Repository repository; + + + public AmazonECRCreateRepositoryTask() { + setDescription("Create ECR repository"); + setGroup("AWS"); + } + + @TaskAction + public void createRepository() { + AmazonECRPluginExtension ext = getProject().getExtensions().getByType(AmazonECRPluginExtension.class); + AmazonECR ecr = ext.getClient(); + + String repositoryName = MoreObjects.firstNonNull(getRepositoryName(), ext.getRepositoryName()); + + CreateRepositoryResult result = ecr.createRepository(new CreateRepositoryRequest() + .withRepositoryName(repositoryName)); + repository = result.getRepository(); + } +} diff --git a/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRDeleteRepositoryTask.java b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRDeleteRepositoryTask.java new file mode 100644 index 00000000..eb63dd08 --- /dev/null +++ b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRDeleteRepositoryTask.java @@ -0,0 +1,56 @@ +/* + * Copyright 2015-2016 the original author or authors. + * + * 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 jp.classmethod.aws.gradle.ecr; + +import lombok.Getter; +import lombok.Setter; + +import org.gradle.api.internal.ConventionTask; +import org.gradle.api.tasks.TaskAction; + +import com.amazonaws.services.ecr.AmazonECR; +import com.amazonaws.services.ecr.model.DeleteRepositoryRequest; +import com.amazonaws.services.ecr.model.DeleteRepositoryResult; +import com.amazonaws.services.ecr.model.Repository; +import com.google.common.base.MoreObjects; + +public class AmazonECRDeleteRepositoryTask extends ConventionTask { + + @Getter + @Setter + private String repositoryName; + + @Getter + private Repository repository; + + + public AmazonECRDeleteRepositoryTask() { + setDescription("Delete ECR repository"); + setGroup("AWS"); + } + + @TaskAction + public void createRepository() { + AmazonECRPluginExtension ext = getProject().getExtensions().getByType(AmazonECRPluginExtension.class); + AmazonECR ecr = ext.getClient(); + + String repositoryName = MoreObjects.firstNonNull(getRepositoryName(), ext.getRepositoryName()); + + DeleteRepositoryResult result = ecr.deleteRepository(new DeleteRepositoryRequest() + .withRepositoryName(repositoryName)); + repository = result.getRepository(); + } +} diff --git a/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRGetAuthorizationTokenTask.java b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRGetAuthorizationTokenTask.java new file mode 100644 index 00000000..df8622f3 --- /dev/null +++ b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRGetAuthorizationTokenTask.java @@ -0,0 +1,62 @@ +/* + * Copyright 2015-2016 the original author or authors. + * + * 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 jp.classmethod.aws.gradle.ecr; + +import java.util.List; + +import lombok.Getter; +import lombok.Setter; + +import org.gradle.api.GradleException; +import org.gradle.api.internal.ConventionTask; +import org.gradle.api.tasks.TaskAction; + +import com.amazonaws.services.ecr.AmazonECR; +import com.amazonaws.services.ecr.model.AuthorizationData; +import com.amazonaws.services.ecr.model.GetAuthorizationTokenRequest; +import com.amazonaws.services.ecr.model.GetAuthorizationTokenResult; + +public class AmazonECRGetAuthorizationTokenTask extends ConventionTask { + + @Getter + @Setter + private List repositoryIds; + + @Getter + private List authorizationData; + + + public AmazonECRGetAuthorizationTokenTask() { + setDescription("Get authorization token for ECR repository"); + setGroup("AWS"); + } + + @TaskAction + public void createRepository() { + List repositoryIds = getRepositoryIds(); + + if (repositoryIds == null || repositoryIds.isEmpty()) { + throw new GradleException("Must specify ECR repositoryIds"); + } + + AmazonECRPluginExtension ext = getProject().getExtensions().getByType(AmazonECRPluginExtension.class); + AmazonECR ecr = ext.getClient(); + + GetAuthorizationTokenResult result = ecr.getAuthorizationToken(new GetAuthorizationTokenRequest() + .withRegistryIds(repositoryIds)); + authorizationData = result.getAuthorizationData(); + } +} diff --git a/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPlugin.java b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPlugin.java new file mode 100644 index 00000000..9c649e02 --- /dev/null +++ b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPlugin.java @@ -0,0 +1,29 @@ +/* + * Copyright 2015-2016 the original author or authors. + * + * 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 jp.classmethod.aws.gradle.ecr; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; + +import jp.classmethod.aws.gradle.AwsPlugin; + +public class AmazonECRPlugin implements Plugin { + + public void apply(Project project) { + project.getPluginManager().apply(AwsPlugin.class); + project.getExtensions().create(AmazonECRPluginExtension.NAME, AmazonECRPluginExtension.class, project); + } +} diff --git a/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPluginExtension.java b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPluginExtension.java new file mode 100644 index 00000000..dec66e21 --- /dev/null +++ b/src/main/java/jp/classmethod/aws/gradle/ecr/AmazonECRPluginExtension.java @@ -0,0 +1,39 @@ +/* + * Copyright 2015-2016 the original author or authors. + * + * 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 jp.classmethod.aws.gradle.ecr; + +import lombok.Getter; +import lombok.Setter; + +import org.gradle.api.Project; + +import com.amazonaws.services.ecr.AmazonECRClient; + +import jp.classmethod.aws.gradle.common.BaseRegionAwarePluginExtension; + +public class AmazonECRPluginExtension extends BaseRegionAwarePluginExtension { + + public static final String NAME = "ecr"; + + @Getter + @Setter + private String repositoryName; + + + public AmazonECRPluginExtension(Project project) { + super(project, AmazonECRClient.class); + } +} diff --git a/src/main/resources/META-INF/gradle-plugins/jp.classmethod.aws.ecr.properties b/src/main/resources/META-INF/gradle-plugins/jp.classmethod.aws.ecr.properties new file mode 100644 index 00000000..4d9a7ee1 --- /dev/null +++ b/src/main/resources/META-INF/gradle-plugins/jp.classmethod.aws.ecr.properties @@ -0,0 +1,17 @@ +# +# Copyright 2013-2016 Classmethod, Inc. +# +# 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. +# + +implementation-class=jp.classmethod.aws.gradle.ecr.AmazonECRPlugin