-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
110 lines (98 loc) · 3.4 KB
/
client.go
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
101
102
103
104
105
106
107
108
109
110
package remy
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/BurntSushi/toml"
)
// See http://docs.oracle.com/cd/E23943_01/web.1111/e24682/toc.htm#RESTS149
// http(s)://host:port/management/tenant-monitoring/path
const (
// MonitorPath is the REST resource path from the root / that points to where the RESTful Management API endpoint is located. As of WLS 12.1.2,
// this is assumed to be /management/tenant-monitoring
MonitorPath string = "/management/tenant-monitoring"
)
// AdminServer contains the configurable details necessary to request resources from a particular AdminServer.
// ServerUrl format should be similar to the following: "http(s)://[serverhost]:[adminport]"
type AdminServer struct {
AdminURL string
Username string
Password string
}
// Wrapper handles all responses sent back from a WLS Rest endpoint. These responses are wrapped by a similar body and item or items tag.
// We simply wrap that so we can get to the meat of it in the underlying Server type
//
// Wrapper is composed of 3 pieces:
// - Body contains
// - the result of the resource request, included as either a specific Item (Datasource, Server,
// Cluster, or Application, or
// - an array of []Item's of the same.
// - Messages contains any error-related messages related to the query.
type Wrapper struct {
Body struct {
Items json.RawMessage `json:"items,omitempty"`
Item json.RawMessage `json:"item,omitempty"`
} `json:"body"`
Messages []string `json:"messages,omitempty"`
}
// requestResource is a wrapper around an http.Client{} instance assuming the following:
// - the request is a GET
// - assumes a JSON Accept header
// - set Basic Authentication based on the *AdminServer passed in
//
// returns the *http.Response or an error
func requestResource(url string, e *AdminServer) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(e.Username, e.Password)
return client.Do(req)
}
func requestAndUnmarshal(url string, e *AdminServer) (*Wrapper, error) {
resp, err := request(url, e)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return unmarshalWrapper(data)
}
// Wrapper function for requestResource(), handling HTTP response codes before unmarshalling responses.
func request(url string, e *AdminServer) (*http.Response, error) {
resp, err := requestResource(url, e)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return resp, nil
}
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Invalid Response Code: %v\nResponse: \n%v", resp.StatusCode, string(body))
}
// Take the raw response from the server and attempt to unmarshal it into the Wrapper type.
func unmarshalWrapper(data []byte) (*Wrapper, error) {
var w Wrapper
err := json.Unmarshal(data, &w)
if err != nil {
return nil, err
}
return &w, nil
}
// EncodeConfigFile will take an AdminServer instance and convert it to a TOML-formatted byte buffer.
// This TOML byte buffer can then be serialized to disk as a configuration file.
func (a AdminServer) EncodeConfigFile() *bytes.Buffer {
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
err := enc.Encode(a)
if err != nil {
panic(fmt.Errorf("unable to encode wlsrest configuration: %s", err))
}
return &buf
}