| 1 |
package cs |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/tls" |
| 6 |
"crypto/x509" |
| 7 |
"encoding/json" |
| 8 |
"io" |
| 9 |
"io/ioutil" |
| 10 |
"log" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"time" |
| 14 |
|
| 15 |
"strings" |
| 16 |
|
| 17 |
"github.com/denverdino/aliyungo/common" |
| 18 |
"github.com/denverdino/aliyungo/util" |
| 19 |
) |
| 20 |
|
| 21 |
type ProjectClient struct { |
| 22 |
clusterId string |
| 23 |
endpoint string |
| 24 |
debug bool |
| 25 |
userAgent string |
| 26 |
httpClient *http.Client |
| 27 |
} |
| 28 |
|
| 29 |
func NewProjectClient(clusterId, endpoint string, clusterCerts ClusterCerts) (client *ProjectClient, err error) { |
| 30 |
|
| 31 |
certs, err := tls.X509KeyPair([]byte(clusterCerts.Cert), []byte(clusterCerts.Key)) |
| 32 |
|
| 33 |
if err != nil { |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
caCertPool := x509.NewCertPool() |
| 38 |
caCertPool.AppendCertsFromPEM([]byte(clusterCerts.CA)) |
| 39 |
|
| 40 |
httpClient := &http.Client{ |
| 41 |
Transport: &http.Transport{ |
| 42 |
TLSClientConfig: &tls.Config{ |
| 43 |
InsecureSkipVerify: true, |
| 44 |
Certificates: []tls.Certificate{certs}, |
| 45 |
ClientCAs: caCertPool, |
| 46 |
ClientAuth: tls.RequireAndVerifyClientCert, |
| 47 |
}, |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
client = &ProjectClient{ |
| 52 |
clusterId: clusterId, |
| 53 |
endpoint: endpoint, |
| 54 |
httpClient: httpClient, |
| 55 |
} |
| 56 |
|
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
func (client *ProjectClient) SetDebug(debug bool) { |
| 62 |
client.debug = debug |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (client *ProjectClient) SetUserAgent(userAgent string) { |
| 67 |
client.userAgent = userAgent |
| 68 |
} |
| 69 |
|
| 70 |
func (client *ProjectClient) ClusterId() string { |
| 71 |
return client.clusterId |
| 72 |
} |
| 73 |
|
| 74 |
func (client *ProjectClient) Endpoint() string { |
| 75 |
return client.endpoint |
| 76 |
} |
| 77 |
|
| 78 |
func (client *ProjectClient) Invoke(method string, path string, query url.Values, args interface{}, response interface{}) error { |
| 79 |
var reqBody []byte |
| 80 |
var err error |
| 81 |
var contentType string |
| 82 |
|
| 83 |
if args != nil { |
| 84 |
reqBody, err = json.Marshal(args) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
contentType = "application/json" |
| 89 |
} |
| 90 |
|
| 91 |
requestURL := client.endpoint + path |
| 92 |
if query != nil && len(query) > 0 { |
| 93 |
requestURL = requestURL + "?" + util.Encode(query) |
| 94 |
} |
| 95 |
var bodyReader io.Reader |
| 96 |
if reqBody != nil { |
| 97 |
bodyReader = bytes.NewReader(reqBody) |
| 98 |
} |
| 99 |
|
| 100 |
httpReq, err := http.NewRequest(method, requestURL, bodyReader) |
| 101 |
if err != nil { |
| 102 |
return common.GetClientError(err) |
| 103 |
} |
| 104 |
|
| 105 |
httpReq.Header.Set("Date", util.GetGMTime()) |
| 106 |
httpReq.Header.Set("Accept", "application/json") |
| 107 |
|
| 108 |
if contentType != "" { |
| 109 |
httpReq.Header.Set("Content-Type", contentType) |
| 110 |
} |
| 111 |
|
| 112 |
if client.userAgent != "" { |
| 113 |
httpReq.Header.Set("User-Agent", client.userAgent) |
| 114 |
} |
| 115 |
|
| 116 |
t0 := time.Now() |
| 117 |
httpResp, err := client.httpClient.Do(httpReq) |
| 118 |
t1 := time.Now() |
| 119 |
if err != nil { |
| 120 |
return common.GetClientError(err) |
| 121 |
} |
| 122 |
statusCode := httpResp.StatusCode |
| 123 |
|
| 124 |
if client.debug { |
| 125 |
log.Printf("Invoke %s %s %d (%v)", method, requestURL, statusCode, t1.Sub(t0)) |
| 126 |
} |
| 127 |
|
| 128 |
defer httpResp.Body.Close() |
| 129 |
body, err := ioutil.ReadAll(httpResp.Body) |
| 130 |
|
| 131 |
if err != nil { |
| 132 |
return common.GetClientError(err) |
| 133 |
} |
| 134 |
|
| 135 |
if client.debug { |
| 136 |
var prettyJSON bytes.Buffer |
| 137 |
err = json.Indent(&prettyJSON, body, "", " ") |
| 138 |
log.Println(string(prettyJSON.Bytes())) |
| 139 |
} |
| 140 |
|
| 141 |
if statusCode >= 400 && statusCode <= 599 { |
| 142 |
errorResponse := common.ErrorResponse{} |
| 143 |
err = json.Unmarshal(body, &errorResponse) |
| 144 |
ecsError := &common.Error{ |
| 145 |
ErrorResponse: errorResponse, |
| 146 |
StatusCode: statusCode, |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if len(strings.TrimSpace(ecsError.Message)) <= 0 { |
| 151 |
ecsError.Message = strings.Trim(string(body[:]), "\n") |
| 152 |
} |
| 153 |
return ecsError |
| 154 |
} |
| 155 |
|
| 156 |
if response != nil { |
| 157 |
err = json.Unmarshal(body, response) |
| 158 |
if err != nil { |
| 159 |
return common.GetClientError(err) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return nil |
| 164 |
} |