feat(Client): implemented idempotent DeleteApp, Error Handling
Some checks failed
Go Tests / go-tests (push) Failing after 1m1s

This commit is contained in:
Christopher Hase 2025-09-08 10:41:31 +02:00
parent ec1abae2d6
commit a98c76ba84
3 changed files with 75 additions and 52 deletions

View file

@ -10,6 +10,8 @@ import (
"net/http" "net/http"
) )
var ErrNotFound = fmt.Errorf("Not found!")
type EdgeConnect struct { type EdgeConnect struct {
BaseURL string BaseURL string
HttpClient *http.Client HttpClient *http.Client
@ -87,6 +89,10 @@ func (e *EdgeConnect) ShowApp(ctx context.Context, appkey AppKey, region string)
return App{}, err return App{}, err
} }
if responses.StatusCode == http.StatusNotFound {
return App{}, fmt.Errorf("Error retrieving App: %w", ErrNotFound)
}
if !responses.IsSuccessful() { if !responses.IsSuccessful() {
return App{}, responses.Error() return App{}, responses.Error()
} }
@ -185,6 +191,10 @@ func (e *EdgeConnect) ShowAppInstance(ctx context.Context, appinstkey AppInstanc
return AppInstance{}, err return AppInstance{}, err
} }
if responses.StatusCode == http.StatusNotFound {
return AppInstance{}, fmt.Errorf("Error retrieving AppInstance: %w", ErrNotFound)
}
if !responses.IsSuccessful() { if !responses.IsSuccessful() {
return AppInstance{}, responses.Error() return AppInstance{}, responses.Error()
} }

View file

@ -143,7 +143,7 @@ func main() {
fmt.Printf("appinst: %v\n", appinst) fmt.Printf("appinst: %v\n", appinst)
fmt.Printf("Error: %v\n", err)*/ fmt.Printf("Error: %v\n", err)*/
edgeprovider, _ := provider.NewEdgeConnectProvider("/home/chris/ipcei/projects/garm-provider-edge-connect/config/config.toml", "lalacontroller") edgeprovider, _, _ := provider.NewEdgeConnectProvider("/home/chris/ipcei/projects/garm-provider-edge-connect/config/config.toml", "lalacontroller")
//providerinst, err := edgeprovider.GetInstance(ctx, appinst.Key.Name) //providerinst, err := edgeprovider.GetInstance(ctx, appinst.Key.Name)
/*_, err := edgeprovider.CreateInstance(ctx, params.BootstrapInstance{ /*_, err := edgeprovider.CreateInstance(ctx, params.BootstrapInstance{

View file

@ -17,6 +17,7 @@ package provider
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -135,51 +136,71 @@ func (a *edgeConnectProvider) CreateInstance(ctx context.Context, bootstrapParam
manifest := fmt.Sprintf("%s\n---\n%s", string(podjson), string(servicejson)) manifest := fmt.Sprintf("%s\n---\n%s", string(podjson), string(servicejson))
err = a.client.CreateApp(ctx, client.NewAppInput{ _, err = a.client.ShowApp(ctx, client.AppKey{
Region: a.cfg.Region, Organization: a.cfg.Organization,
App: client.App{ Name: instancename,
Key: client.AppKey{ Version: "0.0.1",
Organization: a.cfg.Organization, }, a.cfg.Region)
Name: instancename, if err != nil && !errors.As(err, client.ErrNotFound) {
Version: "0.0.1",
},
Deployment: "kubernetes",
ImageType: "Docker",
ImagePath: "edp.buildth.ing/devfw-cicd/nginx",
AllowServerless: true,
ServerlessConfig: struct{}{},
DefaultFlavor: client.Flavor{
Name: "EU.small",
},
DeploymentGenerator: "kubernetes-basic",
DeploymentManifest: manifest,
},
})
if err != nil {
return params.ProviderInstance{}, err return params.ProviderInstance{}, err
} }
if errors.As(err, client.ErrNotFound) {
err = a.client.CreateApp(ctx, client.NewAppInput{
Region: a.cfg.Region,
App: client.App{
Key: client.AppKey{
Organization: a.cfg.Organization,
Name: instancename,
Version: "0.0.1",
},
Deployment: "kubernetes",
ImageType: "Docker",
ImagePath: "edp.buildth.ing/devfw-cicd/nginx",
AllowServerless: true,
ServerlessConfig: struct{}{},
DefaultFlavor: client.Flavor{
Name: "EU.small",
},
DeploymentGenerator: "kubernetes-basic",
DeploymentManifest: manifest,
},
})
if err != nil {
return params.ProviderInstance{}, err
}
}
err = a.client.CreateAppInstance(ctx, client.NewAppInstanceInput{ _, err = a.client.ShowAppInstance(ctx, client.AppInstanceKey{
Region: a.cfg.Region, Organization: a.cfg.Organization,
AppInst: client.AppInstance{ Name: instancename,
Key: client.AppInstanceKey{ CloudletKey: client.CloudletKey(a.cfg.CloudletKey),
Organization: a.cfg.Organization, }, a.cfg.Region)
Name: instancename, if err != nil && !errors.As(err, client.ErrNotFound) {
CloudletKey: client.CloudletKey(a.cfg.CloudletKey),
},
AppKey: client.AppKey{
Organization: a.cfg.Organization,
Name: instancename,
Version: "0.0.1",
},
Flavor: client.Flavor{
Name: "EU.small",
},
},
})
if err != nil {
return params.ProviderInstance{}, err return params.ProviderInstance{}, err
} }
if errors.As(err, client.ErrNotFound) {
err = a.client.CreateAppInstance(ctx, client.NewAppInstanceInput{
Region: a.cfg.Region,
AppInst: client.AppInstance{
Key: client.AppInstanceKey{
Organization: a.cfg.Organization,
Name: instancename,
CloudletKey: client.CloudletKey(a.cfg.CloudletKey),
},
AppKey: client.AppKey{
Organization: a.cfg.Organization,
Name: instancename,
Version: "0.0.1",
},
Flavor: client.Flavor{
Name: "EU.small",
},
},
})
if err != nil {
return params.ProviderInstance{}, err
}
}
instance := params.ProviderInstance{ instance := params.ProviderInstance{
ProviderID: a.controllerID, ProviderID: a.controllerID,
@ -202,6 +223,7 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin
Organization: a.cfg.Organization, Organization: a.cfg.Organization,
}, a.cfg.Region) }, a.cfg.Region)
if err != nil { if err != nil {
log.Printf("Error in method DeleteInstance() ShowAppInstances")
return err return err
} }
@ -220,6 +242,7 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin
Organization: a.cfg.Organization, Organization: a.cfg.Organization,
}, a.cfg.Region) }, a.cfg.Region)
if err != nil { if err != nil {
log.Printf("Error in method DeleteInstance() ShowApps")
return err return err
} }
@ -230,21 +253,11 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin
for _, v := range myapps { for _, v := range myapps {
err = a.client.DeleteApp(ctx, v.Key, a.cfg.Region) err = a.client.DeleteApp(ctx, v.Key, a.cfg.Region)
if err != nil { if err != nil {
log.Printf("Error in method DeleteInstance() DeleteApp")
return err return err
} }
} }
appkey := client.AppKey{
Organization: a.cfg.Organization,
Name: instance,
Version: "0.0.1",
}
err = a.client.DeleteApp(ctx, appkey, a.cfg.Region)
if err != nil {
return err
}
return nil return nil
} }