-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathzone_data_source.go
More file actions
103 lines (87 loc) · 2.92 KB
/
zone_data_source.go
File metadata and controls
103 lines (87 loc) · 2.92 KB
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
package cloud
import (
"context"
"github.com/UpCloudLtd/terraform-provider-upcloud/internal/utils"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/service"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func NewZoneDataSource() datasource.DataSource {
return &zoneDataSource{}
}
var (
_ datasource.DataSource = &zoneDataSource{}
_ datasource.DataSourceWithConfigure = &zoneDataSource{}
)
type zoneDataSource struct {
client *service.Service
}
func (d *zoneDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_zone"
}
func (d *zoneDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.client, resp.Diagnostics = utils.GetClientFromProviderData(req.ProviderData)
}
type zoneModel struct {
ID types.String `tfsdk:"id"`
Description types.String `tfsdk:"description"`
Public types.Bool `tfsdk:"public"`
ParentZone types.String `tfsdk:"parent_zone"`
}
func (d *zoneDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides details on given zone.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
Description: "Identifier of the zone.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "Identifier of the zone. Contains the same value as `id`.",
},
"public": schema.BoolAttribute{
Computed: true,
Description: "Indicates whether the zone is a public zone.",
},
"parent_zone": schema.StringAttribute{
Computed: true,
Description: "Public parent zone of an private cloud zone. Empty for public zones.",
},
},
}
}
func (d *zoneDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data zoneModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
id := data.ID.ValueString()
if id == "" {
resp.Diagnostics.AddError("`id` must be set", "`id` in `upcloud_zone` data source can't be empty.")
return
}
zones, err := d.client.GetZones(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read zones",
utils.ErrorDiagnosticDetail(err),
)
return
}
var zone upcloud.Zone
for _, z := range zones.Zones {
if z.ID == id {
zone = z
break
}
}
if zone.ID == "" {
resp.Diagnostics.AddError("Zone not found", "No zone found with the given ID.")
return
}
data.Description = types.StringValue(zone.Description)
data.Public = types.BoolValue(zone.Public.Bool())
data.ParentZone = types.StringValue(zone.ParentZone)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}