1// +build go1.13
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armstorage
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strings"
19)
20
21// TableClient contains the methods for the Table group.
22// Don't use this type directly, use NewTableClient() instead.
23type TableClient struct {
24	con            *armcore.Connection
25	subscriptionID string
26}
27
28// NewTableClient creates a new instance of TableClient with the specified values.
29func NewTableClient(con *armcore.Connection, subscriptionID string) *TableClient {
30	return &TableClient{con: con, subscriptionID: subscriptionID}
31}
32
33// Create - Creates a new table with the specified table name, under the specified account.
34// If the operation fails it returns the *CloudErrorAutoGenerated error type.
35func (client *TableClient) Create(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableCreateOptions) (TableResponse, error) {
36	req, err := client.createCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
37	if err != nil {
38		return TableResponse{}, err
39	}
40	resp, err := client.con.Pipeline().Do(req)
41	if err != nil {
42		return TableResponse{}, err
43	}
44	if !resp.HasStatusCode(http.StatusOK) {
45		return TableResponse{}, client.createHandleError(resp)
46	}
47	return client.createHandleResponse(resp)
48}
49
50// createCreateRequest creates the Create request.
51func (client *TableClient) createCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableCreateOptions) (*azcore.Request, error) {
52	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
53	if resourceGroupName == "" {
54		return nil, errors.New("parameter resourceGroupName cannot be empty")
55	}
56	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
57	if accountName == "" {
58		return nil, errors.New("parameter accountName cannot be empty")
59	}
60	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
61	if client.subscriptionID == "" {
62		return nil, errors.New("parameter client.subscriptionID cannot be empty")
63	}
64	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
65	if tableName == "" {
66		return nil, errors.New("parameter tableName cannot be empty")
67	}
68	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
69	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
70	if err != nil {
71		return nil, err
72	}
73	req.Telemetry(telemetryInfo)
74	reqQP := req.URL.Query()
75	reqQP.Set("api-version", "2021-04-01")
76	req.URL.RawQuery = reqQP.Encode()
77	req.Header.Set("Accept", "application/json")
78	return req, nil
79}
80
81// createHandleResponse handles the Create response.
82func (client *TableClient) createHandleResponse(resp *azcore.Response) (TableResponse, error) {
83	var val *Table
84	if err := resp.UnmarshalAsJSON(&val); err != nil {
85		return TableResponse{}, err
86	}
87	return TableResponse{RawResponse: resp.Response, Table: val}, nil
88}
89
90// createHandleError handles the Create error response.
91func (client *TableClient) createHandleError(resp *azcore.Response) error {
92	body, err := resp.Payload()
93	if err != nil {
94		return azcore.NewResponseError(err, resp.Response)
95	}
96	errType := CloudErrorAutoGenerated{raw: string(body)}
97	if err := resp.UnmarshalAsJSON(&errType); err != nil {
98		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
99	}
100	return azcore.NewResponseError(&errType, resp.Response)
101}
102
103// Delete - Deletes the table with the specified table name, under the specified account if it exists.
104// If the operation fails it returns the *CloudErrorAutoGenerated error type.
105func (client *TableClient) Delete(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableDeleteOptions) (*http.Response, error) {
106	req, err := client.deleteCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
107	if err != nil {
108		return nil, err
109	}
110	resp, err := client.con.Pipeline().Do(req)
111	if err != nil {
112		return nil, err
113	}
114	if !resp.HasStatusCode(http.StatusNoContent) {
115		return nil, client.deleteHandleError(resp)
116	}
117	return resp.Response, nil
118}
119
120// deleteCreateRequest creates the Delete request.
121func (client *TableClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableDeleteOptions) (*azcore.Request, error) {
122	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
123	if resourceGroupName == "" {
124		return nil, errors.New("parameter resourceGroupName cannot be empty")
125	}
126	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
127	if accountName == "" {
128		return nil, errors.New("parameter accountName cannot be empty")
129	}
130	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
131	if client.subscriptionID == "" {
132		return nil, errors.New("parameter client.subscriptionID cannot be empty")
133	}
134	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
135	if tableName == "" {
136		return nil, errors.New("parameter tableName cannot be empty")
137	}
138	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
139	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
140	if err != nil {
141		return nil, err
142	}
143	req.Telemetry(telemetryInfo)
144	reqQP := req.URL.Query()
145	reqQP.Set("api-version", "2021-04-01")
146	req.URL.RawQuery = reqQP.Encode()
147	req.Header.Set("Accept", "application/json")
148	return req, nil
149}
150
151// deleteHandleError handles the Delete error response.
152func (client *TableClient) deleteHandleError(resp *azcore.Response) error {
153	body, err := resp.Payload()
154	if err != nil {
155		return azcore.NewResponseError(err, resp.Response)
156	}
157	errType := CloudErrorAutoGenerated{raw: string(body)}
158	if err := resp.UnmarshalAsJSON(&errType); err != nil {
159		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
160	}
161	return azcore.NewResponseError(&errType, resp.Response)
162}
163
164// Get - Gets the table with the specified table name, under the specified account if it exists.
165// If the operation fails it returns the *CloudErrorAutoGenerated error type.
166func (client *TableClient) Get(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableGetOptions) (TableResponse, error) {
167	req, err := client.getCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
168	if err != nil {
169		return TableResponse{}, err
170	}
171	resp, err := client.con.Pipeline().Do(req)
172	if err != nil {
173		return TableResponse{}, err
174	}
175	if !resp.HasStatusCode(http.StatusOK) {
176		return TableResponse{}, client.getHandleError(resp)
177	}
178	return client.getHandleResponse(resp)
179}
180
181// getCreateRequest creates the Get request.
182func (client *TableClient) getCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableGetOptions) (*azcore.Request, error) {
183	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
184	if resourceGroupName == "" {
185		return nil, errors.New("parameter resourceGroupName cannot be empty")
186	}
187	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
188	if accountName == "" {
189		return nil, errors.New("parameter accountName cannot be empty")
190	}
191	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
192	if client.subscriptionID == "" {
193		return nil, errors.New("parameter client.subscriptionID cannot be empty")
194	}
195	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
196	if tableName == "" {
197		return nil, errors.New("parameter tableName cannot be empty")
198	}
199	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
200	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
201	if err != nil {
202		return nil, err
203	}
204	req.Telemetry(telemetryInfo)
205	reqQP := req.URL.Query()
206	reqQP.Set("api-version", "2021-04-01")
207	req.URL.RawQuery = reqQP.Encode()
208	req.Header.Set("Accept", "application/json")
209	return req, nil
210}
211
212// getHandleResponse handles the Get response.
213func (client *TableClient) getHandleResponse(resp *azcore.Response) (TableResponse, error) {
214	var val *Table
215	if err := resp.UnmarshalAsJSON(&val); err != nil {
216		return TableResponse{}, err
217	}
218	return TableResponse{RawResponse: resp.Response, Table: val}, nil
219}
220
221// getHandleError handles the Get error response.
222func (client *TableClient) getHandleError(resp *azcore.Response) error {
223	body, err := resp.Payload()
224	if err != nil {
225		return azcore.NewResponseError(err, resp.Response)
226	}
227	errType := CloudErrorAutoGenerated{raw: string(body)}
228	if err := resp.UnmarshalAsJSON(&errType); err != nil {
229		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
230	}
231	return azcore.NewResponseError(&errType, resp.Response)
232}
233
234// List - Gets a list of all the tables under the specified storage account
235// If the operation fails it returns the *CloudErrorAutoGenerated error type.
236func (client *TableClient) List(resourceGroupName string, accountName string, options *TableListOptions) ListTableResourcePager {
237	return &listTableResourcePager{
238		pipeline: client.con.Pipeline(),
239		requester: func(ctx context.Context) (*azcore.Request, error) {
240			return client.listCreateRequest(ctx, resourceGroupName, accountName, options)
241		},
242		responder: client.listHandleResponse,
243		errorer:   client.listHandleError,
244		advancer: func(ctx context.Context, resp ListTableResourceResponse) (*azcore.Request, error) {
245			return azcore.NewRequest(ctx, http.MethodGet, *resp.ListTableResource.NextLink)
246		},
247		statusCodes: []int{http.StatusOK},
248	}
249}
250
251// listCreateRequest creates the List request.
252func (client *TableClient) listCreateRequest(ctx context.Context, resourceGroupName string, accountName string, options *TableListOptions) (*azcore.Request, error) {
253	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables"
254	if resourceGroupName == "" {
255		return nil, errors.New("parameter resourceGroupName cannot be empty")
256	}
257	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
258	if accountName == "" {
259		return nil, errors.New("parameter accountName cannot be empty")
260	}
261	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
262	if client.subscriptionID == "" {
263		return nil, errors.New("parameter client.subscriptionID cannot be empty")
264	}
265	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
266	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
267	if err != nil {
268		return nil, err
269	}
270	req.Telemetry(telemetryInfo)
271	reqQP := req.URL.Query()
272	reqQP.Set("api-version", "2021-04-01")
273	req.URL.RawQuery = reqQP.Encode()
274	req.Header.Set("Accept", "application/json")
275	return req, nil
276}
277
278// listHandleResponse handles the List response.
279func (client *TableClient) listHandleResponse(resp *azcore.Response) (ListTableResourceResponse, error) {
280	var val *ListTableResource
281	if err := resp.UnmarshalAsJSON(&val); err != nil {
282		return ListTableResourceResponse{}, err
283	}
284	return ListTableResourceResponse{RawResponse: resp.Response, ListTableResource: val}, nil
285}
286
287// listHandleError handles the List error response.
288func (client *TableClient) listHandleError(resp *azcore.Response) error {
289	body, err := resp.Payload()
290	if err != nil {
291		return azcore.NewResponseError(err, resp.Response)
292	}
293	errType := CloudErrorAutoGenerated{raw: string(body)}
294	if err := resp.UnmarshalAsJSON(&errType); err != nil {
295		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
296	}
297	return azcore.NewResponseError(&errType, resp.Response)
298}
299
300// Update - Creates a new table with the specified table name, under the specified account.
301// If the operation fails it returns the *CloudErrorAutoGenerated error type.
302func (client *TableClient) Update(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableUpdateOptions) (TableResponse, error) {
303	req, err := client.updateCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
304	if err != nil {
305		return TableResponse{}, err
306	}
307	resp, err := client.con.Pipeline().Do(req)
308	if err != nil {
309		return TableResponse{}, err
310	}
311	if !resp.HasStatusCode(http.StatusOK) {
312		return TableResponse{}, client.updateHandleError(resp)
313	}
314	return client.updateHandleResponse(resp)
315}
316
317// updateCreateRequest creates the Update request.
318func (client *TableClient) updateCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableUpdateOptions) (*azcore.Request, error) {
319	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
320	if resourceGroupName == "" {
321		return nil, errors.New("parameter resourceGroupName cannot be empty")
322	}
323	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
324	if accountName == "" {
325		return nil, errors.New("parameter accountName cannot be empty")
326	}
327	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
328	if client.subscriptionID == "" {
329		return nil, errors.New("parameter client.subscriptionID cannot be empty")
330	}
331	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
332	if tableName == "" {
333		return nil, errors.New("parameter tableName cannot be empty")
334	}
335	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
336	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
337	if err != nil {
338		return nil, err
339	}
340	req.Telemetry(telemetryInfo)
341	reqQP := req.URL.Query()
342	reqQP.Set("api-version", "2021-04-01")
343	req.URL.RawQuery = reqQP.Encode()
344	req.Header.Set("Accept", "application/json")
345	return req, nil
346}
347
348// updateHandleResponse handles the Update response.
349func (client *TableClient) updateHandleResponse(resp *azcore.Response) (TableResponse, error) {
350	var val *Table
351	if err := resp.UnmarshalAsJSON(&val); err != nil {
352		return TableResponse{}, err
353	}
354	return TableResponse{RawResponse: resp.Response, Table: val}, nil
355}
356
357// updateHandleError handles the Update error response.
358func (client *TableClient) updateHandleError(resp *azcore.Response) error {
359	body, err := resp.Payload()
360	if err != nil {
361		return azcore.NewResponseError(err, resp.Response)
362	}
363	errType := CloudErrorAutoGenerated{raw: string(body)}
364	if err := resp.UnmarshalAsJSON(&errType); err != nil {
365		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
366	}
367	return azcore.NewResponseError(&errType, resp.Response)
368}
369