-
Notifications
You must be signed in to change notification settings - Fork 42
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
datasources added: artifactory_file & artifactory_fileinfo #65
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b0dfd46
datasource artifactory_file added
volkc-basf 398f588
fixing code format using gofmt
volkc-basf 51a2b10
go-artifactory lib: switched to v2.5
volkc-basf 29c8eeb
stick to naming conventions for datasources that are used by other pr…
volkc-basf 0ca7f19
dataSources don't need to implement Create, Update and Delete functions.
volkc-basf 3c21e86
code format
volkc-basf ced27f4
added a second datasource that can be used to access file infos only
volkc-basf c9761ff
added docs for artifactory_file & artifactory_fileinfo
volkc-basf ec96e22
Added two links to docs of the new datasources.
volkc-basf 4d4999d
refactoring
volkc-basf 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 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 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 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,167 @@ | ||
package artifactory | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"github.com/atlassian/go-artifactory/v2/artifactory" | ||
"github.com/atlassian/go-artifactory/v2/artifactory/v1" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"io" | ||
"os" | ||
) | ||
|
||
func dataSourceArtifactoryFile() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceFileRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_modified": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"modified_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_updated": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"download_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"mimetype": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"size": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"md5": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha1": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha256": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"output_path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"force_overwrite": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceFileRead(d *schema.ResourceData, m interface{}) error { | ||
c := m.(*artifactory.Artifactory) | ||
|
||
repository := d.Get("repository").(string) | ||
path := d.Get("path").(string) | ||
outputPath := d.Get("output_path").(string) | ||
forceOverwrite := d.Get("force_overwrite").(bool) | ||
|
||
fileInfo, _, err := c.V1.Artifacts.FileInfo(context.Background(), repository, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
skip, err := SkipDownload(fileInfo, outputPath) | ||
if err != nil && !forceOverwrite { | ||
return err | ||
} | ||
|
||
if !skip { | ||
outFile, err := os.Create(outputPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer outFile.Close() | ||
|
||
fileInfo, _, err = c.V1.Artifacts.FileContents(context.Background(), repository, path, outFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return packFileInfo(fileInfo, d) | ||
} | ||
|
||
func SkipDownload(fileInfo *v1.FileInfo, path string) (bool, error) { | ||
const skip = true | ||
const dontSkip = false | ||
|
||
if path == "" { | ||
// no path specified, nothing to download | ||
return skip, nil | ||
} | ||
|
||
if FileExists(path) { | ||
chks_matches, err := VerifySha256Checksum(path, *fileInfo.Checksums.Sha256) | ||
|
||
if chks_matches { | ||
return skip, nil | ||
} else if err != nil { | ||
return dontSkip, err | ||
} else { | ||
return dontSkip, fmt.Errorf("Local file differs from upstream version") | ||
} | ||
} else { | ||
return dontSkip, nil | ||
} | ||
} | ||
|
||
func FileExists(path string) bool { | ||
if _, err := os.Stat(path); err != nil { | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func VerifySha256Checksum(path string, expectedSha256 string) (bool, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
hasher := sha256.New() | ||
|
||
if _, err := io.Copy(hasher, f); err != nil { | ||
return false, err | ||
} | ||
|
||
return hex.EncodeToString(hasher.Sum(nil)) == expectedSha256, nil | ||
} |
This file contains 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,91 @@ | ||
package artifactory | ||
|
||
import ( | ||
"github.com/atlassian/go-artifactory/v2/artifactory/v1" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestSkipDownload(t *testing.T) { | ||
const testString = "test content" | ||
const expectedSha256 = "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72" | ||
|
||
file, err := CreateTempFile(testString) | ||
|
||
assert.Nil(t, err) | ||
|
||
defer CloseAndRemove(file) | ||
|
||
existingPath, _ := filepath.Abs(file.Name()) | ||
nonExistingPath := existingPath + "-doesnt-exist" | ||
|
||
sha256 := expectedSha256 | ||
fileInfo := new(v1.FileInfo) | ||
fileInfo.Checksums = new(v1.Checksums) | ||
fileInfo.Checksums.Sha256 = &sha256 | ||
|
||
skip, err := SkipDownload(fileInfo, existingPath) | ||
assert.Equal(t, true, skip) // file exists, checksum matches => skip | ||
assert.Nil(t, err) | ||
|
||
skip, err = SkipDownload(fileInfo, nonExistingPath) | ||
assert.Equal(t, false, skip) // file doesn't exist => dont skip | ||
assert.Nil(t, err) | ||
|
||
sha256 = "6666666666666666666666666666666666666666666666666666666666666666" | ||
fileInfo.Checksums.Sha256 = &sha256 | ||
|
||
skip, err = SkipDownload(fileInfo, existingPath) | ||
assert.Equal(t, false, skip) // file exists, checksum doesnt match => dont skip & err | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestFileExists(t *testing.T) { | ||
tmpFile, err := CreateTempFile("test") | ||
|
||
assert.Nil(t, err) | ||
|
||
defer CloseAndRemove(tmpFile) | ||
|
||
existingPath, _ := filepath.Abs(tmpFile.Name()) | ||
nonExistingPath := existingPath + "-doesnt-exist" | ||
|
||
assert.Equal(t, true, FileExists(existingPath)) | ||
assert.Equal(t, false, FileExists(nonExistingPath)) | ||
} | ||
|
||
func TestVerifySha256Checksum(t *testing.T) { | ||
const testString = "test content" | ||
const expectedSha256 = "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72" | ||
|
||
file, err := CreateTempFile(testString) | ||
|
||
assert.Nil(t, err) | ||
|
||
defer CloseAndRemove(file) | ||
|
||
filePath, _ := filepath.Abs(file.Name()) | ||
|
||
sha256Verified, err := VerifySha256Checksum(filePath, expectedSha256) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, true, sha256Verified) | ||
} | ||
|
||
func CreateTempFile(content string) (f *os.File, err error) { | ||
file, err := ioutil.TempFile(os.TempDir(), "terraform-provider-artifactory-") | ||
|
||
if content != "" { | ||
file.WriteString(content) | ||
} | ||
|
||
return file, err | ||
} | ||
|
||
func CloseAndRemove(f *os.File) { | ||
f.Close() | ||
os.Remove(f.Name()) | ||
} |
This file contains 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,112 @@ | ||
package artifactory | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/atlassian/go-artifactory/v2/artifactory" | ||
"github.com/atlassian/go-artifactory/v2/artifactory/v1" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceArtifactoryFileInfo() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceFileInfoRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_modified": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"modified_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_updated": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"download_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"mimetype": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"size": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"md5": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha1": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha256": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceFileInfoRead(d *schema.ResourceData, m interface{}) error { | ||
c := m.(*artifactory.Artifactory) | ||
|
||
repository := d.Get("repository").(string) | ||
path := d.Get("path").(string) | ||
|
||
fileInfo, _, err := c.V1.Artifacts.FileInfo(context.Background(), repository, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return packFileInfo(fileInfo, d) | ||
} | ||
|
||
func packFileInfo(fileInfo *v1.FileInfo, d *schema.ResourceData) error { | ||
hasErr := false | ||
logErr := cascadingErr(&hasErr) | ||
|
||
d.SetId(*fileInfo.DownloadUri) | ||
|
||
logErr(d.Set("created", *fileInfo.Created)) | ||
logErr(d.Set("created_by", *fileInfo.CreatedBy)) | ||
logErr(d.Set("last_modified", *fileInfo.LastModified)) | ||
logErr(d.Set("modified_by", *fileInfo.ModifiedBy)) | ||
logErr(d.Set("last_updated", *fileInfo.LastUpdated)) | ||
logErr(d.Set("download_uri", *fileInfo.DownloadUri)) | ||
logErr(d.Set("mimetype", *fileInfo.MimeType)) | ||
logErr(d.Set("size", *fileInfo.Size)) | ||
|
||
if fileInfo.Checksums != nil { | ||
logErr(d.Set("md5", *fileInfo.Checksums.Md5)) | ||
logErr(d.Set("sha1", *fileInfo.Checksums.Sha1)) | ||
logErr(d.Set("sha256", *fileInfo.Checksums.Sha256)) | ||
} | ||
|
||
if hasErr { | ||
return fmt.Errorf("failed to pack fileInfo") | ||
} | ||
|
||
return nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that
output_path
is required, do we still need this? Maybe the verification of the SHAs can be here insteadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to refactor that part, however I believe it's not safe to verify the checksum only. There are multiple reasons why the checksum verification could fail:
However we only want to proceed with the download in the first case. Having that said, we'll still need to check whether the file exists or not.