@@ -72,6 +72,7 @@ var _CloudflareDNSRecord = class _CloudflareDNSRecord extends (_a = CloudflareEn
72
72
this . state . record_id = existing . id ;
73
73
this . state . zone_id = zoneId ;
74
74
this . state . existing = true ;
75
+ this . state . applied_zone_name = this . definition . zone_name ;
75
76
return ;
76
77
}
77
78
const payload = {
@@ -89,24 +90,22 @@ var _CloudflareDNSRecord = class _CloudflareDNSRecord extends (_a = CloudflareEn
89
90
this . state . record_id = createdId ;
90
91
this . state . zone_id = zoneId ;
91
92
this . state . existing = false ;
93
+ this . state . applied_zone_name = this . definition . zone_name ;
92
94
} else {
93
95
const newRec = this . findRecord ( zoneId , this . definition . record_type , this . definition . name ) ;
94
96
if ( newRec ) {
95
97
this . state . record_id = newRec . id ;
96
98
this . state . zone_id = zoneId ;
97
99
this . state . existing = false ;
100
+ this . state . applied_zone_name = this . definition . zone_name ;
98
101
}
99
102
}
100
103
return ;
101
104
}
102
105
update ( ) {
103
- const zoneId = this . resolveZoneId ( ) ;
104
- if ( ! zoneId ) return ;
105
- if ( ! this . state . record_id ) {
106
- const existing = this . findRecord ( zoneId , this . definition . record_type , this . definition . name ) ;
107
- if ( ! existing ) return void this . create ( ) ;
108
- this . state . record_id = existing . id ;
109
- }
106
+ const currentZoneId = this . state . zone_id ;
107
+ const desiredZoneId = this . definition . zone_id ?? ( this . definition . zone_name ? this . findZoneByName ( this . definition . zone_name ) ?. id : currentZoneId ) ;
108
+ if ( ! desiredZoneId ) return ;
110
109
const payload = {
111
110
type : this . definition . record_type ,
112
111
name : this . definition . name
@@ -116,7 +115,69 @@ var _CloudflareDNSRecord = class _CloudflareDNSRecord extends (_a = CloudflareEn
116
115
if ( typeof this . definition . proxied === "boolean" ) payload . proxied = this . definition . proxied ;
117
116
if ( typeof this . definition . priority === "number" ) payload . priority = this . definition . priority ;
118
117
if ( this . definition . data ) payload . data = this . definition . data ;
119
- this . request ( "PUT" , `/zones/${ zoneId } /dns_records/${ this . state . record_id } ` , payload ) ;
118
+ const nameChanged = this . definition . zone_name !== void 0 && this . state . applied_zone_name !== void 0 && this . definition . zone_name !== this . state . applied_zone_name ;
119
+ const idChanged = Boolean ( currentZoneId && currentZoneId !== desiredZoneId ) ;
120
+ if ( nameChanged || idChanged ) {
121
+ const old = { zone : currentZoneId , id : this . state . record_id , owned : this . state . existing === false } ;
122
+ const existing = this . findRecord ( desiredZoneId , this . definition . record_type , this . definition . name ) ;
123
+ if ( existing ) {
124
+ this . state . record_id = existing . id ;
125
+ this . state . zone_id = desiredZoneId ;
126
+ this . state . existing = true ;
127
+ } else {
128
+ const created = this . request ( "POST" , `/zones/${ desiredZoneId } /dns_records` , payload ) ;
129
+ const newId = created ?. result ?. id ;
130
+ if ( newId ) {
131
+ this . state . record_id = newId ;
132
+ this . state . zone_id = desiredZoneId ;
133
+ this . state . existing = false ;
134
+ } else {
135
+ const newRec = this . findRecord ( desiredZoneId , this . definition . record_type , this . definition . name ) ;
136
+ if ( newRec ) {
137
+ this . state . record_id = newRec . id ;
138
+ this . state . zone_id = desiredZoneId ;
139
+ this . state . existing = true ;
140
+ }
141
+ }
142
+ }
143
+ if ( old . zone && old . id && old . owned ) {
144
+ this . request ( "DELETE" , `/zones/${ old . zone } /dns_records/${ old . id } ` ) ;
145
+ }
146
+ if ( this . definition . zone_name !== void 0 ) {
147
+ this . state . applied_zone_name = this . definition . zone_name ;
148
+ }
149
+ return ;
150
+ }
151
+ if ( ! this . state . record_id ) {
152
+ const existing = this . findRecord ( desiredZoneId , this . definition . record_type , this . definition . name ) ;
153
+ if ( ! existing ) {
154
+ const created = this . request ( "POST" , `/zones/${ desiredZoneId } /dns_records` , payload ) ;
155
+ const newId = created ?. result ?. id ;
156
+ if ( newId ) {
157
+ this . state . record_id = newId ;
158
+ this . state . zone_id = desiredZoneId ;
159
+ this . state . existing = false ;
160
+ if ( this . definition . zone_name !== void 0 ) this . state . applied_zone_name = this . definition . zone_name ;
161
+ } else {
162
+ const newRec = this . findRecord ( desiredZoneId , this . definition . record_type , this . definition . name ) ;
163
+ if ( newRec ) {
164
+ this . state . record_id = newRec . id ;
165
+ this . state . zone_id = desiredZoneId ;
166
+ this . state . existing = true ;
167
+ if ( this . definition . zone_name !== void 0 ) this . state . applied_zone_name = this . definition . zone_name ;
168
+ }
169
+ }
170
+ return ;
171
+ }
172
+ this . state . record_id = existing . id ;
173
+ this . state . zone_id = desiredZoneId ;
174
+ this . state . existing = true ;
175
+ }
176
+ this . request ( "PUT" , `/zones/${ desiredZoneId } /dns_records/${ this . state . record_id } ` , payload ) ;
177
+ this . state . zone_id = desiredZoneId ;
178
+ if ( this . definition . zone_name !== void 0 ) {
179
+ this . state . applied_zone_name = this . definition . zone_name ;
180
+ }
120
181
}
121
182
delete ( ) {
122
183
if ( ! this . state . zone_id || ! this . state . record_id ) {
0 commit comments