From 0839b62b846e836f5a743b3b23c51e3b5232e83d Mon Sep 17 00:00:00 2001 From: Manuel Ganter Date: Tue, 2 Sep 2025 16:16:34 +0200 Subject: [PATCH] feat(client): IPCEICIS-5755 introduced api calls against edge conenct --- internal/client/client.go | 196 ++++++++++++++------------------------ internal/client/models.go | 50 ++++++++++ lala/lala.go | 104 +++++++++++++++----- 3 files changed, 201 insertions(+), 149 deletions(-) create mode 100644 internal/client/models.go diff --git a/internal/client/client.go b/internal/client/client.go index 4e4ae9e..60b000a 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -7,17 +7,27 @@ import ( "fmt" "io" "net/http" - "os" ) type EdgeConnect struct { - BaseURL string - HttpClient *http.Client + BaseURL string + HttpClient *http.Client + Credentials Credentials +} + +type Credentials struct { + Username string + Password string } // 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 { + token, err := e.RetrieveToken(ctx) + if err != nil { + return err + } + json_data, err := json.Marshal(input) if err != nil { return err @@ -28,7 +38,7 @@ func (e *EdgeConnect) NewAppInstance(ctx context.Context, input NewAppInstanceIn return err } request.Header.Set("Content-Type", "application/json") - request.Header.Set("Authorization", "Bearer "+os.Getenv("EDGEXR_TOKEN")) + request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) resp, err := e.HttpClient.Do(request) if err != nil { @@ -47,8 +57,12 @@ func (e *EdgeConnect) NewAppInstance(ctx context.Context, input NewAppInstanceIn return nil } -// curl -X POST https://mc.orca.platform.mg3.mdb.osc.live/api/v1/auth/ctrl/CreateApp -H 'Content-Type: application/json' -H "Authorization: Bearer $EDGEXR_TOKEN" -S --data "$CREATEAPP_JSON" --fail-with-body func (e *EdgeConnect) NewApp(ctx context.Context, input NewAppInput) error { + token, err := e.RetrieveToken(ctx) + if err != nil { + return err + } + json_data, err := json.Marshal(input) if err != nil { return err @@ -59,7 +73,7 @@ func (e *EdgeConnect) NewApp(ctx context.Context, input NewAppInput) error { return err } request.Header.Set("Content-Type", "application/json") - request.Header.Set("Authorization", "Bearer "+os.Getenv("EDGEXR_TOKEN")) + request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) resp, err := e.HttpClient.Do(request) if err != nil { @@ -78,59 +92,59 @@ func (e *EdgeConnect) NewApp(ctx context.Context, input NewAppInput) error { return nil } -type NewAppInstanceInput struct { - Region string `json:"region"` - AppInst AppInstance `json:"appinst"` +func (e *EdgeConnect) ShowApp(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() + var response struct { + Data App `json:"data"` + } + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + return App{}, err + } + + return response.Data, nil } -type AppInstance struct { - Key AppInstanceKey `json:"key"` - AppKey AppKey `json:"app_key"` - Flavor Flavor `json:"flavor"` -} - -type AppInstanceKey struct { - Organization string `json:"organization"` - Name string `json:"name"` - CloudletKey CloudletKey `json:"cloudlet_key"` -} - -type CloudletKey struct { - Organization string `json:"organization"` - Name string `json:"name"` -} - -type AppKey struct { - Organization string `json:"organization"` - Name string `json:"name"` - Version string `json:"version"` -} - -type Flavor struct { - Name string `json:"name"` -} - -type NewAppInput struct { - Region string `json:"region"` - App App `json:"app"` -} - -type App struct { - Key AppKey `json:"key"` - Deployment string `json:"deployment"` - ImageType string `json:"image_type"` - ImagePath string `json:"image_path"` - AllowServerless bool `json:"allow_serverless"` - DefaultFlavor Flavor `json:"defaultFlavor"` - ServerlessConfig any `json:"serverless_config"` - DeploymentGenerator string `json:"deployment_generator"` - DeploymentManifest string `json:"deployment_manifest"` -} - -func (e *EdgeConnect) RetrieveToken(ctx context.Context, username, password string) (string, error) { +func (e *EdgeConnect) RetrieveToken(ctx context.Context) (string, error) { json_data, err := json.Marshal(map[string]string{ - "username": username, - "password": password, + "username": e.Credentials.Username, + "password": e.Credentials.Password, }) if err != nil { return "", err @@ -149,81 +163,13 @@ func (e *EdgeConnect) RetrieveToken(ctx context.Context, username, password stri defer resp.Body.Close() - fmt.Printf("Header: %v\n", request.Header) - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - fmt.Printf("Response: %v\n", string(bodyBytes)) - var respData struct { Token string `json:"token"` } - err = json.Unmarshal(bodyBytes, &respData) + err = json.NewDecoder(resp.Body).Decode(&respData) if err != nil { return "", err } return respData.Token, nil } - -// EDGEXR_TOKEN="$(curl -X POST https://mc.orca.platform.mg3.mdb.osc.live/api/v1/login -H 'Content-Type: application/json' --data '{"password": "'${EDGEXR_PLATFORM_PASSWORD}'","username": "'${EDGEXR_PLATFORM_USERNAME}'"}' -sSf | jq -r .token)" - -// CREATEAPP_JSON=$(cat <