@@ -2,13 +2,13 @@ package nat
22
33import (
44 "context"
5- "log "
5+ "strings "
66
77 "github.com/hashicorp/go-multierror"
88 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
11- "github.com/chnsz/golangsdk/openstack/nat/v3/snats "
11+ "github.com/chnsz/golangsdk"
1212
1313 "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
1414 "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
@@ -93,94 +93,168 @@ func ResourcePrivateSnatRule() *schema.Resource {
9393 }
9494}
9595
96+ func buildCreatePrivateSnatRuleBodyParams (d * schema.ResourceData ) map [string ]interface {} {
97+ snatRuleBodyParams := map [string ]interface {}{
98+ "gateway_id" : d .Get ("gateway_id" ),
99+ "transit_ip_ids" : []string {d .Get ("transit_ip_id" ).(string )},
100+ "cidr" : utils .ValueIgnoreEmpty (d .Get ("cidr" )),
101+ "virsubnet_id" : utils .ValueIgnoreEmpty (d .Get ("subnet_id" )),
102+ "description" : utils .ValueIgnoreEmpty (d .Get ("description" )),
103+ }
104+
105+ return map [string ]interface {}{
106+ "snat_rule" : snatRuleBodyParams ,
107+ }
108+ }
109+
96110func resourcePrivateSnatRuleCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
97- cfg := meta .(* config.Config )
98- client , err := cfg .NatV3Client (cfg .GetRegion (d ))
111+ var (
112+ cfg = meta .(* config.Config )
113+ region = cfg .GetRegion (d )
114+ httpUrl = "v3/{project_id}/private-nat/snat-rules"
115+ )
116+
117+ client , err := cfg .NewServiceClient ("nat" , region )
99118 if err != nil {
100119 return diag .Errorf ("error creating NAT v3 client: %s" , err )
101120 }
102121
103- opts := snats.CreateOpts {
104- GatewayId : d .Get ("gateway_id" ).(string ),
105- TransitIpIds : []string {d .Get ("transit_ip_id" ).(string )},
106- Cidr : d .Get ("cidr" ).(string ),
107- SubnetId : d .Get ("subnet_id" ).(string ),
108- Description : d .Get ("description" ).(string ),
122+ createPath := client .Endpoint + httpUrl
123+ createPath = strings .ReplaceAll (createPath , "{project_id}" , client .ProjectID )
124+ createOpt := golangsdk.RequestOpts {
125+ KeepResponseBody : true ,
126+ JSONBody : utils .RemoveNil (buildCreatePrivateSnatRuleBodyParams (d )),
109127 }
110128
111- log .Printf ("[DEBUG] The create options of the SNAT rule (Private NAT) is: %#v" , opts )
112- resp , err := snats .Create (client , opts )
129+ resp , err := client .Request ("POST" , createPath , & createOpt )
113130 if err != nil {
114- return diag .Errorf ("error creating SNAT rule (Private NAT) : %s" , err )
131+ return diag .Errorf ("error creating private SNAT rule: %s" , err )
115132 }
116- d .SetId (resp .ID )
133+
134+ respBody , err := utils .FlattenResponse (resp )
135+ if err != nil {
136+ return diag .FromErr (err )
137+ }
138+
139+ ruleId := utils .PathSearch ("snat_rule.id" , respBody , "" ).(string )
140+ if ruleId == "" {
141+ return diag .Errorf ("error creating private SNAT rule: ID is not found in API response" )
142+ }
143+
144+ d .SetId (ruleId )
117145
118146 return resourcePrivateSnatRuleRead (ctx , d , meta )
119147}
120148
149+ func GetPrivateSnatRule (client * golangsdk.ServiceClient , ruleId string ) (interface {}, error ) {
150+ httpUrl := "v3/{project_id}/private-nat/snat-rules/{snat_rule_id}"
151+ getPath := client .Endpoint + httpUrl
152+ getPath = strings .ReplaceAll (getPath , "{project_id}" , client .ProjectID )
153+ getPath = strings .ReplaceAll (getPath , "{snat_rule_id}" , ruleId )
154+ getOpt := golangsdk.RequestOpts {
155+ KeepResponseBody : true ,
156+ }
157+
158+ getResp , err := client .Request ("GET" , getPath , & getOpt )
159+ if err != nil {
160+ return nil , err
161+ }
162+
163+ return utils .FlattenResponse (getResp )
164+ }
165+
121166func resourcePrivateSnatRuleRead (_ context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
122- cfg := meta .(* config.Config )
123- region := cfg .GetRegion (d )
124- client , err := cfg .NatV3Client (region )
167+ var (
168+ cfg = meta .(* config.Config )
169+ region = cfg .GetRegion (d )
170+ )
171+
172+ client , err := cfg .NewServiceClient ("nat" , region )
125173 if err != nil {
126174 return diag .Errorf ("error creating NAT v3 client: %s" , err )
127175 }
128176
129- resp , err := snats . Get (client , d .Id ())
177+ respBody , err := GetPrivateSnatRule (client , d .Id ())
130178 if err != nil {
131179 // If the private SNAT rule does not exist, the response HTTP status code of the details API is 404.
132180 return common .CheckDeletedDiag (d , err , "error retrieving private SNAT rule" )
133181 }
134- mErr := multierror .Append (nil ,
182+
183+ mErr := multierror .Append (
135184 d .Set ("region" , region ),
136- d .Set ("gateway_id" , resp . GatewayId ),
137- d .Set ("transit_ip_id" , utils .PathSearch ("[0].ID " , resp . TransitIpAssociations , nil )),
138- d .Set ("description" , resp . Description ),
139- d .Set ("subnet_id" , resp . SubnetId ),
140- d .Set ("cidr" , resp . Cidr ),
141- d .Set ("created_at" , resp . CreatedAt ),
142- d .Set ("updated_at" , resp . UpdatedAt ),
143- d .Set ("enterprise_project_id" , resp . EnterpriseProjectId ),
144- d .Set ("transit_ip_address" , utils .PathSearch ("[0].Address " , resp . TransitIpAssociations , nil )),
185+ d .Set ("gateway_id" , utils . PathSearch ( "snat_rule.gateway_id" , respBody , nil ) ),
186+ d .Set ("transit_ip_id" , utils .PathSearch ("snat_rule.transit_ip_associations [0].transit_ip_id " , respBody , nil )),
187+ d .Set ("description" , utils . PathSearch ( "snat_rule.description" , respBody , nil ) ),
188+ d .Set ("subnet_id" , utils . PathSearch ( "snat_rule.virsubnet_id" , respBody , nil ) ),
189+ d .Set ("cidr" , utils . PathSearch ( "snat_rule.cidr" , respBody , nil ) ),
190+ d .Set ("created_at" , utils . PathSearch ( "snat_rule.created_at" , respBody , nil ) ),
191+ d .Set ("updated_at" , utils . PathSearch ( "snat_rule.updated_at" , respBody , nil ) ),
192+ d .Set ("enterprise_project_id" , utils . PathSearch ( "snat_rule.enterprise_project_id" , respBody , nil ) ),
193+ d .Set ("transit_ip_address" , utils .PathSearch ("snat_rule.transit_ip_associations [0].transit_ip_address " , respBody , nil )),
145194 )
146195
147- if err = mErr .ErrorOrNil (); err != nil {
148- return diag .Errorf ("error saving resource fields of the private SNAT rule: %s" , err )
196+ return diag .FromErr (mErr .ErrorOrNil ())
197+ }
198+
199+ func buildUpdatePrivateSnatRuleBodyParams (d * schema.ResourceData ) map [string ]interface {} {
200+ snatRuleBodyParams := map [string ]interface {}{
201+ "transit_ip_ids" : []string {d .Get ("transit_ip_id" ).(string )},
202+ "description" : d .Get ("description" ),
203+ }
204+
205+ return map [string ]interface {}{
206+ "snat_rule" : snatRuleBodyParams ,
149207 }
150- return nil
151208}
152209
153210func resourcePrivateSnatRuleUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
154- cfg := meta .(* config.Config )
155- region := cfg .GetRegion (d )
156- client , err := cfg .NatV3Client (region )
211+ var (
212+ cfg = meta .(* config.Config )
213+ region = cfg .GetRegion (d )
214+ httpUrl = "v3/{project_id}/private-nat/snat-rules/{snat_rule_id}"
215+ )
216+
217+ client , err := cfg .NewServiceClient ("nat" , region )
157218 if err != nil {
158219 return diag .Errorf ("error creating NAT v3 client: %s" , err )
159220 }
160221
161- ruleId := d .Id ()
162- opts := snats.UpdateOpts {
163- TransitIpIds : []string {d .Get ("transit_ip_id" ).(string )},
164- Description : utils .String (d .Get ("description" ).(string )),
222+ updatePath := client .Endpoint + httpUrl
223+ updatePath = strings .ReplaceAll (updatePath , "{project_id}" , client .ProjectID )
224+ updatePath = strings .ReplaceAll (updatePath , "{snat_rule_id}" , d .Id ())
225+ updateOpt := golangsdk.RequestOpts {
226+ KeepResponseBody : true ,
227+ JSONBody : buildUpdatePrivateSnatRuleBodyParams (d ),
165228 }
166229
167- _ , err = snats . Update ( client , ruleId , opts )
230+ _ , err = client . Request ( "PUT" , updatePath , & updateOpt )
168231 if err != nil {
169- return diag .Errorf ("error updating private SNAT rule (%s): %s" , ruleId , err )
232+ return diag .Errorf ("error updating private SNAT rule (%s): %s" , d . Id () , err )
170233 }
171234
172235 return resourcePrivateSnatRuleRead (ctx , d , meta )
173236}
174237
175238func resourcePrivateSnatRuleDelete (_ context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
176- cfg := meta .(* config.Config )
177- client , err := cfg .NatV3Client (cfg .GetRegion (d ))
239+ var (
240+ cfg = meta .(* config.Config )
241+ region = cfg .GetRegion (d )
242+ httpUrl = "v3/{project_id}/private-nat/snat-rules/{snat_rule_id}"
243+ )
244+
245+ client , err := cfg .NewServiceClient ("nat" , region )
178246 if err != nil {
179247 return diag .Errorf ("error creating NAT v3 client: %s" , err )
180248 }
181249
182- ruleId := d .Id ()
183- err = snats .Delete (client , ruleId )
250+ deletePath := client .Endpoint + httpUrl
251+ deletePath = strings .ReplaceAll (deletePath , "{project_id}" , client .ProjectID )
252+ deletePath = strings .ReplaceAll (deletePath , "{snat_rule_id}" , d .Id ())
253+ deleteOpts := golangsdk.RequestOpts {
254+ KeepResponseBody : true ,
255+ }
256+
257+ _ , err = client .Request ("DELETE" , deletePath , & deleteOpts )
184258 if err != nil {
185259 // If the private SNAT rule does not exist, the response HTTP status code of the details API is 404.
186260 return common .CheckDeletedDiag (d , err , "error deleting private SNAT rule" )
0 commit comments