generated from hashicorp/terraform-provider-scaffolding-framework
/
datasource_tenant.go
136 lines (114 loc) · 4 KB
/
datasource_tenant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package provider
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ datasource.DataSource = (*TenantDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*TenantDataSource)(nil)
_ datasource.DataSourceWithConfigValidators = (*TenantDataSource)(nil)
)
func NewTenantDataSource() datasource.DataSource {
return &TenantDataSource{}
}
// TenantDataSource defines the data source implementation.
type TenantDataSource struct {
client *client.Client
}
// TenantDataSourceModel describes the data source data model.
type TenantDataSourceModel struct {
SpaceID types.String `tfsdk:"space_id"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func (d *TenantDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, res *datasource.MetadataResponse) {
res.TypeName = req.ProviderTypeName + "_tenant"
}
// Configure adds the provider configured client to the data source.
func (d *TenantDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, res *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.Client)
if !ok {
res.Diagnostics.Append(ErrUnexpectedDataSourceConfigureType(req.ProviderData))
return
}
d.client = client
}
func (d *TenantDataSource) ConfigValidators(context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{datasourcevalidator.AtLeastOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
)}
}
func (d *TenantDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, res *datasource.SchemaResponse) {
res.Schema = schema.Schema{
MarkdownDescription: "Use this data source to get the ID of a tenant",
Attributes: map[string]schema.Attribute{
"space_id": schema.StringAttribute{
MarkdownDescription: "ID of the space",
Computed: true,
Optional: true,
},
"id": schema.StringAttribute{
MarkdownDescription: "ID of the tenant",
Computed: true,
Optional: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the tenant",
Computed: true,
Optional: true,
},
},
}
}
func (d *TenantDataSource) Read(ctx context.Context, req datasource.ReadRequest, res *datasource.ReadResponse) {
var data TenantDataSourceModel
if res.Diagnostics.Append(req.Config.Get(ctx, &data)...); res.Diagnostics.HasError() {
return
}
spaceID := data.SpaceID.ValueString()
name := data.Name.ValueString()
id := data.ID.ValueString()
query := tenants.TenantsQuery{
Name: name,
IDs: []string{id},
Skip: 0,
Take: 1,
}
identifier := id
if name != "" {
identifier = name
}
tflog.Debug(ctx, "fetched tenant", map[string]interface{}{"tenant_identifier": identifier, "space_id": spaceID})
tenants, err := tenants.Get(d.client, spaceID, query)
if err != nil {
res.Diagnostics.AddError(fmt.Sprintf("Failed to fetch tenant %s", identifier), err.Error())
return
}
if len(tenants.Items) < 1 {
res.Diagnostics.AddError(fmt.Sprintf("Failed to fetch tenant %s", identifier), "tenant not found")
return
}
tenant := tenants.Items[0]
tflog.Debug(ctx, "fetched tenant", map[string]interface{}{"tenant": tenant})
model := TenantDataSourceModel{
SpaceID: types.StringValue(tenant.SpaceID),
ID: types.StringValue(tenant.ID),
Name: types.StringValue(tenant.Name),
}
if res.Diagnostics.Append(res.State.Set(ctx, &model)...); res.Diagnostics.HasError() {
return
}
}