Skip to content

Commit

Permalink
metadata get, set and unset api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed Jun 4, 2024
1 parent 013f46a commit 1653e6b
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 4 deletions.
1 change: 0 additions & 1 deletion cmd/plugin/rpaasv2/cmd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func createMetadata(meta []string, metaType string, isSet bool) (*types.Metadata
item.Value = strings.Split(kv, "=")[1]
} else {
item.Name = kv
item.Delete = true
}

if metaType == "label" {
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/rpaas/fake/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type RpaasManager struct {
FakeUpdateCertManagerRequest func(instanceName string, in clientTypes.CertManager) error
FakeDeleteCertManagerRequest func(instanceName, issuer string) error
FakeGetMetadata func(instanceName string) (*clientTypes.Metadata, error)
FakeSetMetadata func(instanceName string, metadata *clientTypes.Metadata) error
FakeUnsetMetadata func(instanceName string, metadata *clientTypes.Metadata) error
}

func (m *RpaasManager) Log(ctx context.Context, instanceName string, args rpaas.LogArgs) error {
Expand Down Expand Up @@ -357,3 +359,17 @@ func (m *RpaasManager) GetMetadata(ctx context.Context, instance string) (*clien
}
return nil, nil
}

func (m *RpaasManager) SetMetadata(ctx context.Context, instance string, metadata *clientTypes.Metadata) error {
if m.FakeSetMetadata != nil {
return m.FakeSetMetadata(instance, metadata)
}
return nil
}

func (m *RpaasManager) UnsetMetadata(ctx context.Context, instance string, metadata *clientTypes.Metadata) error {
if m.FakeUnsetMetadata != nil {
return m.FakeUnsetMetadata(instance, metadata)
}
return nil
}
68 changes: 68 additions & 0 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -2492,3 +2492,71 @@ func (m *k8sRpaasManager) GetMetadata(ctx context.Context, instanceName string)

return metadata, nil
}

func validateMetadata(items []clientTypes.MetadataItem) error {
for _, item := range items {
if strings.HasPrefix(item.Name, defaultKeyLabelPrefix) {
return &ValidationError{Msg: fmt.Sprintf("metadata key %q is reserved", item.Name)}
}
}
return nil
}

func (m *k8sRpaasManager) SetMetadata(ctx context.Context, instanceName string, metadata *clientTypes.Metadata) error {
instance, err := m.GetInstance(ctx, instanceName)
if err != nil {
return err
}

if err = validateMetadata(metadata.Labels); err != nil {
return err
}

if err = validateMetadata(metadata.Annotations); err != nil {
return err
}

originalInstance := instance.DeepCopy()

if metadata.Labels != nil {
for _, item := range metadata.Labels {
instance.Labels[item.Name] = item.Value
}
}

if metadata.Annotations != nil {
for _, item := range metadata.Annotations {
instance.Annotations[item.Name] = item.Value
}
}

return m.patchInstance(ctx, originalInstance, instance)
}

func (m *k8sRpaasManager) UnsetMetadata(ctx context.Context, instanceName string, metadata *clientTypes.Metadata) error {
instance, err := m.GetInstance(ctx, instanceName)
if err != nil {
return err
}
originalInstance := instance.DeepCopy()

if metadata.Labels != nil {
for _, item := range metadata.Labels {
if _, ok := instance.Labels[item.Name]; !ok {
return &NotFoundError{Msg: fmt.Sprintf("label %q not found in instance %q", item.Name, instanceName)}
}
delete(instance.Labels, item.Name)
}
}

if metadata.Annotations != nil {
for _, item := range metadata.Annotations {
if _, ok := instance.Annotations[item.Name]; !ok {
return &NotFoundError{Msg: fmt.Sprintf("annotation %q not found in instance %q", item.Name, instanceName)}
}
delete(instance.Annotations, item.Name)
}
}

return m.patchInstance(ctx, originalInstance, instance)
}
2 changes: 2 additions & 0 deletions internal/pkg/rpaas/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ type RpaasManager interface {
DeleteCertManagerRequest(ctx context.Context, instanceName, issuer string) error

GetMetadata(ctx context.Context, instanceName string) (*clientTypes.Metadata, error)
SetMetadata(ctx context.Context, instanceName string, metadata *clientTypes.Metadata) error
UnsetMetadata(ctx context.Context, instanceName string, metadata *clientTypes.Metadata) error
}

type CertificateData struct {
Expand Down
5 changes: 2 additions & 3 deletions pkg/rpaas/client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ type Metadata struct {
}

type MetadataItem struct {
Name string `json:"name"`
Value string `json:"value,omitempty"`
Delete bool `json:"delete,omitempty" bson:"-"`
Name string `json:"name"`
Value string `json:"value,omitempty"`
}
2 changes: 2 additions & 0 deletions pkg/web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func newEcho(targetFactory target.Factory) *echo.Echo {
group.DELETE("/:instance/acl", deleteUpstream)
group.GET("/:instance/log", log)
group.GET("/:instance/metadata", getMetadata)
group.POST("/:instance/metadata", setMetadata)
group.DELETE("/:instance/metadata", unsetMetadata)

return e
}
42 changes: 42 additions & 0 deletions pkg/web/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"

"github.com/labstack/echo/v4"

clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
)

func getMetadata(c echo.Context) error {
Expand All @@ -24,3 +26,43 @@ func getMetadata(c echo.Context) error {

return c.JSON(http.StatusOK, metadata)
}

func setMetadata(c echo.Context) error {
ctx := c.Request().Context()
manager, err := getManager(ctx)
if err != nil {
return err
}

var metadata clientTypes.Metadata
if err = c.Bind(&metadata); err != nil {
return err
}

err = manager.SetMetadata(ctx, c.Param("instance"), &metadata)
if err != nil {
return err
}

return c.NoContent(http.StatusOK)
}

func unsetMetadata(c echo.Context) error {
ctx := c.Request().Context()
manager, err := getManager(ctx)
if err != nil {
return err
}

var metadata clientTypes.Metadata
if err = c.Bind(&metadata); err != nil {
return err
}

err = manager.UnsetMetadata(ctx, c.Param("instance"), &metadata)
if err != nil {
return err
}

return c.NoContent(http.StatusOK)
}

0 comments on commit 1653e6b

Please sign in to comment.