aboutsummaryrefslogtreecommitdiff
path: root/container.go
blob: accae8ee868980ca5cab36502210f9eebf755031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/******************************************************************************
*
*  Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
*
*  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

import (
	"net/http"
)

//Container represents a Swift container.
type Container struct {
	a    *Account
	name string
	//cache
	headers *ContainerHeaders
}

//Container returns a handle to the container with the given name within this
//account. This function does not issue any HTTP requests, and therefore cannot
//ensure that the container exists. Use the Exists() function to check for the
//container's existence, or chain this function with the EnsureExists()
//function like so:
//
//    container, err := account.Container("documents").EnsureExists()
func (a *Account) Container(name string) *Container {
	return &Container{a: a, name: name}
}

//Account returns a handle to the account this container is stored in.
func (c *Container) Account() *Account {
	return c.a
}

//Name returns the container name.
func (c *Container) Name() string {
	return c.name
}

//Exists checks if this container exists, potentially by issuing a HEAD request
//if no Headers() have been cached yet.
func (c *Container) Exists() (bool, error) {
	_, err := c.Headers()
	if Is(err, http.StatusNotFound) {
		return false, nil
	} else if err != nil {
		return false, err
	}
	return true, nil
}

//Headers returns the ContainerHeaders for this account. If the ContainerHeaders
//has not been cached yet, a HEAD request is issued on the account.
func (c *Container) Headers() (ContainerHeaders, error) {
	if c.headers != nil {
		return *c.headers, nil
	}

	resp, err := Request{
		Method:            "HEAD",
		ContainerName:     c.name,
		ExpectStatusCodes: []int{204},
	}.Do(c.a.client)
	if err != nil {
		return ContainerHeaders{}, err
	}

	headers := ContainerHeaders(headersFromHTTP(resp.Header))
	err = headers.Validate()
	if err != nil {
		return headers, err
	}
	c.headers = &headers
	return *c.headers, nil
}

//Update updates the container using a POST request. To add URL parameters, pass
//a non-nil *RequestOptions.
//
//If you are not sure whether the container exists, use Create() instead.
//
//A successful POST request implies Invalidate() since it may change metadata.
func (c *Container) Update(headers ContainerHeaders, opts *RequestOptions) error {
	_, err := Request{
		Method:            "POST",
		ContainerName:     c.name,
		Headers:           headersToHTTP(headers),
		Options:           opts,
		ExpectStatusCodes: []int{204},
	}.Do(c.a.client)
	if err == nil {
		c.Invalidate()
	}
	return err
}

//Create creates the container using a PUT request. To add URL parameters, pass
//a non-nil *RequestOptions.
//
//This function can be used regardless of whether the container exists or not.
//
//A successful PUT request implies Invalidate() since it may change metadata.
func (c *Container) Create(headers ContainerHeaders, opts *RequestOptions) error {
	_, err := Request{
		Method:            "PUT",
		ContainerName:     c.name,
		Headers:           headersToHTTP(headers),
		Options:           opts,
		ExpectStatusCodes: []int{201, 202},
	}.Do(c.a.client)
	if err == nil {
		c.Invalidate()
	}
	return err
}

//Delete deletes the container using a DELETE request. To add URL parameters,
//pass a non-nil *RequestOptions.
//
//This operation fails with http.StatusConflict if the container is not empty.
//
//This operation fails with http.StatusNotFound if the container does not exist.
//
//A successful DELETE request implies Invalidate().
func (c *Container) Delete(headers ContainerHeaders, opts *RequestOptions) error {
	_, err := Request{
		Method:            "DELETE",
		ContainerName:     c.name,
		Headers:           headersToHTTP(headers),
		Options:           opts,
		ExpectStatusCodes: []int{204},
	}.Do(c.a.client)
	if err == nil {
		c.Invalidate()
	}
	return err
}

//Invalidate clears the internal cache of this Container instance. The next call
//to Headers() on this instance will issue a HEAD request on the account.
func (c *Container) Invalidate() {
	c.headers = nil
}

//EnsureExists issues a PUT request on this container.
//If the container does not exist yet, it will be created by this call.
//If the container exists already, this call does not change it.
//This function returns the same container again, because its intended use is
//with freshly constructed Container instances like so:
//
//    container, err := account.Container("documents").EnsureExists()
func (c *Container) EnsureExists() (*Container, error) {
	_, err := Request{
		Method:            "PUT",
		ContainerName:     c.name,
		ExpectStatusCodes: []int{201, 202},
	}.Do(c.a.client)
	return c, err
}

// TODO object listing