/**
 * 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.api.model.metrics.v1beta1.NodeMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.dsl.base.OperationSupport;
import io.fabric8.kubernetes.client.utils.URLUtils;
import okhttp3.OkHttpClient;

public class NodeMetricOperationsImpl extends OperationSupport {
  private static String METRIC_ENDPOINT_URL = "apis/metrics.k8s.io/v1beta1/nodes";

  public NodeMetricOperationsImpl(OkHttpClient client, Config config) {
    super(new OperationContext().withOkhttpClient(client).withConfig(config));
  }

  public NodeMetricsList metrics() {
    try {
      String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL);
      return handleMetric(resourceUrl, NodeMetricsList.class);
    } catch(Exception e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }

  public NodeMetrics metrics(String nodeName) {
    try {
      String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL, nodeName);
      return handleMetric(resourceUrl, NodeMetrics.class);
    } catch(Exception e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }
  
  public NodeMetricsList metrics(Map<String ,Object> labelsMap) {
		try {
			
			StringBuilder sb = new StringBuilder();
			sb.append("?labelSelector=");
			for(Map.Entry<String, Object> entry : labelsMap.entrySet()) {
				sb.append(entry.getKey()).append("=").append(entry.getValue().toString()).append(",");
			}
			String paras = sb.toString().substring(0, sb.toString().length() - 1);
			
			String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL, paras);
			return handleMetric(resourceUrl, NodeMetricsList.class);
		} catch(Exception e) {
			throw KubernetesClientException.launderThrowable(e);
		}
  }
}

@@ -651,16 +651,39 @@
 ernal;%0A%0A
+import java.util.Map;%0A%0A
 import i
@@ -1194,18 +1194,17 @@
 pport %7B%0A
-  
+%09
 private 
@@ -1277,18 +1277,17 @@
 odes%22;%0A%0A
-  
+%09
 public N
@@ -1348,20 +1348,18 @@
 nfig) %7B%0A
-    
+%09%09
 super(ne
@@ -1425,23 +1425,21 @@
 nfig));%0A
-  %7D%0A%0A  
+%09%7D%0A%0A%09
 public N
@@ -1465,32 +1465,27 @@
 ics() %7B%0A
-    
+%09%09
 try %7B%0A
-      
+%09%09%09
 String r
@@ -1556,22 +1556,19 @@
 T_URL);%0A
-      
+%09%09%09
 return h
@@ -1608,36 +1608,34 @@
 icsList.class);%0A
-    
+%09%09
 %7D catch(Exceptio
@@ -1633,38 +1633,35 @@
 (Exception e) %7B%0A
-      
+%09%09%09
 throw Kubernetes
@@ -1701,21 +1701,17 @@
 e);%0A
-    %7D%0A  %7D%0A%0A  
+%09%09%7D%0A%09%7D%0A%0A%09
 publ
@@ -1756,24 +1756,19 @@
 ) %7B%0A
-    
+%09%09
 try %7B%0A
-      
+%09%09%09
 Stri
@@ -1853,22 +1853,19 @@
 eName);%0A
-      
+%09%09%09
 return h
@@ -1909,20 +1909,18 @@
 class);%0A
-    
+%09%09
 %7D catch(
@@ -1934,22 +1934,19 @@
 on e) %7B%0A
-      
+%09%09%09
 throw Ku
@@ -1994,23 +1994,17 @@
 e);%0A
-    %7D%0A  %7D%0A  %0A  
+%09%09%7D%0A%09%7D%0A%0A%09
 publ
@@ -2629,10 +2629,10 @@
 %09%09%7D%0A
-  %7D
+%09%7D%0A
 %0A%7D%0A

add filter node metrics via labels