@@ -114,6 +114,66 @@ func (a *API) DeleteActiveActive(ctx context.Context, subscription int, regionId
114114 return nil
115115}
116116
117+ func (a * API ) ListInvitations (ctx context.Context , subscription int ) ([]* TransitGatewayInvitation , error ) {
118+ message := fmt .Sprintf ("list TGw invitations for subscription %d" , subscription )
119+ address := fmt .Sprintf ("/subscriptions/%d/transitGateways/invitations" , subscription )
120+ invitations , err := a .listInvitations (ctx , message , address )
121+ if err != nil {
122+ return nil , wrap404Error (subscription , err )
123+ }
124+ return invitations , nil
125+ }
126+
127+ func (a * API ) ListInvitationsActiveActive (ctx context.Context , subscription int , regionId int ) ([]* TransitGatewayInvitation , error ) {
128+ message := fmt .Sprintf ("list TGw invitations for subscription %d in region %d" , subscription , regionId )
129+ address := fmt .Sprintf ("/subscriptions/%d/regions/%d/transitGateways/invitations" , subscription , regionId )
130+ invitations , err := a .listInvitations (ctx , message , address )
131+ if err != nil {
132+ return nil , wrap404ErrorActiveActive (subscription , regionId , err )
133+ }
134+ return invitations , nil
135+ }
136+
137+ func (a * API ) AcceptInvitation (ctx context.Context , subscription int , tgwInvitationId int ) error {
138+ message := fmt .Sprintf ("accept TGw invitation %d for subscription %d" , tgwInvitationId , subscription )
139+ address := fmt .Sprintf ("/subscriptions/%d/transitGateways/invitations/%d/accept" , subscription , tgwInvitationId )
140+ err := a .acceptInvitation (ctx , message , address )
141+ if err != nil {
142+ return wrap404Error (subscription , err )
143+ }
144+ return nil
145+ }
146+
147+ func (a * API ) AcceptInvitationActiveActive (ctx context.Context , subscription int , regionId int , tgwInvitationId int ) error {
148+ message := fmt .Sprintf ("accept TGw invitation %d for subscription %d in region %d" , tgwInvitationId , subscription , regionId )
149+ address := fmt .Sprintf ("/subscriptions/%d/regions/%d/transitGateways/invitations/%d/accept" , subscription , regionId , tgwInvitationId )
150+ err := a .acceptInvitation (ctx , message , address )
151+ if err != nil {
152+ return wrap404ErrorActiveActive (subscription , regionId , err )
153+ }
154+ return nil
155+ }
156+
157+ func (a * API ) RejectInvitation (ctx context.Context , subscription int , tgwInvitationId int ) error {
158+ message := fmt .Sprintf ("reject TGw invitation %d for subscription %d" , tgwInvitationId , subscription )
159+ address := fmt .Sprintf ("/subscriptions/%d/transitGateways/invitations/%d/reject" , subscription , tgwInvitationId )
160+ err := a .rejectInvitation (ctx , message , address )
161+ if err != nil {
162+ return wrap404Error (subscription , err )
163+ }
164+ return nil
165+ }
166+
167+ func (a * API ) RejectInvitationActiveActive (ctx context.Context , subscription int , regionId int , tgwInvitationId int ) error {
168+ message := fmt .Sprintf ("reject TGw invitation %d for subscription %d in region %d" , tgwInvitationId , subscription , regionId )
169+ address := fmt .Sprintf ("/subscriptions/%d/regions/%d/transitGateways/invitations/%d/reject" , subscription , regionId , tgwInvitationId )
170+ err := a .rejectInvitation (ctx , message , address )
171+ if err != nil {
172+ return wrap404ErrorActiveActive (subscription , regionId , err )
173+ }
174+ return nil
175+ }
176+
117177func (a * API ) get (ctx context.Context , message string , address string ) (* GetAttachmentsTask , error ) {
118178 var task internal.TaskResponse
119179 err := a .client .Get (ctx , message , address , & task )
@@ -124,8 +184,11 @@ func (a *API) get(ctx context.Context, message string, address string) (*GetAtta
124184 a .logger .Printf ("Waiting for tgwGetRequest %d to complete" , task .ID )
125185
126186 err = a .taskWaiter .Wait (ctx , * task .ID )
187+ if err != nil {
188+ return nil , err
189+ }
127190
128- a .logger .Printf ("tgwGetRequest %d completed, possibly with error " , task .ID , err )
191+ a .logger .Printf ("tgwGetRequest %d completed" , task .ID )
129192
130193 var getAttachmentsTask * GetAttachmentsTask
131194 err = a .client .Get (ctx ,
@@ -194,6 +257,70 @@ func (a *API) delete(ctx context.Context, message string, address string) error
194257 return nil
195258}
196259
260+ func (a * API ) listInvitations (ctx context.Context , message string , address string ) ([]* TransitGatewayInvitation , error ) {
261+ var task internal.TaskResponse
262+ err := a .client .Get (ctx , message , address , & task )
263+ if err != nil {
264+ return nil , err
265+ }
266+
267+ a .logger .Printf ("Waiting for tgwListInvitationsRequest %d to complete" , task .ID )
268+
269+ err = a .taskWaiter .Wait (ctx , * task .ID )
270+ if err != nil {
271+ return nil , err
272+ }
273+
274+ a .logger .Printf ("tgwListInvitationsRequest %d completed" , task .ID )
275+
276+ var invitationsResponse * InvitationsResponse
277+ err = a .client .Get (ctx ,
278+ fmt .Sprintf ("retrieve completed tgwListInvitationsRequest task %d" , task .ID ),
279+ "/tasks/" + * task .ID ,
280+ & invitationsResponse ,
281+ )
282+
283+ if err != nil {
284+ return nil , fmt .Errorf ("failed to retrieve completed tgwListInvitationsRequest %d: %w" , task .ID , err )
285+ }
286+
287+ return invitationsResponse .Response .Resource .Resources , nil
288+ }
289+
290+ func (a * API ) acceptInvitation (ctx context.Context , message string , address string ) error {
291+ var task internal.TaskResponse
292+ err := a .client .Put (ctx , message , address , nil , & task )
293+ if err != nil {
294+ return err
295+ }
296+
297+ a .logger .Printf ("Waiting for task %s to finish accepting the TGw invitation" , task )
298+
299+ err = a .taskWaiter .Wait (ctx , * task .ID )
300+ if err != nil {
301+ return fmt .Errorf ("failed when accepting TGw invitation %w" , err )
302+ }
303+
304+ return nil
305+ }
306+
307+ func (a * API ) rejectInvitation (ctx context.Context , message string , address string ) error {
308+ var task internal.TaskResponse
309+ err := a .client .Put (ctx , message , address , nil , & task )
310+ if err != nil {
311+ return err
312+ }
313+
314+ a .logger .Printf ("Waiting for task %s to finish rejecting the TGw invitation" , task )
315+
316+ err = a .taskWaiter .Wait (ctx , * task .ID )
317+ if err != nil {
318+ return fmt .Errorf ("failed when rejecting TGw invitation %w" , err )
319+ }
320+
321+ return nil
322+ }
323+
197324func wrap404Error (subId int , err error ) error {
198325 if v , ok := err .(* internal.HTTPError ); ok && v .StatusCode == http .StatusNotFound {
199326 return & NotFound {subId : subId }
0 commit comments