From a98c76ba84127af541f73b7699fc9525593dfad6 Mon Sep 17 00:00:00 2001 From: Christopher Hase Date: Mon, 8 Sep 2025 10:41:31 +0200 Subject: [PATCH] feat(Client): implemented idempotent DeleteApp, Error Handling --- internal/client/client.go | 10 ++++ lala/lala.go | 2 +- provider/provider.go | 115 +++++++++++++++++++++----------------- 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index d88c8fc..e3b445c 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -10,6 +10,8 @@ import ( "net/http" ) +var ErrNotFound = fmt.Errorf("Not found!") + type EdgeConnect struct { BaseURL string HttpClient *http.Client @@ -87,6 +89,10 @@ func (e *EdgeConnect) ShowApp(ctx context.Context, appkey AppKey, region string) return App{}, err } + if responses.StatusCode == http.StatusNotFound { + return App{}, fmt.Errorf("Error retrieving App: %w", ErrNotFound) + } + if !responses.IsSuccessful() { return App{}, responses.Error() } @@ -185,6 +191,10 @@ func (e *EdgeConnect) ShowAppInstance(ctx context.Context, appinstkey AppInstanc return AppInstance{}, err } + if responses.StatusCode == http.StatusNotFound { + return AppInstance{}, fmt.Errorf("Error retrieving AppInstance: %w", ErrNotFound) + } + if !responses.IsSuccessful() { return AppInstance{}, responses.Error() } diff --git a/lala/lala.go b/lala/lala.go index 161a232..1a90012 100644 --- a/lala/lala.go +++ b/lala/lala.go @@ -143,7 +143,7 @@ func main() { fmt.Printf("appinst: %v\n", appinst) 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) /*_, err := edgeprovider.CreateInstance(ctx, params.BootstrapInstance{ diff --git a/provider/provider.go b/provider/provider.go index 7d6f33c..b94e09c 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -17,6 +17,7 @@ package provider import ( "context" "encoding/json" + "errors" "fmt" "log" "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)) - 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 { + _, err = a.client.ShowApp(ctx, client.AppKey{ + Organization: a.cfg.Organization, + Name: instancename, + Version: "0.0.1", + }, a.cfg.Region) + if err != nil && !errors.As(err, client.ErrNotFound) { 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{ - 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 { + _, err = a.client.ShowAppInstance(ctx, client.AppInstanceKey{ + Organization: a.cfg.Organization, + Name: instancename, + CloudletKey: client.CloudletKey(a.cfg.CloudletKey), + }, a.cfg.Region) + if err != nil && !errors.As(err, client.ErrNotFound) { 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{ ProviderID: a.controllerID, @@ -202,6 +223,7 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin Organization: a.cfg.Organization, }, a.cfg.Region) if err != nil { + log.Printf("Error in method DeleteInstance() ShowAppInstances") return err } @@ -220,6 +242,7 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin Organization: a.cfg.Organization, }, a.cfg.Region) if err != nil { + log.Printf("Error in method DeleteInstance() ShowApps") return err } @@ -230,21 +253,11 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin for _, v := range myapps { err = a.client.DeleteApp(ctx, v.Key, a.cfg.Region) if err != nil { + log.Printf("Error in method DeleteInstance() DeleteApp") 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 }