Skip to content

Commit

Permalink
feat(api): add lineages get endpoint (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed Jun 25, 2021
1 parent f341ffd commit f632586
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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

0 comments on commit f632586

Please sign in to comment.