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	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
13	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
14	"net/http"
15	"net/url"
16	"strings"
17)
18
19// TableClient contains the methods for the Table group.
20// Don't use this type directly, use NewTableClient() instead.
21type TableClient struct {
22	con            *armcore.Connection
23	subscriptionID string
24}
25
26// NewTableClient creates a new instance of TableClient with the specified values.
27func NewTableClient(con *armcore.Connection, subscriptionID string) *TableClient {
28	return &TableClient{con: con, subscriptionID: subscriptionID}
29}
30
31// Create - Creates a new table with the specified table name, under the specified account.
32func (client *TableClient) Create(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableCreateOptions) (TableResponse, error) {
33	req, err := client.createCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
34	if err != nil {
35		return TableResponse{}, err
36	}
37	resp, err := client.con.Pipeline().Do(req)
38	if err != nil {
39		return TableResponse{}, err
40	}
41	if !resp.HasStatusCode(http.StatusOK) {
42		return TableResponse{}, client.createHandleError(resp)
43	}
44	return client.createHandleResponse(resp)
45}
46
47// createCreateRequest creates the Create request.
48func (client *TableClient) createCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableCreateOptions) (*azcore.Request, error) {
49	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
50	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
51	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
52	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
53	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
54	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
55	if err != nil {
56		return nil, err
57	}
58	req.Telemetry(telemetryInfo)
59	query := req.URL.Query()
60	query.Set("api-version", "2019-06-01")
61	req.URL.RawQuery = query.Encode()
62	req.Header.Set("Accept", "application/json")
63	return req, nil
64}
65
66// createHandleResponse handles the Create response.
67func (client *TableClient) createHandleResponse(resp *azcore.Response) (TableResponse, error) {
68	var val *Table
69	if err := resp.UnmarshalAsJSON(&val); err != nil {
70		return TableResponse{}, err
71	}
72	return TableResponse{RawResponse: resp.Response, Table: val}, nil
73}
74
75// createHandleError handles the Create error response.
76func (client *TableClient) createHandleError(resp *azcore.Response) error {
77	var err CloudError
78	if err := resp.UnmarshalAsJSON(&err); err != nil {
79		return err
80	}
81	return azcore.NewResponseError(&err, resp.Response)
82}
83
84// Delete - Deletes the table with the specified table name, under the specified account if it exists.
85func (client *TableClient) Delete(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableDeleteOptions) (*http.Response, error) {
86	req, err := client.deleteCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
87	if err != nil {
88		return nil, err
89	}
90	resp, err := client.con.Pipeline().Do(req)
91	if err != nil {
92		return nil, err
93	}
94	if !resp.HasStatusCode(http.StatusNoContent) {
95		return nil, client.deleteHandleError(resp)
96	}
97	return resp.Response, nil
98}
99
100// deleteCreateRequest creates the Delete request.
101func (client *TableClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableDeleteOptions) (*azcore.Request, error) {
102	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
103	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
104	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
105	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
106	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
107	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
108	if err != nil {
109		return nil, err
110	}
111	req.Telemetry(telemetryInfo)
112	query := req.URL.Query()
113	query.Set("api-version", "2019-06-01")
114	req.URL.RawQuery = query.Encode()
115	req.Header.Set("Accept", "application/json")
116	return req, nil
117}
118
119// deleteHandleError handles the Delete error response.
120func (client *TableClient) deleteHandleError(resp *azcore.Response) error {
121	var err CloudError
122	if err := resp.UnmarshalAsJSON(&err); err != nil {
123		return err
124	}
125	return azcore.NewResponseError(&err, resp.Response)
126}
127
128// Get - Gets the table with the specified table name, under the specified account if it exists.
129func (client *TableClient) Get(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableGetOptions) (TableResponse, error) {
130	req, err := client.getCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
131	if err != nil {
132		return TableResponse{}, err
133	}
134	resp, err := client.con.Pipeline().Do(req)
135	if err != nil {
136		return TableResponse{}, err
137	}
138	if !resp.HasStatusCode(http.StatusOK) {
139		return TableResponse{}, client.getHandleError(resp)
140	}
141	return client.getHandleResponse(resp)
142}
143
144// getCreateRequest creates the Get request.
145func (client *TableClient) getCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableGetOptions) (*azcore.Request, error) {
146	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
147	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
148	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
149	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
150	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
151	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
152	if err != nil {
153		return nil, err
154	}
155	req.Telemetry(telemetryInfo)
156	query := req.URL.Query()
157	query.Set("api-version", "2019-06-01")
158	req.URL.RawQuery = query.Encode()
159	req.Header.Set("Accept", "application/json")
160	return req, nil
161}
162
163// getHandleResponse handles the Get response.
164func (client *TableClient) getHandleResponse(resp *azcore.Response) (TableResponse, error) {
165	var val *Table
166	if err := resp.UnmarshalAsJSON(&val); err != nil {
167		return TableResponse{}, err
168	}
169	return TableResponse{RawResponse: resp.Response, Table: val}, nil
170}
171
172// getHandleError handles the Get error response.
173func (client *TableClient) getHandleError(resp *azcore.Response) error {
174	var err CloudError
175	if err := resp.UnmarshalAsJSON(&err); err != nil {
176		return err
177	}
178	return azcore.NewResponseError(&err, resp.Response)
179}
180
181// List - Gets a list of all the tables under the specified storage account
182func (client *TableClient) List(resourceGroupName string, accountName string, options *TableListOptions) ListTableResourcePager {
183	return &listTableResourcePager{
184		pipeline: client.con.Pipeline(),
185		requester: func(ctx context.Context) (*azcore.Request, error) {
186			return client.listCreateRequest(ctx, resourceGroupName, accountName, options)
187		},
188		responder: client.listHandleResponse,
189		errorer:   client.listHandleError,
190		advancer: func(ctx context.Context, resp ListTableResourceResponse) (*azcore.Request, error) {
191			return azcore.NewRequest(ctx, http.MethodGet, *resp.ListTableResource.NextLink)
192		},
193		statusCodes: []int{http.StatusOK},
194	}
195}
196
197// listCreateRequest creates the List request.
198func (client *TableClient) listCreateRequest(ctx context.Context, resourceGroupName string, accountName string, options *TableListOptions) (*azcore.Request, error) {
199	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables"
200	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
201	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
202	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
203	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
204	if err != nil {
205		return nil, err
206	}
207	req.Telemetry(telemetryInfo)
208	query := req.URL.Query()
209	query.Set("api-version", "2019-06-01")
210	req.URL.RawQuery = query.Encode()
211	req.Header.Set("Accept", "application/json")
212	return req, nil
213}
214
215// listHandleResponse handles the List response.
216func (client *TableClient) listHandleResponse(resp *azcore.Response) (ListTableResourceResponse, error) {
217	var val *ListTableResource
218	if err := resp.UnmarshalAsJSON(&val); err != nil {
219		return ListTableResourceResponse{}, err
220	}
221	return ListTableResourceResponse{RawResponse: resp.Response, ListTableResource: val}, nil
222}
223
224// listHandleError handles the List error response.
225func (client *TableClient) listHandleError(resp *azcore.Response) error {
226	var err CloudError
227	if err := resp.UnmarshalAsJSON(&err); err != nil {
228		return err
229	}
230	return azcore.NewResponseError(&err, resp.Response)
231}
232
233// Update - Creates a new table with the specified table name, under the specified account.
234func (client *TableClient) Update(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableUpdateOptions) (TableResponse, error) {
235	req, err := client.updateCreateRequest(ctx, resourceGroupName, accountName, tableName, options)
236	if err != nil {
237		return TableResponse{}, err
238	}
239	resp, err := client.con.Pipeline().Do(req)
240	if err != nil {
241		return TableResponse{}, err
242	}
243	if !resp.HasStatusCode(http.StatusOK) {
244		return TableResponse{}, client.updateHandleError(resp)
245	}
246	return client.updateHandleResponse(resp)
247}
248
249// updateCreateRequest creates the Update request.
250func (client *TableClient) updateCreateRequest(ctx context.Context, resourceGroupName string, accountName string, tableName string, options *TableUpdateOptions) (*azcore.Request, error) {
251	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/tableServices/default/tables/{tableName}"
252	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
253	urlPath = strings.ReplaceAll(urlPath, "{accountName}", url.PathEscape(accountName))
254	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
255	urlPath = strings.ReplaceAll(urlPath, "{tableName}", url.PathEscape(tableName))
256	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
257	if err != nil {
258		return nil, err
259	}
260	req.Telemetry(telemetryInfo)
261	query := req.URL.Query()
262	query.Set("api-version", "2019-06-01")
263	req.URL.RawQuery = query.Encode()
264	req.Header.Set("Accept", "application/json")
265	return req, nil
266}
267
268// updateHandleResponse handles the Update response.
269func (client *TableClient) updateHandleResponse(resp *azcore.Response) (TableResponse, error) {
270	var val *Table
271	if err := resp.UnmarshalAsJSON(&val); err != nil {
272		return TableResponse{}, err
273	}
274	return TableResponse{RawResponse: resp.Response, Table: val}, nil
275}
276
277// updateHandleError handles the Update error response.
278func (client *TableClient) updateHandleError(resp *azcore.Response) error {
279	var err CloudError
280	if err := resp.UnmarshalAsJSON(&err); err != nil {
281		return err
282	}
283	return azcore.NewResponseError(&err, resp.Response)
284}
285