Skip to content

Commit

Permalink
add delete storage endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lu1as committed Oct 10, 2022
1 parent 11ce06b commit 1f58241
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,13 @@ func Post(w http.ResponseWriter, state *terraform.State, body []byte, store stor

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, "")
}
4 changes: 4 additions & 0 deletions pkg/storage/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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()
}
4 changes: 4 additions & 0 deletions pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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)
}
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 1f58241

Please sign in to comment.