-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add Terraform State Locking/Unlocking Support for Gitea #33277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shurkys
wants to merge
11
commits into
go-gitea:main
Choose a base branch
from
shurkys:terraform_state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+731
−2
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
351b83d
add terraform state package
0457597
fix lint
5ad2c19
fix lint
64f0886
fix view
e58fa36
fix view
0d3d5c7
make fmt
86fa672
Apply suggestions from code review
techknowlogick b5dd7ea
Some refactoring
shurkys 98775ac
Fit TF tests
shurkys 400fb38
Merge branch 'main' into terraform_state
shurkys 0dc7c55
fix lint api_packages_terraform_test
shurkys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package terraform | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"errors" | ||
"io" | ||
|
||
"code.gitea.io/gitea/modules/json" | ||
) | ||
|
||
const ( | ||
PropertyTerraformState = "terraform.state" | ||
) | ||
|
||
// Metadata represents the Terraform backend metadata | ||
// Updated to align with TerraformState structure | ||
// Includes additional metadata fields like Description, Author, and URLs | ||
type Metadata struct { | ||
Version int `json:"version"` | ||
TerraformVersion string `json:"terraform_version,omitempty"` | ||
Serial uint64 `json:"serial"` | ||
Lineage string `json:"lineage"` | ||
Outputs map[string]any `json:"outputs,omitempty"` | ||
Resources []ResourceState `json:"resources,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Author string `json:"author,omitempty"` | ||
ProjectURL string `json:"project_url,omitempty"` | ||
RepositoryURL string `json:"repository_url,omitempty"` | ||
} | ||
|
||
// ResourceState represents the state of a resource | ||
type ResourceState struct { | ||
Mode string `json:"mode"` | ||
Type string `json:"type"` | ||
Name string `json:"name"` | ||
Provider string `json:"provider"` | ||
Instances []InstanceState `json:"instances"` | ||
} | ||
|
||
// InstanceState represents the state of a resource instance | ||
type InstanceState struct { | ||
SchemaVersion int `json:"schema_version"` | ||
Attributes map[string]any `json:"attributes"` | ||
} | ||
|
||
// ParseMetadataFromState retrieves metadata from the archive with Terraform state | ||
func ParseMetadataFromState(r io.Reader) (*Metadata, error) { | ||
gzr, err := gzip.NewReader(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer gzr.Close() | ||
|
||
tr := tar.NewReader(gzr) | ||
for { | ||
hd, err := tr.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if hd.Typeflag != tar.TypeReg { | ||
continue | ||
} | ||
|
||
// Looking for the state.json file | ||
if hd.Name == "state.json" { | ||
return ParseStateFile(tr) | ||
} | ||
} | ||
|
||
return nil, errors.New("state.json not found in archive") | ||
} | ||
|
||
// ParseStateFile parses the state.json file and returns Terraform metadata | ||
func ParseStateFile(r io.Reader) (*Metadata, error) { | ||
var stateData Metadata | ||
if err := json.NewDecoder(r).Decode(&stateData); err != nil { | ||
return nil, err | ||
} | ||
return &stateData, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
techknowlogick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// SPDX-License-Identifier: MIT | ||
|
||
package terraform | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"compress/gzip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestParseMetadataFromState tests the ParseMetadataFromState function | ||
func TestParseMetadataFromState(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []byte | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "valid state file", | ||
input: createValidStateArchive(), | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "missing state.json file", | ||
input: createInvalidStateArchive(), | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "corrupt archive", | ||
input: []byte("invalid archive data"), | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := bytes.NewReader(tt.input) | ||
metadata, err := ParseMetadataFromState(r) | ||
|
||
if tt.expectedError { | ||
assert.Error(t, err) | ||
assert.Nil(t, metadata) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, metadata) | ||
// Optionally, check if certain fields are populated correctly | ||
assert.NotEmpty(t, metadata.Lineage) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// createValidStateArchive creates a valid TAR.GZ archive with a sample state.json | ||
func createValidStateArchive() []byte { | ||
metadata := `{ | ||
"version": 4, | ||
"terraform_version": "1.2.0", | ||
"serial": 1, | ||
"lineage": "abc123", | ||
"resources": [], | ||
"description": "Test project", | ||
"author": "Test Author", | ||
"project_url": "http://example.com", | ||
"repository_url": "http://repo.com" | ||
}` | ||
|
||
// Create a gzip writer and tar writer | ||
buf := new(bytes.Buffer) | ||
gz := gzip.NewWriter(buf) | ||
tw := tar.NewWriter(gz) | ||
|
||
// Add the state.json file to the tar | ||
hdr := &tar.Header{ | ||
Name: "state.json", | ||
Size: int64(len(metadata)), | ||
Mode: 0o600, | ||
} | ||
if err := tw.WriteHeader(hdr); err != nil { | ||
panic(err) | ||
} | ||
if _, err := tw.Write([]byte(metadata)); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Close the writers | ||
if err := tw.Close(); err != nil { | ||
panic(err) | ||
} | ||
if err := gz.Close(); err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf.Bytes() | ||
} | ||
|
||
// createInvalidStateArchive creates an invalid TAR.GZ archive (missing state.json) | ||
func createInvalidStateArchive() []byte { | ||
// Create a tar archive without the state.json file | ||
buf := new(bytes.Buffer) | ||
gz := gzip.NewWriter(buf) | ||
tw := tar.NewWriter(gz) | ||
|
||
// Add an empty file to the tar (but not state.json) | ||
hdr := &tar.Header{ | ||
Name: "other_file.txt", | ||
Size: 0, | ||
Mode: 0o600, | ||
} | ||
if err := tw.WriteHeader(hdr); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Close the writers | ||
if err := tw.Close(); err != nil { | ||
panic(err) | ||
} | ||
if err := gz.Close(); err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf.Bytes() | ||
} | ||
|
||
// TestParseStateFile tests the ParseStateFile function directly | ||
func TestParseStateFile(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "valid state.json", | ||
input: `{"version":4,"terraform_version":"1.2.0","serial":1,"lineage":"abc123"}`, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "invalid JSON", | ||
input: `{"version":4,"terraform_version"}`, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := bytes.NewReader([]byte(tt.input)) | ||
metadata, err := ParseStateFile(r) | ||
|
||
if tt.expectedError { | ||
assert.Error(t, err) | ||
assert.Nil(t, metadata) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, metadata) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.