2025-09-02 15:23:19 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2025-09-05 14:37:34 +02:00
|
|
|
"io"
|
2025-09-05 11:24:01 +02:00
|
|
|
|
2025-09-04 11:19:07 +02:00
|
|
|
"log"
|
2025-09-02 15:23:19 +02:00
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type EdgeConnect struct {
|
2025-09-02 16:16:34 +02:00
|
|
|
BaseURL string
|
|
|
|
|
HttpClient *http.Client
|
|
|
|
|
Credentials Credentials
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Credentials struct {
|
|
|
|
|
Username string
|
|
|
|
|
Password string
|
2025-09-02 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// curl -X POST https://mc.orca.platform.mg3.mdb.osc.live/api/v1/auth/ctrl/CreateAppInst -H 'Content-Type: application/json' -H "Authorization: Bearer $EDGEXR_TOKEN" -S --data "$CREATEAPPINSTANCE_JSON" --fail-with-body
|
|
|
|
|
|
|
|
|
|
func (e *EdgeConnect) NewAppInstance(ctx context.Context, input NewAppInstanceInput) error {
|
2025-09-02 16:16:34 +02:00
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
2025-09-05 11:45:04 +02:00
|
|
|
log.Printf("failed to retrieve token %v\n", err)
|
2025-09-02 16:16:34 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 15:23:19 +02:00
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
2025-09-05 11:45:04 +02:00
|
|
|
log.Printf("failed to marshal NewAppInstanceInput %v\n", err)
|
2025-09-02 15:23:19 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/CreateAppInst", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
2025-09-05 11:45:04 +02:00
|
|
|
log.Printf("failed to create request %v\n", err)
|
2025-09-02 15:23:19 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
2025-09-02 16:16:34 +02:00
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
2025-09-02 15:23:19 +02:00
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
2025-09-05 11:45:04 +02:00
|
|
|
log.Printf("failed to execute request %v\n", err)
|
2025-09-02 15:23:19 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2025-09-05 14:37:34 +02:00
|
|
|
bytes, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("err while io.ReadAll: %v\n", err)
|
|
|
|
|
return err
|
2025-09-02 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 14:37:34 +02:00
|
|
|
log.Printf("Body %v\n", string(bytes))
|
|
|
|
|
|
|
|
|
|
log.Printf("status code %v\n", resp.Status)
|
|
|
|
|
|
2025-09-02 15:23:19 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *EdgeConnect) NewApp(ctx context.Context, input NewAppInput) error {
|
2025-09-02 16:16:34 +02:00
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
2025-09-05 14:37:34 +02:00
|
|
|
log.Printf("err while RetrieveToken: %v\n", err)
|
2025-09-02 16:16:34 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 15:23:19 +02:00
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
2025-09-05 14:37:34 +02:00
|
|
|
log.Printf("err while Marshal: %v\n", err)
|
2025-09-02 15:23:19 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/CreateApp", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
2025-09-05 14:37:34 +02:00
|
|
|
log.Printf("err while NewRequestWithContext: %v\n", err)
|
2025-09-02 15:23:19 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
2025-09-02 16:16:34 +02:00
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
2025-09-02 15:23:19 +02:00
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
2025-09-05 14:37:34 +02:00
|
|
|
log.Printf("err while HttpClient.Do: %v\n", err)
|
2025-09-02 15:23:19 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2025-09-05 14:37:34 +02:00
|
|
|
|
2025-09-02 15:23:19 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2025-09-05 14:37:34 +02:00
|
|
|
log.Printf("status code %v\n", resp.Status)
|
|
|
|
|
bytes, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("err while io.ReadAll: %v\n", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("Body %v\n", string(bytes))
|
|
|
|
|
|
2025-09-02 15:23:19 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
func (e *EdgeConnect) ShowApp(ctx context.Context, appkey AppKey, region string) (App, error) {
|
|
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return App{}, err
|
|
|
|
|
}
|
2025-09-02 15:23:19 +02:00
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
input := struct {
|
|
|
|
|
App struct {
|
|
|
|
|
Key AppKey `json:"key"`
|
|
|
|
|
} `json:"App"`
|
|
|
|
|
Region string `json:"Region"`
|
|
|
|
|
}{
|
|
|
|
|
App: struct {
|
|
|
|
|
Key AppKey `json:"key"`
|
|
|
|
|
}{
|
|
|
|
|
Key: appkey,
|
|
|
|
|
},
|
|
|
|
|
Region: region,
|
|
|
|
|
}
|
2025-09-02 15:23:19 +02:00
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return App{}, err
|
|
|
|
|
}
|
2025-09-02 15:23:19 +02:00
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/ShowApp", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return App{}, err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
2025-09-02 15:23:19 +02:00
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return App{}, err
|
|
|
|
|
}
|
2025-09-02 15:23:19 +02:00
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
var response struct {
|
|
|
|
|
Data App `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return App{}, err
|
|
|
|
|
}
|
2025-09-02 15:23:19 +02:00
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
return response.Data, nil
|
2025-09-02 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-04 11:19:07 +02:00
|
|
|
func (e *EdgeConnect) ShowApps(ctx context.Context, appkey AppKey, region string) ([]App, error) {
|
|
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []App{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := struct {
|
|
|
|
|
App struct {
|
|
|
|
|
Key AppKey `json:"key"`
|
|
|
|
|
} `json:"App"`
|
|
|
|
|
Region string `json:"Region"`
|
|
|
|
|
}{
|
|
|
|
|
App: struct {
|
|
|
|
|
Key AppKey `json:"key"`
|
|
|
|
|
}{
|
|
|
|
|
Key: appkey,
|
|
|
|
|
},
|
|
|
|
|
Region: region,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []App{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/ShowApp", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []App{}, err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []App{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
type response struct {
|
|
|
|
|
Data App `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apps := []App{}
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
for {
|
|
|
|
|
var d response
|
|
|
|
|
if err := decoder.Decode(&d); err != nil {
|
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
apps = append(apps, d.Data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apps, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 13:36:57 +02:00
|
|
|
func (e *EdgeConnect) ShowAppInstance(ctx context.Context, appinstkey AppInstanceKey, region string) (AppInstance, error) {
|
|
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := struct {
|
|
|
|
|
App struct {
|
|
|
|
|
AppInstKey AppInstanceKey `json:"key"`
|
|
|
|
|
} `json:"appinst"`
|
|
|
|
|
Region string `json:"Region"`
|
|
|
|
|
}{
|
|
|
|
|
App: struct {
|
|
|
|
|
AppInstKey AppInstanceKey `json:"key"`
|
|
|
|
|
}{
|
|
|
|
|
AppInstKey: appinstkey,
|
|
|
|
|
},
|
|
|
|
|
Region: region,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/ShowAppInst", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
var response struct {
|
|
|
|
|
Data AppInstance `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.Data, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 11:47:52 +02:00
|
|
|
func (e *EdgeConnect) ShowAppInstances(ctx context.Context, appinstkey AppInstanceKey, region string) ([]AppInstance, error) {
|
|
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := struct {
|
|
|
|
|
App struct {
|
|
|
|
|
AppInstKey AppInstanceKey `json:"key"`
|
|
|
|
|
} `json:"appinst"`
|
|
|
|
|
Region string `json:"Region"`
|
|
|
|
|
}{
|
|
|
|
|
App: struct {
|
|
|
|
|
AppInstKey AppInstanceKey `json:"key"`
|
|
|
|
|
}{
|
|
|
|
|
AppInstKey: appinstkey,
|
|
|
|
|
},
|
|
|
|
|
Region: region,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/ShowAppInst", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []AppInstance{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
type response struct {
|
|
|
|
|
Data AppInstance `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appinstances := []AppInstance{}
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
for {
|
|
|
|
|
var d response
|
|
|
|
|
if err := decoder.Decode(&d); err != nil {
|
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
appinstances = append(appinstances, d.Data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return appinstances, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 16:16:34 +02:00
|
|
|
func (e *EdgeConnect) RetrieveToken(ctx context.Context) (string, error) {
|
2025-09-02 15:23:19 +02:00
|
|
|
json_data, err := json.Marshal(map[string]string{
|
2025-09-02 16:16:34 +02:00
|
|
|
"username": e.Credentials.Username,
|
|
|
|
|
"password": e.Credentials.Password,
|
2025-09-02 15:23:19 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/login", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
var respData struct {
|
|
|
|
|
Token string `json:"token"`
|
|
|
|
|
}
|
2025-09-02 16:16:34 +02:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&respData)
|
2025-09-02 15:23:19 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return respData.Token, nil
|
|
|
|
|
}
|
2025-09-03 11:09:11 +02:00
|
|
|
|
|
|
|
|
func (e *EdgeConnect) DeleteApp(ctx context.Context, appkey AppKey, region string) error {
|
|
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := struct {
|
|
|
|
|
App struct {
|
|
|
|
|
Key AppKey `json:"key"`
|
|
|
|
|
} `json:"App"`
|
|
|
|
|
Region string `json:"Region"`
|
|
|
|
|
}{
|
|
|
|
|
App: struct {
|
|
|
|
|
Key AppKey `json:"key"`
|
|
|
|
|
}{
|
|
|
|
|
Key: appkey,
|
|
|
|
|
},
|
|
|
|
|
Region: region,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/DeleteApp", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2025-09-05 11:24:01 +02:00
|
|
|
/*bodyBytes, err := io.ReadAll(resp.Body)
|
2025-09-03 11:09:11 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-09-05 11:24:01 +02:00
|
|
|
fmt.Printf("Response: %v\n", string(bodyBytes))*/
|
2025-09-03 11:09:11 +02:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 13:36:57 +02:00
|
|
|
func (e *EdgeConnect) DeleteAppInstance(ctx context.Context, appinstancekey AppInstanceKey, region string) error {
|
2025-09-03 11:09:11 +02:00
|
|
|
token, err := e.RetrieveToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := struct {
|
|
|
|
|
App struct {
|
|
|
|
|
AppInstKey AppInstanceKey `json:"key"`
|
|
|
|
|
} `json:"appinst"`
|
|
|
|
|
Region string `json:"Region"`
|
|
|
|
|
}{
|
|
|
|
|
App: struct {
|
|
|
|
|
AppInstKey AppInstanceKey `json:"key"`
|
|
|
|
|
}{
|
|
|
|
|
AppInstKey: appinstancekey,
|
|
|
|
|
},
|
|
|
|
|
Region: region,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_data, err := json.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/DeleteAppInst", bytes.NewBuffer(json_data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-09-04 10:30:52 +02:00
|
|
|
|
2025-09-03 11:09:11 +02:00
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
|
|
|
|
|
|
resp, err := e.HttpClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2025-09-05 11:24:01 +02:00
|
|
|
/*bodyBytes, err := io.ReadAll(resp.Body)
|
2025-09-03 11:09:11 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-09-05 11:24:01 +02:00
|
|
|
fmt.Printf("Response: %v%v\n", resp.StatusCode, string(bodyBytes))*/
|
2025-09-03 11:09:11 +02:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|