diff --git a/internal/client/client.go b/internal/client/client.go index d87357c..ddf5d9e 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -250,6 +250,64 @@ func (e *EdgeConnect) ShowAppInstance(ctx context.Context, appinstkey AppInstanc return response.Data, nil } +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 +} + func (e *EdgeConnect) RetrieveToken(ctx context.Context) (string, error) { json_data, err := json.Marshal(map[string]string{ "username": e.Credentials.Username, diff --git a/provider/provider.go b/provider/provider.go index 1b2dfd4..a293318 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -20,6 +20,7 @@ import ( "fmt" "net/http" "os" + "strings" "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/config" "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/internal/client" @@ -251,7 +252,46 @@ func (a *edgeConnectProvider) GetInstance(ctx context.Context, instance string) // ListInstances will list all instances for a provider. func (a *edgeConnectProvider) ListInstances(ctx context.Context, poolID string) ([]params.ProviderInstance, error) { - return nil, nil + apps, err := a.client.ShowAppInstances(ctx, client.AppInstanceKey{ + Organization: a.cfg.Organization, + }, a.cfg.Region) + if err != nil { + return nil, err + } + + myappintances := filter(apps, func(app client.AppInstance) bool { return strings.HasPrefix(app.Key.Name, poolID) }) + + providerinstances := []params.ProviderInstance{} + + for _, v := range myappintances { + providerInstance := params.ProviderInstance{ + ProviderID: a.controllerID, + Name: v.Key.Name, + OSType: params.Linux, + OSArch: params.Amd64, + OSName: "lala", + OSVersion: "lalatest", + Status: params.InstanceStatusUnknown, + } + + if v.State == "Ready" { + providerInstance.Status = params.InstanceRunning + } + + providerinstances = append(providerinstances, providerInstance) + } + + return providerinstances, nil +} + +func filter[T any](s []T, predicate func(T) bool) []T { + result := make([]T, 0, len(s)) // Pre-allocate for efficiency + for _, v := range s { + if predicate(v) { + result = append(result, v) + } + } + return result } // RemoveAllInstances will remove all instances created by this provider.