-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CreateOnlyResourceOperationsImpl.java
86 lines (76 loc) · 3.19 KB
/
CreateOnlyResourceOperationsImpl.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
/**
* Copyright (C) 2015 Red Hat, 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.
*/
package io.fabric8.kubernetes.client.dsl.internal;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.CreateOnlyResourceOperations;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.dsl.base.OperationSupport;
import okhttp3.OkHttpClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
public class CreateOnlyResourceOperationsImpl<T, D> extends OperationSupport implements CreateOnlyResourceOperations<T, D> {
private Class<T> subjectAccessRequestClass;
public CreateOnlyResourceOperationsImpl(OkHttpClient client, Config config, String apiGroupName, String apiGroupVersion, String plural, Class<T> subjectAccessRequestClass) {
this(new OperationContext().withOkhttpClient(client).withConfig(config), apiGroupName, apiGroupVersion, plural, subjectAccessRequestClass);
this.subjectAccessRequestClass = subjectAccessRequestClass;
}
public CreateOnlyResourceOperationsImpl(OperationContext context, String apiGroupName, String apiGroupVersion, String plural, Class<T> subjectAccessRequestClass) {
super(context.withApiGroupName(apiGroupName)
.withApiGroupVersion(apiGroupVersion)
.withPlural(plural));
this.subjectAccessRequestClass = subjectAccessRequestClass;
}
@Override
public boolean isResourceNamespaced() {
return false;
}
@Override
public T create(T... resources) {
try {
if (resources.length > 1) {
throw new IllegalArgumentException("Too many items to create.");
} else if (resources.length == 1) {
return handleCreate(resources[0], subjectAccessRequestClass);
} else {
return handleCreate(getItem(), subjectAccessRequestClass);
}
} catch (ExecutionException | IOException e) {
throw KubernetesClientException.launderThrowable(e);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw KubernetesClientException.launderThrowable(ie);
}
}
@Override
public T create(T item) {
try {
return handleCreate(item, subjectAccessRequestClass);
} catch (ExecutionException | IOException e) {
throw KubernetesClientException.launderThrowable(e);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw KubernetesClientException.launderThrowable(ie);
}
}
@Override
public D createNew() {
throw new IllegalStateException("this operation is not supported, please use create() instead");
}
public T getItem() {
return (T) context.getItem();
}
}