Skip to content

Commit e5478a3

Browse files
committed
datasource/gitlab_repository_file: Refactored to avoid code duplication
1 parent c1e131f commit e5478a3

File tree

6 files changed

+145
-137
lines changed

6 files changed

+145
-137
lines changed

docs/data-sources/repository_file.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ data "gitlab_repository_file" "example" {
2929
### Required
3030

3131
- **file_path** (String) The full path of the file. It must be relative to the root of the project without a leading slash `/`.
32-
- **project** (String) The ID of the project.
32+
- **project** (String) The name or ID of the project.
3333
- **ref** (String) The name of branch, tag or commit.
3434

3535
### Optional
@@ -38,13 +38,13 @@ data "gitlab_repository_file" "example" {
3838

3939
### Read-Only
4040

41-
- **blob_id** (String) String, blob id.
42-
- **commit_id** (String) String, commit id.
43-
- **content** (String) String, base64 encoded file content.
44-
- **content_sha256** (String) String, content sha256 digest.
45-
- **encoding** (String) String, file encoding.
46-
- **file_name** (String) String, file name.
47-
- **last_commit_id** (String) String, last commit id.
48-
- **size** (Number) Integer, file size.
41+
- **blob_id** (String) The blob id.
42+
- **commit_id** (String) The commit id.
43+
- **content** (String) base64 encoded file content. No other encoding is currently supported, because of a [GitLab API bug](https://gitlab.com/gitlab-org/gitlab/-/issues/342430).
44+
- **content_sha256** (String) File content sha256 digest.
45+
- **encoding** (String) The file content encoding.
46+
- **file_name** (String) The filename.
47+
- **last_commit_id** (String) The last known commit id.
48+
- **size** (Number) The file size.
4949

5050

docs/resources/repository_file.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ resource "gitlab_repository_file" "this" {
4949
- **commit_message** (String) Commit message.
5050
- **content** (String) base64 encoded file content. No other encoding is currently supported, because of a [GitLab API bug](https://gitlab.com/gitlab-org/gitlab/-/issues/342430).
5151
- **file_path** (String) The full path of the file. It must be relative to the root of the project without a leading slash `/`.
52-
- **project** (String) The ID of the project.
52+
- **project** (String) The name or ID of the project.
5353

5454
### Optional
5555

@@ -60,7 +60,14 @@ resource "gitlab_repository_file" "this" {
6060

6161
### Read-Only
6262

63-
- **encoding** (String) Content encoding.
63+
- **blob_id** (String) The blob id.
64+
- **commit_id** (String) The commit id.
65+
- **content_sha256** (String) File content sha256 digest.
66+
- **encoding** (String) The file content encoding.
67+
- **file_name** (String) The filename.
68+
- **last_commit_id** (String) The last known commit id.
69+
- **ref** (String) The name of branch, tag or commit.
70+
- **size** (Number) The file size.
6471

6572
## Import
6673

internal/provider/data_source_gitlab_repository_file.go

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,7 @@ var _ = registerDataSource("gitlab_repository_file", func() *schema.Resource {
1717
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/repository_files.html)`,
1818

1919
ReadContext: dataSourceGitlabRepositoryFileRead,
20-
Schema: map[string]*schema.Schema{
21-
"project": {
22-
Description: "The ID of the project.",
23-
Type: schema.TypeString,
24-
Required: true,
25-
},
26-
"file_path": {
27-
Description: "The full path of the file. It must be relative to the root of the project without a leading slash `/`.",
28-
Type: schema.TypeString,
29-
Required: true,
30-
},
31-
"ref": {
32-
Description: "The name of branch, tag or commit.",
33-
Type: schema.TypeString,
34-
Required: true,
35-
},
36-
"file_name": {
37-
Description: "String, file name.",
38-
Type: schema.TypeString,
39-
Computed: true,
40-
},
41-
"size": {
42-
Description: "Integer, file size.",
43-
Type: schema.TypeInt,
44-
Computed: true,
45-
},
46-
"encoding": {
47-
Description: "String, file encoding.",
48-
Type: schema.TypeString,
49-
Computed: true,
50-
},
51-
"content": {
52-
Description: "String, base64 encoded file content.",
53-
Type: schema.TypeString,
54-
Computed: true,
55-
},
56-
"content_sha256": {
57-
Description: "String, content sha256 digest.",
58-
Type: schema.TypeString,
59-
Computed: true,
60-
},
61-
"blob_id": {
62-
Description: "String, blob id.",
63-
Type: schema.TypeString,
64-
Computed: true,
65-
},
66-
"commit_id": {
67-
Description: "String, commit id.",
68-
Type: schema.TypeString,
69-
Computed: true,
70-
},
71-
"last_commit_id": {
72-
Description: "String, last commit id.",
73-
Type: schema.TypeString,
74-
Computed: true,
75-
},
76-
},
20+
Schema: datasourceSchemaFromResourceSchema(gitlabRepositoryFileGetSchema(), "project", "file_path", "ref"),
7721
}
7822
})
7923

@@ -94,17 +38,9 @@ func dataSourceGitlabRepositoryFileRead(ctx context.Context, d *schema.ResourceD
9438

9539
d.SetId(fmt.Sprintf("%s:%s:%s", project, repositoryFile.Ref, repositoryFile.FilePath))
9640

97-
d.Set("project", project)
98-
d.Set("file_name", repositoryFile.FileName)
99-
d.Set("file_path", repositoryFile.FilePath)
100-
d.Set("size", repositoryFile.Size)
101-
d.Set("encoding", repositoryFile.Encoding)
102-
d.Set("content", repositoryFile.Content)
103-
d.Set("content_sha256", repositoryFile.SHA256)
104-
d.Set("ref", repositoryFile.Ref)
105-
d.Set("blob_id", repositoryFile.BlobID)
106-
d.Set("commit_id", repositoryFile.CommitID)
107-
d.Set("last_commit_id", repositoryFile.LastCommitID)
108-
41+
stateMap := gitlabRepositoryFileToStateMap(project, repositoryFile)
42+
if err := setStateMapInResourceData(stateMap, d); err != nil {
43+
return diag.FromErr(err)
44+
}
10945
return nil
11046
}

internal/provider/data_source_gitlab_repository_file_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ resource "gitlab_repository_file" "foo" {
6767
}
6868
6969
data "gitlab_repository_file" "foo" {
70-
project = %s
71-
file_path = "testfile-meow"
70+
project = gitlab_repository_file.foo.project
71+
file_path = gitlab_repository_file.foo.file_path
7272
ref = "main"
7373
}
74-
`, project, project)
74+
`, project)
7575
}

internal/provider/resource_gitlab_repository_file.go

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,37 @@ var _ = registerResource("gitlab_repository_file", func() *schema.Resource {
3434
// However, we don't support the `encoding` parameter as it seems to be broken.
3535
// Only a value of `base64` is supported, all others, including the documented default `text`, lead to
3636
// a `400 {error: encoding does not have a valid value}` error.
37-
Schema: map[string]*schema.Schema{
38-
"project": {
39-
Description: "The ID of the project.",
40-
Type: schema.TypeString,
41-
Required: true,
42-
ForceNew: true,
37+
Schema: constructSchema(
38+
map[string]*schema.Schema{
39+
"branch": {
40+
Description: "Name of the branch to which to commit to.",
41+
Type: schema.TypeString,
42+
Required: true,
43+
ForceNew: true,
44+
},
45+
"start_branch": {
46+
Description: "Name of the branch to start the new commit from.",
47+
Type: schema.TypeString,
48+
Optional: true,
49+
},
50+
"author_email": {
51+
Description: "Email of the commit author.",
52+
Type: schema.TypeString,
53+
Optional: true,
54+
},
55+
"author_name": {
56+
Description: "Name of the commit author.",
57+
Type: schema.TypeString,
58+
Optional: true,
59+
},
60+
"commit_message": {
61+
Description: "Commit message.",
62+
Type: schema.TypeString,
63+
Required: true,
64+
},
4365
},
44-
"file_path": {
45-
Description: "The full path of the file. It must be relative to the root of the project without a leading slash `/`.",
46-
Type: schema.TypeString,
47-
Required: true,
48-
ForceNew: true,
49-
},
50-
"branch": {
51-
Description: "Name of the branch to which to commit to.",
52-
Type: schema.TypeString,
53-
Required: true,
54-
ForceNew: true,
55-
},
56-
"start_branch": {
57-
Description: "Name of the branch to start the new commit from.",
58-
Type: schema.TypeString,
59-
Optional: true,
60-
},
61-
"author_email": {
62-
Description: "Email of the commit author.",
63-
Type: schema.TypeString,
64-
Optional: true,
65-
},
66-
"author_name": {
67-
Description: "Name of the commit author.",
68-
Type: schema.TypeString,
69-
Optional: true,
70-
},
71-
"content": {
72-
Description: "base64 encoded file content. No other encoding is currently supported, because of a [GitLab API bug](https://gitlab.com/gitlab-org/gitlab/-/issues/342430).",
73-
Type: schema.TypeString,
74-
Required: true,
75-
ValidateFunc: validateBase64Content,
76-
},
77-
"commit_message": {
78-
Description: "Commit message.",
79-
Type: schema.TypeString,
80-
Required: true,
81-
},
82-
"encoding": {
83-
Description: "Content encoding.",
84-
Type: schema.TypeString,
85-
Computed: true,
86-
},
87-
},
66+
gitlabRepositoryFileGetSchema(),
67+
),
8868
}
8969
})
9070

@@ -136,11 +116,11 @@ func resourceGitlabRepositoryFileRead(ctx context.Context, d *schema.ResourceDat
136116
}
137117

138118
d.SetId(resourceGitLabRepositoryFileBuildId(project, branch, repositoryFile.FilePath))
139-
d.Set("project", project)
140-
d.Set("file_path", repositoryFile.FilePath)
141119
d.Set("branch", repositoryFile.Ref)
142-
d.Set("encoding", repositoryFile.Encoding)
143-
d.Set("content", repositoryFile.Content)
120+
stateMap := gitlabRepositoryFileToStateMap(project, repositoryFile)
121+
if err = setStateMapInResourceData(stateMap, d); err != nil {
122+
return diag.FromErr(err)
123+
}
144124

145125
return nil
146126
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package provider
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
"github.com/xanzy/go-gitlab"
6+
)
7+
8+
func gitlabRepositoryFileGetSchema() map[string]*schema.Schema {
9+
return map[string]*schema.Schema{
10+
"project": {
11+
Description: "The name or ID of the project.",
12+
Type: schema.TypeString,
13+
Required: true,
14+
ForceNew: true,
15+
},
16+
"file_path": {
17+
Description: "The full path of the file. It must be relative to the root of the project without a leading slash `/`.",
18+
Type: schema.TypeString,
19+
Required: true,
20+
ForceNew: true,
21+
},
22+
"ref": {
23+
Description: "The name of branch, tag or commit.",
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"file_name": {
28+
Description: "The filename.",
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"size": {
33+
Description: "The file size.",
34+
Type: schema.TypeInt,
35+
Computed: true,
36+
},
37+
"encoding": {
38+
Description: "The file content encoding.",
39+
Type: schema.TypeString,
40+
Computed: true,
41+
},
42+
"content": {
43+
Description: "base64 encoded file content. No other encoding is currently supported, because of a [GitLab API bug](https://gitlab.com/gitlab-org/gitlab/-/issues/342430).",
44+
Type: schema.TypeString,
45+
Required: true,
46+
ValidateFunc: validateBase64Content,
47+
},
48+
"content_sha256": {
49+
Description: "File content sha256 digest.",
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"blob_id": {
54+
Description: "The blob id.",
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"commit_id": {
59+
Description: "The commit id.",
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"last_commit_id": {
64+
Description: "The last known commit id.",
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
68+
}
69+
}
70+
71+
func gitlabRepositoryFileToStateMap(project string, repositoryFile *gitlab.File) map[string]interface{} {
72+
stateMap := make(map[string]interface{})
73+
stateMap["project"] = project
74+
stateMap["file_name"] = repositoryFile.FileName
75+
stateMap["file_path"] = repositoryFile.FilePath
76+
stateMap["size"] = repositoryFile.Size
77+
stateMap["encoding"] = repositoryFile.Encoding
78+
stateMap["content"] = repositoryFile.Content
79+
stateMap["content_sha256"] = repositoryFile.SHA256
80+
stateMap["ref"] = repositoryFile.Ref
81+
stateMap["blob_id"] = repositoryFile.BlobID
82+
stateMap["commit_id"] = repositoryFile.CommitID
83+
stateMap["last_commit_id"] = repositoryFile.LastCommitID
84+
return stateMap
85+
}

0 commit comments

Comments
 (0)