@@ -8,118 +8,135 @@ import (
88 "fmt"
99 "time"
1010
11+ "github.com/hashicorp/go-azure-helpers/lang/pointer"
1112 "github.com/hashicorp/go-azure-helpers/lang/response"
1213 "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
1314 "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
14- "github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
1515 "github.com/hashicorp/go-azure-sdk/resource-manager/dns/2018-05-01/zones"
16- "github.com/hashicorp/terraform-provider-azurerm/internal/clients "
16+ "github.com/hashicorp/terraform-provider-azurerm/internal/sdk "
1717 "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
18- "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
1918)
2019
21- func dataSourceDnsZone () * pluginsdk.Resource {
22- return & pluginsdk.Resource {
23- Read : dataSourceDnsZoneRead ,
20+ var _ sdk.DataSource = DnsZoneDataResource {}
2421
25- Timeouts : & pluginsdk.ResourceTimeout {
26- Read : pluginsdk .DefaultTimeout (5 * time .Minute ),
22+ type DnsZoneDataResource struct {}
23+
24+ func (DnsZoneDataResource ) ModelObject () interface {} {
25+ return & DnsZoneDataResourceModel {}
26+ }
27+
28+ func (d DnsZoneDataResource ) IDValidationFunc () pluginsdk.SchemaValidateFunc {
29+ return zones .ValidateDnsZoneID
30+ }
31+
32+ func (DnsZoneDataResource ) ResourceType () string {
33+ return "azurerm_dns_zone"
34+ }
35+
36+ func (DnsZoneDataResource ) Arguments () map [string ]* pluginsdk.Schema {
37+ return map [string ]* pluginsdk.Schema {
38+ "name" : {
39+ Type : pluginsdk .TypeString ,
40+ Required : true ,
2741 },
2842
29- Schema : map [string ]* pluginsdk.Schema {
30- "name" : {
31- Type : pluginsdk .TypeString ,
32- Required : true ,
33- },
34-
35- "resource_group_name" : {
36- // TODO: we need a CommonSchema type for this which doesn't have ForceNew
37- Type : pluginsdk .TypeString ,
38- Optional : true ,
39- Computed : true ,
40- },
41-
42- "number_of_record_sets" : {
43- Type : pluginsdk .TypeInt ,
44- Computed : true ,
45- },
46-
47- "max_number_of_record_sets" : {
48- Type : pluginsdk .TypeInt ,
49- Computed : true ,
50- },
51-
52- "name_servers" : {
53- Type : pluginsdk .TypeSet ,
54- Computed : true ,
55- Elem : & pluginsdk.Schema {Type : pluginsdk .TypeString },
56- Set : pluginsdk .HashString ,
57- },
58-
59- "tags" : commonschema .TagsDataSource (),
43+ "resource_group_name" : {
44+ // TODO: we need a CommonSchema type for this which doesn't have ForceNew
45+ Type : pluginsdk .TypeString ,
46+ Optional : true ,
47+ Computed : true ,
6048 },
6149 }
6250}
6351
64- func dataSourceDnsZoneRead (d * pluginsdk.ResourceData , meta interface {}) error {
65- client := meta .(* clients.Client ).Dns .Zones
66- subscriptionId := meta .(* clients.Client ).Account .SubscriptionId
67- ctx , cancel := timeouts .ForRead (meta .(* clients.Client ).StopContext , d )
68- defer cancel ()
69-
70- id := zones .NewDnsZoneID (subscriptionId , d .Get ("resource_group_name" ).(string ), d .Get ("name" ).(string ))
71- var zone * zones.Zone
72- if id .ResourceGroupName != "" {
73- resp , err := client .Get (ctx , id )
74- if err != nil {
75- if response .WasNotFound (resp .HttpResponse ) {
76- return fmt .Errorf ("%s was not found" , id )
77- }
78- return fmt .Errorf ("retrieving %s: %+v" , id , err )
79- }
52+ func (DnsZoneDataResource ) Attributes () map [string ]* pluginsdk.Schema {
53+ return map [string ]* pluginsdk.Schema {
54+ "number_of_record_sets" : {
55+ Type : pluginsdk .TypeInt ,
56+ Computed : true ,
57+ },
8058
81- zone = resp .Model
82- } else {
83- result , resourceGroupName , err := findZone (ctx , client , id .SubscriptionId , id .DnsZoneName )
84- if err != nil {
85- return err
86- }
59+ "max_number_of_record_sets" : {
60+ Type : pluginsdk .TypeInt ,
61+ Computed : true ,
62+ },
8763
88- if resourceGroupName == nil {
89- return fmt .Errorf ("unable to locate the Resource Group for DNS Zone %q in Subscription %q" , id .DnsZoneName , subscriptionId )
90- }
64+ "name_servers" : {
65+ Type : pluginsdk .TypeSet ,
66+ Computed : true ,
67+ Elem : & pluginsdk.Schema {Type : pluginsdk .TypeString },
68+ Set : pluginsdk .HashString ,
69+ },
9170
92- zone = result
93- id .ResourceGroupName = * resourceGroupName
71+ "tags" : commonschema .TagsDataSource (),
9472 }
73+ }
9574
96- if zone == nil {
97- return fmt .Errorf ("retrieving %s: `model` was nil" , id )
98- }
75+ type DnsZoneDataResourceModel struct {
76+ Name string `tfschema:"name"`
77+ ResourceGroupName string `tfschema:"resource_group_name"`
78+ NumberOfRecordSets int64 `tfschema:"number_of_record_sets"`
79+ MaxNumberOfRecordSets int64 `tfschema:"max_number_of_record_sets"`
80+ NameServers []string `tfschema:"name_servers"`
81+ Tags map [string ]string `tfschema:"tags"`
82+ }
9983
100- d .SetId (id .ID ())
84+ func (DnsZoneDataResource ) Read () sdk.ResourceFunc {
85+ return sdk.ResourceFunc {
86+ Timeout : 5 * time .Minute ,
87+ Func : func (ctx context.Context , metadata sdk.ResourceMetaData ) error {
88+ client := metadata .Client .Dns .Zones
89+ subscriptionId := metadata .Client .Account .SubscriptionId
10190
102- d .Set ("name" , id .DnsZoneName )
103- d .Set ("resource_group_name" , id .ResourceGroupName )
91+ var state DnsZoneDataResourceModel
92+ if err := metadata .Decode (& state ); err != nil {
93+ return fmt .Errorf ("decoding: %+v" , err )
94+ }
10495
105- if props := zone .Properties ; props != nil {
106- d .Set ("number_of_record_sets" , props .NumberOfRecordSets )
107- d .Set ("max_number_of_record_sets" , props .MaxNumberOfRecordSets )
96+ id := zones .NewDnsZoneID (subscriptionId , state .ResourceGroupName , state .Name )
97+ var zone * zones.Zone
98+ if id .ResourceGroupName != "" {
99+ resp , err := client .Get (ctx , id )
100+ if err != nil {
101+ if response .WasNotFound (resp .HttpResponse ) {
102+ return fmt .Errorf ("%s was not found" , id )
103+ }
104+ return fmt .Errorf ("retrieving %s: %+v" , id , err )
105+ }
106+
107+ zone = resp .Model
108+ } else {
109+ result , resourceGroupName , err := findZone (ctx , client , id .SubscriptionId , id .DnsZoneName )
110+ if err != nil {
111+ return err
112+ }
113+
114+ if resourceGroupName == nil {
115+ return fmt .Errorf ("unable to locate the Resource Group for DNS Zone %q in Subscription %q" , id .DnsZoneName , subscriptionId )
116+ }
117+
118+ zone = result
119+ id .ResourceGroupName = pointer .From (resourceGroupName )
120+ state .ResourceGroupName = pointer .From (resourceGroupName )
121+ }
108122
109- nameServers := make ([]string , 0 )
110- if ns := props .NameServers ; ns != nil {
111- nameServers = * ns
112- }
113- if err := d .Set ("name_servers" , nameServers ); err != nil {
114- return err
115- }
116- }
123+ if zone == nil {
124+ return fmt .Errorf ("retrieving %s: `model` was nil" , id )
125+ }
117126
118- if err := tags .FlattenAndSet (d , zone .Tags ); err != nil {
119- return err
120- }
127+ metadata .SetID (id )
128+
129+ if props := zone .Properties ; props != nil {
130+ state .NumberOfRecordSets = pointer .From (props .NumberOfRecordSets )
131+ state .MaxNumberOfRecordSets = pointer .From (props .MaxNumberOfRecordSets )
132+ state .NameServers = pointer .From (props .NameServers )
133+ }
121134
122- return nil
135+ state .Tags = pointer .From (zone .Tags )
136+
137+ return metadata .Encode (& state )
138+ },
139+ }
123140}
124141
125142func findZone (ctx context.Context , client * zones.ZonesClient , subscriptionId , name string ) (* zones.Zone , * string , error ) {
0 commit comments