forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
97 lines (83 loc) · 2.8 KB
/
operations.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
// +build go1.7
package management
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import (
"encoding/xml"
"errors"
"fmt"
"time"
)
var (
// ErrOperationCancelled from WaitForOperation when the polling loop is
// cancelled through signaling the channel.
ErrOperationCancelled = errors.New("Polling for operation status cancelled")
)
// GetOperationStatusResponse represents an in-flight operation. Use
// client.GetOperationStatus() to get the operation given the operation ID, or
// use WaitForOperation() to poll and wait until the operation has completed.
// See https://msdn.microsoft.com/en-us/library/azure/ee460783.aspx
type GetOperationStatusResponse struct {
XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure Operation"`
ID string
Status OperationStatus
HTTPStatusCode string
Error *AzureError
}
// OperationStatus describes the states an Microsoft Azure Service Management
// operation an be in.
type OperationStatus string
// List of states an operation can be reported as
const (
OperationStatusInProgress OperationStatus = "InProgress"
OperationStatusSucceeded OperationStatus = "Succeeded"
OperationStatusFailed OperationStatus = "Failed"
)
// OperationID is assigned by Azure API and can be used to look up the status of
// an operation
type OperationID string
func (c client) GetOperationStatus(operationID OperationID) (GetOperationStatusResponse, error) {
operation := GetOperationStatusResponse{}
if operationID == "" {
return operation, fmt.Errorf(errParamNotSpecified, "operationID")
}
url := fmt.Sprintf("operations/%s", operationID)
response, azureErr := c.SendAzureGetRequest(url)
if azureErr != nil {
return operation, azureErr
}
err := xml.Unmarshal(response, &operation)
return operation, err
}
func (c client) WaitForOperation(operationID OperationID, cancel chan struct{}) error {
for {
done, err := c.checkOperationStatus(operationID)
if err != nil || done {
return err
}
select {
case <-time.After(c.config.OperationPollInterval):
case <-cancel:
return ErrOperationCancelled
}
}
}
func (c client) checkOperationStatus(id OperationID) (done bool, err error) {
op, err := c.GetOperationStatus(id)
if err != nil {
return false, fmt.Errorf("Failed to get operation status '%s': %v", id, err)
}
switch op.Status {
case OperationStatusSucceeded:
return true, nil
case OperationStatusFailed:
if op.Error != nil {
return true, op.Error
}
return true, fmt.Errorf("Azure Operation (x-ms-request-id=%s) has failed", id)
case OperationStatusInProgress:
return false, nil
default:
return false, fmt.Errorf("Unknown operation status returned from API: %s (x-ms-request-id=%s)", op.Status, id)
}
}