garm-provider-edge-connect/internal/client/client.go
Christopher Hase ebd16d7c50
Some checks failed
Go Tests / go-tests (push) Failing after 59s
feat(Client): fix DeleteInstance
2025-09-08 14:18:15 +02:00

303 lines
6.8 KiB
Go

package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
var ErrNotFound = fmt.Errorf("Not found!")
type EdgeConnect struct {
BaseURL string
HttpClient *http.Client
Credentials Credentials
}
type Credentials struct {
Username string
Password string
}
func (e *EdgeConnect) RetrieveToken(ctx context.Context) (string, error) {
json_data, err := json.Marshal(map[string]string{
"username": e.Credentials.Username,
"password": e.Credentials.Password,
})
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"`
}
err = json.NewDecoder(resp.Body).Decode(&respData)
if err != nil {
return "", err
}
return respData.Token, nil
}
func (e *EdgeConnect) CreateApp(ctx context.Context, input NewAppInput) error {
json_data, err := json.Marshal(input)
if err != nil {
return err
}
response, err := call[App](ctx, e, "/api/v1/auth/ctrl/CreateApp", json_data)
if err != nil {
return err
}
return response.Error()
}
func (e *EdgeConnect) ShowApp(ctx context.Context, appkey AppKey, region string) (App, error) {
input := struct {
App App `json:"App"`
Region string `json:"Region"`
}{
App: App{Key: appkey},
Region: region,
}
json_data, err := json.Marshal(input)
if err != nil {
return App{}, err
}
responses, err := call[App](ctx, e, "/api/v1/auth/ctrl/ShowApp", json_data)
if err != nil {
return App{}, err
}
if responses.StatusCode == http.StatusNotFound {
return App{}, fmt.Errorf("Error retrieving App: %w", ErrNotFound)
}
if !responses.IsSuccessful() {
return App{}, responses.Error()
}
apps := responses.GetData()
if len(apps) > 0 {
return apps[0], nil
}
return App{}, fmt.Errorf("could not find app with region/key: %s/%v", region, appkey)
}
func (e *EdgeConnect) ShowApps(ctx context.Context, appkey AppKey, region string) ([]App, error) {
input := struct {
App App `json:"App"`
Region string `json:"Region"`
}{
App: App{Key: appkey},
Region: region,
}
json_data, err := json.Marshal(input)
if err != nil {
return nil, err
}
responses, err := call[App](ctx, e, "/api/v1/auth/ctrl/ShowApp", json_data)
if err != nil {
return nil, err
}
if !responses.IsSuccessful() && responses.StatusCode != http.StatusNotFound {
return nil, responses.Error()
}
return responses.GetData(), nil
}
func (e *EdgeConnect) DeleteApp(ctx context.Context, appkey AppKey, region string) error {
input := struct {
App App `json:"App"`
Region string `json:"Region"`
}{
App: App{Key: appkey},
Region: region,
}
json_data, err := json.Marshal(input)
if err != nil {
return err
}
response, err := call[App](ctx, e, "/api/v1/auth/ctrl/DeleteApp", json_data)
if err != nil {
return err
}
if !response.IsSuccessful() && response.StatusCode != 404 {
return response.Error()
}
return nil
}
func (e *EdgeConnect) CreateAppInstance(ctx context.Context, input NewAppInstanceInput) error {
json_data, err := json.Marshal(input)
if err != nil {
log.Printf("failed to marshal NewAppInstanceInput %v\n", err)
return err
}
responses, err := call[AppInstance](ctx, e, "/api/v1/auth/ctrl/CreateAppInst", json_data)
if err != nil {
return err
}
return responses.Error()
}
func (e *EdgeConnect) ShowAppInstance(ctx context.Context, appinstkey AppInstanceKey, region string) (AppInstance, error) {
input := struct {
App AppInstance `json:"appinst"`
Region string `json:"Region"`
}{
App: AppInstance{Key: appinstkey},
Region: region,
}
json_data, err := json.Marshal(input)
if err != nil {
return AppInstance{}, err
}
responses, err := call[AppInstance](ctx, e, "/api/v1/auth/ctrl/ShowAppInst", json_data)
if err != nil {
return AppInstance{}, err
}
if responses.StatusCode == http.StatusNotFound {
return AppInstance{}, fmt.Errorf("Error retrieving AppInstance: %w", ErrNotFound)
}
if !responses.IsSuccessful() {
return AppInstance{}, responses.Error()
}
data := responses.GetData()
if len(data) > 0 {
return data[0], nil
}
return AppInstance{}, fmt.Errorf("could not find app: %v", responses)
}
func (e *EdgeConnect) ShowAppInstances(ctx context.Context, appinstkey AppInstanceKey, region string) ([]AppInstance, error) {
input := struct {
App AppInstance `json:"appinst"`
Region string `json:"Region"`
}{
App: AppInstance{Key: appinstkey},
Region: region,
}
json_data, err := json.Marshal(input)
if err != nil {
return nil, err
}
responses, err := call[AppInstance](ctx, e, "/api/v1/auth/ctrl/ShowAppInst", json_data)
if err != nil {
return nil, err
}
if !responses.IsSuccessful() && responses.StatusCode != http.StatusNotFound {
return nil, responses.Error()
}
return responses.GetData(), nil
}
func (e *EdgeConnect) DeleteAppInstance(ctx context.Context, appinstancekey AppInstanceKey, region string) error {
input := struct {
AppInstance AppInstance `json:"appinst"`
Region string `json:"Region"`
}{
AppInstance: AppInstance{Key: appinstancekey},
Region: region,
}
json_data, err := json.Marshal(input)
if err != nil {
return err
}
responses, err := call[AppInstance](ctx, e, "/api/v1/auth/ctrl/DeleteAppInst", json_data)
if err != nil {
return err
}
return responses.Error()
}
func call[T any](ctx context.Context, client *EdgeConnect, path string, body []byte) (Responses[T], error) {
log.Printf("call(): trying %s\n", path)
token, err := client.RetrieveToken(ctx)
if err != nil {
return Responses[T]{}, err
}
request, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s", client.BaseURL, path), bytes.NewBuffer(body))
if err != nil {
return Responses[T]{}, err
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := client.HttpClient.Do(request)
if err != nil {
return Responses[T]{}, err
}
defer resp.Body.Close()
responses := Responses[T]{}
responses.StatusCode = resp.StatusCode
if resp.StatusCode == http.StatusNotFound {
log.Printf("Error in call %s: %v", path, ErrNotFound)
return responses, nil
}
decoder := json.NewDecoder(resp.Body)
for {
var d Response[T]
if err := decoder.Decode(&d); err != nil {
if err.Error() == "EOF" {
break
}
log.Printf("Error in call %s: %v", path, err)
return Responses[T]{}, fmt.Errorf("Error in call %s: %w", path, err)
}
responses.Responses = append(responses.Responses, d)
}
log.Printf("call(): %s resulting in %v and %v messages\n", path, resp.StatusCode, len(responses.Responses))
return responses, nil
}