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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add endpoint to get lineages from database #176

Merged
merged 1 commit into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions api/api.go
Expand Up @@ -331,3 +331,22 @@ func ManagePlans(w http.ResponseWriter, r *http.Request, db *db.Database) {
http.Error(w, "Invalid request method.", 405)
}
}

// GetLineages recover all Lineage from db.
// Optional "&limit=X" parameter to limit requested quantity of them.
// Sorted by most recent to oldest.
func GetLineages(w http.ResponseWriter, r *http.Request, db *db.Database) {
w.Header().Set("Access-Control-Allow-Origin", "*")
limit := r.URL.Query().Get("limit")
lineages := db.GetLineages(limit)

j, err := json.Marshal(lineages)
if err != nil {
log.Errorf("Failed to marshal lineages: %v", err)
JSONError(w, "Failed to marshal lineages", err)
return
}
if _, err := io.WriteString(w, string(j)); err != nil {
log.Error(err.Error())
}
}
20 changes: 20 additions & 0 deletions db/db.go
Expand Up @@ -630,6 +630,26 @@ func (db *Database) GetPlans(lineage, limitStr string) (plans []types.Plan) {
return
}

// GetLineages retrieves all Lineage from the database
func (db *Database) GetLineages(limitStr string) (lineages []types.Lineage) {
var limit int
if limitStr == "" {
limit = -1
} else {
var err error
limit, err = strconv.Atoi(limitStr)
if err != nil {
log.Warnf("GetLineages limit ignored: %v", err)
limit = -1
}
}

db.Order("created_at desc").
Limit(limit).
Find(&lineages)
return
}

// DefaultVersion returns the detault VersionID for a given State path
// Copied and adapted from github.com/hashicorp/terraform/command/jsonstate/state.go
func (db *Database) DefaultVersion(path string) (version string, err error) {
Expand Down
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -197,6 +197,7 @@ func main() {
http.HandleFunc(util.GetFullPath("api/attribute/keys"), handleWithDB(api.ListAttributeKeys, database))
http.HandleFunc(util.GetFullPath("api/tf_versions"), handleWithDB(api.ListTfVersions, database))
http.HandleFunc(util.GetFullPath("api/plans"), handleWithDB(api.ManagePlans, database))
http.HandleFunc(util.GetFullPath("api/lineages"), handleWithDB(api.GetLineages, database))

// Start server
log.Debugf("Listening on port %d\n", c.Web.Port)
Expand Down