diff --git a/internal/client/client.go b/internal/client/client.go new file mode 100644 index 0000000..4e4ae9e --- /dev/null +++ b/internal/client/client.go @@ -0,0 +1,229 @@ +package client + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "os" +) + +type EdgeConnect struct { + BaseURL string + HttpClient *http.Client +} + +// 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 { + json_data, err := json.Marshal(input) + if err != nil { + return err + } + + request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/CreateAppInst", bytes.NewBuffer(json_data)) + if err != nil { + return err + } + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Authorization", "Bearer "+os.Getenv("EDGEXR_TOKEN")) + + resp, err := e.HttpClient.Do(request) + if err != nil { + return err + } + + 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)) + + 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 { + json_data, err := json.Marshal(input) + if err != nil { + return err + } + + request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/CreateApp", bytes.NewBuffer(json_data)) + if err != nil { + return err + } + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Authorization", "Bearer "+os.Getenv("EDGEXR_TOKEN")) + + resp, err := e.HttpClient.Do(request) + if err != nil { + return err + } + + 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)) + + return nil +} + +type NewAppInstanceInput struct { + Region string `json:"region"` + AppInst AppInstance `json:"appinst"` +} + +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) { + json_data, err := json.Marshal(map[string]string{ + "username": username, + "password": 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() + + 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) + 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 <