Debian Codegraph @

storage/2018-11-09/blob/containers/get_properties.go

raw
1 package containers
2
3 import (
4 "context"
5 "net/http"
6 "strings"
7
8 "github.com/Azure/go-autorest/autorest"
9 "github.com/Azure/go-autorest/autorest/azure"
10 "github.com/Azure/go-autorest/autorest/validation"
11 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
12 "github.com/tombuildsstuff/giovanni/storage/internal/metadata"
13 )
14
15 // GetProperties returns the properties for this Container without a Lease
16 func (client Client) GetProperties(ctx context.Context, accountName, containerName string) (ContainerProperties, error) {
17 // If specified, Get Container Properties only succeeds if the container’s lease is active and matches this ID.
18 // If there is no active lease or the ID does not match, 412 (Precondition Failed) is returned.
19 return client.GetPropertiesWithLeaseID(ctx, accountName, containerName, "")
20 }
21
22 // GetPropertiesWithLeaseID returns the properties for this Container using the specified LeaseID
23 func (client Client) GetPropertiesWithLeaseID(ctx context.Context, accountName, containerName, leaseID string) (result ContainerProperties, err error) {
24 if accountName == "" {
25 return result, validation.NewError("containers.Client", "GetPropertiesWithLeaseID", "`accountName` cannot be an empty string.")
26 }
27 if containerName == "" {
28 return result, validation.NewError("containers.Client", "GetPropertiesWithLeaseID", "`containerName` cannot be an empty string.")
29 }
30
31 req, err := client.GetPropertiesWithLeaseIDPreparer(ctx, accountName, containerName, leaseID)
32 if err != nil {
33 err = autorest.NewErrorWithError(err, "containers.Client", "GetProperties", nil, "Failure preparing request")
34 return
35 }
36
37 resp, err := client.GetPropertiesWithLeaseIDSender(req)
38 if err != nil {
39 result.Response = autorest.Response{Response: resp}
40 err = autorest.NewErrorWithError(err, "containers.Client", "GetProperties", resp, "Failure sending request")
41 return
42 }
43
44 result, err = client.GetPropertiesWithLeaseIDResponder(resp)
45 if err != nil {
46 err = autorest.NewErrorWithError(err, "containers.Client", "GetProperties", resp, "Failure responding to request")
47 return
48 }
49
50 return
51 }
52
53 // GetPropertiesWithLeaseIDPreparer prepares the GetPropertiesWithLeaseID request.
54 func (client Client) GetPropertiesWithLeaseIDPreparer(ctx context.Context, accountName, containerName, leaseID string) (*http.Request, error) {
55 pathParameters := map[string]interface{}{
56 "containerName": autorest.Encode("path", containerName),
57 }
58
59 queryParameters := map[string]interface{}{
60 "restype": autorest.Encode("path", "container"),
61 }
62
63 headers := map[string]interface{}{
64 "x-ms-version": APIVersion,
65 }
66
67 // If specified, Get Container Properties only succeeds if the container’s lease is active and matches this ID.
68 // If there is no active lease or the ID does not match, 412 (Precondition Failed) is returned.
69 if leaseID != "" {
70 headers["x-ms-lease-id"] = leaseID
71 }
72
73 preparer := autorest.CreatePreparer(
74 autorest.AsContentType("application/xml; charset=utf-8"),
75 autorest.AsGet(),
76 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
77 autorest.WithPathParameters("/{containerName}", pathParameters),
78 autorest.WithQueryParameters(queryParameters),
79 autorest.WithHeaders(headers))
80 return preparer.Prepare((&http.Request{}).WithContext(ctx))
81 }
82
83 // GetPropertiesWithLeaseIDSender sends the GetPropertiesWithLeaseID request. The method will close the
84 // http.Response Body if it receives an error.
85 func (client Client) GetPropertiesWithLeaseIDSender(req *http.Request) (*http.Response, error) {
86 return autorest.SendWithSender(client, req,
87 azure.DoRetryWithRegistration(client.Client))
88 }
89
90 // GetPropertiesWithLeaseIDResponder handles the response to the GetPropertiesWithLeaseID request. The method always
91 // closes the http.Response Body.
92 func (client Client) GetPropertiesWithLeaseIDResponder(resp *http.Response) (result ContainerProperties, err error) {
93 if resp != nil {
94 result.LeaseStatus = LeaseStatus(resp.Header.Get("x-ms-lease-status"))
95 result.LeaseState = LeaseState(resp.Header.Get("x-ms-lease-state"))
96 if result.LeaseStatus == Locked {
97 duration := LeaseDuration(resp.Header.Get("x-ms-lease-duration"))
98 result.LeaseDuration = &duration
99 }
100
101 // If this header is not returned in the response, the container is private to the account owner.
102 accessLevel := resp.Header.Get("x-ms-blob-public-access")
103 if accessLevel != "" {
104 result.AccessLevel = AccessLevel(accessLevel)
105 } else {
106 result.AccessLevel = Private
107 }
108
109 // we can't necessarily use strconv.ParseBool here since this could be nil (only in some API versions)
110 result.HasImmutabilityPolicy = strings.EqualFold(resp.Header.Get("x-ms-has-immutability-policy"), "true")
111 result.HasLegalHold = strings.EqualFold(resp.Header.Get("x-ms-has-legal-hold"), "true")
112
113 result.MetaData = metadata.ParseFromHeaders(resp.Header)
114 }
115
116 err = autorest.Respond(
117 resp,
118 client.ByInspecting(),
119 azure.WithErrorUnlessStatusCode(http.StatusOK),
120 autorest.ByClosing())
121 result.Response = autorest.Response{Response: resp}
122
123 return
124 }