|
1 | 1 | package google |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "fmt" |
6 | 5 |
|
7 | | - "google.golang.org/api/dns/v1" |
8 | | - |
9 | | - "github.com/hashicorp/terraform-plugin-framework/datasource" |
10 | | - "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
11 | | - "github.com/hashicorp/terraform-plugin-framework/diag" |
12 | | - "github.com/hashicorp/terraform-plugin-framework/types" |
13 | | - "github.com/hashicorp/terraform-plugin-log/tflog" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
14 | 7 | ) |
15 | 8 |
|
16 | | -var _ datasource.DataSource = &GoogleDnsManagedZoneDataSource{} |
17 | | - |
18 | | -func NewGoogleDnsManagedZoneDataSource() datasource.DataSource { |
19 | | - return &GoogleDnsManagedZoneDataSource{} |
20 | | -} |
21 | | - |
22 | | -// GoogleDnsManagedZoneDataSource defines the data source implementation |
23 | | -type GoogleDnsManagedZoneDataSource struct { |
24 | | - client *dns.Service |
25 | | - project types.String |
26 | | -} |
27 | | - |
28 | | -type GoogleDnsManagedZoneModel struct { |
29 | | - Id types.String `tfsdk:"id"` |
30 | | - DnsName types.String `tfsdk:"dns_name"` |
31 | | - Name types.String `tfsdk:"name"` |
32 | | - Description types.String `tfsdk:"description"` |
33 | | - ManagedZoneId types.Int64 `tfsdk:"managed_zone_id"` |
34 | | - NameServers types.List `tfsdk:"name_servers"` |
35 | | - Visibility types.String `tfsdk:"visibility"` |
36 | | - Project types.String `tfsdk:"project"` |
37 | | -} |
| 9 | +func dataSourceDnsManagedZone() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + Read: dataSourceDnsManagedZoneRead, |
38 | 12 |
|
39 | | -func (d *GoogleDnsManagedZoneDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
40 | | - resp.TypeName = req.ProviderTypeName + "_dns_managed_zone" |
41 | | -} |
42 | | - |
43 | | -func (d *GoogleDnsManagedZoneDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
44 | | - resp.Schema = schema.Schema{ |
45 | | - // This description is used by the documentation generator and the language server. |
46 | | - MarkdownDescription: "Provides access to a zone's attributes within Google Cloud DNS", |
47 | | - |
48 | | - Attributes: map[string]schema.Attribute{ |
49 | | - "dns_name": schema.StringAttribute{ |
50 | | - Description: "The fully qualified DNS name of this zone.", |
51 | | - MarkdownDescription: "The fully qualified DNS name of this zone.", |
52 | | - Computed: true, |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "dns_name": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Computed: true, |
53 | 17 | }, |
54 | 18 |
|
55 | | - "name": schema.StringAttribute{ |
56 | | - Description: "A unique name for the resource.", |
57 | | - MarkdownDescription: "A unique name for the resource.", |
58 | | - Required: true, |
| 19 | + "name": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
59 | 22 | }, |
60 | 23 |
|
61 | | - "description": schema.StringAttribute{ |
62 | | - Description: "A textual description field.", |
63 | | - MarkdownDescription: "A textual description field.", |
64 | | - Computed: true, |
| 24 | + "description": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Computed: true, |
65 | 27 | }, |
66 | 28 |
|
67 | | - "managed_zone_id": schema.Int64Attribute{ |
68 | | - Description: "Unique identifier for the resource; defined by the server.", |
69 | | - MarkdownDescription: "Unique identifier for the resource; defined by the server.", |
70 | | - Computed: true, |
| 29 | + "managed_zone_id": { |
| 30 | + Type: schema.TypeInt, |
| 31 | + Computed: true, |
| 32 | + Description: `Unique identifier for the resource; defined by the server.`, |
71 | 33 | }, |
72 | 34 |
|
73 | | - "name_servers": schema.ListAttribute{ |
74 | | - Description: "The list of nameservers that will be authoritative for this " + |
75 | | - "domain. Use NS records to redirect from your DNS provider to these names, " + |
76 | | - "thus making Google Cloud DNS authoritative for this zone.", |
77 | | - MarkdownDescription: "The list of nameservers that will be authoritative for this " + |
78 | | - "domain. Use NS records to redirect from your DNS provider to these names, " + |
79 | | - "thus making Google Cloud DNS authoritative for this zone.", |
80 | | - Computed: true, |
81 | | - ElementType: types.StringType, |
| 35 | + "name_servers": { |
| 36 | + Type: schema.TypeList, |
| 37 | + Computed: true, |
| 38 | + Elem: &schema.Schema{ |
| 39 | + Type: schema.TypeString, |
| 40 | + }, |
82 | 41 | }, |
83 | 42 |
|
84 | | - "visibility": schema.StringAttribute{ |
85 | | - Description: "The zone's visibility: public zones are exposed to the Internet, " + |
86 | | - "while private zones are visible only to Virtual Private Cloud resources.", |
87 | | - MarkdownDescription: "The zone's visibility: public zones are exposed to the Internet, " + |
88 | | - "while private zones are visible only to Virtual Private Cloud resources.", |
| 43 | + "visibility": { |
| 44 | + Type: schema.TypeString, |
89 | 45 | Computed: true, |
90 | 46 | }, |
91 | 47 |
|
92 | 48 | // Google Cloud DNS ManagedZone resources do not have a SelfLink attribute. |
93 | | - "project": schema.StringAttribute{ |
94 | | - Description: "The ID of the project for the Google Cloud.", |
95 | | - MarkdownDescription: "The ID of the project for the Google Cloud.", |
96 | | - Optional: true, |
97 | | - }, |
98 | | - "id": schema.StringAttribute{ |
99 | | - Description: "DNS managed zone identifier", |
100 | | - MarkdownDescription: "DNS managed zone identifier", |
101 | | - Computed: true, |
| 49 | + "project": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
102 | 52 | }, |
103 | 53 | }, |
104 | 54 | } |
105 | 55 | } |
106 | 56 |
|
107 | | -func (d *GoogleDnsManagedZoneDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
108 | | - // Prevent panic if the provider has not been configured. |
109 | | - if req.ProviderData == nil { |
110 | | - return |
| 57 | +func dataSourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error { |
| 58 | + config := meta.(*Config) |
| 59 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 60 | + if err != nil { |
| 61 | + return err |
111 | 62 | } |
112 | 63 |
|
113 | | - p, ok := req.ProviderData.(*frameworkProvider) |
114 | | - if !ok { |
115 | | - resp.Diagnostics.AddError( |
116 | | - "Unexpected Data Source Configure Type", |
117 | | - fmt.Sprintf("Expected *frameworkProvider, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
118 | | - ) |
119 | | - return |
| 64 | + project, err := getProject(d, config) |
| 65 | + if err != nil { |
| 66 | + return err |
120 | 67 | } |
121 | 68 |
|
122 | | - d.client = p.NewDnsClient(p.userAgent, &resp.Diagnostics) |
123 | | - d.project = p.project |
124 | | -} |
125 | | - |
126 | | -func (d *GoogleDnsManagedZoneDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
127 | | - var data GoogleDnsManagedZoneModel |
128 | | - var metaData *ProviderMetaModel |
129 | | - var diags diag.Diagnostics |
| 69 | + name := d.Get("name").(string) |
| 70 | + d.SetId(fmt.Sprintf("projects/%s/managedZones/%s", project, name)) |
130 | 71 |
|
131 | | - // Read Provider meta into the meta model |
132 | | - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metaData)...) |
133 | | - if resp.Diagnostics.HasError() { |
134 | | - return |
| 72 | + zone, err := config.NewDnsClient(userAgent).ManagedZones.Get( |
| 73 | + project, name).Do() |
| 74 | + if err != nil { |
| 75 | + return handleNotFoundError(err, d, fmt.Sprintf("dataSourceDnsManagedZone %q", name)) |
135 | 76 | } |
136 | 77 |
|
137 | | - d.client.UserAgent = generateFrameworkUserAgentString(metaData, d.client.UserAgent) |
138 | | - |
139 | | - // Read Terraform configuration data into the model |
140 | | - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
141 | | - if resp.Diagnostics.HasError() { |
142 | | - return |
| 78 | + if err := d.Set("dns_name", zone.DnsName); err != nil { |
| 79 | + return fmt.Errorf("Error setting dns_name: %s", err) |
143 | 80 | } |
144 | | - |
145 | | - data.Project = getProjectFramework(data.Project, d.project, &resp.Diagnostics) |
146 | | - if resp.Diagnostics.HasError() { |
147 | | - return |
| 81 | + if err := d.Set("name", zone.Name); err != nil { |
| 82 | + return fmt.Errorf("Error setting name: %s", err) |
148 | 83 | } |
149 | | - |
150 | | - data.Id = types.StringValue(fmt.Sprintf("projects/%s/managedZones/%s", data.Project.ValueString(), data.Name.ValueString())) |
151 | | - clientResp, err := d.client.ManagedZones.Get(data.Project.ValueString(), data.Name.ValueString()).Do() |
152 | | - if err != nil { |
153 | | - handleDatasourceNotFoundError(ctx, err, &resp.State, fmt.Sprintf("dataSourceDnsManagedZone %q", data.Name.ValueString()), &resp.Diagnostics) |
| 84 | + if err := d.Set("description", zone.Description); err != nil { |
| 85 | + return fmt.Errorf("Error setting description: %s", err) |
154 | 86 | } |
155 | | - |
156 | | - tflog.Trace(ctx, "read dns record set data source") |
157 | | - |
158 | | - data.DnsName = types.StringValue(clientResp.DnsName) |
159 | | - data.Description = types.StringValue(clientResp.Description) |
160 | | - data.ManagedZoneId = types.Int64Value(int64(clientResp.Id)) |
161 | | - data.Visibility = types.StringValue(clientResp.Visibility) |
162 | | - data.NameServers, diags = types.ListValueFrom(ctx, types.StringType, clientResp.NameServers) |
163 | | - resp.Diagnostics.Append(diags...) |
164 | | - if resp.Diagnostics.HasError() { |
165 | | - return |
| 87 | + if err := d.Set("managed_zone_id", zone.Id); err != nil { |
| 88 | + return fmt.Errorf("Error setting managed_zone_id: %s", err) |
| 89 | + } |
| 90 | + if err := d.Set("name_servers", zone.NameServers); err != nil { |
| 91 | + return fmt.Errorf("Error setting name_servers: %s", err) |
| 92 | + } |
| 93 | + if err := d.Set("visibility", zone.Visibility); err != nil { |
| 94 | + return fmt.Errorf("Error setting visibility: %s", err) |
| 95 | + } |
| 96 | + if err := d.Set("project", project); err != nil { |
| 97 | + return fmt.Errorf("Error setting project: %s", err) |
166 | 98 | } |
167 | 99 |
|
168 | | - // Save data into Terraform state |
169 | | - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 100 | + return nil |
170 | 101 | } |
0 commit comments