55 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
66 "log"
77 "strconv"
8+ "strings"
89)
910
1011var hstsConfigResource = schema.Resource {
@@ -69,13 +70,32 @@ func resourceSiteSSLSettings() *schema.Resource {
6970 Delete : resourceSiteSSLSettingsDelete ,
7071 Importer : & schema.ResourceImporter {
7172 State : func (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
72- siteID , err := strconv .Atoi (d .Id ())
73- if err != nil {
74- fmt .Errorf ("failed to convert Site Id from import command, actual value: %s, expected numeric id" , d .Id ())
73+ idSlice := strings .Split (d .Id (), "/" )
74+ log .Printf ("[DEBUG] Starting to import site ssl settings. Parameters: %s\n " , d .Id ())
75+
76+ if len (idSlice ) > 2 || idSlice [0 ] == "" {
77+ return nil , fmt .Errorf ("unexpected format of ID (%q), expected site_id or site_id/account_id" , d .Id ())
7578 }
7679
80+ siteID , err := strconv .Atoi (idSlice [0 ])
81+ if err != nil {
82+ fmt .Errorf ("failed to convert Site Id from import command, actual value: %s, expected numeric id" , idSlice [0 ])
83+ }
7784 d .Set ("site_id" , siteID )
78- log .Printf ("[DEBUG] Import Site Config JSON for Site ID %d" , siteID )
85+
86+ if len (idSlice ) == 2 {
87+ if idSlice [1 ] == "" {
88+ return nil , fmt .Errorf ("unexpected format of ID (%q), expected site_id or site_id/account_id" , d .Id ())
89+ }
90+
91+ accountID , err := strconv .Atoi (idSlice [1 ])
92+ if err != nil {
93+ fmt .Errorf ("failed to convert Account Id from import command, actual value: %s, expected numeric id" , idSlice [1 ])
94+ }
95+ d .Set ("account_id" , accountID )
96+ }
97+
98+ log .Printf ("[DEBUG] Import Site ssl settings for Site ID %d" , siteID )
7999 return []* schema.ResourceData {d }, nil
80100 },
81101 },
@@ -87,6 +107,11 @@ func resourceSiteSSLSettings() *schema.Resource {
87107 Required : true ,
88108 ForceNew : true ,
89109 },
110+ "account_id" : {
111+ Description : "Numeric identifier of the account in which the site is located" ,
112+ Type : schema .TypeInt ,
113+ Optional : true ,
114+ },
90115 "hsts" : {
91116 Type : schema .TypeSet ,
92117 Optional : true ,
@@ -108,7 +133,7 @@ func resourceSiteSSLSettingsUpdate(d *schema.ResourceData, m interface{}) error
108133
109134 setting := getSSLSettingsDTO (d )
110135
111- _ , err := client .UpdateSiteSSLSettings (d .Get ("site_id" ).(int ), setting )
136+ _ , err := client .UpdateSiteSSLSettings (d .Get ("site_id" ).(int ), d . Get ( "account_id" ).( int ), setting )
112137
113138 if err != nil {
114139 return err
@@ -120,7 +145,7 @@ func resourceSiteSSLSettingsUpdate(d *schema.ResourceData, m interface{}) error
120145func resourceSiteSSLSettingsRead (d * schema.ResourceData , m interface {}) error {
121146 client := m .(* Client )
122147
123- settingsData , statusCode , err := client .ReadSiteSSLSettings (d .Get ("site_id" ).(int ))
148+ settingsData , statusCode , err := client .ReadSiteSSLSettings (d .Get ("site_id" ).(int ), d . Get ( "account_id" ).( int ) )
124149 if statusCode == 404 {
125150 d .SetId ("" )
126151 return nil
@@ -150,7 +175,7 @@ func resourceSiteSSLSettingsDelete(d *schema.ResourceData, m interface{}) error
150175 // If more settings are implemented in the endpoint, add delete logic for them here.
151176 setting := prepareDisableHSTSStructure ()
152177 prepareDefaultTLSStructure (& setting )
153- var _ , err = client .UpdateSiteSSLSettings (d .Get ("site_id" ).(int ), setting )
178+ var _ , err = client .UpdateSiteSSLSettings (d .Get ("site_id" ).(int ), d . Get ( "account_id" ).( int ), setting )
154179
155180 if err != nil {
156181 return err
@@ -160,7 +185,7 @@ func resourceSiteSSLSettingsDelete(d *schema.ResourceData, m interface{}) error
160185}
161186
162187func prepareDisableHSTSStructure () SSLSettingsResponse {
163- disableHSTSSetting := HSTSConfiguration {
188+ disableHSTSSetting := & HSTSConfiguration {
164189 IsEnabled : false ,
165190 }
166191
@@ -185,7 +210,7 @@ func prepareDefaultTLSStructure(settingsData *SSLSettingsResponse) {
185210
186211func mapHSTSResponseToHSTSResource (d * schema.ResourceData , settingsData * SSLSettingsResponse ) {
187212 // handle HSTS remote configuration mapping
188- var hstsSettingsFromServer HSTSConfiguration
213+ var hstsSettingsFromServer * HSTSConfiguration
189214 hstsSettingsFromServer = settingsData .Data [0 ].HstsConfiguration
190215 // Get the "hsts" attribute from the resource data
191216 // Create a map to hold the values for the "hsts" nested object
@@ -202,12 +227,15 @@ func mapHSTSResponseToHSTSResource(d *schema.ResourceData, settingsData *SSLSett
202227 // END HSTS mapping
203228}
204229
205- func mapHSTSResourceToHSTSDTO (d * schema.ResourceData ) HSTSConfiguration {
206- hsts := d .Get ("hsts" ).(* schema.Set )
230+ func mapHSTSResourceToHSTSDTO (d * schema.ResourceData ) * HSTSConfiguration {
231+ hsts , ok := d .Get ("hsts" ).(* schema.Set )
232+ if ! ok || hsts .Len () == 0 {
233+ return nil
234+ }
207235 hstsList := hsts .List ()
208236 hstsMap := hstsList [0 ].(map [string ]interface {})
209237
210- return HSTSConfiguration {
238+ return & HSTSConfiguration {
211239 IsEnabled : hstsMap ["is_enabled" ].(bool ),
212240 MaxAge : hstsMap ["max_age" ].(int ),
213241 PreLoaded : hstsMap ["pre_loaded" ].(bool ),
0 commit comments