/
AbstractBucketLifecycleManager.java
175 lines (161 loc) · 6.67 KB
/
AbstractBucketLifecycleManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2013 Google Inc. All Rights Reserved.
*
* 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 com.google.jenkins.plugins.storage;
import java.io.IOException;
import javax.annotation.Nullable;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.Bucket;
import com.google.common.collect.ImmutableList;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.util.ConflictException;
import com.google.jenkins.plugins.util.Executor;
import com.google.jenkins.plugins.util.ExecutorException;
import com.google.jenkins.plugins.util.NotFoundException;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.TaskListener;
/**
* This extension point may be implemented to surface the object lifecycle
* options available on cloud storage buckets. Generally the expectation is
* that the UI will ask for the bucket, and surface some additional UI for
* capturing the lifecycle features of the plugin.
*
* This is done by implementing these two overrides:
* <ul>
* <li><code>checkBucket</code>: Validated the annotations on a pre-existing
* bucket, returning it if they are satisfactory, and throwing a
* InvalidAnnotationException if we must update it.
* <li><code>decorateBucket</code>: Annotates either a new or existing bucket
* with the lifecycle features of the plugin.
* </ul>
*
* NOTE: This extends {@link AbstractUpload}, but isn't really an upload. You
* could reason about it as an empty upload to a bucket with special bucket
* annotation properties.
*
* TODO(mattmoor): We should factor out a common AbstractStorageOperation base
* class that this and AbstractUpload can share. The current entrypoint is
* benign enough (see "perform").
*
* @see com.google.jenkins.plugins.storage.ExpiringBucketLifecycleManager
*/
public abstract class AbstractBucketLifecycleManager extends AbstractUpload {
/**
* Constructs the base bucket OLM plugin from the bucket name and module.
*
* NOTE: this hides the "make public" and "for failed" options of the
* base, which are mainly relevant for true uploads.
*/
public AbstractBucketLifecycleManager(String bucket,
@Nullable UploadModule module) {
super(bucket, false /*sharedPublicly*/,
false /*forFailedJobs*/, module);
}
/**
* {@inheritDoc}
*/
@Override
@Nullable
protected final UploadSpec getInclusions(AbstractBuild<?, ?> build,
FilePath workspace, TaskListener listener) throws UploadException {
// Return an empty list, we don't actually do any uploads.
return new UploadSpec(workspace, ImmutableList.<FilePath>of());
}
/**
* This overrides the core implementation to provide additional hooks for
* decorating storage objects with lifecycle annotations.
*
* @param credentials The credentials with which to fetch/create the bucket
* @param bucketName The top-level bucket name to ensure exists
* @return an instance of the named bucket, created or retrieved.
* @throws UploadException if any issues are encountered
*/
@Override
protected Bucket getOrCreateBucket(Storage service,
GoogleRobotCredentials credentials, Executor executor, String bucketName)
throws UploadException {
try {
try {
try {
return checkBucket(executor.execute(service.buckets()
.get(bucketName)
.setProjection("full"))); // to retrieve the bucket ACLs
} catch (NotFoundException e) {
try {
// This is roughly the opposite of how the command-line sample does
// things. We do things this way to optimize for the case where the
// bucket already exists.
Bucket bucket = new Bucket().setName(bucketName);
// Annotate the bucket with our lifecycle properties.
bucket = decorateBucket(bucket);
bucket = executor.execute(service.buckets()
.insert(credentials.getProjectId(), bucket)
.setProjection("full")); // to retrieve the bucket ACLs
return bucket;
} catch (ConflictException ex) {
// If we get back a "Conflict" response, it means that the bucket
// was inserted between when we first tried to get it and were able
// to successfully insert one.
// NOTE: This could be due to an initial insertion attempt
// succeeding but returning an exception, or a race with another
// service.
return checkBucket(executor.execute(service.buckets()
.get(bucketName)
.setProjection("full"))); // to retrieve the bucket ACLs
}
}
} catch (InvalidAnnotationException nae) {
Bucket bucket = nae.getBucket();
bucket = decorateBucket(bucket);
// If it exists, but isn't annotated, then update it with the annotated
// version.
return executor.execute(service.buckets()
.update(bucketName, bucket)
.setProjection("full"));
}
} catch (ExecutorException e) {
throw new UploadException(
Messages.AbstractUpload_ExceptionGetBucket(bucketName), e);
} catch (IOException e) {
throw new UploadException(
Messages.AbstractUpload_ExceptionGetBucket(bucketName), e);
}
}
/**
* This is intended to be an identity function that throws when the input
* is not adequately annotated.
*
* @param bucket the pre-existing bucket whose annotations to validate
* @throws InvalidAnnotationException if not annotated properly
*/
protected abstract Bucket checkBucket(Bucket bucket)
throws InvalidAnnotationException;
/**
* A hook by which extensions may annotate a new or existing bucket.
*
* @param bucket the bucket to annotate and return
*/
protected abstract Bucket decorateBucket(Bucket bucket);
/**
* Boilerplate, see:
* https://wiki.jenkins-ci.org/display/JENKINS/Defining+a+new+extension+point
*/
public AbstractBucketLifecycleManagerDescriptor getDescriptor() {
return (AbstractBucketLifecycleManagerDescriptor)
Hudson.getInstance().getDescriptor(getClass());
}
}