Skip to content

Commit

Permalink
fix return 404 if state does not exist
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Krischer <tobikris@users.noreply.github.com>
  • Loading branch information
lu1as and tobikris committed Mar 28, 2023
1 parent a42c0c1 commit 584f42d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -130,7 +131,11 @@ func Get(w http.ResponseWriter, r *http.Request, state *terraform.State, store s
log.Debugf("get state with id %s", state.ID)
stateID := state.ID
state, err := store.GetState(state.ID)
if err != nil {
if errors.Is(err, storage.ErrStateNotFound) {
log.Debugf("state with id %s does not exist", stateID)
HTTPResponse(w, r, http.StatusNotFound, err.Error())
return
} else if err != nil {
log.Warnf("failed to get state with id %s: %v", stateID, err)
HTTPResponse(w, r, http.StatusBadRequest, err.Error())
return
Expand Down
9 changes: 2 additions & 7 deletions pkg/storage/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

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

Expand Down Expand Up @@ -35,13 +36,7 @@ func (f *FileSystemStorage) SaveState(s *terraform.State) error {

func (f *FileSystemStorage) GetState(id string) (*terraform.State, error) {
if _, err := os.Stat(f.getFileName(id)); errors.Is(err, os.ErrNotExist) {
f, err := os.Create(f.getFileName(id))
if err != nil {
return nil, err
}
defer f.Close()

return &terraform.State{}, nil
return nil, storage.ErrStateNotFound
}

d, err := os.ReadFile(f.getFileName(id))
Expand Down
6 changes: 5 additions & 1 deletion pkg/storage/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

pgclient "github.com/nimbolus/terraform-backend/pkg/client/postgres"
"github.com/nimbolus/terraform-backend/pkg/storage"
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

Expand Down Expand Up @@ -70,7 +72,9 @@ func (p *PostgresStorage) GetState(id string) (*terraform.State, error) {
s := &terraform.State{}

err := p.db.QueryRow(`SELECT state_data FROM `+p.table+` WHERE state_id = $1`, id).Scan(&s.Data)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, storage.ErrStateNotFound
} else if err != nil {
return nil, err
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"

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

Expand Down Expand Up @@ -67,7 +68,7 @@ func (s *S3Storage) GetState(id string) (*terraform.State, error) {
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(obj); err != nil {
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
return state, nil
return nil, storage.ErrStateNotFound
}
return state, err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package storage

import (
"errors"

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

var (
ErrStateNotFound = errors.New("state does not exist")
)

type Storage interface {
GetName() string
SaveState(s *terraform.State) error
Expand Down
6 changes: 6 additions & 0 deletions pkg/storage/util/storagetest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"errors"
"testing"

"github.com/spf13/viper"
Expand All @@ -21,6 +22,11 @@ func StorageTest(t *testing.T, s storage.Storage) {
Data: []byte("test"),
}

nonExisting, err := s.GetState(state.ID)
if nonExisting != nil || !errors.Is(err, storage.ErrStateNotFound) {
t.Error("non existing state should return ErrStateNotFound")
}

if err := s.SaveState(state); err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 584f42d

Please sign in to comment.