-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[olivercodes] feat(gh-2): first pass at namespace api
- Loading branch information
1 parent
e5bd357
commit 7a7d509
Showing
10 changed files
with
307 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package handler | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"twdps.io/lab-api-teams/pkg/domain" | ||
"twdps.io/lab-api-teams/pkg/service" | ||
) | ||
|
||
type NamespaceHandler struct { | ||
namespaceService service.NamespaceService | ||
} | ||
|
||
func NewNamespaceHandler(namespaceService service.NamespaceService) *NamespaceHandler { | ||
return &NamespaceHandler{namespaceService: namespaceService} | ||
} | ||
|
||
func (handler *NamespaceHandler) GetNamespaces(c *gin.Context) { | ||
namespaces, err := handler.namespaceService.GetNamespaces() | ||
if err != nil { | ||
log.Fatalf("Failed to call GetNamespaces %v", err) | ||
} | ||
|
||
c.IndentedJSON(http.StatusOK, namespaces) | ||
} | ||
|
||
func (handler *NamespaceHandler) AddNamespace(c *gin.Context) { | ||
var newNamespace domain.Namespace | ||
|
||
if err := c.BindJSON(&newNamespace); err != nil { | ||
log.Printf("error %+v", err) | ||
return | ||
} | ||
|
||
err := handler.namespaceService.AddNamespace(newNamespace) | ||
if err != nil { | ||
c.IndentedJSON(http.StatusInternalServerError, err) | ||
} | ||
|
||
c.IndentedJSON(http.StatusCreated, newNamespace) | ||
} | ||
|
||
func (handler *NamespaceHandler) GetNamespacesMaster(c *gin.Context) { | ||
namespaces, err := handler.namespaceService.GetNamespacesMaster() | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to call get namespaces master: %v", err) | ||
} | ||
|
||
c.IndentedJSON(http.StatusOK, namespaces) | ||
} | ||
|
||
func (handler *NamespaceHandler) GetNamespacesStandard(c *gin.Context) { | ||
namespaces, err := handler.namespaceService.GetNamespacesStandard() | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to call get namespaces master: %v", err) | ||
} | ||
|
||
c.IndentedJSON(http.StatusOK, namespaces) | ||
} | ||
|
||
func (handler *NamespaceHandler) GetNamespacesCustom(c *gin.Context) { | ||
namespaces, err := handler.namespaceService.GetNamespacesCustom() | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to call get namespaces master: %v", err) | ||
} | ||
|
||
c.IndentedJSON(http.StatusOK, namespaces) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package repository | ||
|
||
// type NamespaceRepository interface { | ||
// GetNamespaces() ([]Namespace, error) | ||
// GetNamespacesByAttribute(attrKey string, attrValue string) ([]Namespace, error) | ||
// GetNamespaceByID(namespaceID string) (Namespace, error) | ||
// AddNamespace(namespace Namespace) error | ||
// UpdateNamespace(namespace Namespace) error | ||
// RemoveNamespace(namespace Namespace) (Namespace, error) | ||
// } | ||
|
||
// TODO - redis repository tests | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/go-redis/redis/v8" | ||
"twdps.io/lab-api-teams/pkg/domain" | ||
) | ||
|
||
type RedisNamespaceRepository struct { | ||
client *redis.Client | ||
ctx context.Context | ||
} | ||
|
||
func NewRedisNamespaceRepository() *RedisNamespaceRepository { | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
DB: 0, | ||
}) | ||
|
||
return &RedisNamespaceRepository{ | ||
client: rdb, | ||
ctx: context.Background(), | ||
} | ||
} | ||
|
||
func (store *RedisNamespaceRepository) GetNamespaces() ([]domain.Namespace, error) { | ||
namespaceIDs, err := store.client.SMembers(store.ctx, "namespaces").Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var namespaces []domain.Namespace | ||
for _, namespaceID := range namespaceIDs { | ||
namespaceHash, err := store.client.HGet(store.ctx, "namespace:"+namespaceID, "data").Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var namespace domain.Namespace | ||
err = json.Unmarshal([]byte(namespaceHash), &namespace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
namespaces = append(namespaces, namespace) | ||
} | ||
|
||
return namespaces, nil | ||
} | ||
|
||
func (store *RedisNamespaceRepository) AddNamespace(namespace domain.Namespace) error { | ||
serializedNamespace, err := json.Marshal(namespace) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Save the namespace in a hash | ||
if err := store.client.HSet(store.ctx, "namespace:"+namespace.NamespaceID, "data", serializedNamespace).Err(); err != nil { | ||
return err | ||
} | ||
|
||
// Add namespace's ID to the set of all namespaces | ||
if err := store.client.SAdd(store.ctx, "namespaces", namespace.NamespaceID).Err(); err != nil { | ||
return err | ||
} | ||
|
||
// Update the genre index | ||
if err := store.client.SAdd(store.ctx, "index:namespace:namespaceType:"+namespace.NamespaceType, namespace.NamespaceID).Err(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (store *RedisNamespaceRepository) GetNamespacesByType(nsType string) ([]domain.Namespace, error) { | ||
namespaceIDs, err := store.client.SMembers(store.ctx, "index:namespace:namespaceType:"+nsType).Result() | ||
log.Printf("%+v", namespaceIDs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var namespaces []domain.Namespace | ||
for _, namespaceID := range namespaceIDs { | ||
nsHash, err := store.client.HGet(store.ctx, "namespace:"+namespaceID, "data").Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var namespace domain.Namespace | ||
err = json.Unmarshal([]byte(nsHash), &namespace) | ||
if err != nil { | ||
return nil, err // TODO transient/err | ||
} | ||
|
||
namespaces = append(namespaces, namespace) | ||
} | ||
|
||
return namespaces, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package service | ||
|
||
import ( | ||
domain "twdps.io/lab-api-teams/pkg/domain" | ||
) | ||
|
||
type NamespaceService interface { | ||
GetNamespaces() ([]domain.Namespace, error) | ||
AddNamespace(ns domain.Namespace) error | ||
GetNamespacesMaster() ([]domain.Namespace, error) | ||
GetNamespacesStandard() ([]domain.Namespace, error) | ||
GetNamespacesCustom() ([]domain.Namespace, error) | ||
} | ||
|
||
type namespaceServiceImpl struct { | ||
repo domain.NamespaceRepository | ||
} | ||
|
||
func NewNamespaceService(repo domain.NamespaceRepository) NamespaceService { | ||
return &namespaceServiceImpl{ | ||
repo: repo, | ||
} | ||
} | ||
|
||
func (s *namespaceServiceImpl) GetNamespaces() ([]domain.Namespace, error) { | ||
namespaces, err := s.repo.GetNamespaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return namespaces, nil | ||
} | ||
|
||
func (s *namespaceServiceImpl) GetNamespacesStandard() ([]domain.Namespace, error) { | ||
namespaces, err := s.repo.GetNamespacesByType("standard") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return namespaces, nil | ||
} | ||
|
||
func (s *namespaceServiceImpl) GetNamespacesCustom() ([]domain.Namespace, error) { | ||
namespaces, err := s.repo.GetNamespacesByType("custom") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return namespaces, nil | ||
} | ||
|
||
func (s *namespaceServiceImpl) GetNamespacesMaster() ([]domain.Namespace, error) { | ||
namespaces, err := s.repo.GetNamespacesByType("master") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return namespaces, nil | ||
} | ||
|
||
func (s *namespaceServiceImpl) AddNamespace(namespace domain.Namespace) error { | ||
if err := s.repo.AddNamespace(namespace); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// func (s *namespaceServiceImpl) UpdateNamespace(namespace domain.Namespace) error { | ||
// if err := s.repo.UpdateNamespace(namespace); err != nil { | ||
// return err | ||
// } | ||
// return nil | ||
// } | ||
|
||
// func (s *namespaceServiceImpl) GetTeamNamespacesByTeamID(teamID string) ([]domain.Namespace, error) { | ||
// ns, err := s.repo.GetNamespacesByAttribute("namespaceTeamID", teamID) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
// return ns, nil | ||
// } | ||
|
||
// func (s *namespaceServiceImpl) GetTeamNamespaceByID(nsID string) (domain.Namespace, error) { | ||
// ns, err := s.repo.GetNamespaceByID(nsID) | ||
// if err != nil { | ||
// return domain.Namespace{}, err | ||
// } | ||
|
||
// return ns, nil | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package service |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
curl http://localhost:8080/teams --include --header "Content-Type: application/json" --request "POST" --data '{"TeamID": "team-sapphire", "TeamType": "normal", "TeamDescription": "Sapphire frontend team", "TeamRAM": 32, "TeamCPU": 12, "TeamRamLimit": 64, "TeamCpuLimit": 24}' | ||
curl http://localhost:8080/teams | ||
|
||
curl http://localhost:8080/teams --include --header "Content-Type: application/json" --request "POST" --data '{"NamespaceID": "default", "NamespaceType": "master", "NamespaceRam": 32, "NamespaceCpu": 12, "NamespaceInMesh": true, "NamespaceFromDefault": false}' |