-
Notifications
You must be signed in to change notification settings - Fork 36
/
poller_delete.go
127 lines (107 loc) · 3.32 KB
/
poller_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package resourcemanager
import (
"context"
"fmt"
"net/http"
"time"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
var _ pollers.PollerType = &deletePoller{}
type deletePoller struct {
apiVersion string
client *Client
initialRetryDuration time.Duration
originalUri string
resourcePath string
}
func deletePollerFromResponse(response *client.Response, client *Client, pollingInterval time.Duration) (*deletePoller, error) {
// if we've gotten to this point then we're polling against a Resource Manager resource/operation of some kind
// we next need to determine if the current URI is a Resource Manager resource, or if we should be polling on the
// resource (e.g. `/my/resource`) rather than an operation on the resource (e.g. `/my/resource/start`)
if response.Request == nil {
return nil, fmt.Errorf("request was nil")
}
originalUri := response.Request.URL.RequestURI()
if response.Request.URL == nil {
return nil, fmt.Errorf("request url was nil")
}
// all Resource Manager operations require the `api-version` querystring
apiVersion := response.Request.URL.Query().Get("api-version")
if apiVersion == "" {
return nil, fmt.Errorf("unable to determine `api-version` from %q", originalUri)
}
resourcePath, err := resourceManagerResourcePathFromUri(originalUri)
if err != nil {
return nil, fmt.Errorf("determining Resource Manager Resource Path from %q: %+v", originalUri, err)
}
return &deletePoller{
apiVersion: apiVersion,
client: client,
initialRetryDuration: pollingInterval,
originalUri: originalUri,
resourcePath: *resourcePath,
}, nil
}
func (p deletePoller) Poll(ctx context.Context) (result *pollers.PollResult, err error) {
opts := client.RequestOptions{
ContentType: "application/json; charset=utf-8",
ExpectedStatusCodes: []int{
http.StatusOK,
http.StatusNotFound,
},
HttpMethod: http.MethodGet,
OptionsObject: deleteOptions{
apiVersion: p.apiVersion,
},
Path: p.resourcePath,
}
req, err := p.client.NewRequest(ctx, opts)
if err != nil {
return nil, fmt.Errorf("building request: %+v", err)
}
resp, err := p.client.Execute(ctx, req)
if err != nil {
return nil, fmt.Errorf("executing request: %+v", err)
}
if resp == nil {
return nil, pollers.PollingDroppedConnectionError{}
}
result = &pollers.PollResult{
PollInterval: p.initialRetryDuration,
}
if resp.Response != nil {
switch resp.StatusCode {
case http.StatusNotFound:
{
result.Status = pollers.PollingStatusSucceeded
return
}
case http.StatusOK:
{
result.Status = pollers.PollingStatusInProgress
return
}
}
err = fmt.Errorf("unexpected status code when polling for resource after deletion, expected a 200/204 but got %d", resp.StatusCode)
}
return
}
var _ client.Options = deleteOptions{}
type deleteOptions struct {
apiVersion string
}
func (p deleteOptions) ToHeaders() *client.Headers {
return &client.Headers{}
}
func (p deleteOptions) ToOData() *odata.Query {
return &odata.Query{}
}
func (p deleteOptions) ToQuery() *client.QueryParams {
q := client.QueryParams{}
q.Append("api-version", p.apiVersion)
return &q
}