Skip to content

Commit

Permalink
Invoke API to update resource only when there is change in "body"
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Jun 22, 2022
1 parent c7bb768 commit a1b5c9c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ The document of this provider is available on [Terraform Provider Registry](http
- Different authentication choices: HTTP auth (Basic, Bearer), API Key auth and OAuth2 (client credential, password credential).
- Support resources created via either `PUT` or `POST`
- Support polling asynchronous operations
- Support `ignore_changes` to ignore changes for the the specified properties
- Partial `body` tracking: only the specified properties of the resource in the `body` attribute is tracked for diffs

## Why
Expand Down
49 changes: 26 additions & 23 deletions internal/provider/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,36 +559,39 @@ func (r resource) Update(ctx context.Context, req tfsdk.UpdateResourceRequest, r
return
}

response, err := c.Update(ctx, state.ID.Value, plan.Body.Value, *opt)
if err != nil {
resp.Diagnostics.AddError(
"Error to call update",
err.Error(),
)
return
}
if !response.IsSuccess() {
resp.Diagnostics.AddError(
fmt.Sprintf("Update API returns %d", response.StatusCode()),
string(response.Body()),
)
return
}

// For LRO, wait for completion
if opt.PollOpt != nil {
p, err := client.NewPollable(*response, *opt.PollOpt)
// Invoke API to Update the resource only when there are changes in the body.
if state.Body.Value != plan.Body.Value {
response, err := c.Update(ctx, state.ID.Value, plan.Body.Value, *opt)
if err != nil {
resp.Diagnostics.AddError(
"Update: Failed to build poller from the response of the initiated request",
"Error to call update",
err.Error(),
)
return
}
if err := p.PollUntilDone(ctx, c); err != nil {
if !response.IsSuccess() {
resp.Diagnostics.AddError(
"Update: Polling failure",
err.Error(),
fmt.Sprintf("Update API returns %d", response.StatusCode()),
string(response.Body()),
)
return
}

// For LRO, wait for completion
if opt.PollOpt != nil {
p, err := client.NewPollable(*response, *opt.PollOpt)
if err != nil {
resp.Diagnostics.AddError(
"Update: Failed to build poller from the response of the initiated request",
err.Error(),
)
}
if err := p.PollUntilDone(ctx, c); err != nil {
resp.Diagnostics.AddError(
"Update: Polling failure",
err.Error(),
)
}
}
}

Expand Down

0 comments on commit a1b5c9c

Please sign in to comment.