-
Notifications
You must be signed in to change notification settings - Fork 470
Check future regardless of error in async helpers and return VM result for existing VM #1837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,15 +78,29 @@ func (ac *AzureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.Resou | |
| ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualmachines.AzureClient.CreateOrUpdate") | ||
| defer done() | ||
|
|
||
| vm, err := ac.vmParams(ctx, spec) | ||
| var existingVM interface{} | ||
|
|
||
| if existing, err := ac.Get(ctx, spec.ResourceGroupName(), spec.ResourceName()); err != nil && !azure.ResourceNotFound(err) { | ||
| return nil, nil, errors.Wrapf(err, "failed to get virtual machine %s in %s", spec.ResourceName(), spec.ResourceGroupName()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we handle errors that are not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it's a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah, this used to be in a different form in |
||
| } else if err == nil { | ||
| existingVM = existing | ||
| } | ||
|
|
||
| params, err := spec.Parameters(existingVM) | ||
| if err != nil { | ||
| return nil, nil, errors.Wrapf(err, "failed to get desired parameters for virtual machine %s", spec.ResourceName()) | ||
| } else if vm == nil { | ||
| // nothing to do here. | ||
| return nil, nil, nil | ||
| } | ||
|
|
||
| future, err := ac.virtualmachines.CreateOrUpdate(ctx, spec.ResourceGroupName(), spec.ResourceName(), *vm) | ||
| vm, ok := params.(compute.VirtualMachine) | ||
| if !ok { | ||
| if params == nil { | ||
| // nothing to do here. | ||
| return existingVM, nil, nil | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this addresses #1697 (comment)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we move this to line 86 and invoke spec.Parameters() can be only for new vms? wdyt?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this specific VM service we technically could because we never want to update existing VMs but this pattern works best for all services in general: we pass in the existing resource to |
||
| } | ||
| return nil, nil, errors.Errorf("%T is not a compute.VirtualMachine", params) | ||
| } | ||
|
|
||
| future, err := ac.virtualmachines.CreateOrUpdate(ctx, spec.ResourceGroupName(), spec.ResourceName(), vm) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
@@ -186,35 +200,3 @@ func (ac *AzureClient) Result(ctx context.Context, futureData azureautorest.Futu | |
|
|
||
| return result(ac.virtualmachines) | ||
| } | ||
|
|
||
| // vmParams creates a VirtualMachine object from the given spec. | ||
| func (ac *AzureClient) vmParams(ctx context.Context, spec azure.ResourceSpecGetter) (*compute.VirtualMachine, error) { | ||
| ctx, span := tele.Tracer().Start(ctx, "virtualmachines.AzureClient.vmParams") | ||
| defer span.End() | ||
|
|
||
| var params interface{} | ||
| var existing interface{} | ||
|
|
||
| if existingVM, err := ac.Get(ctx, spec.ResourceGroupName(), spec.ResourceName()); err != nil && !azure.ResourceNotFound(err) { | ||
| return nil, errors.Wrapf(err, "failed to get virtual machine %s in %s", spec.ResourceName(), spec.ResourceGroupName()) | ||
| } else if err == nil { | ||
| // virtual machine already exists | ||
| existing = existingVM | ||
| } | ||
|
|
||
| params, err := spec.Parameters(existing) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| vm, ok := params.(compute.VirtualMachine) | ||
| if !ok { | ||
| if params == nil { | ||
| // nothing to do here. | ||
| return nil, nil | ||
| } | ||
| return nil, errors.Errorf("%T is not a compute.VirtualMachine", params) | ||
| } | ||
|
|
||
| return &vm, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karuppiah7890 please take a look. I believe this addresses your comment in #1684 (comment). I think it makes sense to not require an error (that way this will work whether an error is returned or not). However, I still think we shouldn't silence the error returned by
WaitForCompletionRefin the clients since eventually it might return a different error besides context deadline exceeded.