@@ -4,8 +4,11 @@ import (
44 "errors"
55 "fmt"
66 "strings"
7+ "time"
78
89 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"
912 "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1013)
1114
@@ -46,3 +49,210 @@ func FindDefaultReverse(address string) string {
4649 }
4750 return strings .Join (parts , "-" ) + ".instances.scw.cloud"
4851}
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+ Resale : contactMap ["resale" ].(bool ),
173+ WhoisOptIn : contactMap ["whois_opt_in" ].(bool ),
174+ }
175+
176+ if v , ok := contactMap ["company_name" ].(string ); ok {
177+ contact .CompanyName = scw .StringPtr (v )
178+ }
179+ if v , ok := contactMap ["email_alt" ].(string ); ok {
180+ contact .EmailAlt = scw .StringPtr (v )
181+ }
182+ if v , ok := contactMap ["fax_number" ].(string ); ok {
183+ contact .FaxNumber = scw .StringPtr (v )
184+ }
185+ if v , ok := contactMap ["address_line_2" ].(string ); ok {
186+ contact .AddressLine2 = scw .StringPtr (v )
187+ }
188+ if v , ok := contactMap ["vat_identification_code" ].(string ); ok {
189+ contact .VatIDentificationCode = scw .StringPtr (v )
190+ }
191+ if v , ok := contactMap ["company_identification_code" ].(string ); ok {
192+ contact .CompanyIDentificationCode = scw .StringPtr (v )
193+ }
194+ if v , ok := contactMap ["state" ].(string ); ok {
195+ contact .State = scw .StringPtr (v )
196+ }
197+
198+ if extFr , ok := contactMap ["extension_fr" ].(map [string ]interface {}); ok {
199+ contact .ExtensionFr = expandContactExtension (extFr , "fr" ).(* domain.ContactExtensionFR )
200+ }
201+ if extEu , ok := contactMap ["extension_eu" ].(map [string ]interface {}); ok {
202+ contact .ExtensionEu = expandContactExtension (extEu , "eu" ).(* domain.ContactExtensionEU )
203+ }
204+ if extNl , ok := contactMap ["extension_nl" ].(map [string ]interface {}); ok {
205+ contact .ExtensionNl = expandContactExtension (extNl , "nl" ).(* domain.ContactExtensionNL )
206+ }
207+
208+ return contact
209+ }
210+
211+ func parseEnum [T ~ string ](data map [string ]interface {}, key string , defaultValue T ) T {
212+ if value , ok := data [key ].(string ); ok {
213+ return T (value )
214+ }
215+ return defaultValue
216+ }
217+
218+ func parseStruct [T any ](data map [string ]interface {}, key string ) * T {
219+ if nested , ok := data [key ].(map [string ]interface {}); ok {
220+ var result T
221+ mapToStruct (nested , result )
222+ return & result
223+ }
224+ return nil
225+ }
226+
227+ func mapToStruct (data map [string ]interface {}, target interface {}) {
228+ switch t := target .(type ) {
229+ case * domain.ContactExtensionFRIndividualInfo :
230+ if v , ok := data ["whois_opt_in" ].(bool ); ok {
231+ t .WhoisOptIn = v
232+ }
233+ case * domain.ContactExtensionFRDunsInfo :
234+ if v , ok := data ["duns_id" ].(string ); ok {
235+ t .DunsID = v
236+ }
237+ if v , ok := data ["local_id" ].(string ); ok {
238+ t .LocalID = v
239+ }
240+ case * domain.ContactExtensionFRAssociationInfo :
241+ if v , ok := data ["publication_jo" ].(string ); ok {
242+ if parsedTime , err := time .Parse (time .RFC3339 , v ); err == nil {
243+ t .PublicationJo = & parsedTime
244+ }
245+ }
246+ if v , ok := data ["publication_jo_page" ].(float64 ); ok {
247+ t .PublicationJoPage = uint32 (v )
248+ }
249+ case * domain.ContactExtensionFRTrademarkInfo :
250+ if v , ok := data ["trademark_inpi" ].(string ); ok {
251+ t .TrademarkInpi = v
252+ }
253+ case * domain.ContactExtensionFRCodeAuthAfnicInfo :
254+ if v , ok := data ["code_auth_afnic" ].(string ); ok {
255+ t .CodeAuthAfnic = v
256+ }
257+ }
258+ }
0 commit comments