/
api_blueprints_deploy.go
173 lines (149 loc) · 4.01 KB
/
api_blueprints_deploy.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package apstra
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"time"
)
const (
apiUrlBlueprintDeploy = apiUrlBlueprintById + apiUrlPathDelim + "deploy"
apiUrlBlueprintRevisions = apiUrlBlueprintById + apiUrlPathDelim + "revisions"
)
type DeployStatus int
type deployStatus string
const (
DeployStatusSuccess = DeployStatus(iota)
DeployStatusFailure
DeployStatusUnknown = "unknown deploy status '%s'"
deployStatusSuccess = deployStatus("success")
deployStatusFailure = deployStatus("failure")
deployStatusUnknown = "unknown redundancy protocol '%d'"
)
func (o DeployStatus) Int() int {
return int(o)
}
func (o DeployStatus) String() string {
switch o {
case DeployStatusSuccess:
return string(deployStatusSuccess)
case DeployStatusFailure:
return string(deployStatusFailure)
default:
return fmt.Sprintf(deployStatusUnknown, o)
}
}
func (o deployStatus) string() string {
return string(o)
}
func (o deployStatus) parse() (int, error) {
switch o {
case deployStatusSuccess:
return int(DeployStatusSuccess), nil
case deployStatusFailure:
return int(DeployStatusFailure), nil
default:
return 0, fmt.Errorf(DeployStatusUnknown, o)
}
}
type BlueprintDeployRequest struct {
Id ObjectId
Description string
Version int
}
type rawBlueprintDeployResponse struct {
Status deployStatus `json:"state"`
Version int `json:"version"`
Error *string `json:"error,omitempty"`
}
func (o *rawBlueprintDeployResponse) polish() (*BlueprintDeployResponse, error) {
status, err := o.Status.parse()
if err != nil {
return nil, err
}
return &BlueprintDeployResponse{
Status: DeployStatus(status),
Version: o.Version,
Error: o.Error,
}, nil
}
type BlueprintDeployResponse struct {
Status DeployStatus `json:"state"`
Version int `json:"version"`
Error *string `json:"error,omitempty"`
}
func (o *Client) deployBlueprint(ctx context.Context, in *BlueprintDeployRequest) (*rawBlueprintDeployResponse, error) {
request := &struct {
Description string `json:"description"`
Version int `json:"version"`
}{
Description: in.Description,
Version: in.Version,
}
deployUrl, err := url.Parse(fmt.Sprintf(apiUrlBlueprintDeploy, in.Id))
if err != nil {
return nil, err
}
err = o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPut,
url: deployUrl,
apiInput: request,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
response := &rawBlueprintDeployResponse{}
err = o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
url: deployUrl,
apiResponse: &response,
})
return response, convertTtaeToAceWherePossible(err)
}
type rawBlueprintRevision struct {
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
AutoSaved bool `json:"auto_saved"`
UserSaved bool `json:"user_saved"`
UserIp string `json:"user_ip"`
User string `json:"user"`
RevisionId string `json:"revision_id"`
}
func (o *rawBlueprintRevision) polish() (*BlueprintRevision, error) {
revisionId, err := strconv.Atoi(o.RevisionId)
if err != nil {
return nil, fmt.Errorf("error parsing blueprint revision %q to integer - %w",
o.RevisionId, err)
}
return &BlueprintRevision{
Description: o.Description,
CreatedAt: o.CreatedAt,
AutoSaved: o.AutoSaved,
UserSaved: o.UserSaved,
UserIp: net.ParseIP(o.UserIp),
User: o.User,
RevisionId: revisionId,
}, nil
}
type BlueprintRevision struct {
Description string
CreatedAt time.Time
AutoSaved bool
UserSaved bool
UserIp net.IP
User string
RevisionId int
}
func (o *Client) getBlueprintRevisions(ctx context.Context, id ObjectId) ([]rawBlueprintRevision, error) {
result := &struct {
Items []rawBlueprintRevision `json:"items"`
}{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlBlueprintRevisions, id),
apiResponse: result,
})
return result.Items, convertTtaeToAceWherePossible(err)
}