-
Notifications
You must be signed in to change notification settings - Fork 56
/
startup_script.go
136 lines (109 loc) · 4.05 KB
/
startup_script.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const scriptPath = "/v2/startup-scripts"
// StartupScriptService is the interface to interact with the startup script endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/startup
type StartupScriptService interface { //nolint:dupl
Create(ctx context.Context, req *StartupScriptReq) (*StartupScript, *http.Response, error)
Get(ctx context.Context, scriptID string) (*StartupScript, *http.Response, error)
Update(ctx context.Context, scriptID string, scriptReq *StartupScriptReq) error
Delete(ctx context.Context, scriptID string) error
List(ctx context.Context, options *ListOptions) ([]StartupScript, *Meta, *http.Response, error)
}
// StartupScriptServiceHandler handles interaction with the startup script methods for the Vultr API
type StartupScriptServiceHandler struct {
client *Client
}
// StartupScript represents an startup script on Vultr
type StartupScript struct {
ID string `json:"id"`
DateCreated string `json:"date_created"`
DateModified string `json:"date_modified"`
Name string `json:"name"`
Type string `json:"type"`
Script string `json:"script"`
}
// StartupScriptReq is the user struct for create and update calls
type StartupScriptReq struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Script string `json:"script,omitempty"`
}
type startupScriptsBase struct {
StartupScripts []StartupScript `json:"startup_scripts"`
Meta *Meta `json:"meta"`
}
type startupScriptBase struct {
StartupScript *StartupScript `json:"startup_script"`
}
var _ StartupScriptService = &StartupScriptServiceHandler{}
// Create a startup script
func (s *StartupScriptServiceHandler) Create(ctx context.Context, scriptReq *StartupScriptReq) (*StartupScript, *http.Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodPost, scriptPath, scriptReq)
if err != nil {
return nil, nil, err
}
script := new(startupScriptBase)
resp, err := s.client.DoWithContext(ctx, req, script)
if err != nil {
return nil, resp, err
}
return script.StartupScript, resp, nil
}
// Get a single startup script
func (s *StartupScriptServiceHandler) Get(ctx context.Context, scriptID string) (*StartupScript, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", scriptPath, scriptID)
req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
script := new(startupScriptBase)
resp, err := s.client.DoWithContext(ctx, req, script)
if err != nil {
return nil, resp, err
}
return script.StartupScript, resp, nil
}
// Update will update the given startup script. Empty strings will be ignored.
func (s *StartupScriptServiceHandler) Update(ctx context.Context, scriptID string, scriptReq *StartupScriptReq) error {
uri := fmt.Sprintf("%s/%s", scriptPath, scriptID)
req, err := s.client.NewRequest(ctx, http.MethodPatch, uri, scriptReq)
if err != nil {
return err
}
_, err = s.client.DoWithContext(ctx, req, nil)
return err
}
// Delete the specified startup script from your account.
func (s *StartupScriptServiceHandler) Delete(ctx context.Context, scriptID string) error {
uri := fmt.Sprintf("%s/%s", scriptPath, scriptID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = s.client.DoWithContext(ctx, req, nil)
return err
}
// List all the startup scripts associated with your Vultr account
func (s *StartupScriptServiceHandler) List(ctx context.Context, options *ListOptions) ([]StartupScript, *Meta, *http.Response, error) { //nolint:dupl,lll
req, err := s.client.NewRequest(ctx, http.MethodGet, scriptPath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
scripts := new(startupScriptsBase)
resp, err := s.client.DoWithContext(ctx, req, scripts)
if err != nil {
return nil, nil, resp, err
}
return scripts.StartupScripts, scripts.Meta, resp, nil
}