rohankanojia

Untitled

Jul 30th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. /**
  2.  * Copyright (C) 2015 Red Hat, Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *         http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package io.fabric8.openshift.client.dsl.internal;
  17.  
  18. import io.fabric8.kubernetes.client.dsl.base.OperationContext;
  19. import io.fabric8.kubernetes.client.utils.URLUtils;
  20. import io.fabric8.openshift.client.DefaultOpenShiftClient;
  21. import io.fabric8.openshift.client.OpenShiftClient;
  22. import io.fabric8.openshift.client.OpenshiftAdapterSupport;
  23. import okhttp3.OkHttpClient;
  24. import okhttp3.Request;
  25. import io.fabric8.kubernetes.api.builder.Function;
  26. import io.fabric8.kubernetes.api.model.Status;
  27. import io.fabric8.kubernetes.client.KubernetesClientException;
  28. import io.fabric8.kubernetes.client.dsl.base.OperationSupport;
  29. import io.fabric8.openshift.api.model.DoneableProjectRequest;
  30. import io.fabric8.openshift.api.model.ProjectRequest;
  31. import io.fabric8.openshift.client.OpenShiftConfig;
  32. import io.fabric8.openshift.client.dsl.ProjectRequestOperation;
  33.  
  34. import java.io.IOException;
  35. import java.net.MalformedURLException;
  36. import java.net.URL;
  37. import java.util.concurrent.ExecutionException;
  38.  
  39. import static io.fabric8.openshift.client.OpenShiftAPIGroups.PROJECT;
  40.  
  41. public class ProjectRequestsOperationImpl extends OperationSupport implements ProjectRequestOperation {
  42.  
  43.   public ProjectRequestsOperationImpl(OkHttpClient client, OpenShiftConfig config) {
  44.     this(new OperationContext().withOkhttpClient(client).withConfig(config));
  45.   }
  46.  
  47.   public ProjectRequestsOperationImpl(OperationContext context) {
  48.     super(context.withApiGroupName(PROJECT)
  49.       .withPlural("projectrequests"));
  50.   }
  51.  
  52.   @Override
  53.   public boolean isResourceNamespaced() {
  54.     return false;
  55.   }
  56.  
  57.   @Override
  58.   public URL getRootUrl() {
  59.     OpenShiftConfig config = OpenShiftConfig.wrap(context.getConfig());
  60.     OpenShiftClient oc = new DefaultOpenShiftClient(context.getClient(), config);
  61.     if (config.isOpenShiftAPIGroups(oc)) {
  62.       return super.getRootUrl();
  63.     } else {
  64.       try {
  65.         return new URL(OpenShiftConfig.wrap(getConfig()).getOpenShiftUrl());
  66.       } catch (MalformedURLException e) {
  67.         throw KubernetesClientException.launderThrowable(e);
  68.       }
  69.     }
  70.   }
  71.  
  72.   private ProjectRequest updateApiVersion(ProjectRequest p) {
  73.     if (p.getApiVersion() == null) {
  74.       p.setApiVersion(this.apiGroupVersion);
  75.     }
  76.     return p;
  77.   }
  78.  
  79.   @Override
  80.   public ProjectRequest create(ProjectRequest... resources) {
  81.     try {
  82.       if (resources.length > 1) {
  83.         throw new IllegalArgumentException("Too many items to create.");
  84.       } else if (resources.length == 1) {
  85.         return handleCreate(updateApiVersion(resources[0]), ProjectRequest.class);
  86.       } else if (getItem() == null) {
  87.         throw new IllegalArgumentException("Nothing to create.");
  88.       } else {
  89.         return handleCreate(updateApiVersion(getItem()), ProjectRequest.class);
  90.       }
  91.     } catch (InterruptedException | ExecutionException | IOException e) {
  92.       throw KubernetesClientException.launderThrowable(e);
  93.     }
  94.   }
  95.  
  96.   @Override
  97.   public DoneableProjectRequest createNew() {
  98.     return new DoneableProjectRequest(item -> {
  99.       try {
  100.         return create(item);
  101.       } catch (Exception e) {
  102.         throw KubernetesClientException.launderThrowable(e);
  103.       }
  104.     });
  105.   }
  106.  
  107.   @Override
  108.   public Status list() {
  109.     try {
  110.       URL requestUrl = getNamespacedUrl();
  111.       Request.Builder requestBuilder = new Request.Builder().get().url(requestUrl);
  112.       return handleResponse(requestBuilder, Status.class);
  113.     } catch (InterruptedException | ExecutionException | IOException e) {
  114.       throw KubernetesClientException.launderThrowable(e);
  115.     }
  116.   }
  117.  
  118.   @Override
  119.   public Status list(Integer limitVal, String continueVal) {
  120.     try {
  121.       URL requestUrl = getNamespacedUrl();
  122.       if(limitVal != null) {
  123.         requestUrl = new URL(URLUtils.join(requestUrl.toString(), "?limit=" + limitVal.toString()));
  124.       } else if(continueVal != null) {
  125.         requestUrl = new URL(URLUtils.join(requestUrl.toString(), "?limit=" + limitVal.toString() + "&continue=" + continueVal));
  126.       }
  127.       Request.Builder requestBuilder = new Request.Builder().get().url(requestUrl);
  128.       return handleResponse(requestBuilder, Status.class);
  129.     } catch (InterruptedException | ExecutionException | IOException e) {
  130.       throw KubernetesClientException.launderThrowable(e);
  131.     }
  132.  
  133.   }
  134.  
  135.   public ProjectRequest getItem() {
  136.     return (ProjectRequest) context.getItem();
  137.   }
  138. }
Add Comment
Please, Sign In to add comment