Skip to content

Commit 7e4c037

Browse files
author
Kerry Wilson
committed
plan route and import fix
1 parent b3bed72 commit 7e4c037

9 files changed

Lines changed: 94 additions & 31 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Continuous integration service for [Terraform](https://terraform.io).
44

5+
## Current State
6+
* Requires terraform repositories to be checked out to a directory (CHECKOUT_DIR) and to have a terraform.tfplan
7+
58
## Planned Features
69
* Securely stores template variables
710
* Generates plans when changes committed to source
@@ -11,6 +14,15 @@ Continuous integration service for [Terraform](https://terraform.io).
1114
* Backend written in go
1215
* Frontend vue.js app
1316

17+
### API Endpoints
18+
19+
#### Projects
20+
21+
* `/status` - (GET) Get service status
22+
* `/api/projects` - (GET, PUT) List all projects, create project
23+
* `/api/projects/{guid}` - (POST, DELETE) Update or delete projects
24+
* `/api/plan/{guid}` - (GET) Return the current plan associated with the project guid
25+
1426
## Testing
1527
* `make test`
1628

config/config.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import (
77
"strconv"
88

99
"github.com/hashicorp/logutils"
10-
"github.com/webdevwilson/terraform-ui/persist"
11-
"github.com/webdevwilson/terraform-ui/task"
10+
"github.com/webdevwilson/terraform-ci/persist"
11+
"github.com/webdevwilson/terraform-ci/task"
1212
)
1313

1414
// Settings contains all the configuration values for the service
1515
type Settings struct {
16-
LogLevel string
17-
SiteRoot string
18-
Port int
19-
Store persist.Store
20-
Executor *task.Executor
16+
LogLevel string
17+
SiteRoot string
18+
CheckoutDirectory string
19+
Port int
20+
Store persist.Store
21+
Executor *task.Executor
2122
}
2223

2324
var settings *Settings
@@ -33,10 +34,17 @@ func init() {
3334

3435
executor := task.NewExecutor(store)
3536

37+
workingDir, err := os.Getwd()
38+
39+
if err != nil {
40+
log.Fatalf("[FATAL] Failed to get current working directory: %s", workingDir)
41+
}
42+
3643
// initialize settings
3744
settings = &Settings{
3845
envOr("LOG_LEVEL", "INFO"),
39-
envOrFunc("SITE_ROOT", defaultSiteRoot),
46+
envOr("SITE_ROOT", path.Join(workingDir, "site", "dist")),
47+
envOr("CHECKOUT_DIR", path.Join(workingDir, ".state", "projects")),
4048
envOrInt("PORT", 3000),
4149
store,
4250
executor,
@@ -66,15 +74,6 @@ func defaultStatePath() (statePath string) {
6674
return
6775
}
6876

69-
func defaultSiteRoot() (siteRoot string) {
70-
wd, err := os.Getwd()
71-
if err != nil {
72-
log.Fatalf("[FATAL] Failed to get current working directory: %s", wd)
73-
}
74-
siteRoot = path.Join(wd, "site", "dist")
75-
return
76-
}
77-
7877
// env returns environment variables. fatal error if it does not exist
7978
func env(name string) (v string) {
8079
if v = os.Getenv(name); len(v) == 0 {

main.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,49 @@ package main
22

33
import (
44
"log"
5+
"path"
6+
"path/filepath"
57

68
"fmt"
79

8-
"github.com/webdevwilson/terraform-ui/config"
9-
"github.com/webdevwilson/terraform-ui/routes"
10-
_ "github.com/webdevwilson/terraform-ui/task"
10+
"github.com/webdevwilson/terraform-ci/config"
11+
"github.com/webdevwilson/terraform-ci/model"
12+
"github.com/webdevwilson/terraform-ci/routes"
13+
_ "github.com/webdevwilson/terraform-ci/task"
1114
)
1215

1316
func main() {
14-
port := config.Get().Port
17+
settings := config.Get()
18+
port := settings.Port
1519
log.Print(fmt.Sprintf("[INFO] Listening on port %d ...", port))
1620

17-
routes.StartServer(port)
21+
// this should be temporary, until we can get a proper CRUD frontend
22+
bootstrapProjects(settings.CheckoutDirectory)
23+
24+
go routes.StartServer(port)
1825

1926
for {
2027
}
2128
}
29+
30+
func bootstrapProjects(dir string) {
31+
dirs, err := filepath.Glob(path.Join(dir, "*"))
32+
if err != nil {
33+
log.Fatalf("[FATAL] Error encountered loading projects: %s", err)
34+
}
35+
36+
for _, dir := range dirs {
37+
name := path.Base(dir)
38+
prj, err := model.GetProjectByName(name)
39+
40+
if err != nil {
41+
log.Fatalf("[FATAL] Error bootstrapping projects: %s", err)
42+
}
43+
44+
if prj == nil {
45+
model.CreateProject(&model.Project{
46+
Name: name,
47+
})
48+
}
49+
}
50+
}

model/project.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package model
22

33
import (
4-
"github.com/webdevwilson/terraform-ui/config"
5-
"github.com/webdevwilson/terraform-ui/persist"
4+
"github.com/webdevwilson/terraform-ci/config"
5+
"github.com/webdevwilson/terraform-ci/persist"
66
)
77

88
const projectNS = "projects"
@@ -32,6 +32,7 @@ func ListProjects() (projects []Project, err error) {
3232
projects = make([]Project, len(guids))
3333
for i, guid := range guids {
3434
err = store.Get(projectNS, guid, &projects[i])
35+
projects[i].GUID = guid
3536
if err != nil {
3637
return
3738
}
@@ -53,6 +54,28 @@ func GetProject(guid string) (*Project, error) {
5354
return &prj, err
5455
}
5556

57+
// GetProjectByName returns the named project
58+
func GetProjectByName(name string) (*Project, error) {
59+
60+
projects, err := store.List(projectNS)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
for _, guid := range projects {
66+
prj, err := GetProject(guid)
67+
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
if prj.Name == name {
73+
return prj, nil
74+
}
75+
}
76+
return nil, nil
77+
}
78+
5679
// CreateProject
5780
func CreateProject(prj *Project) (err error) {
5881
var guid string

routes/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"sync"
99

1010
"github.com/gorilla/mux"
11-
"github.com/webdevwilson/terraform-ui/config"
12-
"github.com/webdevwilson/terraform-ui/persist"
11+
"github.com/webdevwilson/terraform-ci/config"
12+
"github.com/webdevwilson/terraform-ci/persist"
1313
)
1414

1515
// Route function interface

routes/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/json"
77

88
"github.com/gorilla/mux"
9-
"github.com/webdevwilson/terraform-ui/model"
9+
"github.com/webdevwilson/terraform-ci/model"
1010
)
1111

1212
func init() {

routes/project_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"strings"
1515

1616
"github.com/stretchr/testify/assert"
17-
"github.com/webdevwilson/terraform-ui/config"
18-
"github.com/webdevwilson/terraform-ui/model"
17+
"github.com/webdevwilson/terraform-ci/config"
18+
"github.com/webdevwilson/terraform-ci/model"
1919
)
2020

2121
var prjs = []model.Project{

routes/site.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package routes
22

3-
import "github.com/webdevwilson/terraform-ui/config"
3+
import "github.com/webdevwilson/terraform-ci/config"
44

55
func init() {
66
//r := Router()

task/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"runtime/debug"
1111

1212
uuid "github.com/nu7hatch/gouuid"
13-
"github.com/webdevwilson/terraform-ui/persist"
13+
"github.com/webdevwilson/terraform-ci/persist"
1414
)
1515

1616
const persistNamespace = "executions"

0 commit comments

Comments
 (0)