1
1
package io .kubernetes .client ;
2
2
3
- import io .kubernetes .client .ApiException ;
4
- import io .kubernetes .client .Configuration ;
5
- import io .kubernetes .client .models .V1ObjectMeta ;
6
- import io .kubernetes .client .proto .Meta .Status ;
7
- import io .kubernetes .client .proto .Runtime .TypeMeta ;
8
- import io .kubernetes .client .proto .Runtime .Unknown ;
3
+ import java .io .IOException ;
4
+ import java .io .InputStream ;
5
+ import java .util .ArrayList ;
6
+ import java .util .Arrays ;
7
+ import java .util .HashMap ;
9
8
10
9
import com .google .common .io .ByteStreams ;
11
10
import com .google .common .primitives .Bytes ;
12
- import com .google .protobuf .Descriptors ;
13
11
import com .google .protobuf .Message ;
14
12
import com .squareup .okhttp .MediaType ;
15
13
import com .squareup .okhttp .Request ;
16
14
import com .squareup .okhttp .RequestBody ;
17
15
import com .squareup .okhttp .Response ;
18
- import com .squareup .okhttp .ResponseBody ;
19
- import okio .ByteString ;
20
16
21
- import java .io .IOException ;
22
- import java .io .InputStream ;
23
- import java .util .Arrays ;
24
- import java .util .ArrayList ;
25
- import java .util .HashMap ;
17
+ import io .kubernetes .client .proto .Meta .DeleteOptions ;
18
+ import io .kubernetes .client .proto .Meta .Status ;
19
+ import io .kubernetes .client .proto .Runtime .TypeMeta ;
20
+ import io .kubernetes .client .proto .Runtime .Unknown ;
26
21
27
22
public class ProtoClient {
28
23
/**
@@ -139,8 +134,42 @@ public <T extends Message> ObjectOrStatus<T> update(T obj, String path, String a
139
134
* @param path The path to call in the API server
140
135
* @return The response status
141
136
*/
142
- public <T extends Message > Status delete (T .Builder builder , String path ) throws ApiException , IOException {
143
- return request (builder , path , "DELETE" , null , null , null ).status ;
137
+ public <T extends Message > ObjectOrStatus <T > delete (T .Builder builder , String path ) throws ApiException , IOException {
138
+ return request (builder , path , "DELETE" , null , null , null );
139
+ }
140
+
141
+ /**
142
+ * Delete a kubernetes API object using protocol buffer encoding.
143
+ * @param builder The builder for the response
144
+ * @param path The path to call in the API server
145
+ * @param deleteOptions optional deleteOptions
146
+ * @return The response status
147
+ */
148
+ public <T extends Message > ObjectOrStatus <T > delete (T .Builder builder , String path ,DeleteOptions deleteOptions ) throws ApiException , IOException {
149
+ if (deleteOptions == null ) {
150
+ return delete (builder ,path );
151
+ }
152
+ else {
153
+ HashMap <String , String > headers = new HashMap <String , String >();
154
+ headers .put ("Content-Type" , MEDIA_TYPE );
155
+ headers .put ("Accept" , MEDIA_TYPE );
156
+ Request request = apiClient .buildRequest (path , "DELETE" , new ArrayList <Pair >(), new ArrayList <Pair >(), null ,
157
+ headers , new HashMap <String , Object >(), new String [0 ], null );
158
+ byte [] bytes = encode (deleteOptions , "v1" , "DeleteOptions" );
159
+ request = request .newBuilder ().delete (RequestBody .create (MediaType .parse (MEDIA_TYPE ), bytes )).build ();
160
+ Response resp = apiClient .getHttpClient ().newCall (request ).execute ();
161
+ Unknown u = parse (resp .body ().byteStream ());
162
+ resp .body ().close ();
163
+
164
+ if (u .getTypeMeta ().getApiVersion ().equals ("v1" ) &&
165
+ u .getTypeMeta ().getKind ().equals ("Status" )) {
166
+ Status status = Status .newBuilder ().mergeFrom (u .getRaw ()).build ();
167
+ return new ObjectOrStatus (null , status );
168
+ }
169
+
170
+ return new ObjectOrStatus ((T ) builder .mergeFrom (u .getRaw ()).build (), null );
171
+ }
172
+
144
173
}
145
174
146
175
/**
@@ -157,13 +186,20 @@ public <T extends Message> Status delete(T.Builder builder, String path) throws
157
186
public <T extends Message > ObjectOrStatus <T > request (T .Builder builder , String path , String method , T body , String apiVersion ,
158
187
String kind ) throws ApiException , IOException {
159
188
HashMap <String , String > headers = new HashMap <String , String >();
160
- headers .put ("Content-type " , MEDIA_TYPE );
189
+ headers .put ("Content-Type " , MEDIA_TYPE );
161
190
headers .put ("Accept" , MEDIA_TYPE );
162
191
Request request = apiClient .buildRequest (path , method , new ArrayList <Pair >(), new ArrayList <Pair >(), null ,
163
192
headers , new HashMap <String , Object >(), new String [0 ], null );
164
193
if (body != null ) {
165
194
byte [] bytes = encode (body , apiVersion , kind );
166
- request = request .newBuilder ().post (RequestBody .create (MediaType .parse (MEDIA_TYPE ), bytes )).build ();
195
+ if ("POST" .equals (method ))
196
+ request = request .newBuilder ().post (RequestBody .create (MediaType .parse (MEDIA_TYPE ), bytes )).build ();
197
+ else if ("PUT" .equals (method ))
198
+ request = request .newBuilder ().put (RequestBody .create (MediaType .parse (MEDIA_TYPE ), bytes )).build ();
199
+ else if ("PATCH" .equals (method ))
200
+ request = request .newBuilder ().patch (RequestBody .create (MediaType .parse (MEDIA_TYPE ), bytes )).build ();
201
+ else
202
+ throw new ApiException ("Unknown proto client API method: " +method );
167
203
}
168
204
Response resp = apiClient .getHttpClient ().newCall (request ).execute ();
169
205
Unknown u = parse (resp .body ().byteStream ());
@@ -203,4 +239,4 @@ private Unknown parse(InputStream stream) throws ApiException, IOException {
203
239
}
204
240
return Unknown .parseFrom (stream );
205
241
}
206
- }
242
+ }
0 commit comments