|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + gitlab "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = registerDataSource("gitlab_repository_file", func() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: "Allows you to receive information about file in repository like name, size, content. File content is Base64 encoded. This endpoint can be accessed without authentication if the repository is publicly accessible.", |
| 16 | + ReadContext: dataSourceGitlabRepositoryFileRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "project": { |
| 19 | + Description: "The ID of the project.", |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "file_path": { |
| 24 | + Description: "The full path of the file. It must be relative to the root of the project without a leading slash `/`.", |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + }, |
| 28 | + "ref": { |
| 29 | + Description: "The name of branch, tag or commit.", |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + "file_name": { |
| 34 | + Description: "String, file name.", |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + "size": { |
| 39 | + Description: "Integer, file size.", |
| 40 | + Type: schema.TypeInt, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "encoding": { |
| 44 | + Description: "String, file encoding.", |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "content": { |
| 49 | + Description: "String, base64 encoded file content.", |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "content_sha256": { |
| 54 | + Description: "String, content sha256 digest.", |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "blob_id": { |
| 59 | + Description: "String, blob id.", |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "commit_id": { |
| 64 | + Description: "String, commit id.", |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "last_commit_id": { |
| 69 | + Description: "String, last commit id.", |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +}) |
| 76 | + |
| 77 | +func dataSourceGitlabRepositoryFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 78 | + client := meta.(*gitlab.Client) |
| 79 | + project := d.Get("project").(string) |
| 80 | + filePath := d.Get("file_path").(string) |
| 81 | + |
| 82 | + options := &gitlab.GetFileOptions{ |
| 83 | + Ref: gitlab.String(d.Get("ref").(string)), |
| 84 | + } |
| 85 | + |
| 86 | + repositoryFile, resp, err := client.RepositoryFiles.GetFile(project, filePath, options, gitlab.WithContext(ctx)) |
| 87 | + if err != nil { |
| 88 | + log.Printf("[DEBUG] file %s not found, response %v", filePath, resp) |
| 89 | + return diag.FromErr(err) |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(fmt.Sprintf("%s:%s:%s", project, repositoryFile.Ref, repositoryFile.FilePath)) |
| 93 | + |
| 94 | + d.Set("project", project) |
| 95 | + d.Set("file_name", repositoryFile.FileName) |
| 96 | + d.Set("file_path", repositoryFile.FilePath) |
| 97 | + d.Set("size", repositoryFile.Size) |
| 98 | + d.Set("encoding", repositoryFile.Encoding) |
| 99 | + d.Set("content", repositoryFile.Content) |
| 100 | + d.Set("content_sha256", repositoryFile.SHA256) |
| 101 | + d.Set("ref", repositoryFile.Ref) |
| 102 | + d.Set("blob_id", repositoryFile.BlobID) |
| 103 | + d.Set("commit_id", repositoryFile.CommitID) |
| 104 | + d.Set("last_commit_id", repositoryFile.LastCommitID) |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
0 commit comments