diff --git a/context/context.go b/context/context.go index c471f01e..19f3b503 100644 --- a/context/context.go +++ b/context/context.go @@ -272,7 +272,7 @@ func (context *AptlyContext) GetPublishedStorage(name string) aptly.PublishedSto var err error publishedStorage, err = swift.NewPublishedStorage(params.UserName, params.Password, - params.AuthUrl, params.Tenant, params.TenantId, params.Container, params.Prefix) + params.AuthURL, params.Tenant, params.TenantID, params.Container, params.Prefix) if err != nil { Fatal(err) } diff --git a/swift/public.go b/swift/public.go index 1614fa17..76d1fd29 100644 --- a/swift/public.go +++ b/swift/public.go @@ -14,13 +14,13 @@ import ( // PublishedStorage abstract file system with published files (actually hosted on Swift) type PublishedStorage struct { - conn swift.Connection - container string - prefix string - support_bulk_delete bool + conn swift.Connection + container string + prefix string + supportBulkDelete bool } -type SwiftInfo map[string]interface{} +type swiftInfo map[string]interface{} // Check interface var ( @@ -29,7 +29,7 @@ var ( // NewPublishedStorage creates new instance of PublishedStorage with specified Swift access // keys, tenant and tenantId -func NewPublishedStorage(username string, password string, authUrl string, tenant string, tenantId string, container string, prefix string) (*PublishedStorage, error) { +func NewPublishedStorage(username string, password string, authURL string, tenant string, tenantID string, container string, prefix string) (*PublishedStorage, error) { if username == "" { if username = os.Getenv("OS_USERNAME"); username == "" { username = os.Getenv("ST_USER") @@ -40,49 +40,49 @@ func NewPublishedStorage(username string, password string, authUrl string, tenan password = os.Getenv("ST_KEY") } } - if authUrl == "" { - if authUrl = os.Getenv("OS_AUTH_URL"); authUrl == "" { - authUrl = os.Getenv("ST_AUTH") + if authURL == "" { + if authURL = os.Getenv("OS_AUTH_URL"); authURL == "" { + authURL = os.Getenv("ST_AUTH") } } if tenant == "" { tenant = os.Getenv("OS_TENANT_NAME") } - if tenantId == "" { - tenantId = os.Getenv("OS_TENANT_ID") + if tenantID == "" { + tenantID = os.Getenv("OS_TENANT_ID") } ct := swift.Connection{ UserName: username, ApiKey: password, - AuthUrl: authUrl, + AuthUrl: authURL, UserAgent: "aptly/" + aptly.Version, Tenant: tenant, - TenantId: tenantId, + TenantId: tenantID, ConnectTimeout: 60 * time.Second, Timeout: 60 * time.Second, } err := ct.Authenticate() if err != nil { - return nil, fmt.Errorf("Swift authentication failed: %s", err) + return nil, fmt.Errorf("swift authentication failed: %s", err) } - var bulk_delete bool + var bulkDelete bool resp, err := http.Get(filepath.Join(ct.StorageUrl, "..", "..") + "/info") if err == nil { defer resp.Body.Close() decoder := json.NewDecoder(resp.Body) - var infos SwiftInfo + var infos swiftInfo if decoder.Decode(&infos) == nil { - _, bulk_delete = infos["bulk_delete"] + _, bulkDelete = infos["bulk_delete"] } } result := &PublishedStorage{ - conn: ct, - container: container, - prefix: prefix, - support_bulk_delete: bulk_delete, + conn: ct, + container: container, + prefix: prefix, + supportBulkDelete: bulkDelete, } return result, nil @@ -135,23 +135,25 @@ func (storage *PublishedStorage) RemoveDirs(path string, progress aptly.Progress opts := swift.ObjectsOpts{ Prefix: path, } - if objects, err := storage.conn.ObjectNamesAll(storage.container, &opts); err != nil { - return fmt.Errorf("error removing dir %s from %s: %s", path, storage, err) - } else { - for index, name := range objects { - objects[index] = name[len(storage.prefix):] - } - var multi_delete bool = true - if storage.support_bulk_delete { - _, err := storage.conn.BulkDelete(storage.container, objects) - multi_delete = err != nil - } - if multi_delete { - for _, name := range objects { - if err := storage.conn.ObjectDelete(storage.container, name); err != nil { - return err - } + objects, err := storage.conn.ObjectNamesAll(storage.container, &opts) + if err != nil { + return fmt.Errorf("error removing dir %s from %s: %s", path, storage, err) + } + + for index, name := range objects { + objects[index] = name[len(storage.prefix):] + } + + multiDelete := true + if storage.supportBulkDelete { + _, err := storage.conn.BulkDelete(storage.container, objects) + multiDelete = err != nil + } + if multiDelete { + for _, name := range objects { + if err := storage.conn.ObjectDelete(storage.container, name); err != nil { + return err } } } diff --git a/swift/public_test.go b/swift/public_test.go index c38e546f..c32175ec 100644 --- a/swift/public_test.go +++ b/swift/public_test.go @@ -1,22 +1,18 @@ package swift import ( - "github.com/smira/aptly/files" "github.com/ncw/swift/swifttest" + "github.com/smira/aptly/files" + . "gopkg.in/check.v1" "io/ioutil" "os" "path/filepath" - . "gopkg.in/check.v1" ) const ( - TEST_ADDRESS = "localhost:5324" - AUTH_URL = "http://" + TEST_ADDRESS + "/v1.0" - PROXY_URL = "http://" + TEST_ADDRESS + "/proxy" - USERNAME = "test" - APIKEY = "apikey" - AUTH_TOKEN = "token" + TestAddress = "localhost:5324" + AuthURL = "http://" + TestAddress + "/v1.0" ) type PublishedStorageSuite struct { @@ -28,14 +24,14 @@ var _ = Suite(&PublishedStorageSuite{}) func (s *PublishedStorageSuite) SetUpTest(c *C) { var err error - s.srv, err = swifttest.NewSwiftServer(TEST_ADDRESS) + s.srv, err = swifttest.NewSwiftServer(TestAddress) c.Assert(err, IsNil) c.Assert(s.srv, NotNil) - s.storage, err = NewPublishedStorage("swifttest", "swifttest", AUTH_URL, "", "", "test", "") + s.storage, err = NewPublishedStorage("swifttest", "swifttest", AuthURL, "", "", "test", "") c.Assert(err, IsNil) - s.prefixedStorage, err = NewPublishedStorage("swifttest", "swifttest", AUTH_URL, "", "", "test", "lala") + s.prefixedStorage, err = NewPublishedStorage("swifttest", "swifttest", AuthURL, "", "", "test", "lala") c.Assert(err, IsNil) s.storage.conn.ContainerCreate("test", nil) @@ -46,7 +42,7 @@ func (s *PublishedStorageSuite) TearDownTest(c *C) { } func (s *PublishedStorageSuite) TestNewPublishedStorage(c *C) { - stor, err := NewPublishedStorage("swifttest", "swifttest", AUTH_URL, "", "", "", "") + stor, err := NewPublishedStorage("swifttest", "swifttest", AuthURL, "", "", "", "") c.Check(stor, NotNil) c.Check(err, IsNil) } diff --git a/utils/config.go b/utils/config.go index 60a85ecc..1eb936dc 100644 --- a/utils/config.go +++ b/utils/config.go @@ -40,13 +40,13 @@ type S3PublishRoot struct { // SwiftPublishRoot describes single OpenStack Swift publishing entry point type SwiftPublishRoot struct { - UserName string `json:"osname"` - Password string `json:"password"` - AuthUrl string `json:"authurl"` - Tenant string `json:"tenant"` - TenantId string `json:"tenantid"` - Prefix string `json:"prefix"` - Container string `json:"container"` + UserName string `json:"osname"` + Password string `json:"password"` + AuthURL string `json:"authurl"` + Tenant string `json:"tenant"` + TenantID string `json:"tenantid"` + Prefix string `json:"prefix"` + Container string `json:"container"` } // Config is configuration for aptly, shared by all modules