This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_client.go
More file actions
100 lines (89 loc) · 3.07 KB
/
api_client.go
File metadata and controls
100 lines (89 loc) · 3.07 KB
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
/*
Copyright 2019 IBM Corporation
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 main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
// function to calculate component status.
type calculateComponentStatusFunc func(destUrl string, resInfo *resourceInfo) (status string, flyover string, flyOverNLS string, retErr error)
// http client to API srever
var client = &http.Client{}
/* Call API server to calculate component status */
func calculateComponentStatus(destURL string, resInfo *resourceInfo) (status string, flyover string, flyoverNLS string, retErr error) {
status = ""
flyover = ""
flyoverNLS = ""
query := url.QueryEscape(resInfo.namespace)
apiVersion := resInfo.apiVersion
if !strings.Contains(apiVersion, "/") {
apiVersion = "/" + apiVersion
if logger.IsEnabled(LogTypeDebug) {
logger.Log(CallerName(), LogTypeDebug, fmt.Sprintf("Using apiVersion: %s instead of %s", apiVersion, resInfo.apiVersion))
}
}
urlPath := destURL + "/kappnav/status/" + resInfo.name + "/" + resInfo.kind + "?namespace=" + query + "&apiversion=" + url.QueryEscape(apiVersion)
if logger.IsEnabled(LogTypeDebug) {
logger.Log(CallerName(), LogTypeDebug, fmt.Sprintf("urlPath: %s", urlPath))
}
resp, retErr := client.Get(urlPath)
if retErr != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", "", "", fmt.Errorf("Failed. status: %s urlPath: %s", resp.Status, urlPath)
}
var result interface{}
retErr = json.NewDecoder(resp.Body).Decode(&result)
if retErr != nil {
return
}
if logger.IsEnabled(LogTypeDebug) {
logger.Log(CallerName(), LogTypeDebug, fmt.Sprintf("type is: %T\n", result))
}
switch result.(type) {
case map[string]interface{}:
resultMap := result.(map[string]interface{})
var tmp interface{}
var ok bool
tmp, ok = resultMap["value"]
if ok {
status = tmp.(string)
}
tmp, ok = resultMap["flyover"]
if ok {
flyover = tmp.(string)
}
tmp, ok = resultMap["flyover.nls"]
if ok {
bytes, err := json.Marshal(tmp)
if err != nil {
if logger.IsEnabled(LogTypeError) {
logger.Log(CallerName(), LogTypeError, fmt.Sprintf("Unable to marshal flyover.nls: %s", tmp))
}
} else {
flyoverNLS = string(bytes)
}
}
default:
retErr = fmt.Errorf("Failed: don't know how to process returned object of type %T", result)
}
if logger.IsEnabled(LogTypeExit) {
logger.Log(CallerName(), LogTypeExit, fmt.Sprintf("url: %s, kind: %s, namespace: %s, name: %s: status: %s, flyover: %s, flyovernLS: %s, err: %s", destURL, resInfo.kind, resInfo.namespace, resInfo.name, status, flyover, flyoverNLS, retErr))
}
return
}