Skip to content

Commit cc664f9

Browse files
authored
Merge pull request #338 from breml/cleanup-generated-datasources-projects-targets
Cleanup generated datasources project and target attributes
2 parents cd137b3 + 01a8994 commit cc664f9

28 files changed

+50
-141
lines changed

cmd/generate-datasources/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ type Entity struct {
4949
// `has-no-project` needs to be set to `true`.
5050
HasNoProject bool `yaml:"has-no-project"`
5151

52+
// If a resource has a target.
53+
// Most resources do not have a target attribute. If a resource does have a
54+
// target, `has-target` needs to be set to `true`.
55+
HasTarget bool `yaml:"has-target"`
56+
5257
// If a resource has no status attribute.
5358
// Most resources do have a status attribute. If this is not the case,
5459
// `has-no-status` needs to be set to `true`.

cmd/generate-datasources/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ type entityArgs struct {
113113
// Inverted value from `has-no-project`.
114114
HasProject bool
115115

116+
// Value from `has-target`.
117+
HasTarget bool
118+
116119
// Inverted value from `has-no-status`.
117120
HasStatus bool
118121

@@ -189,6 +192,7 @@ func main() {
189192
HasParent: entity.ParentName != "",
190193
ParentName: entity.ParentName,
191194
HasProject: !entity.HasNoProject,
195+
HasTarget: entity.HasTarget,
192196
HasStatus: !entity.HasNoStatus,
193197
HasLocation: entity.HasLocation,
194198
HasLocations: entity.HasLocations,

cmd/generate-datasources/tmpl/datasource.go.gotmpl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,22 @@ import (
8989
"context"
9090
"fmt"
9191

92+
{{ if or .HasProject .HasTarget -}}
9293
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
93-
{{ if $requireAttr }}"github.com/hashicorp/terraform-plugin-framework/attr"{{ end }}
94+
{{ end -}}
95+
{{ if $requireAttr -}}
96+
"github.com/hashicorp/terraform-plugin-framework/attr"
97+
{{ end -}}
9498
"github.com/hashicorp/terraform-plugin-framework/datasource"
9599
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
100+
{{ if or .HasProject .HasTarget -}}
96101
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
102+
{{ end -}}
97103
"github.com/hashicorp/terraform-plugin-framework/diag"
98104
"github.com/hashicorp/terraform-plugin-framework/types"
99-
{{ if $requireBasetypes }}"github.com/hashicorp/terraform-plugin-framework/types/basetypes"{{ end }}
105+
{{ if $requireBasetypes -}}
106+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
107+
{{ end }}
100108

101109
"github.com/lxc/terraform-provider-incus/internal/common"
102110
"github.com/lxc/terraform-provider-incus/internal/errors"
@@ -114,7 +122,9 @@ type {{ .Name | pascalcase }}DataSourceModel struct {
114122
{{- if .HasProject }}
115123
Project types.String `tfsdk:"project"`
116124
{{- end }}
125+
{{- if .HasTarget }}
117126
Target types.String `tfsdk:"target"`
127+
{{- end }}
118128
Remote types.String `tfsdk:"remote"`
119129

120130
Description types.String `tfsdk:"description"`
@@ -182,13 +192,15 @@ func (d *{{ .Name | pascalcase }}DataSource) Schema(ctx context.Context, req dat
182192
"remote": schema.StringAttribute{
183193
Optional: true,
184194
},
195+
{{- if .HasTarget }}
185196

186197
"target": schema.StringAttribute{
187198
Optional: true,
188199
Validators: []validator.String{
189200
stringvalidator.LengthAtLeast(1),
190201
},
191202
},
203+
{{- end }}
192204

193205
"description": schema.StringAttribute{
194206
Optional: true,
@@ -304,7 +316,11 @@ func (d *{{ .Name | pascalcase }}DataSource) Read(ctx context.Context, req datas
304316
{{- else }}
305317
providerProjectName := ""
306318
{{- end }}
319+
{{- if .HasTarget }}
307320
providerTarget := state.Target.ValueString()
321+
{{- else }}
322+
providerTarget := ""
323+
{{- end }}
308324
server, err := d.provider.InstanceServer(providerRemote, providerProjectName, providerTarget)
309325
if err != nil {
310326
resp.Diagnostics.Append(errors.NewInstanceServerError(err))

cmd/generate-datasources/tmpl/docs.md.gotmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ output "{{ .Name }}_{{ .ObjectNamePropertyName }}" {
5959
{{- with $.ExtraDescriptions.remote }}
6060
{{ . | indent 2 }}
6161
{{- end }}
62+
{{- if .HasTarget }}
6263

6364
* `target` - *Optional* - Specify a target node in a cluster.
6465
{{- with $.ExtraDescriptions.target }}
6566
{{ . | indent 2 }}
6667
{{- end }}
68+
{{- end }}
6769

6870
## Attribute Reference
6971

docs/data-sources/instance.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ output "instance_name" {
2424
* `remote` - *Optional* - The remote in which the resource was created. If
2525
not provided, the provider's default remote will be used.
2626

27-
* `target` - *Optional* - Specify a target node in a cluster.
28-
2927
## Attribute Reference
3028

3129
* `description` - Description of the instance.

docs/data-sources/network_acl.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ output "network_acl_name" {
2424
* `remote` - *Optional* - The remote in which the resource was created. If
2525
not provided, the provider's default remote will be used.
2626

27-
* `target` - *Optional* - Specify a target node in a cluster.
28-
2927
## Attribute Reference
3028

3129
* `description` - Description of the network ACL.

docs/data-sources/network_address_set.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ output "network_address_set_name" {
2424
* `remote` - *Optional* - The remote in which the resource was created. If
2525
not provided, the provider's default remote will be used.
2626

27-
* `target` - *Optional* - Specify a target node in a cluster.
28-
2927
## Attribute Reference
3028

3129
* `description` - Description of the network address set.

docs/data-sources/network_integration.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ output "network_integration_name" {
1919

2020
* `name` - **Required** - Name of the network integration.
2121

22-
* `project` - *Optional* - Name of the project where the network integration is be stored.
23-
2422
* `remote` - *Optional* - The remote in which the resource was created. If
2523
not provided, the provider's default remote will be used.
2624

27-
* `target` - *Optional* - Specify a target node in a cluster.
28-
2925
## Attribute Reference
3026

3127
* `description` - Description of the network integration.

docs/data-sources/network_load_balancer.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ output "network_load_balancer_listen_address" {
2727
* `remote` - *Optional* - The remote in which the resource was created. If
2828
not provided, the provider's default remote will be used.
2929

30-
* `target` - *Optional* - Specify a target node in a cluster.
31-
3230
## Attribute Reference
3331

3432
* `description` - Description of the network load balancer.

docs/data-sources/network_peer.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ output "network_peer_name" {
2727
* `remote` - *Optional* - The remote in which the resource was created. If
2828
not provided, the provider's default remote will be used.
2929

30-
* `target` - *Optional* - Specify a target node in a cluster.
31-
3230
## Attribute Reference
3331

3432
* `description` - Description of the network peer.

0 commit comments

Comments
 (0)