Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Update sample-app wrt. Create/Update split, clean up the code
Browse files Browse the repository at this point in the history
Signed-off-by: Dennis Marttinen <dennis@weave.works>
  • Loading branch information
twelho committed Aug 21, 2020
1 parent 5beb8b8 commit ef8bafd
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions cmd/sample-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ func run(identityFile, gitURL string) error {
return echo.NewHTTPError(http.StatusBadRequest, "Please set name")
}

objKey := storage.NewObjectKey(storage.NewKindKey(carGVK), runtime.NewIdentifier("default/"+name))
obj, err := plainStorage.Get(objKey)
obj, err := plainStorage.Get(carKeyForName(name))
if err != nil {
return err
}
Expand All @@ -146,18 +145,25 @@ func run(identityFile, gitURL string) error {
return echo.NewHTTPError(http.StatusBadRequest, "Please set name")
}

obj := &v1alpha1.Car{}
obj.Name = name
obj.Namespace = "default"
obj.Spec.Brand = fmt.Sprintf("Acura-%03d", rand.Intn(1000))

err := plainStorage.Set(obj)
err := plainStorage.Create(newCar(name))
if err != nil {
return err
}
return c.String(200, "OK!")
})

e.PUT("/plain/:name", func(c echo.Context) error {
name := c.Param("name")
if len(name) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Please set name")
}

if err := setNewCarStatus(plainStorage, carKeyForName(name)); err != nil {
return err
}
return c.String(200, "OK!")
})

e.GET("/git/", func(c echo.Context) error {
objs, err := gitStorage.List(storage.NewKindKey(carGVK))
if err != nil {
Expand All @@ -166,22 +172,15 @@ func run(identityFile, gitURL string) error {
return c.JSON(http.StatusOK, objs)
})

e.POST("/git/:name", func(c echo.Context) error {
e.PUT("/git/:name", func(c echo.Context) error {
name := c.Param("name")
if len(name) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Please set name")
}

objKey := storage.NewObjectKey(storage.NewKindKey(carGVK), runtime.NewIdentifier("default/"+name))
err := gitStorage.Transaction(context.Background(), "foo-branch-", func(ctx context.Context, s storage.Storage) (*transaction.CommitSpec, error) {
obj, err := s.Get(objKey)
if err != nil {
return nil, err
}
car := obj.(*v1alpha1.Car)
car.Status.Distance = rand.Uint64()
car.Status.Speed = rand.Float64() * 100
if err := s.Set(car); err != nil {
if err := setNewCarStatus(s, objKey); err != nil {
return nil, err
}

Expand Down Expand Up @@ -226,3 +225,29 @@ func parseVersionFlag() {
os.Exit(0)
}
}

func carKeyForName(name string) storage.ObjectKey {
return storage.NewObjectKey(storage.NewKindKey(carGVK), runtime.NewIdentifier("default/"+name))
}

func newCar(name string) *v1alpha1.Car {
obj := &v1alpha1.Car{}
obj.Name = name
obj.Namespace = "default"
obj.Spec.Brand = fmt.Sprintf("Acura-%03d", rand.Intn(1000))

return obj
}

func setNewCarStatus(s storage.Storage, key storage.ObjectKey) error {
obj, err := s.Get(key)
if err != nil {
return err
}

car := obj.(*v1alpha1.Car)
car.Status.Distance = rand.Uint64()
car.Status.Speed = rand.Float64() * 100

return s.Update(car)
}

0 comments on commit ef8bafd

Please sign in to comment.