Skip to content

Commit

Permalink
groups: extended API with some minor clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
davidallendj committed Oct 28, 2024
1 parent 71092b9 commit 63a232c
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 69 deletions.
Binary file modified cmd/cloud-init-server/cloud-init-server
Binary file not shown.
128 changes: 98 additions & 30 deletions cmd/cloud-init-server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,48 +215,57 @@ func (h CiHandler) DeleteEntry(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, map[string]string{"status": "success"})
}

func (h CiHandler) AddGroupData(w http.ResponseWriter, r *http.Request) {
// type alias to simplify abstraction
func parseData(w http.ResponseWriter, r *http.Request) (citypes.GroupData, error) {
var (
id string = chi.URLParam(r, "id")
body []byte
data memstore.Data
err error
data citypes.GroupData
)

// read the POST body for JSON data
body, err = io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return nil, err
}

// unmarshal data to add to MemStore
// unmarshal data to add to group data
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return data, nil
}

func (h CiHandler) AddGroups(w http.ResponseWriter, r *http.Request) {
// type alias to simplify abstraction
var (
data citypes.GroupData
err error
)

data, err = parseData(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// add a new group to meta-data if it doesn't already exist
err = h.store.AddGroups(id, data)
err = h.store.AddGroups(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

}

func (h CiHandler) GetGroupData(w http.ResponseWriter, r *http.Request) {
func (h CiHandler) GetGroups(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
data memstore.Data
data citypes.GroupData
bytes []byte
err error
)

// get group data from MemStore if it exists
data, err = h.store.GetGroups(id)
data, err = h.store.GetGroups()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -276,53 +285,112 @@ func (h CiHandler) GetGroupData(w http.ResponseWriter, r *http.Request) {
// received in the request should ONLY contain the data to be included for a
// "meta-data.groups" and NOT "meta-data". See "AddGroup" in 'ciMemStore.go'
// for an example.
func (h CiHandler) UpdateGroupData(w http.ResponseWriter, r *http.Request) {
func (h CiHandler) UpdateGroups(w http.ResponseWriter, r *http.Request) {
// type alias to simplify abstraction
var (
data citypes.GroupData
err error
)

data, err = parseData(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// update groups in meta-data
err = h.store.UpdateGroups(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func (h CiHandler) RemoveGroups(w http.ResponseWriter, r *http.Request) {
// remove group data with specified name
var err = h.store.RemoveGroups()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func writeInternalError(w http.ResponseWriter, err string) {
http.Error(w, err, http.StatusInternalServerError)
// log.Error().Err(err)
}

func (h CiHandler) AddGroupData(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
body []byte
data memstore.Data
data citypes.GroupData
err error
)

// read the POST body for JSON data
body, err = io.ReadAll(r.Body)
data, err = parseData(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// unmarshal data to add to MemStore
err = json.Unmarshal(body, &data)
err = h.store.AddGroupData(id, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// update groups in meta-data
err = h.store.UpdateGroups(id, data)
}

func (h CiHandler) GetGroupData(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
data citypes.GroupData
bytes []byte
err error
)

data, err = h.store.GetGroupData(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

bytes, err = yaml.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bytes)
}
func (h CiHandler) UpdateGroupData(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
data citypes.GroupData
err error
)

data, err = parseData(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// update group key-value data
err = h.store.UpdateGroupData(id, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func (h CiHandler) RemoveGroupData(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
err error
)

// remove group data with specified name
err = h.store.RemoveGroups(id)
err = h.store.RemoveGroupData(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func writeInternalError(w http.ResponseWriter, err string) {
http.Error(w, err, http.StatusInternalServerError)
// log.Error().Err(err)
}
12 changes: 8 additions & 4 deletions cmd/cloud-init-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ func initCiRouter(router chi.Router, handler *CiHandler) {
router.Get("/{id}/vendor-data", handler.GetDataByMAC(VendorData))
router.Put("/{id}", handler.UpdateEntry)
router.Delete("/{id}", handler.DeleteEntry)
router.Post("/{id}/groups", handler.AddGroupData)
router.Get("/{id}/groups", handler.GetGroupData)
router.Put("/{id}/groups", handler.UpdateGroupData)
router.Delete("/{id}/groups", handler.RemoveGroupData)
router.Post("/groups", handler.AddGroups)
router.Get("/groups", handler.GetGroups)
router.Put("/groups", handler.UpdateGroups)
router.Delete("/groups", handler.RemoveGroups)
router.Post("/groups/{id}", handler.AddGroupData)
router.Get("/groups/{id}", handler.GetGroupData)
router.Put("/groups/{id}", handler.UpdateGroupData)
router.Delete("/groups/{id}", handler.RemoveGroupData)
}
17 changes: 11 additions & 6 deletions cmd/cloud-init-server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import (
)

// ciStore is an interface for storing cloud-init entries

type ciStore interface {
Add(name string, ci citypes.CI) error
Get(name string, sm *smdclient.SMDClient) (citypes.CI, error)
List() (map[string]citypes.CI, error)
Update(name string, ci citypes.CI) error
Remove(name string) error

// metadata group API
AddGroups(name string, groupData map[string]any) error
GetGroups(name string) (map[string]any, error)
UpdateGroups(name string, groupData map[string]any) error
RemoveGroups(name string) error
// metadata groups API
AddGroups(groupsData citypes.GroupData) error
GetGroups() (citypes.GroupData, error)
UpdateGroups(groupsData citypes.GroupData) error
RemoveGroups() error

// extended group API
AddGroupData(groupName string, groupData citypes.GroupData) error
GetGroupData(groupName string) (citypes.GroupData, error)
UpdateGroupData(groupName string, groupData citypes.GroupData) error
RemoveGroupData(groupName string) error
}
Loading

0 comments on commit 63a232c

Please sign in to comment.