Skip to content

Commit

Permalink
remove support for obsolete Limes APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
majewsky committed May 10, 2024
1 parent ab1098e commit 01d47d0
Show file tree
Hide file tree
Showing 12 changed files with 5 additions and 243 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/gophercloud/gophercloud v1.11.0
github.com/gophercloud/utils v0.0.0-20231010081019-80377eca5d56
github.com/sapcc/go-api-declarations v1.11.0
github.com/sapcc/go-api-declarations v1.11.2
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -7,8 +7,8 @@ github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/C
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/sapcc/go-api-declarations v1.11.0 h1:k/U/Hdv2UbBdAyxfnWUyhs8hl74fCrNn8aIWj+k42u4=
github.com/sapcc/go-api-declarations v1.11.0/go.mod h1:83R3hTANhuRXt/pXDby37IJetw8l7DG41s33Tp9NXxI=
github.com/sapcc/go-api-declarations v1.11.2 h1:CFVxgWqrs4WBJhbA0nJNQ/0mBpAxmNFuUZtuEITltFU=
github.com/sapcc/go-api-declarations v1.11.2/go.mod h1:83R3hTANhuRXt/pXDby37IJetw8l7DG41s33Tp9NXxI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
47 changes: 2 additions & 45 deletions resources/v1/domains/requests.go
Expand Up @@ -16,8 +16,6 @@
package domains

import (
"net/http"

"github.com/gophercloud/gophercloud"
"github.com/sapcc/go-api-declarations/limes"
limesresources "github.com/sapcc/go-api-declarations/limes/resources"
Expand Down Expand Up @@ -64,7 +62,7 @@ func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) (r CommonResult) {
url += q
}

resp, err := c.Get(url, &r.Body, &gophercloud.RequestOpts{ //nolint:bodyclose // already closed by gophercloud
resp, err := c.Get(url, &r.Body, &gophercloud.RequestOpts{
MoreHeaders: headers,
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
Expand Down Expand Up @@ -112,50 +110,9 @@ func Get(c *gophercloud.ServiceClient, domainID string, opts GetOptsBuilder) (r
url += q
}

resp, err := c.Get(url, &r.Body, &gophercloud.RequestOpts{ //nolint:bodyclose // already closed by gophercloud
resp, err := c.Get(url, &r.Body, &gophercloud.RequestOpts{
MoreHeaders: headers,
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}

// UpdateOptsBuilder allows extensions to add additional parameters to the Update request.
type UpdateOptsBuilder interface {
ToDomainUpdateMap() (map[string]string, map[string]interface{}, error)
}

// UpdateOpts contains parameters to update a domain.
type UpdateOpts struct {
Services limesresources.QuotaRequest `json:"services"`
}

// ToDomainUpdateMap formats a UpdateOpts into a map of headers and a request body.
func (opts UpdateOpts) ToDomainUpdateMap() (headers map[string]string, requestBody map[string]interface{}, err error) {
h, err := gophercloud.BuildHeaders(opts)
if err != nil {
return nil, nil, err
}

b, err := gophercloud.BuildRequestBody(opts, "domain")
if err != nil {
return nil, nil, err
}

return h, b, nil
}

// Update modifies the attributes of a domain.
func Update(c *gophercloud.ServiceClient, domainID string, opts UpdateOptsBuilder) (r UpdateResult) {
url := updateURL(c, domainID)
h, b, err := opts.ToDomainUpdateMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Put(url, b, nil, &gophercloud.RequestOpts{ //nolint:bodyclose // already closed by gophercloud
OkCodes: []int{http.StatusAccepted},
MoreHeaders: h,
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
6 changes: 0 additions & 6 deletions resources/v1/domains/results.go
Expand Up @@ -25,12 +25,6 @@ type CommonResult struct {
gophercloud.Result
}

// UpdateResult is the result of an Update operation. Call its appropriate
// ExtractErr method to extract the error from the result.
type UpdateResult struct {
gophercloud.ErrResult
}

// ExtractDomains interprets a CommonResult as a slice of Domains.
func (r CommonResult) ExtractDomains() ([]limesresources.DomainReport, error) {
var s struct {
Expand Down
11 changes: 0 additions & 11 deletions resources/v1/domains/testing/fixtures.go
Expand Up @@ -68,14 +68,3 @@ func HandleGetDomainSuccessfully(t *testing.T) {
w.Write(jsonBytes) //nolint:errcheck
})
}

// HandleUpdateDomainSuccessfully creates an HTTP handler at `/v1/domains/:domain_id` on the
// test handler mux that tests domain update.
func HandleUpdateDomainSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/domains/uuid-for-karachi", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.WriteHeader(http.StatusAccepted)
})
}
21 changes: 0 additions & 21 deletions resources/v1/domains/testing/requests_test.go
Expand Up @@ -348,27 +348,6 @@ func TestGetDomainFiltered(t *testing.T) {
th.CheckDeepEquals(t, expected, actual)
}

func TestUpdateDomain(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdateDomainSuccessfully(t)

opts := domains.UpdateOpts{
Services: limesresources.QuotaRequest{
"shared": limesresources.ServiceQuotaRequest{
"things": limesresources.ResourceQuotaRequest{
Value: 99,
Unit: limes.UnitNone,
},
},
},
}

// if update succeeds then a 202 (no error) is returned.
err := domains.Update(fake.ServiceClient(), "uuid-for-karachi", opts).ExtractErr()
th.AssertNoErr(t, err)
}

func p2time(timestamp int64) *limes.UnixEncodedTime {
t := limes.UnixEncodedTime{Time: time.Unix(timestamp, 0).UTC()}
return &t
Expand Down
4 changes: 0 additions & 4 deletions resources/v1/domains/urls.go
Expand Up @@ -23,7 +23,3 @@ func listURL(client *gophercloud.ServiceClient) string {
func getURL(client *gophercloud.ServiceClient, domainID string) string {
return client.ServiceURL("domains", domainID)
}

func updateURL(client *gophercloud.ServiceClient, domainID string) string {
return client.ServiceURL("domains", domainID)
}
49 changes: 0 additions & 49 deletions resources/v1/projects/requests.go
Expand Up @@ -15,7 +15,6 @@
package projects

import (
"io"
"net/http"

"github.com/gophercloud/gophercloud"
Expand Down Expand Up @@ -121,54 +120,6 @@ func Get(c *gophercloud.ServiceClient, domainID, projectID string, opts GetOptsB
return
}

// UpdateOptsBuilder allows extensions to add additional parameters to the Update request.
type UpdateOptsBuilder interface {
ToProjectUpdateMap() (map[string]string, map[string]interface{}, error)
}

// UpdateOpts contains parameters to update a project.
type UpdateOpts struct {
Services limesresources.QuotaRequest `json:"services"`
Bursting *limesresources.ProjectBurstingInfo `json:"bursting,omitempty"`
}

// ToProjectUpdateMap formats a UpdateOpts into a map of headers and a request body.
func (opts UpdateOpts) ToProjectUpdateMap() (headers map[string]string, requestBody map[string]interface{}, err error) {
h, err := gophercloud.BuildHeaders(opts)
if err != nil {
return nil, nil, err
}

b, err := gophercloud.BuildRequestBody(opts, "project")
if err != nil {
return nil, nil, err
}

return h, b, nil
}

// Update modifies the attributes of a project and returns the response body which contains non-fatal error messages.
func Update(c *gophercloud.ServiceClient, domainID, projectID string, opts UpdateOptsBuilder) (r UpdateResult) {
url := updateURL(c, domainID, projectID)
h, b, err := opts.ToProjectUpdateMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Put(url, b, nil, &gophercloud.RequestOpts{
OkCodes: []int{http.StatusAccepted},
MoreHeaders: h,
KeepResponseBody: true,
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
if r.Err != nil {
return
}
defer resp.Body.Close()
r.Body, r.Err = io.ReadAll(resp.Body)
return
}

// Sync schedules a sync task that pulls a project's data from the backing services
// into Limes' local database.
func Sync(c *gophercloud.ServiceClient, domainID, projectID string) (r SyncResult) {
Expand Down
12 changes: 0 additions & 12 deletions resources/v1/projects/results.go
Expand Up @@ -25,18 +25,6 @@ type CommonResult struct {
gophercloud.Result
}

// UpdateResult is the result of an Update operation. Call its appropriate
// Extract method to extract the error and the warning body from the result.
type UpdateResult struct {
gophercloud.Result
Body []byte
}

// Extract interprets a UpdateResult as an update warning body and error
func (r UpdateResult) Extract() ([]byte, error) {
return r.Body, r.Err
}

// SyncResult is the result of an Sync operation. Call its appropriate
// ExtractErr method to extract the error from the result.
type SyncResult struct {
Expand Down
36 changes: 0 additions & 36 deletions resources/v1/projects/testing/fixtures.go
Expand Up @@ -16,7 +16,6 @@
package testing

import (
"fmt"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -80,17 +79,6 @@ func HandleGetProjectSuccessfully(t *testing.T) {
})
}

// HandleUpdateProjectSuccessfully creates an HTTP handler at `/domains/:domain_id/projects/:project_id` on the
// test handler mux that tests project updates.
func HandleUpdateProjectSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/domains/uuid-for-germany/projects/uuid-for-berlin", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.WriteHeader(http.StatusAccepted)
})
}

// HandleSyncProjectSuccessfully creates an HTTP handler at `/domains/:domain_id/projects/:project_id/sync` on the
// test handler mux that syncs a project.
func HandleSyncProjectSuccessfully(t *testing.T) {
Expand All @@ -101,27 +89,3 @@ func HandleSyncProjectSuccessfully(t *testing.T) {
w.WriteHeader(http.StatusAccepted)
})
}

// HandleUpdateProjectPartly creates an HTTP handler at `/domains/:domain_id/projects/:project_id` on the
// test handler mux that tests partly project updates.
func HandleUpdateProjectPartly(t *testing.T) {
th.Mux.HandleFunc("/domains/uuid-for-germany/projects/uuid-for-berlin", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.WriteHeader(http.StatusAccepted)
fmt.Fprint(w, `it is currently not allowed to set bursting.enabled and quotas in the same request`)
})
}

// HandleUpdateProjectUnsuccessfully creates an HTTP handler at `/domains/:domain_id/projects/:project_id` on the
// test handler mux that tests an unsuccessful project updates.
func HandleUpdateProjectUnsuccessfully(t *testing.T) {
th.Mux.HandleFunc("/domains/uuid-for-germany/projects/uuid-for-berlin", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `it is currently not allowed to set bursting.enabled and quotas in the same request`)
})
}
52 changes: 0 additions & 52 deletions resources/v1/projects/testing/requests_test.go
Expand Up @@ -16,11 +16,9 @@ package testing

import (
"encoding/json"
"errors"
"testing"
"time"

"github.com/gophercloud/gophercloud"
th "github.com/gophercloud/gophercloud/testhelper"
fakeclient "github.com/gophercloud/gophercloud/testhelper/client"
"github.com/sapcc/go-api-declarations/limes"
Expand Down Expand Up @@ -409,56 +407,6 @@ func TestGetProjectFiltered(t *testing.T) {
th.CheckDeepEquals(t, expected, actual)
}

func TestUpdateProject(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdateProjectSuccessfully(t)

opts := projects.UpdateOpts{
Services: limesresources.QuotaRequest{
"compute": limesresources.ServiceQuotaRequest{
"cores": limesresources.ResourceQuotaRequest{
Value: 42,
Unit: limes.UnitNone,
},
},
},
}

// if update succeeds then a 202 (no error) is returned.
warn, err := projects.Update(fakeclient.ServiceClient(), "uuid-for-germany", "uuid-for-berlin", opts).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, []byte{}, warn)
}

func TestUpdateProjectWarning(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdateProjectPartly(t)

// expecting to get 202 response code with a warning
body, err := projects.Update(fakeclient.ServiceClient(), "uuid-for-germany", "uuid-for-berlin", projects.UpdateOpts{}).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, body, []byte("it is currently not allowed to set bursting.enabled and quotas in the same request"))
}

func TestUpdateProjectError(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleUpdateProjectUnsuccessfully(t)

// expecting to get 400 response code
warn, err := projects.Update(fakeclient.ServiceClient(), "uuid-for-germany", "uuid-for-berlin", projects.UpdateOpts{}).Extract()
th.AssertErr(t, err)
th.AssertDeepEquals(t, []byte(nil), warn)
var gerr gophercloud.ErrDefault400
if ok := errors.As(err, &gerr); ok {
th.AssertDeepEquals(t, gerr.Body, []byte("it is currently not allowed to set bursting.enabled and quotas in the same request"))
} else {
t.Fatalf("Unexpected error response")
}
}

func TestSyncProject(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
Expand Down
4 changes: 0 additions & 4 deletions resources/v1/projects/urls.go
Expand Up @@ -24,10 +24,6 @@ func getURL(client *gophercloud.ServiceClient, domainID, projectID string) strin
return client.ServiceURL("domains", domainID, "projects", projectID)
}

func updateURL(client *gophercloud.ServiceClient, domainID, projectID string) string {
return client.ServiceURL("domains", domainID, "projects", projectID)
}

func syncURL(client *gophercloud.ServiceClient, domainID, projectID string) string {
return client.ServiceURL("domains", domainID, "projects", projectID, "sync")
}

0 comments on commit 01d47d0

Please sign in to comment.