Skip to content

Commit

Permalink
feat(lineage): update search view and state explorer to use lineages
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed Jul 12, 2021
1 parent 8070e34 commit abd2349
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
15 changes: 15 additions & 0 deletions api/api.go
Expand Up @@ -42,6 +42,21 @@ func ListStates(w http.ResponseWriter, _ *http.Request, d *db.Database) {
}
}

// ListStatesWithLineages lists distinct State paths with associated Lineages
func ListStatesWithLineages(w http.ResponseWriter, _ *http.Request, d *db.Database) {
w.Header().Set("Access-Control-Allow-Origin", "*")
states := d.ListStatesWithLineages()

j, err := json.Marshal(states)
if err != nil {
JSONError(w, "Failed to marshal states", err)
return
}
if _, err := io.WriteString(w, string(j)); err != nil {
log.Error(err.Error())
}
}

// ListTerraformVersionsWithCount lists Terraform versions with their associated
// counts, sorted by the 'orderBy' parameter (version by default)
func ListTerraformVersionsWithCount(w http.ResponseWriter, r *http.Request, d *db.Database) {
Expand Down
20 changes: 19 additions & 1 deletion db/db.go
Expand Up @@ -131,7 +131,7 @@ func (db *Database) stateS3toDB(sf *statefile.File, path string, versionID strin
Version: version,
TFVersion: sf.TerraformVersion.String(),
Serial: int64(sf.Serial),
LineageID: sql.NullInt64{Int64: int64(lineage.ID)},
LineageID: sql.NullInt64{Int64: int64(lineage.ID), Valid: true},
}

for _, m := range sf.State.Modules {
Expand Down Expand Up @@ -436,6 +436,24 @@ func (db *Database) ListStates() (states []string) {
return
}

// ListStatesWithLineages returns a slice of all distinct State paths with Lineage from the Database
func (db *Database) ListStatesWithLineages() (states []interface{}) {
type jsonStateLineage struct {
Path string `json:"path"`
Lineage string `json:"lineage"`
}
rows, _ := db.Table("states").Joins("JOIN lineages ON lineages.id = states.lineage_id").Select("DISTINCT ON(states.path) states.path, lineages.value as lineage").Rows()
defer rows.Close()
for rows.Next() {
var state jsonStateLineage
if err := rows.Scan(&state.Path, &state.Lineage); err != nil {
log.Error(err.Error())
}
states = append(states, state)
}
return
}

// ListTerraformVersionsWithCount returns a slice of maps of Terraform versions
// mapped to the count of most recent State paths using them.
// ListTerraformVersionsWithCount also takes a query with possible parameter 'orderBy'
Expand Down
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -183,6 +183,7 @@ func main() {
http.HandleFunc(util.GetFullPath("api/version"), getVersion)
http.HandleFunc(util.GetFullPath("api/user"), api.GetUser)
http.HandleFunc(util.GetFullPath("api/states"), handleWithDB(api.ListStates, database))
http.HandleFunc(util.GetFullPath("api/states_lineages"), handleWithDB(api.ListStatesWithLineages, database))
http.HandleFunc(util.GetFullPath("api/states/stats"), handleWithDB(api.ListStateStats, database))
http.HandleFunc(util.GetFullPath("api/states/tfversion/count"),
handleWithDB(api.ListTerraformVersionsWithCount, database))
Expand Down
2 changes: 1 addition & 1 deletion static/navbar.html
Expand Up @@ -32,7 +32,7 @@
title="Choose a state file">
<ui-select-match placeholder="{{placeholder}}">{{placeholder}}</ui-select-match>
<ui-select-choices repeat="state in states | filter: $select.search | orderBy">
<a href="state/{{state}}" target="_self">{{state}}</a>
<a href="lineage/{{state.lineage}}" target="_self">{{state.path}}</a>
</ui-select-choices>
</ui-select>
</li>
Expand Down
2 changes: 1 addition & 1 deletion static/search.html
Expand Up @@ -127,7 +127,7 @@
</thead>
<tbody>
<tr ng-repeat="r in results.results">
<td><a href="state/{{r.path}}?versionid={{r.version_id}}#{{r.module_path}}.{{r.resource_name}}"><span class="glyphicon glyphicon-link" aria-hidden="true"></span></a></td>
<td><a href="lineage/{{r.lineage_value}}?versionid={{r.version_id}}#{{r.module_path}}.{{r.resource_name}}"><span class="glyphicon glyphicon-link" aria-hidden="true"></span></a></td>
<td>{{r.path}}</td>
<td>{{r.tf_version}}</td>
<td>{{r.serial}}</td>
Expand Down
2 changes: 1 addition & 1 deletion static/terraboard.js
Expand Up @@ -219,7 +219,7 @@ app.controller("tbNavCtrl",
}
});

$http.get('api/states').then(function(response){
$http.get('api/states_lineages').then(function(response){
$scope.states = response.data;
});

Expand Down

0 comments on commit abd2349

Please sign in to comment.