feat(client): implemented ListInstances
Some checks failed
Go Tests / go-tests (push) Failing after 1m30s
Some checks failed
Go Tests / go-tests (push) Failing after 1m30s
This commit is contained in:
parent
cf88522e57
commit
90ceb69a18
2 changed files with 99 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue