Compare commits
15 Commits
debug/chec
...
main
Author | SHA1 | Date | |
---|---|---|---|
88c88cac5b | |||
1ae38c98ad | |||
2d517cc594 | |||
a9c82bd261 | |||
79aec86f5f | |||
9b3dfc7576 | |||
037ae74782 | |||
b81c60a3ce | |||
363ac94c47 | |||
378f9e5095 | |||
659b494ee4 | |||
92965c6af2 | |||
70cb5aec9f | |||
d59e77d5a2 | |||
ff1b857ab0 |
@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
@ -29,28 +28,29 @@ type PeerCache struct {
|
||||
}
|
||||
|
||||
// urlFormat formats the URL of the peer with the data type API function
|
||||
func (p *PeerCache) urlFormat(url string, dt tools.DataType) string {
|
||||
func (p *PeerCache) urlFormat(hostUrl string, dt tools.DataType) string {
|
||||
// localhost is replaced by the local peer URL
|
||||
// because localhost must collide on a web request security protocol
|
||||
localhost := ""
|
||||
if strings.Contains(url, "localhost") {
|
||||
/*localhost := ""
|
||||
if strings.Contains(hostUrl, "localhost") {
|
||||
localhost = "localhost"
|
||||
}
|
||||
if strings.Contains(url, "127.0.0.1") {
|
||||
if strings.Contains(hostUrl, "127.0.0.1") {
|
||||
localhost = "127.0.0.1"
|
||||
}
|
||||
if localhost != "" {
|
||||
r := regexp.MustCompile("(" + localhost + ":[0-9]+)")
|
||||
t := r.FindString(url)
|
||||
t := r.FindString(hostUrl)
|
||||
if t != "" {
|
||||
url = strings.Replace(url, t, dt.API()+":8080/oc", -1)
|
||||
hostUrl = strings.Replace(hostUrl, t, dt.API()+":8080/oc", -1)
|
||||
} else {
|
||||
url = strings.ReplaceAll(url, localhost, dt.API()+":8080/oc")
|
||||
hostUrl = strings.ReplaceAll(hostUrl, localhost, dt.API()+":8080/oc")
|
||||
}
|
||||
} else {
|
||||
url = dt.API() + "/" + url
|
||||
}
|
||||
return url
|
||||
} else {*/
|
||||
hostUrl = hostUrl + "/" + strings.ReplaceAll(dt.API(), "oc-", "")
|
||||
//}
|
||||
fmt.Println("Contacting", hostUrl)
|
||||
return hostUrl
|
||||
}
|
||||
|
||||
// checkPeerStatus checks the status of a peer
|
||||
@ -65,22 +65,20 @@ func (p *PeerCache) checkPeerStatus(peerID string, appName string) (*Peer, bool)
|
||||
state, services := api.CheckRemotePeer(url)
|
||||
res.(*Peer).ServicesState = services // Update the services states of the peer
|
||||
access.UpdateOne(res, peerID) // Update the peer in the db
|
||||
fmt.Println("URL peer : ", url)
|
||||
fmt.Println("State : ", state)
|
||||
fmt.Println("Services : ", services)
|
||||
return res.(*Peer), state != tools.DEAD && services[appName] == 0 // Return the peer and its status
|
||||
}
|
||||
|
||||
// LaunchPeerExecution launches an execution on a peer
|
||||
// The method contacts the path described by : peer.Url + datatype path (from enums) + replacement of id by dataID
|
||||
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
||||
dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
||||
fmt.Println("Launching peer execution on", caller.URLS, dt, method)
|
||||
methods := caller.URLS[dt] // Get the methods url of the data type
|
||||
if m, ok := methods[method]; !ok || m == "" {
|
||||
return nil, errors.New("no path found")
|
||||
return nil, errors.New("Requested method " + method.String() + " not declared in HTTPCaller")
|
||||
}
|
||||
meth := methods[method] // Get the method url to execute
|
||||
meth = strings.ReplaceAll(meth, ":id", dataID) // Replace the id in the url in case of a DELETE / UPDATE method (it's a standard naming in OC)
|
||||
path := methods[method] // Get the path corresponding to the action we want to execute
|
||||
path = strings.ReplaceAll(path, ":id", dataID) // Replace the id in the path in case of a DELETE / UPDATE method (it's a standard naming in OC)
|
||||
url := ""
|
||||
|
||||
// Check the status of the peer
|
||||
@ -88,7 +86,7 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
||||
// If the peer is not reachable, add the execution to the failed executions list
|
||||
pexec := &PeerExecution{
|
||||
Method: method.String(),
|
||||
Url: p.urlFormat((mypeer.Url)+meth, dt),
|
||||
Url: p.urlFormat((mypeer.Url), dt) + path, // the url is constitued of : host URL + resource path + action path (ex : mypeer.com/datacenter/resourcetype/path/to/action)
|
||||
Body: body,
|
||||
DataType: dt.EnumIndex(),
|
||||
DataID: dataID,
|
||||
@ -101,7 +99,7 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
||||
return nil, errors.New("peer not found")
|
||||
}
|
||||
// If the peer is reachable, launch the execution
|
||||
url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL
|
||||
url = p.urlFormat((mypeer.Url), dt) + path // Format the URL
|
||||
tmp := mypeer.FailedExecution // Get the failed executions list
|
||||
mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list
|
||||
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
|
||||
|
@ -39,7 +39,7 @@ var ADMIRALTY_SOURCEAPI = DATACENTERAPI+"/admiralty/source"
|
||||
var ADMIRALTY_TARGETAPI = DATACENTERAPI+"/admiralty/target"
|
||||
var ADMIRALTY_SECRETAPI = DATACENTERAPI+"/admiralty/secret"
|
||||
var ADMIRALTY_KUBECONFIGAPI = DATACENTERAPI+"/admiralty/kubeconfig"
|
||||
var ADMIRALTY_NODESAPI = DATACENTERAPI+"/admiralty/nodes"
|
||||
var ADMIRALTY_NODESAPI = DATACENTERAPI+"/admiralty/node"
|
||||
|
||||
// Bind the standard API name to the data type
|
||||
var DefaultAPI = [...]string{
|
||||
|
@ -3,6 +3,7 @@ package tools
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -49,8 +50,9 @@ func ToMethod(str string) METHOD {
|
||||
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
|
||||
|
||||
type HTTPCaller struct {
|
||||
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
||||
Disabled bool // Disabled flag
|
||||
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
||||
Disabled bool // Disabled flag
|
||||
LastResults map[string]interface{} // Used to store information regarding the last execution of a given method on a given data type
|
||||
}
|
||||
|
||||
// NewHTTPCaller creates a new instance of the HTTP Caller
|
||||
@ -76,17 +78,33 @@ func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) (
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
}
|
||||
|
||||
// CallPut calls the DELETE method on the HTTP server
|
||||
func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error) {
|
||||
resp, err := http.NewRequest("DELETE", url+subpath, nil)
|
||||
if err != nil || resp == nil || resp.Body == nil {
|
||||
req, err := http.NewRequest("DELETE", url+subpath, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil || req == nil || req.Body == nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
}
|
||||
|
||||
// CallPost calls the POST method on the HTTP server
|
||||
@ -105,7 +123,12 @@ func (caller *HTTPCaller) CallPost(url string, subpath string, body interface{},
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
}
|
||||
|
||||
// CallPost calls the POST method on the HTTP server
|
||||
@ -123,7 +146,12 @@ func (caller *HTTPCaller) CallPut(url string, subpath string, body map[string]in
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
}
|
||||
|
||||
// CallRaw calls the Raw method on the HTTP server
|
||||
@ -143,7 +171,12 @@ func (caller *HTTPCaller) CallRaw(method string, url string, subpath string,
|
||||
req.AddCookie(c)
|
||||
}
|
||||
client := &http.Client{}
|
||||
return client.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// CallRaw calls the Raw method on the HTTP server
|
||||
@ -163,3 +196,17 @@ func (caller *HTTPCaller) CallForm(method string, url string, subpath string,
|
||||
client := &http.Client{}
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
func (caller *HTTPCaller) StoreResp(resp *http.Response) error {
|
||||
caller.LastResults = make(map[string]interface{})
|
||||
caller.LastResults["header"] = resp.Header
|
||||
caller.LastResults["code"] = resp.StatusCode
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading the body of the last request")
|
||||
return err
|
||||
}
|
||||
|
||||
caller.LastResults["body"] = data
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user