Skip to content

Commit 15f292f

Browse files
add put url params;
turn on update_on_create in all simple example resources
1 parent b028757 commit 15f292f

13 files changed

+89
-8
lines changed

gen/definitions/fabric_l3_virtual_network.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ put_id_include_path: "0.id"
1010
put_no_id: true
1111
doc_category: SDA
1212
mutex: true
13+
allow_existing_on_create: true
1314
attributes:
1415
- model_name: virtualNetworkName
1516
requires_replace: true

gen/definitions/ip_pool.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ get_requires_id: true
55
id_from_query_path: response
66
get_extra_query_params: "?limit=1000"
77
doc_category: Network Settings
8+
allow_existing_on_create: true
89
attributes:
910
- model_name: ipPoolName
1011
tf_name: name

gen/definitions/ip_pool_reservation.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ put_id_query_param: id
88
import_no_id: true
99
data_source_no_id: true
1010
doc_category: Network Settings
11+
allow_existing_on_create: true
1112
attributes:
1213
- model_name: siteId
1314
type: String

gen/definitions/transit_network.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ put_no_id: true
99
put_id_include_path: 0.id
1010
skip_minimum_test: true
1111
doc_category: SDA
12+
allow_existing_on_create: true
1213
attributes:
1314
- model_name: name
1415
type: String

gen/templates/resource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,14 @@ func (r *{{camelCase .Name}}Resource) Create(ctx context.Context, req resource.C
573573
if !r.AllowExistingOnCreate {
574574
tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))
575575
} else {
576+
params = ""
577+
{{- if hasCreateQueryPath .Attributes}}
578+
{{- $createQueryPath := getCreateQueryPath .Attributes}}
579+
params += "/" + url.QueryEscape(plan.{{toGoName $createQueryPath.TfName}}.Value{{$createQueryPath.Type}}())
580+
{{- end}}
581+
{{- if .PutIdQueryParam}}
582+
params += "?{{.PutIdQueryParam}}=" + url.QueryEscape(plan.Id.ValueString())
583+
{{- end}}
576584
body = plan.toBody(ctx, {{camelCase .Name}}{Id: plan.Id})
577585
{{- if .PostUpdate}}
578586
res, err = r.client.Post(plan.getPath() + params, body {{- if .MaxAsyncWaitTime }}, func(r *cc.Req) { r.MaxAsyncWaitTime={{.MaxAsyncWaitTime}} }{{end}}{{- if .Mutex }}, cc.UseMutex{{- end}})

internal/provider/resource_catalystcenter_area.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (r *AreaResource) Create(ctx context.Context, req resource.CreateRequest, r
122122
if !r.AllowExistingOnCreate {
123123
tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))
124124
} else {
125+
params = ""
125126
body = plan.toBody(ctx, Area{Id: plan.Id})
126127
res, err = r.client.Put(plan.getPath()+"/"+url.QueryEscape(plan.Id.ValueString())+params, body)
127128
if err != nil {

internal/provider/resource_catalystcenter_building.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func (r *BuildingResource) Create(ctx context.Context, req resource.CreateReques
138138
if !r.AllowExistingOnCreate {
139139
tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))
140140
} else {
141+
params = ""
141142
body = plan.toBody(ctx, Building{Id: plan.Id})
142143
res, err = r.client.Put(plan.getPath()+"/"+url.QueryEscape(plan.Id.ValueString())+params, body)
143144
if err != nil {

internal/provider/resource_catalystcenter_fabric_l3_virtual_network.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,12 @@ func (r *FabricL3VirtualNetworkResource) Create(ctx context.Context, req resourc
149149
params := ""
150150
res, err := r.client.Post(plan.getPath()+params, body, cc.UseMutex)
151151
if err != nil {
152-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "POST", err, res.String()))
153-
return
152+
if r.AllowExistingOnCreate {
153+
tflog.Info(ctx, fmt.Sprintf("Failed to configure object (%s), got error: %s, %s. allow_existing_on_create is true, beginning update", "POST", err, res.String()))
154+
} else {
155+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "POST", err, res.String()))
156+
return
157+
}
154158
}
155159

156160
var fabricIds []string
@@ -169,6 +173,17 @@ func (r *FabricL3VirtualNetworkResource) Create(ctx context.Context, req resourc
169173
return
170174
}
171175
plan.Id = types.StringValue(res.Get("response.0.id").String())
176+
if r.AllowExistingOnCreate {
177+
params = ""
178+
body = plan.toBody(ctx, FabricL3VirtualNetwork{Id: plan.Id})
179+
res, err = r.client.Put(plan.getPath()+params, body)
180+
if err != nil {
181+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to configure object (%s), got error: %s, %s", "PUT", err, res.String()))
182+
return
183+
}
184+
tflog.Debug(ctx, fmt.Sprintf("%s: Fallback to update existing resource finished successfully", plan.Id.ValueString()))
185+
186+
}
172187
}
173188

174189
tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))

internal/provider/resource_catalystcenter_fabric_site.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (r *FabricSiteResource) Create(ctx context.Context, req resource.CreateRequ
140140
if !r.AllowExistingOnCreate {
141141
tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))
142142
} else {
143+
params = ""
143144
body = plan.toBody(ctx, FabricSite{Id: plan.Id})
144145
res, err = r.client.Put(plan.getPath()+params, body, cc.UseMutex)
145146
if err != nil {

internal/provider/resource_catalystcenter_floor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func (r *FloorResource) Create(ctx context.Context, req resource.CreateRequest,
151151
if !r.AllowExistingOnCreate {
152152
tflog.Debug(ctx, fmt.Sprintf("%s: Create finished successfully", plan.Id.ValueString()))
153153
} else {
154+
params = ""
154155
body = plan.toBody(ctx, Floor{Id: plan.Id})
155156
res, err = r.client.Put(plan.getPath()+"/"+url.QueryEscape(plan.Id.ValueString())+params, body)
156157
if err != nil {

0 commit comments

Comments
 (0)