Skip to content

Commit 5fd4230

Browse files
committed
Support optional arguments in datasourceSchemaFromResourceSchema
1 parent 41a4ddb commit 5fd4230

6 files changed

+11
-7
lines changed

internal/provider/data_source_gitlab_project_issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var _ = registerDataSource("gitlab_project_issue", func() *schema.Resource {
1515
**Upstream API**: [GitLab API docs](https://docs.gitlab.com/ee/api/issues.html)`,
1616

1717
ReadContext: dataSourceGitlabProjectIssueRead,
18-
Schema: datasourceSchemaFromResourceSchema(gitlabProjectIssueGetSchema(), []string{"project", "iid"}),
18+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectIssueGetSchema(), []string{"project", "iid"}, nil),
1919
}
2020
})
2121

internal/provider/data_source_gitlab_project_issues.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ var _ = registerDataSource("gitlab_project_issues", func() *schema.Resource {
206206
Type: schema.TypeList,
207207
Computed: true,
208208
Elem: &schema.Resource{
209-
Schema: datasourceSchemaFromResourceSchema(gitlabProjectIssueGetSchema(), nil),
209+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectIssueGetSchema(), nil, nil),
210210
},
211211
},
212212
},

internal/provider/data_source_gitlab_project_tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var _ = registerDataSource("gitlab_project_tag", func() *schema.Resource {
1717

1818
ReadContext: dataSourceGitlabProjectTagRead,
1919
Schema: constructSchema(
20-
datasourceSchemaFromResourceSchema(gitlabProjectTagGetSchema(), []string{"name"}),
20+
datasourceSchemaFromResourceSchema(gitlabProjectTagGetSchema(), []string{"name"}, nil),
2121
map[string]*schema.Schema{
2222
"project": {
2323
Description: "The ID or URL-encoded path of the project owned by the authenticated user.",

internal/provider/data_source_gitlab_project_tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ = registerDataSource("gitlab_project_tags", func() *schema.Resource {
4444
Type: schema.TypeList,
4545
Computed: true,
4646
Elem: &schema.Resource{
47-
Schema: datasourceSchemaFromResourceSchema(gitlabProjectTagGetSchema(), nil),
47+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectTagGetSchema(), nil, nil),
4848
},
4949
},
5050
},

internal/provider/data_source_gitlab_repository_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +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: datasourceSchemaFromResourceSchema(gitlabRepositoryFileGetSchema(), []string{"project", "file_path", "ref"}),
20+
Schema: datasourceSchemaFromResourceSchema(gitlabRepositoryFileGetSchema(), []string{"project", "file_path", "ref"}, nil),
2121
}
2222
})
2323

internal/provider/util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func attributeNamesFromSchema(schema map[string]*schema.Schema) []string {
346346
// - all attributes have ForceNew, Required = false
347347
// - Validation funcs and attributes (e.g. MaxItems) are not copied
348348
// Adapted from https://github.com/hashicorp/terraform-provider-google/blob/1a72f93a8dcf6f1e59d5f25aefcb6d794a116bf5/google/datasource_helpers.go#L13
349-
func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema, arguments []string) map[string]*schema.Schema {
349+
func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema, arguments []string, optionalArguments []string) map[string]*schema.Schema {
350350
ds := make(map[string]*schema.Schema, len(rs))
351351
for k, v := range rs {
352352
dv := &schema.Schema{
@@ -362,6 +362,10 @@ func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema, arguments
362362
dv.Required = false
363363
}
364364

365+
if contains(optionalArguments, k) {
366+
dv.Optional = true
367+
}
368+
365369
switch v.Type {
366370
case schema.TypeSet:
367371
dv.Set = v.Set
@@ -373,7 +377,7 @@ func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema, arguments
373377
if elem, ok := v.Elem.(*schema.Resource); ok {
374378
// handle the case where the Element is a sub-resource
375379
dv.Elem = &schema.Resource{
376-
Schema: datasourceSchemaFromResourceSchema(elem.Schema, nil),
380+
Schema: datasourceSchemaFromResourceSchema(elem.Schema, nil, nil),
377381
}
378382
} else {
379383
// handle simple primitive case

0 commit comments

Comments
 (0)