Skip to content

Commit 481d8d4

Browse files
committed
Properly allow arguments id or path_with_namespace for project data source
The problem with the `gitlab_project` data source was that if the id is a path with namespace and the reading of the data source is delayed until apply, e.g. seeing something like this: ``` # module.main.data.gitlab_project.some_project will be read during apply # (config refers to values not yet known) <= data "gitlab_project" "some_project" { + archived = (known after apply) + default_branch = (known after apply) + description = (known after apply) + http_url_to_repo = (known after apply) + id = "timo.furrer/some-project" + issues_enabled = (known after apply) + lfs_enabled = (known after apply) + merge_requests_enabled = (known after apply) + name = (known after apply) + namespace_id = (known after apply) + path = (known after apply) + path_with_namespace = (known after apply) + pipelines_enabled = (known after apply) + push_rules = (known after apply) + remove_source_branch_after_merge = (known after apply) + request_access_enabled = (known after apply) + runners_token = (known after apply) + snippets_enabled = (known after apply) + ssh_url_to_repo = (known after apply) + visibility_level = (known after apply) + web_url = (known after apply) + wiki_enabled = (known after apply) ``` The `id` is the `path_with_namespace`, which ends up like that in the state (as the string path). However, the data source in fact stores the actually project id (which it gets from the API) in the state durig apply (see https://github.com/gitlabhq/terraform-provider-gitlab/blob/8bc5606d786d73cb9e1947d0f9bc941c2e4f61b0/gitlab/data_source_gitlab_project.go#L173) which leads to an inconsistent final plan in dependent resources as shown here: ``` ╷ │ Error: Provider produced inconsistent final plan │ │ When expanding the plan for │ module.main.gitlab_repository_file.some_file │ to include new values learned so far during apply, provider │ "registry.terraform.io/timofurrer/gitlab-repository-files" produced an │ invalid new value for .project: was │ cty.StringVal("timo.furrer/some-project"), but now │ cty.StringVal("30716"). │ │ This is a bug in the provider, which should be reported in the provider's │ own issue tracker. ╵ ``` The root cause for this is that the data source always stores the actual project id in the state no matter if a `path_with_namespace` was actually given. The `id` attribute is NOT marked as being `Computed`, but it sometimes is (when the `path_with_namespace` is given). We cannot mark the `id` attribute as `Required` and `Computed`, because this is not allowed by terraform (makes sense :D). Therefore, I've used the same pattern as in other data sources (althought I'd argue that the handling is not quite consistent here across them :( ). The implementation here should be (please verify) backwards compatible. However, people can still run into the same issue as before when specifying the `path_with_namespace` in the `id` argument, but can reasolve it by switching to the `path_with_namespace` argument ;) For long term, and when we have a major release I suggest to break the interface in these data sources to make it more consistent.
1 parent 8bc5606 commit 481d8d4

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

gitlab/data_source_gitlab_project.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ func dataSourceGitlabProject() *schema.Resource {
1919
Schema: map[string]*schema.Schema{
2020
"id": {
2121
Type: schema.TypeString,
22-
Required: true,
22+
Optional: true,
23+
Computed: true,
24+
ExactlyOneOf: []string{
25+
"id",
26+
"path_with_namespace",
27+
},
28+
Description: "The ID of the project",
2329
},
2430
"name": {
2531
Type: schema.TypeString,
@@ -31,7 +37,13 @@ func dataSourceGitlabProject() *schema.Resource {
3137
},
3238
"path_with_namespace": {
3339
Type: schema.TypeString,
40+
Optional: true,
3441
Computed: true,
42+
ExactlyOneOf: []string{
43+
"id",
44+
"path_with_namespace",
45+
},
46+
Description: "The full path of the project including the namespace",
3547
},
3648
"description": {
3749
Type: schema.TypeString,
@@ -163,9 +175,16 @@ func dataSourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, me
163175

164176
log.Printf("[INFO] Reading Gitlab project")
165177

166-
v, _ := d.GetOk("id")
178+
var pid interface{}
179+
if v, ok := d.GetOk("id"); ok {
180+
pid = v
181+
} else if v, ok := d.GetOk("path_with_namespace"); ok {
182+
pid = v
183+
} else {
184+
return diag.Errorf("Must specify either id or path_with_namespace")
185+
}
167186

168-
found, _, err := client.Projects.GetProject(v, nil, gitlab.WithContext(ctx))
187+
found, _, err := client.Projects.GetProject(pid, nil, gitlab.WithContext(ctx))
169188
if err != nil {
170189
return diag.FromErr(err)
171190
}

gitlab/data_source_gitlab_project_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ resource "gitlab_project" "test"{
8383
}
8484
8585
data "gitlab_project" "foo" {
86-
id = gitlab_project.test.path_with_namespace
86+
path_with_namespace = gitlab_project.test.path_with_namespace
8787
}
8888
`, projectname, projectname)
8989
}

0 commit comments

Comments
 (0)