Skip to content

Commit

Permalink
Merge pull request #10 from nimbolus/add-delete-state
Browse files Browse the repository at this point in the history
Add delete state endpoint
  • Loading branch information
tobikris committed Oct 11, 2022
2 parents b0ca4b0 + 23a7bbe commit 6c0dfc8
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ go.work

# End of https://www.toptal.com/developers/gitignore/api/go

.vscode
.env
terraform-backend
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ services:
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
minio:
image: minio/minio
environment:
MINIO_ROOT_USER: root
MINIO_ROOT_PASSWORD: password
command: server /storage
ports:
- "9000:9000"

volumes:
states:
10 changes: 9 additions & 1 deletion pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,13 @@ func Post(r *http.Request, w http.ResponseWriter, state *terraform.State, body [

func Delete(w http.ResponseWriter, state *terraform.State, store storage.Storage) {
log.Debugf("delete state with id %s", state.ID)
HTTPResponse(w, http.StatusNotImplemented, "Delete state is not implemented")

err := store.DeleteState(state.ID)
if err != nil {
log.Warnf("failed to delete state with id %s: %v", state.ID, err)
HTTPResponse(w, http.StatusInternalServerError, err.Error())
return
}

HTTPResponse(w, http.StatusOK, "")
}
6 changes: 5 additions & 1 deletion pkg/storage/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (f *FileSystemStorage) GetName() string {
}

func (f *FileSystemStorage) SaveState(s *terraform.State) error {
return os.WriteFile(fmt.Sprintf("%s/%s.tfstate", f.directory, s.ID), s.Data, 0600)
return os.WriteFile(f.getFileName(s.ID), s.Data, 0600)
}

func (f *FileSystemStorage) GetState(id string) (*terraform.State, error) {
Expand All @@ -54,6 +54,10 @@ func (f *FileSystemStorage) GetState(id string) (*terraform.State, error) {
}, nil
}

func (f *FileSystemStorage) DeleteState(id string) error {
return os.Remove(f.getFileName(id))
}

func (f *FileSystemStorage) getFileName(id string) string {
return fmt.Sprintf("%s/%s.tfstate", f.directory, id)
}
16 changes: 16 additions & 0 deletions pkg/storage/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package filesystem

import (
"testing"

"github.com/nimbolus/terraform-backend/pkg/storage/util"
)

func TestStorage(t *testing.T) {
s, err := NewFileSystemStorage("./storage")
if err != nil {
t.Error(err)
}

util.StorageTest(t, s)
}
4 changes: 4 additions & 0 deletions pkg/storage/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ func (p *PostgresStorage) GetState(id string) (*terraform.State, error) {

return s, nil
}

func (p *PostgresStorage) DeleteState(id string) error {
return p.db.QueryRow(`DELETE FROM `+p.table+` WHERE state_id = $1`, id).Err()
}
12 changes: 9 additions & 3 deletions pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ func NewS3Storage(endpoint, bucket, accessKey, secretKey string, useSSL bool) (*
Secure: useSSL,
})
if err != nil {
return nil, fmt.Errorf("failed to initialize minio client: %v", err)
return nil, fmt.Errorf("failed to initialize minio client: %w", err)
}

if exists, err := client.BucketExists(context.Background(), bucket); err != nil {
return nil, fmt.Errorf("failed to check for bucket: %v", err)
return nil, fmt.Errorf("failed to check for bucket: %w", err)
} else if !exists {
return nil, fmt.Errorf("bucket does not exist")
if err = client.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil {
return nil, fmt.Errorf("bucket does not exist and creation failed: %w", err)
}
}

return &S3Storage{
Expand Down Expand Up @@ -74,6 +76,10 @@ func (s *S3Storage) GetState(id string) (*terraform.State, error) {
return state, nil
}

func (s *S3Storage) DeleteState(id string) error {
return s.client.RemoveObject(context.Background(), s.bucket, getObjectName(id), minio.RemoveObjectOptions{})
}

func getObjectName(id string) string {
return fmt.Sprintf("%s.tfstate", id)
}
19 changes: 19 additions & 0 deletions pkg/storage/s3/s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build integration || s3
// +build integration s3

package s3

import (
"testing"

"github.com/nimbolus/terraform-backend/pkg/storage/util"
)

func TestStorage(t *testing.T) {
s, err := NewS3Storage("localhost:9000", "tf-backend-integration-test", "root", "password", false)
if err != nil {
t.Fatal(err)
}

util.StorageTest(t, s)
}
1 change: 1 addition & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type Storage interface {
GetName() string
SaveState(s *terraform.State) error
GetState(id string) (*terraform.State, error)
DeleteState(id string) error
}
5 changes: 5 additions & 0 deletions pkg/storage/util/storagetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ func StorageTest(t *testing.T, s storage.Storage) {
if string(state.Data) != string(savedState.Data) {
t.Errorf("state data does not match")
}

err = s.DeleteState(state.ID)
if err != nil {
t.Error(err)
}
}

0 comments on commit 6c0dfc8

Please sign in to comment.