From f5e33c280f225b04652b6898d586c5d9ec60052e Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Mon, 26 Feb 2018 20:46:52 +0100 Subject: add struct Capabilities, Account.{,Raw}Capabilities --- account.go | 50 +++++++++++++++++++++++++++++++++ capabilities.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 capabilities.go diff --git a/account.go b/account.go index 91d1ce5..1e50707 100644 --- a/account.go +++ b/account.go @@ -19,8 +19,11 @@ package schwift import ( + "encoding/json" "fmt" + "net/http" "regexp" + "strings" ) //Account represents a Swift account. @@ -31,6 +34,7 @@ type Account struct { name string //cache headers *AccountHeaders + caps *Capabilities } var endpointURLRegexp = regexp.MustCompile(`^(.*/)v1/(.*)/$`) @@ -169,3 +173,49 @@ func (a *Account) Create(headers AccountHeaders, opts *RequestOptions) error { func (a *Account) Containers() *ContainerIterator { return &ContainerIterator{Account: a} } + +//Capabilities queries the GET /info endpoint of the Swift server providing +//this account. Capabilities are cached, so the GET request will only be sent +//once during the first call to this method. +func (a *Account) Capabilities() (Capabilities, error) { + if a.caps != nil { + return *a.caps, nil + } + + buf, err := a.RawCapabilities() + if err != nil { + return Capabilities{}, err + } + + var caps Capabilities + err = json.Unmarshal(buf, &caps) + if err != nil { + return caps, err + } + + a.caps = &caps + return caps, nil +} + +//RawCapabilities queries the GET /info endpoint of the Swift server providing +//this account, and returns the response body. Unlike Account.Capabilities, +//this method does not employ any caching. +func (a *Account) RawCapabilities() ([]byte, error) { + //This method is the only one in Schwift that bypasses struct Request since + //the request URL is not below the endpoint URL. + + uri := a.backend.EndpointURL() + uri = strings.TrimPrefix(uri, "/") + uri = strings.TrimPrefix(uri, "/v1") + uri += "/info" + + req, err := http.NewRequest("GET", uri, nil) + if err != nil { + return nil, err + } + resp, err := a.backend.Do(req) + if err != nil { + return nil, err + } + return collectResponseBody(resp) +} diff --git a/capabilities.go b/capabilities.go new file mode 100644 index 0000000..aec9ef1 --- /dev/null +++ b/capabilities.go @@ -0,0 +1,87 @@ +/****************************************************************************** +* +* Copyright 2018 Stefan Majewsky +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +******************************************************************************/ + +package schwift + +//Capabilities describes a subset of the capabilities that Swift can report +//under its /info endpoint. This struct is obtained through the +//Account.Capabilities() method. To query capabilities not represented in this +//struct, see Account.QueryCapabilities(). +// +//All direct members of struct Capabilities, except for "Swift", are pointers. +//If any of these is nil, it indicates that the middleware corresponding to +//that field is not supported on this server. +type Capabilities struct { + BulkDelete *struct { + MaximumDeletesPerRequest uint `json:"max_deletes_per_request"` + MaximumFailedDeletes uint `json:"max_failed_deletes"` + } `json:"bulk_delete"` + BulkUpload *struct { + MaximumContainersPerExtraction uint `json:"max_containers_per_extraction"` + MaximumFailedExtractions uint `json:"max_failed_extractions"` + } `json:"bulk_upload"` + StaticLargeObject *struct { + MaximumManifestSegments uint `json:"max_manifest_segments"` + MaximumManifestSize uint `json:"max_manifest_size"` + MinimumSegmentSize uint `json:"min_segment_size"` + } `json:"slo"` + Swift struct { + AccountAutocreate bool `json:"account_autocreate"` + AccountListingLimit uint `json:"account_listing_limit"` + AllowAccountManagement bool `json:"allow_account_management"` + ContainerListingLimit uint `json:"container_listing_limit"` + ExtraHeaderCount uint `json:"extra_header_count"` + MaximumAccountNameLength uint `json:"max_account_name_length"` + MaximumContainerNameLength uint `json:"max_container_name_length"` + MaximumFileSize uint `json:"max_file_size"` + MaximumHeaderSize uint `json:"max_header_size"` + MaximumMetaCount uint `json:"max_meta_count"` + MaximumMetaNameLength uint `json:"max_meta_name_length"` + MaximumMetaOverallSize uint `json:"max_meta_overall_size"` + MaximumMetaValueLength uint `json:"max_meta_value_length"` + MaximumObjectNameLength uint `json:"max_object_name_length"` + Policies []StoragePolicySpec `json:"policies"` + StrictCORSMode bool `json:"strict_cors_mode"` + Version string `json:"version"` + } `json:"swift"` + Swift3 *struct { + AllowMultipartUploads bool `json:"allow_multipart_uploads"` + MaximumBucketListing uint `json:"max_bucket_listing"` + MaximumMultiDeleteObjects uint `json:"max_multi_delete_objects"` + MaximumPartsListing uint `json:"max_parts_listing"` + MaximumUploadPartNumber uint `json:"max_upload_part_num"` + Version string `json:"version"` + } `json:"swift3"` + TempAuth *struct { + AccountACLs bool `json:"account_acls"` + } `json:"tempauth"` + TempURL *struct { + IncomingAllowHeaders []string `json:"incoming_allow_headers"` + IncomingRemoveHeaders []string `json:"incoming_remove_headers"` + Methods []string `json:"methods"` + OutgoingAllowHeaders []string `json:"outgoing_allow_headers"` + OutgoingRemoveHeaders []string `json:"outgoing_remove_headers"` + } `json:"tempurl"` +} + +//StoragePolicySpec is a subtype that appears in struct Capabilities. +type StoragePolicySpec struct { + Name string `json:"name"` + Aliases string `json:"aliases"` + Default bool `json:"default"` +} -- cgit v1.2.3