6
6
"net/url"
7
7
"time"
8
8
9
+ "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/hetzner/hcloud-go/hcloud/exp/ctxutil"
9
10
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/hetzner/hcloud-go/hcloud/schema"
10
11
)
11
12
@@ -54,9 +55,21 @@ const (
54
55
type ActionError struct {
55
56
Code string
56
57
Message string
58
+
59
+ action * Action
60
+ }
61
+
62
+ // Action returns the [Action] that triggered the error if available.
63
+ func (e ActionError ) Action () * Action {
64
+ return e .action
57
65
}
58
66
59
67
func (e ActionError ) Error () string {
68
+ action := e .Action ()
69
+ if action != nil {
70
+ // For easier debugging, the error string contains the Action ID.
71
+ return fmt .Sprintf ("%s (%s, %d)" , e .Message , e .Code , action .ID )
72
+ }
60
73
return fmt .Sprintf ("%s (%s)" , e .Message , e .Code )
61
74
}
62
75
@@ -65,6 +78,7 @@ func (a *Action) Error() error {
65
78
return ActionError {
66
79
Code : a .ErrorCode ,
67
80
Message : a .ErrorMessage ,
81
+ action : a ,
68
82
}
69
83
}
70
84
return nil
@@ -111,11 +125,15 @@ func (c *ActionClient) List(ctx context.Context, opts ActionListOpts) ([]*Action
111
125
}
112
126
113
127
// All returns all actions.
128
+ //
129
+ // Deprecated: It is required to pass in a list of IDs since 30 January 2025. Please use [ActionClient.AllWithOpts] instead.
114
130
func (c * ActionClient ) All (ctx context.Context ) ([]* Action , error ) {
115
131
return c .action .All (ctx , ActionListOpts {ListOpts : ListOpts {PerPage : 50 }})
116
132
}
117
133
118
134
// AllWithOpts returns all actions for the given options.
135
+ //
136
+ // It is required to set [ActionListOpts.ID]. Any other fields set in the opts are ignored.
119
137
func (c * ActionClient ) AllWithOpts (ctx context.Context , opts ActionListOpts ) ([]* Action , error ) {
120
138
return c .action .All (ctx , opts )
121
139
}
@@ -136,65 +154,43 @@ func (c *ResourceActionClient) getBaseURL() string {
136
154
137
155
// GetByID retrieves an action by its ID. If the action does not exist, nil is returned.
138
156
func (c * ResourceActionClient ) GetByID (ctx context.Context , id int64 ) (* Action , * Response , error ) {
139
- req , err := c .client .NewRequest (ctx , "GET" , fmt .Sprintf ("%s/actions/%d" , c .getBaseURL (), id ), nil )
140
- if err != nil {
141
- return nil , nil , err
142
- }
157
+ opPath := c .getBaseURL () + "/actions/%d"
158
+ ctx = ctxutil .SetOpPath (ctx , opPath )
143
159
144
- var body schema.ActionGetResponse
145
- resp , err := c .client .Do (req , & body )
160
+ reqPath := fmt .Sprintf (opPath , id )
161
+
162
+ respBody , resp , err := getRequest [schema.ActionGetResponse ](ctx , c .client , reqPath )
146
163
if err != nil {
147
164
if IsError (err , ErrorCodeNotFound ) {
148
165
return nil , resp , nil
149
166
}
150
- return nil , nil , err
167
+ return nil , resp , err
151
168
}
152
- return ActionFromSchema (body .Action ), resp , nil
169
+ return ActionFromSchema (respBody .Action ), resp , nil
153
170
}
154
171
155
172
// List returns a list of actions for a specific page.
156
173
//
157
174
// Please note that filters specified in opts are not taken into account
158
175
// when their value corresponds to their zero value or when they are empty.
159
176
func (c * ResourceActionClient ) List (ctx context.Context , opts ActionListOpts ) ([]* Action , * Response , error ) {
160
- req , err := c .client .NewRequest (
161
- ctx ,
162
- "GET" ,
163
- fmt .Sprintf ("%s/actions?%s" , c .getBaseURL (), opts .values ().Encode ()),
164
- nil ,
165
- )
166
- if err != nil {
167
- return nil , nil , err
168
- }
177
+ opPath := c .getBaseURL () + "/actions?%s"
178
+ ctx = ctxutil .SetOpPath (ctx , opPath )
179
+
180
+ reqPath := fmt .Sprintf (opPath , opts .values ().Encode ())
169
181
170
- var body schema.ActionListResponse
171
- resp , err := c .client .Do (req , & body )
182
+ respBody , resp , err := getRequest [schema.ActionListResponse ](ctx , c .client , reqPath )
172
183
if err != nil {
173
- return nil , nil , err
174
- }
175
- actions := make ([]* Action , 0 , len (body .Actions ))
176
- for _ , i := range body .Actions {
177
- actions = append (actions , ActionFromSchema (i ))
184
+ return nil , resp , err
178
185
}
179
- return actions , resp , nil
186
+
187
+ return allFromSchemaFunc (respBody .Actions , ActionFromSchema ), resp , nil
180
188
}
181
189
182
190
// All returns all actions for the given options.
183
191
func (c * ResourceActionClient ) All (ctx context.Context , opts ActionListOpts ) ([]* Action , error ) {
184
- allActions := []* Action {}
185
-
186
- err := c .client .all (func (page int ) (* Response , error ) {
192
+ return iterPages (func (page int ) ([]* Action , * Response , error ) {
187
193
opts .Page = page
188
- actions , resp , err := c .List (ctx , opts )
189
- if err != nil {
190
- return resp , err
191
- }
192
- allActions = append (allActions , actions ... )
193
- return resp , nil
194
+ return c .List (ctx , opts )
194
195
})
195
- if err != nil {
196
- return nil , err
197
- }
198
-
199
- return allActions , nil
200
196
}
0 commit comments