forked from hashicorp/terraform-provider-google
/
data_source_dns_managed_zone.go
200 lines (168 loc) · 7.3 KB
/
data_source_dns_managed_zone.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dns
import (
"context"
"fmt"
"google.golang.org/api/dns/v1"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-google/google/fwmodels"
"github.com/hashicorp/terraform-provider-google/google/fwresource"
"github.com/hashicorp/terraform-provider-google/google/fwtransport"
)
// Ensure the implementation satisfies the expected interfaces
var (
_ datasource.DataSource = &GoogleDnsManagedZoneDataSource{}
_ datasource.DataSourceWithConfigure = &GoogleDnsManagedZoneDataSource{}
)
func NewGoogleDnsManagedZoneDataSource() datasource.DataSource {
return &GoogleDnsManagedZoneDataSource{}
}
// GoogleDnsManagedZoneDataSource defines the data source implementation
type GoogleDnsManagedZoneDataSource struct {
client *dns.Service
project types.String
}
type GoogleDnsManagedZoneModel struct {
Id types.String `tfsdk:"id"`
DnsName types.String `tfsdk:"dns_name"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
ManagedZoneId types.Int64 `tfsdk:"managed_zone_id"`
NameServers types.List `tfsdk:"name_servers"`
Visibility types.String `tfsdk:"visibility"`
Project types.String `tfsdk:"project"`
}
func (d *GoogleDnsManagedZoneDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dns_managed_zone"
}
func (d *GoogleDnsManagedZoneDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Provides access to a zone's attributes within Google Cloud DNS",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "A unique name for the resource.",
MarkdownDescription: "A unique name for the resource.",
Required: true,
},
// Google Cloud DNS ManagedZone resources do not have a SelfLink attribute.
"project": schema.StringAttribute{
Description: "The ID of the project for the Google Cloud.",
MarkdownDescription: "The ID of the project for the Google Cloud.",
Optional: true,
},
"dns_name": schema.StringAttribute{
Description: "The fully qualified DNS name of this zone.",
MarkdownDescription: "The fully qualified DNS name of this zone.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "A textual description field.",
MarkdownDescription: "A textual description field.",
Computed: true,
},
"managed_zone_id": schema.Int64Attribute{
Description: "Unique identifier for the resource; defined by the server.",
MarkdownDescription: "Unique identifier for the resource; defined by the server.",
Computed: true,
},
"name_servers": schema.ListAttribute{
Description: "The list of nameservers that will be authoritative for this " +
"domain. Use NS records to redirect from your DNS provider to these names, " +
"thus making Google Cloud DNS authoritative for this zone.",
MarkdownDescription: "The list of nameservers that will be authoritative for this " +
"domain. Use NS records to redirect from your DNS provider to these names, " +
"thus making Google Cloud DNS authoritative for this zone.",
Computed: true,
ElementType: types.StringType,
},
"visibility": schema.StringAttribute{
Description: "The zone's visibility: public zones are exposed to the Internet, " +
"while private zones are visible only to Virtual Private Cloud resources.",
MarkdownDescription: "The zone's visibility: public zones are exposed to the Internet, " +
"while private zones are visible only to Virtual Private Cloud resources.",
Computed: true,
},
"id": schema.StringAttribute{
Description: "DNS managed zone identifier",
MarkdownDescription: "DNS managed zone identifier",
Computed: true,
},
},
}
}
func (d *GoogleDnsManagedZoneDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
p, ok := req.ProviderData.(*fwtransport.FrameworkProviderConfig)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *fwtransport.FrameworkProviderConfig, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = p.NewDnsClient(p.UserAgent, &resp.Diagnostics)
d.project = p.Project
}
func (d *GoogleDnsManagedZoneDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data GoogleDnsManagedZoneModel
var metaData *fwmodels.ProviderMetaModel
var diags diag.Diagnostics
// Read Provider meta into the meta model
resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metaData)...)
if resp.Diagnostics.HasError() {
return
}
d.client.UserAgent = fwtransport.GenerateFrameworkUserAgentString(metaData, d.client.UserAgent)
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
data.Project = fwresource.GetProjectFramework(data.Project, d.project, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
data.Id = types.StringValue(fmt.Sprintf("projects/%s/managedZones/%s", data.Project.ValueString(), data.Name.ValueString()))
clientResp, err := d.client.ManagedZones.Get(data.Project.ValueString(), data.Name.ValueString()).Do()
if err != nil {
fwtransport.HandleDatasourceNotFoundError(ctx, err, &resp.State, fmt.Sprintf("dataSourceDnsManagedZone %q", data.Name.ValueString()), &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
}
tflog.Trace(ctx, "read dns managed zone data source")
data.DnsName = types.StringValue(clientResp.DnsName)
data.Description = types.StringValue(clientResp.Description)
data.ManagedZoneId = types.Int64Value(int64(clientResp.Id))
data.Visibility = types.StringValue(clientResp.Visibility)
data.NameServers, diags = types.ListValueFrom(ctx, types.StringType, clientResp.NameServers)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func getDnsManagedZoneAttrs() map[string]attr.Type {
dnsManagedZoneAttrs := map[string]attr.Type{
"name": types.StringType,
"project": types.StringType,
"dns_name": types.StringType,
"description": types.StringType,
"managed_zone_id": types.Int64Type,
"name_servers": types.ListType{}.WithElementType(types.StringType),
"visibility": types.StringType,
"id": types.StringType,
}
return dnsManagedZoneAttrs
}