@@ -4,8 +4,11 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"strings"
7
+ "time"
7
8
8
9
domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
10
+ "github.com/scaleway/scaleway-sdk-go/api/std"
11
+ "github.com/scaleway/scaleway-sdk-go/scw"
9
12
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
10
13
)
11
14
@@ -46,3 +49,220 @@ func FindDefaultReverse(address string) string {
46
49
}
47
50
return strings .Join (parts , "-" ) + ".instances.scw.cloud"
48
51
}
52
+
53
+ func ExpandContact (contactMap map [string ]interface {}) * domain.Contact {
54
+ if contactMap == nil {
55
+ return nil
56
+ }
57
+
58
+ contact := & domain.Contact {
59
+ PhoneNumber : contactMap ["phone_number" ].(string ),
60
+ LegalForm : domain .ContactLegalForm (contactMap ["legal_form" ].(string )),
61
+ Firstname : contactMap ["firstname" ].(string ),
62
+ Lastname : contactMap ["lastname" ].(string ),
63
+ Email : contactMap ["email" ].(string ),
64
+ AddressLine1 : contactMap ["address_line_1" ].(string ),
65
+ Zip : contactMap ["zip" ].(string ),
66
+ City : contactMap ["city" ].(string ),
67
+ Country : contactMap ["country" ].(string ),
68
+ }
69
+
70
+ // Optional fields
71
+ if v , ok := contactMap ["company_name" ].(string ); ok && v != "" {
72
+ contact .CompanyName = v
73
+ }
74
+ if v , ok := contactMap ["email_alt" ].(string ); ok && v != "" {
75
+ contact .EmailAlt = v
76
+ }
77
+ if v , ok := contactMap ["fax_number" ].(string ); ok && v != "" {
78
+ contact .FaxNumber = v
79
+ }
80
+ if v , ok := contactMap ["address_line_2" ].(string ); ok && v != "" {
81
+ contact .AddressLine2 = v
82
+ }
83
+ if v , ok := contactMap ["vat_identification_code" ].(string ); ok && v != "" {
84
+ contact .VatIDentificationCode = v
85
+ }
86
+ if v , ok := contactMap ["company_identification_code" ].(string ); ok && v != "" {
87
+ contact .CompanyIDentificationCode = v
88
+ }
89
+ if v , ok := contactMap ["lang" ].(string ); ok && v != "" {
90
+ contact .Lang = std .LanguageCode (v )
91
+ }
92
+ if v , ok := contactMap ["resale" ].(bool ); ok {
93
+ contact .Resale = v
94
+ }
95
+ if v , ok := contactMap ["state" ].(string ); ok && v != "" {
96
+ contact .State = v
97
+ }
98
+ if v , ok := contactMap ["whois_opt_in" ].(bool ); ok {
99
+ contact .WhoisOptIn = v
100
+ }
101
+
102
+ if extFr , ok := contactMap ["extension_fr" ].(map [string ]interface {}); ok && len (extFr ) > 0 {
103
+ contact .ExtensionFr = expandContactExtension (extFr , "fr" ).(* domain.ContactExtensionFR )
104
+ }
105
+ if extEu , ok := contactMap ["extension_eu" ].(map [string ]interface {}); ok && len (extEu ) > 0 {
106
+ contact .ExtensionEu = expandContactExtension (extEu , "eu" ).(* domain.ContactExtensionEU )
107
+ }
108
+ if extNl , ok := contactMap ["extension_nl" ].(map [string ]interface {}); ok && len (extNl ) > 0 {
109
+ contact .ExtensionNl = expandContactExtension (extNl , "nl" ).(* domain.ContactExtensionNL )
110
+ }
111
+
112
+ return contact
113
+ }
114
+
115
+ func expandContactExtension (extensionMap map [string ]interface {}, extensionType string ) interface {} {
116
+ if extensionMap == nil || len (extensionMap ) == 0 {
117
+ return nil
118
+ }
119
+
120
+ switch extensionType {
121
+ case "fr" :
122
+ return & domain.ContactExtensionFR {
123
+ Mode : parseEnum [domain.ContactExtensionFRMode ](extensionMap , "mode" , domain .ContactExtensionFRModeModeUnknown ),
124
+ IndividualInfo : parseStruct [domain.ContactExtensionFRIndividualInfo ](extensionMap , "individual_info" ),
125
+ DunsInfo : parseStruct [domain.ContactExtensionFRDunsInfo ](extensionMap , "duns_info" ),
126
+ AssociationInfo : parseStruct [domain.ContactExtensionFRAssociationInfo ](extensionMap , "association_info" ),
127
+ TrademarkInfo : parseStruct [domain.ContactExtensionFRTrademarkInfo ](extensionMap , "trademark_info" ),
128
+ CodeAuthAfnicInfo : parseStruct [domain.ContactExtensionFRCodeAuthAfnicInfo ](extensionMap , "code_auth_afnic_info" ),
129
+ }
130
+ case "nl" :
131
+ legalFormRegistrationNumber := ""
132
+ if value , ok := extensionMap ["legal_form_registration_number" ]; ok {
133
+ if str , isString := value .(string ); isString {
134
+ legalFormRegistrationNumber = str
135
+ }
136
+ }
137
+
138
+ return & domain.ContactExtensionNL {
139
+ LegalForm : parseEnum [domain.ContactExtensionNLLegalForm ](extensionMap , "legal_form" , domain .ContactExtensionNLLegalFormLegalFormUnknown ),
140
+ LegalFormRegistrationNumber : legalFormRegistrationNumber ,
141
+ }
142
+ case "eu" :
143
+ europeanCitizenship := ""
144
+ if value , ok := extensionMap ["european_citizenship" ]; ok {
145
+ if str , isString := value .(string ); isString {
146
+ europeanCitizenship = str
147
+ }
148
+ }
149
+ return & domain.ContactExtensionEU {
150
+ EuropeanCitizenship : europeanCitizenship ,
151
+ }
152
+ default :
153
+ return nil
154
+ }
155
+ }
156
+
157
+ func ExpandNewContact (contactMap map [string ]interface {}) * domain.NewContact {
158
+ if contactMap == nil {
159
+ return nil
160
+ }
161
+
162
+ contact := & domain.NewContact {
163
+ PhoneNumber : contactMap ["phone_number" ].(string ),
164
+ LegalForm : domain .ContactLegalForm (contactMap ["legal_form" ].(string )),
165
+ Firstname : contactMap ["firstname" ].(string ),
166
+ Lastname : contactMap ["lastname" ].(string ),
167
+ Email : contactMap ["email" ].(string ),
168
+ AddressLine1 : contactMap ["address_line_1" ].(string ),
169
+ Zip : contactMap ["zip" ].(string ),
170
+ City : contactMap ["city" ].(string ),
171
+ Country : contactMap ["country" ].(string ),
172
+ }
173
+
174
+ if v , ok := contactMap ["resale" ].(bool ); ok {
175
+ contact .Resale = v
176
+ } else {
177
+ contact .Resale = false
178
+ }
179
+
180
+ if v , ok := contactMap ["whois_opt_in" ].(bool ); ok {
181
+ contact .WhoisOptIn = v
182
+ } else {
183
+ contact .WhoisOptIn = false
184
+ }
185
+
186
+ if v , ok := contactMap ["company_name" ].(string ); ok {
187
+ contact .CompanyName = scw .StringPtr (v )
188
+ }
189
+ if v , ok := contactMap ["email_alt" ].(string ); ok {
190
+ contact .EmailAlt = scw .StringPtr (v )
191
+ }
192
+ if v , ok := contactMap ["fax_number" ].(string ); ok {
193
+ contact .FaxNumber = scw .StringPtr (v )
194
+ }
195
+ if v , ok := contactMap ["address_line_2" ].(string ); ok {
196
+ contact .AddressLine2 = scw .StringPtr (v )
197
+ }
198
+ if v , ok := contactMap ["vat_identification_code" ].(string ); ok {
199
+ contact .VatIDentificationCode = scw .StringPtr (v )
200
+ }
201
+ if v , ok := contactMap ["company_identification_code" ].(string ); ok {
202
+ contact .CompanyIDentificationCode = scw .StringPtr (v )
203
+ }
204
+ if v , ok := contactMap ["state" ].(string ); ok {
205
+ contact .State = scw .StringPtr (v )
206
+ }
207
+
208
+ if extFr , ok := contactMap ["extension_fr" ].(map [string ]interface {}); ok {
209
+ contact .ExtensionFr = expandContactExtension (extFr , "fr" ).(* domain.ContactExtensionFR )
210
+ }
211
+ if extEu , ok := contactMap ["extension_eu" ].(map [string ]interface {}); ok {
212
+ contact .ExtensionEu = expandContactExtension (extEu , "eu" ).(* domain.ContactExtensionEU )
213
+ }
214
+ if extNl , ok := contactMap ["extension_nl" ].(map [string ]interface {}); ok {
215
+ contact .ExtensionNl = expandContactExtension (extNl , "nl" ).(* domain.ContactExtensionNL )
216
+ }
217
+
218
+ return contact
219
+ }
220
+
221
+ func parseEnum [T ~ string ](data map [string ]interface {}, key string , defaultValue T ) T {
222
+ if value , ok := data [key ].(string ); ok {
223
+ return T (value )
224
+ }
225
+ return defaultValue
226
+ }
227
+
228
+ func parseStruct [T any ](data map [string ]interface {}, key string ) * T {
229
+ if nested , ok := data [key ].(map [string ]interface {}); ok {
230
+ var result T
231
+ mapToStruct (nested , & result )
232
+ return & result
233
+ }
234
+ return nil
235
+ }
236
+
237
+ func mapToStruct (data map [string ]interface {}, target interface {}) {
238
+ switch t := target .(type ) {
239
+ case * domain.ContactExtensionFRIndividualInfo :
240
+ if v , ok := data ["whois_opt_in" ].(bool ); ok {
241
+ t .WhoisOptIn = v
242
+ }
243
+ case * domain.ContactExtensionFRDunsInfo :
244
+ if v , ok := data ["duns_id" ].(string ); ok {
245
+ t .DunsID = v
246
+ }
247
+ if v , ok := data ["local_id" ].(string ); ok {
248
+ t .LocalID = v
249
+ }
250
+ case * domain.ContactExtensionFRAssociationInfo :
251
+ if v , ok := data ["publication_jo" ].(string ); ok {
252
+ if parsedTime , err := time .Parse (time .RFC3339 , v ); err == nil {
253
+ t .PublicationJo = & parsedTime
254
+ }
255
+ }
256
+ if v , ok := data ["publication_jo_page" ].(float64 ); ok {
257
+ t .PublicationJoPage = uint32 (v )
258
+ }
259
+ case * domain.ContactExtensionFRTrademarkInfo :
260
+ if v , ok := data ["trademark_inpi" ].(string ); ok {
261
+ t .TrademarkInpi = v
262
+ }
263
+ case * domain.ContactExtensionFRCodeAuthAfnicInfo :
264
+ if v , ok := data ["code_auth_afnic" ].(string ); ok {
265
+ t .CodeAuthAfnic = v
266
+ }
267
+ }
268
+ }
0 commit comments