Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud-Init Rewrite #37

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
de29fd1
feat: add user data and vendor data handlers, and scripts for group m…
alexlovelltroy Dec 14, 2024
cf21006
feat: add unit tests for cloud-init server handlers and implement loc…
alexlovelltroy Dec 15, 2024
9a66aa4
feat: implement fake SMD client handlers for adding and listing nodes…
alexlovelltroy Dec 15, 2024
3d758cd
feat: remove obsolete JSON configuration files and update add_node.sh…
alexlovelltroy Dec 16, 2024
a39b70c
feat: refactor instance data generation and update group metadata han…
alexlovelltroy Dec 16, 2024
0bdd18a
feat: enhance cloud-init server with group user data handling and imp…
alexlovelltroy Dec 17, 2024
fb14ba8
feat: add scripts for hostname override and cluster defaults, refacto…
alexlovelltroy Dec 17, 2024
d0759c3
feat: add update node functionality to FakeSMDClient and correspondin…
alexlovelltroy Dec 18, 2024
a092a2d
fix: remove unnecessary unlock call in PopulateNodes method of SMDClient
alexlovelltroy Dec 18, 2024
c9e1653
fix: streamline locking mechanism in PopulateNodes method of SMDClient
alexlovelltroy Dec 18, 2024
072f57f
feat: add base URL configuration for cloud-init server and update met…
alexlovelltroy Dec 18, 2024
84ae128
fix: update SetClusterDefaults method to use locking and improve clus…
alexlovelltroy Dec 18, 2024
ddf450f
feat: update cluster defaults demo script to show ssh key and base-url
alexlovelltroy Dec 18, 2024
54a36ef
fix: improve error handling in GroupUserDataHandler to return empty c…
alexlovelltroy Dec 18, 2024
b7f93b6
fix: update base URL in demo script and trim trailing slashes in SetC…
alexlovelltroy Dec 18, 2024
e519ed2
Allow node update to include own ip/mac
alexlovelltroy Dec 19, 2024
a097b29
fix: update UpdateGroupData method to support creating group data if …
alexlovelltroy Dec 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions cmd/cloud-init-server/group_handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package main

import (
"encoding/json"
"net/http"

"github.com/OpenCHAMI/cloud-init/pkg/cistore"
"github.com/go-chi/chi/v5"
yaml "gopkg.in/yaml.v2"
)

func (h CiHandler) GetGroups(w http.ResponseWriter, r *http.Request) {
var (
groups map[string]cistore.GroupData
bytes []byte
err error
)
groups = h.store.GetGroups()
bytes, err = json.MarshalIndent(groups, "", "\t")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := w.Write(bytes); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

/*
AddGroupHandler adds a new group with it's associated data specified by the user.

*/
// AddGroupHandler handles the HTTP request for adding a new group.
// It parses the request data into a GroupData struct, validates it,
// and then attempts to store it using the handler's store. If successful,
// it sets the Location header to the new group's URL and responds with
// HTTP status 201 Created. If there is an error during parsing or storing,
// it responds with the appropriate HTTP error status.
//
// Curl Example:
//
// curl -X POST http://localhost:27777/cloud-init/admin/groups/ \
// -H "Content-Type: application/json" \
// -d '{
// "name": "x3000",
// "description": "Cabinet x3000",
// "data": {
// "syslog_aggregator": "192.168.0.1"
// },
// "file": {
// "content": "#cloud-config\nrsyslog:\n remotes: {x3000: \"192.168.0.5\"}\nservice_reload_command: auto\n",
// "encoding": "plain"
// }
// }'
// It parses the request data into a GroupData struct and attempts to add it to the store.
// Encoding options are "plain" or "base64".
// If parsing fails, it responds with a 422 Unprocessable Entity status.
// If adding the group data to the store fails, it responds with a 409 Conflict status.
// On success, it sets the Location header to the new group's URL and responds with a 201 Created status.
func (h CiHandler) AddGroupHandler(w http.ResponseWriter, r *http.Request) {
var (
data cistore.GroupData
err error
)

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

err = h.store.AddGroupData(data.Name, data)
if err != nil {
http.Error(w, err.Error(), http.StatusConflict)
return
}
w.Header().Set("Location", "/groups/"+data.Name)
w.WriteHeader(http.StatusCreated)

}

func (h CiHandler) GetGroupHandler(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
data cistore.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) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) {
var (
groupName string = chi.URLParam(r, "name")
data cistore.GroupData
err error
)

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

// update group key-value data
err = h.store.UpdateGroupData(groupName, data, true)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", "/groups/"+data.Name)
w.WriteHeader(http.StatusCreated)
}

func (h CiHandler) RemoveGroupHandler(w http.ResponseWriter, r *http.Request) {
var (
id string = chi.URLParam(r, "id")
err error
)
err = h.store.RemoveGroupData(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
Loading
Loading