From 1b0a6d13f15d31bb78631f94a715dcd50da5173e Mon Sep 17 00:00:00 2001 From: Pierre Prinetti Date: Wed, 28 Feb 2024 10:57:54 +0100 Subject: [PATCH] Bump Gophercloud to v2-beta.2 Use `context.Context`. --- gnocchi/metric/v1/archivepolicies/doc.go | 2 +- gnocchi/metric/v1/archivepolicies/requests.go | 18 ++-- .../archivepolicies/testing/requests_test.go | 13 +-- gnocchi/metric/v1/measures/doc.go | 2 +- gnocchi/metric/v1/measures/requests.go | 13 +-- .../v1/measures/testing/requests_test.go | 9 +- gnocchi/metric/v1/metrics/doc.go | 2 +- gnocchi/metric/v1/metrics/requests.go | 14 +-- .../v1/metrics/testing/requests_test.go | 9 +- gnocchi/metric/v1/resources/doc.go | 2 +- gnocchi/metric/v1/resources/requests.go | 17 ++-- .../v1/resources/testing/requests_test.go | 17 ++-- gnocchi/metric/v1/resourcetypes/doc.go | 2 +- gnocchi/metric/v1/resourcetypes/requests.go | 17 ++-- .../v1/resourcetypes/testing/requests_test.go | 13 +-- gnocchi/metric/v1/status/requests.go | 6 +- .../metric/v1/status/testing/requests_test.go | 5 +- go.mod | 2 +- go.sum | 4 +- .../blockstorage/extensions/backups/utils.go | 10 +- openstack/blockstorage/v1/snapshots/utils.go | 6 +- openstack/blockstorage/v1/volumes/utils.go | 6 +- openstack/blockstorage/v2/snapshots/utils.go | 6 +- openstack/blockstorage/v2/volumes/utils.go | 6 +- .../v3/availabilityzones/utils.go | 6 +- openstack/blockstorage/v3/snapshots/utils.go | 10 +- openstack/blockstorage/v3/volumes/utils.go | 10 +- openstack/clientconfig/doc.go | 6 +- openstack/clientconfig/requests.go | 9 +- .../compute/v2/availabilityzones/utils.go | 6 +- openstack/compute/v2/flavors/utils.go | 10 +- openstack/compute/v2/servers/utils.go | 9 +- openstack/helpers/projectpurge.go | 97 ++++++++++--------- openstack/imageservice/v2/images/utils.go | 10 +- .../v2/extensions/security/groups/utils.go | 6 +- openstack/networking/v2/networks/utils.go | 10 +- openstack/networking/v2/ports/utils.go | 10 +- openstack/networking/v2/subnets/utils.go | 10 +- .../objectstorage/v1/objects/download.go | 27 +++--- .../objectstorage/v1/objects/manifest.go | 9 +- .../v1/objects/testing/manifest_test.go | 5 +- openstack/objectstorage/v1/objects/upload.go | 55 ++++++----- .../sharedfilesystems/v2/shares/utils.go | 10 +- .../sharedfilesystems/v2/sharetypes/utils.go | 10 +- .../sharedfilesystems/v2/snapshots/utils.go | 10 +- terraform/auth/config.go | 80 +++++++-------- 46 files changed, 338 insertions(+), 278 deletions(-) diff --git a/gnocchi/metric/v1/archivepolicies/doc.go b/gnocchi/metric/v1/archivepolicies/doc.go index 0d980ecd..40efd1ba 100644 --- a/gnocchi/metric/v1/archivepolicies/doc.go +++ b/gnocchi/metric/v1/archivepolicies/doc.go @@ -4,7 +4,7 @@ through the Gnocchi API. Example of Listing archive policies - allPages, err := archivepolicies.List(gnocchiClient).AllPages() + allPages, err := archivepolicies.List(gnocchiClient).AllPages(ctx) if err != nil { panic(err) } diff --git a/gnocchi/metric/v1/archivepolicies/requests.go b/gnocchi/metric/v1/archivepolicies/requests.go index 73599d2d..f43b1e77 100644 --- a/gnocchi/metric/v1/archivepolicies/requests.go +++ b/gnocchi/metric/v1/archivepolicies/requests.go @@ -1,6 +1,8 @@ package archivepolicies import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/pagination" ) @@ -13,8 +15,8 @@ func List(client *gophercloud.ServiceClient) pagination.Pager { } // Get retrieves a specific Gnocchi archive policy based on its name. -func Get(c *gophercloud.ServiceClient, archivePolicyName string) (r GetResult) { - _, r.Err = c.Get(getURL(c, archivePolicyName), &r.Body, nil) +func Get(ctx context.Context, c *gophercloud.ServiceClient, archivePolicyName string) (r GetResult) { + _, r.Err = c.Get(ctx, getURL(c, archivePolicyName), &r.Body, nil) return } @@ -64,13 +66,13 @@ func (opts CreateOpts) ToArchivePolicyCreateMap() (map[string]interface{}, error } // Create requests the creation of a new Gnocchi archive policy on the server. -func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { +func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { b, err := opts.ToArchivePolicyCreateMap() if err != nil { r.Err = err return } - _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{201}, }) @@ -95,13 +97,13 @@ func (opts UpdateOpts) ToArchivePolicyUpdateMap() (map[string]interface{}, error } // Update accepts a UpdateOpts and updates an existing Gnocchi archive policy using the values provided. -func Update(client *gophercloud.ServiceClient, archivePolicyName string, opts UpdateOptsBuilder) (r UpdateResult) { +func Update(ctx context.Context, client *gophercloud.ServiceClient, archivePolicyName string, opts UpdateOptsBuilder) (r UpdateResult) { b, err := opts.ToArchivePolicyUpdateMap() if err != nil { r.Err = err return } - _, r.Err = client.Patch(updateURL(client, archivePolicyName), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Patch(ctx, updateURL(client, archivePolicyName), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{200}, }) @@ -109,12 +111,12 @@ func Update(client *gophercloud.ServiceClient, archivePolicyName string, opts Up } // Delete accepts a Gnocchi archive policy by its name. -func Delete(c *gophercloud.ServiceClient, archivePolicyName string) (r DeleteResult) { +func Delete(ctx context.Context, c *gophercloud.ServiceClient, archivePolicyName string) (r DeleteResult) { requestOpts := &gophercloud.RequestOpts{ MoreHeaders: map[string]string{ "Accept": "application/json, */*", }, } - _, r.Err = c.Delete(deleteURL(c, archivePolicyName), requestOpts) + _, r.Err = c.Delete(ctx, deleteURL(c, archivePolicyName), requestOpts) return } diff --git a/gnocchi/metric/v1/archivepolicies/testing/requests_test.go b/gnocchi/metric/v1/archivepolicies/testing/requests_test.go index 36381fd2..392c1cac 100644 --- a/gnocchi/metric/v1/archivepolicies/testing/requests_test.go +++ b/gnocchi/metric/v1/archivepolicies/testing/requests_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "net/http" "testing" @@ -27,7 +28,7 @@ func TestListArchivePolicies(t *testing.T) { expected := ListArchivePoliciesExpected pages := 0 - err := archivepolicies.List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { + err := archivepolicies.List(fake.ServiceClient()).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { pages++ actual, err := archivepolicies.ExtractArchivePolicies(page) @@ -58,7 +59,7 @@ func TestListArchivePoliciesAllPages(t *testing.T) { fmt.Fprintf(w, ArchivePoliciesListResult) }) - allPages, err := archivepolicies.List(fake.ServiceClient()).AllPages() + allPages, err := archivepolicies.List(fake.ServiceClient()).AllPages(context.TODO()) th.AssertNoErr(t, err) _, err = archivepolicies.ExtractArchivePolicies(allPages) th.AssertNoErr(t, err) @@ -78,7 +79,7 @@ func TestGetArchivePolicy(t *testing.T) { fmt.Fprintf(w, ArchivePolicyGetResult) }) - s, err := archivepolicies.Get(fake.ServiceClient(), "test_policy").Extract() + s, err := archivepolicies.Get(context.TODO(), fake.ServiceClient(), "test_policy").Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, s.AggregationMethods, []string{ @@ -138,7 +139,7 @@ func TestCreate(t *testing.T) { }, Name: "test_policy", } - s, err := archivepolicies.Create(fake.ServiceClient(), opts).Extract() + s, err := archivepolicies.Create(context.TODO(), fake.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, s.AggregationMethods, []string{ @@ -191,7 +192,7 @@ func TestUpdateArchivePolicy(t *testing.T) { }, }, } - s, err := archivepolicies.Update(fake.ServiceClient(), "test_policy", updateOpts).Extract() + s, err := archivepolicies.Update(context.TODO(), fake.ServiceClient(), "test_policy", updateOpts).Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, s.AggregationMethods, []string{ @@ -223,6 +224,6 @@ func TestDelete(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - res := archivepolicies.Delete(fake.ServiceClient(), "test_policy") + res := archivepolicies.Delete(context.TODO(), fake.ServiceClient(), "test_policy") th.AssertNoErr(t, res.Err) } diff --git a/gnocchi/metric/v1/measures/doc.go b/gnocchi/metric/v1/measures/doc.go index 13262bc2..d2977787 100644 --- a/gnocchi/metric/v1/measures/doc.go +++ b/gnocchi/metric/v1/measures/doc.go @@ -10,7 +10,7 @@ Example of Listing measures of a known metric Granularity: "1h", Start: &startTime, } - allPages, err := measures.List(gnocchiClient, metricID, listOpts).AllPages() + allPages, err := measures.List(gnocchiClient, metricID, listOpts).AllPages(ctx) if err != nil { panic(err) } diff --git a/gnocchi/metric/v1/measures/requests.go b/gnocchi/metric/v1/measures/requests.go index ca1884a7..5d9bae0a 100644 --- a/gnocchi/metric/v1/measures/requests.go +++ b/gnocchi/metric/v1/measures/requests.go @@ -1,6 +1,7 @@ package measures import ( + "context" "fmt" "net/url" "time" @@ -121,13 +122,13 @@ func (opts CreateOpts) ToMeasureCreateMap() (map[string]interface{}, error) { } // Create requests the creation of a new measures in the single Gnocchi metric. -func Create(client *gophercloud.ServiceClient, metricID string, opts CreateOptsBuilder) (r CreateResult) { +func Create(ctx context.Context, client *gophercloud.ServiceClient, metricID string, opts CreateOptsBuilder) (r CreateResult) { b, err := opts.ToMeasureCreateMap() if err != nil { r.Err = err return } - _, r.Err = client.Post(createURL(client, metricID), b["measures"], &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, createURL(client, metricID), b["measures"], &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{202}, MoreHeaders: map[string]string{ "Accept": "application/json, */*", @@ -202,14 +203,14 @@ func (opts BatchCreateMetricsOpts) ToMeasuresBatchCreateMetricsMap() (map[string } // BatchCreateMetrics requests the creation of a new measures for different metrics. -func BatchCreateMetrics(client *gophercloud.ServiceClient, opts BatchCreateMetricsOpts) (r BatchCreateMetricsResult) { +func BatchCreateMetrics(ctx context.Context, client *gophercloud.ServiceClient, opts BatchCreateMetricsOpts) (r BatchCreateMetricsResult) { b, err := opts.ToMeasuresBatchCreateMetricsMap() if err != nil { r.Err = err return } - _, r.Err = client.Post(batchCreateMetricsURL(client), b["batchCreateMetrics"], &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, batchCreateMetricsURL(client), b["batchCreateMetrics"], &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{202}, MoreHeaders: map[string]string{ "Accept": "application/json, */*", @@ -372,7 +373,7 @@ func (opts BatchCreateResourcesMetricsOpts) ToMeasuresBatchCreateResourcesMetric } // BatchCreateResourcesMetrics requests the creation of new measures inside metrics via resource IDs and metric names. -func BatchCreateResourcesMetrics(client *gophercloud.ServiceClient, opts BatchCreateResourcesMetricsOptsBuilder) (r BatchCreateResourcesMetricsResult) { +func BatchCreateResourcesMetrics(ctx context.Context, client *gophercloud.ServiceClient, opts BatchCreateResourcesMetricsOptsBuilder) (r BatchCreateResourcesMetricsResult) { url := batchCreateResourcesMetricsURL(client) if opts != nil { query, err := opts.ToMeasuresBatchCreateResourcesMetricsQuery() @@ -389,7 +390,7 @@ func BatchCreateResourcesMetrics(client *gophercloud.ServiceClient, opts BatchCr return } - _, r.Err = client.Post(url, b["batchCreateResourcesMetrics"], &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, url, b["batchCreateResourcesMetrics"], &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{202}, MoreHeaders: map[string]string{ "Accept": "application/json, */*", diff --git a/gnocchi/metric/v1/measures/testing/requests_test.go b/gnocchi/metric/v1/measures/testing/requests_test.go index c8f01dba..3c649285 100644 --- a/gnocchi/metric/v1/measures/testing/requests_test.go +++ b/gnocchi/metric/v1/measures/testing/requests_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "net/http" "testing" @@ -36,7 +37,7 @@ func TestListMeasures(t *testing.T) { } expected := ListMeasuresExpected pages := 0 - err := measures.List(fake.ServiceClient(), metricID, opts).EachPage(func(page pagination.Page) (bool, error) { + err := measures.List(fake.ServiceClient(), metricID, opts).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { pages++ actual, err := measures.ExtractMeasures(page) @@ -81,7 +82,7 @@ func TestCreateMeasures(t *testing.T) { }, }, } - res := measures.Create(fake.ServiceClient(), "9e5a6441-1044-4181-b66e-34e180753040", createOpts) + res := measures.Create(context.TODO(), fake.ServiceClient(), "9e5a6441-1044-4181-b66e-34e180753040", createOpts) th.AssertNoErr(t, res.Err) } @@ -129,7 +130,7 @@ func TestBatchCreateMetrics(t *testing.T) { }, }, } - res := measures.BatchCreateMetrics(fake.ServiceClient(), createOpts) + res := measures.BatchCreateMetrics(context.TODO(), fake.ServiceClient(), createOpts) th.AssertNoErr(t, res.Err) } @@ -209,6 +210,6 @@ func TestBatchCreateResourcesMetrics(t *testing.T) { }, }, } - res := measures.BatchCreateResourcesMetrics(fake.ServiceClient(), createOpts) + res := measures.BatchCreateResourcesMetrics(context.TODO(), fake.ServiceClient(), createOpts) th.AssertNoErr(t, res.Err) } diff --git a/gnocchi/metric/v1/metrics/doc.go b/gnocchi/metric/v1/metrics/doc.go index 1585dd80..6638218b 100644 --- a/gnocchi/metric/v1/metrics/doc.go +++ b/gnocchi/metric/v1/metrics/doc.go @@ -7,7 +7,7 @@ Example of Listing metrics Limit: 25, } - allPages, err := metrics.List(gnocchiClient, listOpts).AllPages() + allPages, err := metrics.List(gnocchiClient, listOpts).AllPages(ctx) if err != nil { panic(err) } diff --git a/gnocchi/metric/v1/metrics/requests.go b/gnocchi/metric/v1/metrics/requests.go index 9328e7cd..dc3e2397 100644 --- a/gnocchi/metric/v1/metrics/requests.go +++ b/gnocchi/metric/v1/metrics/requests.go @@ -1,6 +1,8 @@ package metrics import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/pagination" ) @@ -68,8 +70,8 @@ func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { } // Get retrieves a specific Gnocchi metric based on its id. -func Get(c *gophercloud.ServiceClient, metricID string) (r GetResult) { - _, r.Err = c.Get(getURL(c, metricID), &r.Body, nil) +func Get(ctx context.Context, c *gophercloud.ServiceClient, metricID string) (r GetResult) { + _, r.Err = c.Get(ctx, getURL(c, metricID), &r.Body, nil) return } @@ -109,25 +111,25 @@ func (opts CreateOpts) ToMetricCreateMap() (map[string]interface{}, error) { } // Create requests the creation of a new Gnocchi metric on the server. -func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { +func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { b, err := opts.ToMetricCreateMap() if err != nil { r.Err = err return } - _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{201}, }) return } // Delete accepts a unique ID and deletes the Gnocchi metric associated with it. -func Delete(c *gophercloud.ServiceClient, metricID string) (r DeleteResult) { +func Delete(ctx context.Context, c *gophercloud.ServiceClient, metricID string) (r DeleteResult) { requestOpts := &gophercloud.RequestOpts{ MoreHeaders: map[string]string{ "Accept": "application/json, */*", }, } - _, r.Err = c.Delete(deleteURL(c, metricID), requestOpts) + _, r.Err = c.Delete(ctx, deleteURL(c, metricID), requestOpts) return } diff --git a/gnocchi/metric/v1/metrics/testing/requests_test.go b/gnocchi/metric/v1/metrics/testing/requests_test.go index 8565e447..9ee549d1 100644 --- a/gnocchi/metric/v1/metrics/testing/requests_test.go +++ b/gnocchi/metric/v1/metrics/testing/requests_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "net/http" "testing" @@ -38,7 +39,7 @@ func TestList(t *testing.T) { count := 0 - metrics.List(fake.ServiceClient(), metrics.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { + metrics.List(fake.ServiceClient(), metrics.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { count++ actual, err := metrics.ExtractMetrics(page) if err != nil { @@ -75,7 +76,7 @@ func TestGet(t *testing.T) { fmt.Fprintf(w, MetricGetResult) }) - s, err := metrics.Get(fake.ServiceClient(), "0ddf61cf-3747-4f75-bf13-13c28ff03ae3").Extract() + s, err := metrics.Get(context.TODO(), fake.ServiceClient(), "0ddf61cf-3747-4f75-bf13-13c28ff03ae3").Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, s.ArchivePolicy, archivepolicies.ArchivePolicy{ @@ -144,7 +145,7 @@ func TestCreate(t *testing.T) { ResourceID: "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55", Unit: "B/s", } - s, err := metrics.Create(fake.ServiceClient(), opts).Extract() + s, err := metrics.Create(context.TODO(), fake.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.ArchivePolicyName, "high") @@ -167,6 +168,6 @@ func TestDelete(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - res := metrics.Delete(fake.ServiceClient(), "01b2953e-de74-448a-a305-c84440697933") + res := metrics.Delete(context.TODO(), fake.ServiceClient(), "01b2953e-de74-448a-a305-c84440697933") th.AssertNoErr(t, res.Err) } diff --git a/gnocchi/metric/v1/resources/doc.go b/gnocchi/metric/v1/resources/doc.go index ab19629b..36a56a24 100644 --- a/gnocchi/metric/v1/resources/doc.go +++ b/gnocchi/metric/v1/resources/doc.go @@ -8,7 +8,7 @@ Example of Listing resources Details: True, } - allPages, err := resources.List(gnocchiClient, listOpts, resourceType).AllPages() + allPages, err := resources.List(gnocchiClient, listOpts, resourceType).AllPages(ctx) if err != nil { panic(err) } diff --git a/gnocchi/metric/v1/resources/requests.go b/gnocchi/metric/v1/resources/requests.go index 159838ac..0b294bd5 100644 --- a/gnocchi/metric/v1/resources/requests.go +++ b/gnocchi/metric/v1/resources/requests.go @@ -1,6 +1,7 @@ package resources import ( + "context" "time" "github.com/gophercloud/gophercloud/v2" @@ -62,8 +63,8 @@ func List(c *gophercloud.ServiceClient, opts ListOptsBuilder, resourceType strin } // Get retrieves a specific Gnocchi resource based on its type and ID. -func Get(c *gophercloud.ServiceClient, resourceType string, resourceID string) (r GetResult) { - _, r.Err = c.Get(getURL(c, resourceType, resourceID), &r.Body, nil) +func Get(ctx context.Context, c *gophercloud.ServiceClient, resourceType string, resourceID string) (r GetResult) { + _, r.Err = c.Get(ctx, getURL(c, resourceType, resourceID), &r.Body, nil) return } @@ -125,13 +126,13 @@ func (opts CreateOpts) ToResourceCreateMap() (map[string]interface{}, error) { } // Create requests the creation of a new Gnocchi resource on the server. -func Create(client *gophercloud.ServiceClient, resourceType string, opts CreateOptsBuilder) (r CreateResult) { +func Create(ctx context.Context, client *gophercloud.ServiceClient, resourceType string, opts CreateOptsBuilder) (r CreateResult) { b, err := opts.ToResourceCreateMap() if err != nil { r.Err = err return } - _, r.Err = client.Post(createURL(client, resourceType), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, createURL(client, resourceType), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{201}, }) return @@ -193,25 +194,25 @@ func (opts UpdateOpts) ToResourceUpdateMap() (map[string]interface{}, error) { // Update accepts a UpdateOpts struct and updates an existing Gnocchi resource using the // values provided. -func Update(c *gophercloud.ServiceClient, resourceType, resourceID string, opts UpdateOptsBuilder) (r UpdateResult) { +func Update(ctx context.Context, c *gophercloud.ServiceClient, resourceType, resourceID string, opts UpdateOptsBuilder) (r UpdateResult) { b, err := opts.ToResourceUpdateMap() if err != nil { r.Err = err return } - _, r.Err = c.Patch(updateURL(c, resourceType, resourceID), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = c.Patch(ctx, updateURL(c, resourceType, resourceID), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{200}, }) return } // Delete accepts a unique ID and deletes the Gnocchi resource associated with it. -func Delete(c *gophercloud.ServiceClient, resourceType, resourceID string) (r DeleteResult) { +func Delete(ctx context.Context, c *gophercloud.ServiceClient, resourceType, resourceID string) (r DeleteResult) { requestOpts := &gophercloud.RequestOpts{ MoreHeaders: map[string]string{ "Accept": "application/json, */*", }, } - _, r.Err = c.Delete(deleteURL(c, resourceType, resourceID), requestOpts) + _, r.Err = c.Delete(ctx, deleteURL(c, resourceType, resourceID), requestOpts) return } diff --git a/gnocchi/metric/v1/resources/testing/requests_test.go b/gnocchi/metric/v1/resources/testing/requests_test.go index 881c8362..30c0a532 100644 --- a/gnocchi/metric/v1/resources/testing/requests_test.go +++ b/gnocchi/metric/v1/resources/testing/requests_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "net/http" "testing" @@ -36,7 +37,7 @@ func TestList(t *testing.T) { count := 0 - resources.List(fake.ServiceClient(), resources.ListOpts{}, "generic").EachPage(func(page pagination.Page) (bool, error) { + resources.List(fake.ServiceClient(), resources.ListOpts{}, "generic").EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { count++ actual, err := resources.ExtractResources(page) if err != nil { @@ -73,7 +74,7 @@ func TestGet(t *testing.T) { fmt.Fprintf(w, ResourceGetResult) }) - s, err := resources.Get(fake.ServiceClient(), "compute_instance_network", "75274f99-faf6-4112-a6d5-2794cb07c789").Extract() + s, err := resources.Get(context.TODO(), fake.ServiceClient(), "compute_instance_network", "75274f99-faf6-4112-a6d5-2794cb07c789").Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.CreatedByProjectID, "3d40ca37723449118987b9f288f4ae84") @@ -121,7 +122,7 @@ func TestCreateWithoutMetrics(t *testing.T) { ProjectID: "4154f088-8333-4e04-94c4-1155c33c0fc9", UserID: "bd5874d6-6662-4b24-a9f01c128871e4ac", } - s, err := resources.Create(fake.ServiceClient(), "generic", opts).Extract() + s, err := resources.Create(context.TODO(), fake.ServiceClient(), "generic", opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.CreatedByProjectID, "3d40ca37-7234-4911-8987b9f288f4ae84") @@ -169,7 +170,7 @@ func TestCreateLinkMetrics(t *testing.T) { "network.outgoing.bytes.rate": "dc9f3198-155b-4b88-a92c-58a3853ce2b2", }, } - s, err := resources.Create(fake.ServiceClient(), "compute_instance_network", opts).Extract() + s, err := resources.Create(context.TODO(), fake.ServiceClient(), "compute_instance_network", opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.CreatedByProjectID, "3d40ca37-7234-4911-8987b9f288f4ae84") @@ -219,7 +220,7 @@ func TestCreateWithMetrics(t *testing.T) { }, }, } - s, err := resources.Create(fake.ServiceClient(), "compute_instance_disk", opts).Extract() + s, err := resources.Create(context.TODO(), fake.ServiceClient(), "compute_instance_disk", opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.CreatedByProjectID, "3d40ca37-7234-4911-8987b9f288f4ae84") @@ -264,7 +265,7 @@ func TestUpdateLinkMetrics(t *testing.T) { EndedAt: &endedAt, Metrics: &metrics, } - s, err := resources.Update(fake.ServiceClient(), "compute_instance_network", "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55", updateOpts).Extract() + s, err := resources.Update(context.TODO(), fake.ServiceClient(), "compute_instance_network", "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55", updateOpts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.CreatedByProjectID, "3d40ca37-7234-4911-8987b9f288f4ae84") @@ -311,7 +312,7 @@ func TestUpdateCreateMetrics(t *testing.T) { StartedAt: &startedAt, Metrics: &metrics, } - s, err := resources.Update(fake.ServiceClient(), "compute_instance_network", "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55", updateOpts).Extract() + s, err := resources.Update(context.TODO(), fake.ServiceClient(), "compute_instance_network", "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55", updateOpts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.CreatedByProjectID, "3d40ca37-7234-4911-8987b9f288f4ae84") @@ -341,6 +342,6 @@ func TestDelete(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - res := resources.Delete(fake.ServiceClient(), "generic", "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55") + res := resources.Delete(context.TODO(), fake.ServiceClient(), "generic", "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55") th.AssertNoErr(t, res.Err) } diff --git a/gnocchi/metric/v1/resourcetypes/doc.go b/gnocchi/metric/v1/resourcetypes/doc.go index 23678ff9..1ee40e7a 100644 --- a/gnocchi/metric/v1/resourcetypes/doc.go +++ b/gnocchi/metric/v1/resourcetypes/doc.go @@ -3,7 +3,7 @@ Package resourcetypes provides ability to manage resource types through the Gnoc Example of Listing resource types - allPages, err := resourcetypes.List(client).AllPages() + allPages, err := resourcetypes.List(client).AllPages(ctx) if err != nil { panic(err) } diff --git a/gnocchi/metric/v1/resourcetypes/requests.go b/gnocchi/metric/v1/resourcetypes/requests.go index db11338f..97f19dcd 100644 --- a/gnocchi/metric/v1/resourcetypes/requests.go +++ b/gnocchi/metric/v1/resourcetypes/requests.go @@ -1,6 +1,7 @@ package resourcetypes import ( + "context" "fmt" "strings" @@ -16,8 +17,8 @@ func List(client *gophercloud.ServiceClient) pagination.Pager { } // Get retrieves a specific Gnocchi resource type based on its name. -func Get(c *gophercloud.ServiceClient, resourceTypeName string) (r GetResult) { - _, r.Err = c.Get(getURL(c, resourceTypeName), &r.Body, nil) +func Get(ctx context.Context, c *gophercloud.ServiceClient, resourceTypeName string) (r GetResult) { + _, r.Err = c.Get(ctx, getURL(c, resourceTypeName), &r.Body, nil) return } @@ -85,13 +86,13 @@ func (opts CreateOpts) ToResourceTypeCreateMap() (map[string]interface{}, error) } // Create requests the creation of a new Gnocchi resource type on the server. -func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { +func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { b, err := opts.ToResourceTypeCreateMap() if err != nil { r.Err = err return } - _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{201}, }) return @@ -175,13 +176,13 @@ func (opts UpdateOpts) ToResourceTypeUpdateMap() ([]map[string]interface{}, erro } // Update requests the update operation over existsing Gnocchi resource type. -func Update(client *gophercloud.ServiceClient, resourceTypeName string, opts UpdateOptsBuilder) (r UpdateResult) { +func Update(ctx context.Context, client *gophercloud.ServiceClient, resourceTypeName string, opts UpdateOptsBuilder) (r UpdateResult) { b, err := opts.ToResourceTypeUpdateMap() if err != nil { r.Err = err return } - _, r.Err = client.Patch(updateURL(client, resourceTypeName), b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Patch(ctx, updateURL(client, resourceTypeName), b, &r.Body, &gophercloud.RequestOpts{ OkCodes: []int{200}, MoreHeaders: map[string]string{ "Content-Type": "application/json-patch+json", @@ -191,12 +192,12 @@ func Update(client *gophercloud.ServiceClient, resourceTypeName string, opts Upd } // Delete accepts a human-readable name and deletes the Gnocchi resource type associated with it. -func Delete(c *gophercloud.ServiceClient, resourceTypeName string) (r DeleteResult) { +func Delete(ctx context.Context, c *gophercloud.ServiceClient, resourceTypeName string) (r DeleteResult) { requestOpts := &gophercloud.RequestOpts{ MoreHeaders: map[string]string{ "Accept": "application/json, */*", }, } - _, r.Err = c.Delete(deleteURL(c, resourceTypeName), requestOpts) + _, r.Err = c.Delete(ctx, deleteURL(c, resourceTypeName), requestOpts) return } diff --git a/gnocchi/metric/v1/resourcetypes/testing/requests_test.go b/gnocchi/metric/v1/resourcetypes/testing/requests_test.go index ded4784f..de081fd0 100644 --- a/gnocchi/metric/v1/resourcetypes/testing/requests_test.go +++ b/gnocchi/metric/v1/resourcetypes/testing/requests_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "net/http" "testing" @@ -27,7 +28,7 @@ func TestList(t *testing.T) { count := 0 - resourcetypes.List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { + resourcetypes.List(fake.ServiceClient()).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { count++ actual, err := resourcetypes.ExtractResourceTypes(page) if err != nil { @@ -65,7 +66,7 @@ func TestGet(t *testing.T) { fmt.Fprintf(w, ResourceTypeGetResult) }) - s, err := resourcetypes.Get(fake.ServiceClient(), "compute_instance").Extract() + s, err := resourcetypes.Get(context.TODO(), fake.ServiceClient(), "compute_instance").Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "compute_instance") @@ -108,7 +109,7 @@ func TestCreateWithoutAttributes(t *testing.T) { opts := resourcetypes.CreateOpts{ Name: "identity_project", } - s, err := resourcetypes.Create(fake.ServiceClient(), opts).Extract() + s, err := resourcetypes.Create(context.TODO(), fake.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "identity_project") @@ -151,7 +152,7 @@ func TestCreateWithAttributes(t *testing.T) { }, }, } - s, err := resourcetypes.Create(fake.ServiceClient(), opts).Extract() + s, err := resourcetypes.Create(context.TODO(), fake.ServiceClient(), opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "compute_instance_network") @@ -225,7 +226,7 @@ func TestUpdate(t *testing.T) { }, } - s, err := resourcetypes.Update(fake.ServiceClient(), "identity_project", opts).Extract() + s, err := resourcetypes.Update(context.TODO(), fake.ServiceClient(), "identity_project", opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "identity_project") @@ -264,6 +265,6 @@ func TestDelete(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - res := resourcetypes.Delete(fake.ServiceClient(), "compute_instance_network") + res := resourcetypes.Delete(context.TODO(), fake.ServiceClient(), "compute_instance_network") th.AssertNoErr(t, res.Err) } diff --git a/gnocchi/metric/v1/status/requests.go b/gnocchi/metric/v1/status/requests.go index c5c1555f..1a91351b 100644 --- a/gnocchi/metric/v1/status/requests.go +++ b/gnocchi/metric/v1/status/requests.go @@ -1,6 +1,8 @@ package status import ( + "context" + "github.com/gophercloud/gophercloud/v2" ) @@ -22,7 +24,7 @@ func (opts GetOpts) ToStatusGetQuery() (string, error) { } // Get retrieves the overall status of the Gnocchi installation. -func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) (r GetResult) { +func Get(ctx context.Context, c *gophercloud.ServiceClient, opts GetOptsBuilder) (r GetResult) { url := getURL(c) if opts != nil { query, err := opts.ToStatusGetQuery() @@ -32,6 +34,6 @@ func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) (r GetResult) { } url += query } - _, r.Err = c.Get(url, &r.Body, nil) + _, r.Err = c.Get(ctx, url, &r.Body, nil) return } diff --git a/gnocchi/metric/v1/status/testing/requests_test.go b/gnocchi/metric/v1/status/testing/requests_test.go index d953fe9d..021cc5b1 100644 --- a/gnocchi/metric/v1/status/testing/requests_test.go +++ b/gnocchi/metric/v1/status/testing/requests_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "net/http" "testing" @@ -30,7 +31,7 @@ func TestGetWithDetails(t *testing.T) { Details: &details, } - s, err := status.Get(fake.ServiceClient(), getOpts).Extract() + s, err := status.Get(context.TODO(), fake.ServiceClient(), getOpts).Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, s.Metricd, GetStatusWithDetailsExpected.Metricd) th.AssertDeepEquals(t, s.Storage, GetStatusWithDetailsExpected.Storage) @@ -56,7 +57,7 @@ func TestGetWithoutDetails(t *testing.T) { Details: &details, } - s, err := status.Get(fake.ServiceClient(), getOpts).Extract() + s, err := status.Get(context.TODO(), fake.ServiceClient(), getOpts).Extract() th.AssertNoErr(t, err) th.AssertDeepEquals(t, s.Metricd, GetStatusWithoutDetailsExpected.Metricd) th.AssertDeepEquals(t, s.Storage, GetStatusWithoutDetailsExpected.Storage) diff --git a/go.mod b/go.mod index 1a5e2793..7458e8bf 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/gophercloud/utils/v2 require ( - github.com/gophercloud/gophercloud/v2 v2.0.0-beta.1 + github.com/gophercloud/gophercloud/v2 v2.0.0-beta.2 github.com/hashicorp/go-uuid v1.0.3 github.com/mitchellh/go-homedir v1.1.0 golang.org/x/sys v0.17.0 diff --git a/go.sum b/go.sum index 03b17de9..43b8936f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/gophercloud/gophercloud/v2 v2.0.0-beta.1 h1:FYzj3NlCp3wTJahn9VHETY0WQlqWhHylkjhrZDAJpyQ= -github.com/gophercloud/gophercloud/v2 v2.0.0-beta.1/go.mod h1:p9iS4WRTI84BeqLH7Q7lu0V2UryWXvHQuqgThrB9xuU= +github.com/gophercloud/gophercloud/v2 v2.0.0-beta.2 h1:JWv6L7eg3+aIS57n11YlVvtn1pVCKpVlMo24ANj/OVc= +github.com/gophercloud/gophercloud/v2 v2.0.0-beta.2/go.mod h1:Sy5GHwY4iazyaRf94rzL/VxJToVWn8WnIH+1cXqoAks= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= 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= diff --git a/openstack/blockstorage/extensions/backups/utils.go b/openstack/blockstorage/extensions/backups/utils.go index 1e6da571..91156e05 100644 --- a/openstack/blockstorage/extensions/backups/utils.go +++ b/openstack/blockstorage/extensions/backups/utils.go @@ -1,14 +1,16 @@ package backups import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/extensions/backups" ) // IDFromName is a convenience function that returns a backup's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := backups.List(client, backups.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/blockstorage/v1/snapshots/utils.go b/openstack/blockstorage/v1/snapshots/utils.go index ab7ff691..5414546f 100644 --- a/openstack/blockstorage/v1/snapshots/utils.go +++ b/openstack/blockstorage/v1/snapshots/utils.go @@ -1,12 +1,14 @@ package snapshots import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v1/snapshots" ) // IDFromName is a convenience function that returns a snapshot's ID given its name. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" @@ -14,7 +16,7 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) Name: name, } - pages, err := snapshots.List(client, listOpts).AllPages() + pages, err := snapshots.List(client, listOpts).AllPages(ctx) if err != nil { return "", err } diff --git a/openstack/blockstorage/v1/volumes/utils.go b/openstack/blockstorage/v1/volumes/utils.go index eccb497f..8da29a37 100644 --- a/openstack/blockstorage/v1/volumes/utils.go +++ b/openstack/blockstorage/v1/volumes/utils.go @@ -1,12 +1,14 @@ package volumes import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v1/volumes" ) // IDFromName is a convenience function that returns a volume's ID given its name. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" @@ -14,7 +16,7 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) Name: name, } - pages, err := volumes.List(client, listOpts).AllPages() + pages, err := volumes.List(client, listOpts).AllPages(ctx) if err != nil { return "", err } diff --git a/openstack/blockstorage/v2/snapshots/utils.go b/openstack/blockstorage/v2/snapshots/utils.go index 5098a27f..72cc8f61 100644 --- a/openstack/blockstorage/v2/snapshots/utils.go +++ b/openstack/blockstorage/v2/snapshots/utils.go @@ -1,12 +1,14 @@ package snapshots import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v2/snapshots" ) // IDFromName is a convenience function that returns a snapshot's ID given its name. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" @@ -14,7 +16,7 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) Name: name, } - pages, err := snapshots.List(client, listOpts).AllPages() + pages, err := snapshots.List(client, listOpts).AllPages(ctx) if err != nil { return "", err } diff --git a/openstack/blockstorage/v2/volumes/utils.go b/openstack/blockstorage/v2/volumes/utils.go index 8b8e71e2..846224f5 100644 --- a/openstack/blockstorage/v2/volumes/utils.go +++ b/openstack/blockstorage/v2/volumes/utils.go @@ -1,12 +1,14 @@ package volumes import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v2/volumes" ) // IDFromName is a convenience function that returns a volume's ID given its name. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" @@ -14,7 +16,7 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) Name: name, } - pages, err := volumes.List(client, listOpts).AllPages() + pages, err := volumes.List(client, listOpts).AllPages(ctx) if err != nil { return "", err } diff --git a/openstack/blockstorage/v3/availabilityzones/utils.go b/openstack/blockstorage/v3/availabilityzones/utils.go index 885c541c..ad824b3a 100644 --- a/openstack/blockstorage/v3/availabilityzones/utils.go +++ b/openstack/blockstorage/v3/availabilityzones/utils.go @@ -1,15 +1,17 @@ package availabilityzones import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/extensions/availabilityzones" ) // ListAvailableAvailabilityZones is a convenience function that return a slice of available Availability Zones. -func ListAvailableAvailabilityZones(client *gophercloud.ServiceClient) ([]string, error) { +func ListAvailableAvailabilityZones(ctx context.Context, client *gophercloud.ServiceClient) ([]string, error) { var zones []string - allPages, err := availabilityzones.List(client).AllPages() + allPages, err := availabilityzones.List(client).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/blockstorage/v3/snapshots/utils.go b/openstack/blockstorage/v3/snapshots/utils.go index a15fe22e..d2a6a47c 100644 --- a/openstack/blockstorage/v3/snapshots/utils.go +++ b/openstack/blockstorage/v3/snapshots/utils.go @@ -1,14 +1,16 @@ package snapshots import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/snapshots" ) // IDFromName is a convenience function that returns a snapshot's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := snapshots.List(client, snapshots.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/blockstorage/v3/volumes/utils.go b/openstack/blockstorage/v3/volumes/utils.go index b1ec1f1e..b62d6384 100644 --- a/openstack/blockstorage/v3/volumes/utils.go +++ b/openstack/blockstorage/v3/volumes/utils.go @@ -1,14 +1,16 @@ package volumes import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes" ) // IDFromName is a convenience function that returns a volume's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := volumes.List(client, volumes.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/clientconfig/doc.go b/openstack/clientconfig/doc.go index d052d090..3f85b673 100644 --- a/openstack/clientconfig/doc.go +++ b/openstack/clientconfig/doc.go @@ -10,7 +10,7 @@ Example to Create a Provider Client From clouds.yaml Cloud: "hawaii", } - pClient, err := clientconfig.AuthenticatedClient(opts) + pClient, err := clientconfig.AuthenticatedClient(ctx, opts) if err != nil { panic(err) } @@ -27,7 +27,7 @@ Example to Manually Create a Provider Client }, } - pClient, err := clientconfig.AuthenticatedClient(opts) + pClient, err := clientconfig.AuthenticatedClient(ctx, opts) if err != nil { panic(err) } @@ -38,7 +38,7 @@ Example to Create a Service Client from clouds.yaml Cloud: "hawaii", } - computeClient, err := clientconfig.NewServiceClient("compute", opts) + computeClient, err := clientconfig.NewServiceClient(ctx, "compute", opts) if err != nil { panic(err) } diff --git a/openstack/clientconfig/requests.go b/openstack/clientconfig/requests.go index 26938f7a..133fe0fe 100644 --- a/openstack/clientconfig/requests.go +++ b/openstack/clientconfig/requests.go @@ -1,6 +1,7 @@ package clientconfig import ( + "context" "errors" "fmt" "net/http" @@ -726,17 +727,17 @@ func v3auth(cloud *Cloud, opts *ClientOpts) (*gophercloud.AuthOptions, error) { // AuthenticatedClient is a convenience function to get a new provider client // based on a clouds.yaml entry. -func AuthenticatedClient(opts *ClientOpts) (*gophercloud.ProviderClient, error) { +func AuthenticatedClient(ctx context.Context, opts *ClientOpts) (*gophercloud.ProviderClient, error) { ao, err := AuthOptions(opts) if err != nil { return nil, err } - return openstack.AuthenticatedClient(*ao) + return openstack.AuthenticatedClient(ctx, *ao) } // NewServiceClient is a convenience function to get a new service client. -func NewServiceClient(service string, opts *ClientOpts) (*gophercloud.ServiceClient, error) { +func NewServiceClient(ctx context.Context, service string, opts *ClientOpts) (*gophercloud.ServiceClient, error) { cloud := new(Cloud) // If no opts were passed in, create an empty ClientOpts. @@ -838,7 +839,7 @@ func NewServiceClient(service string, opts *ClientOpts) (*gophercloud.ServiceCli pClient.HTTPClient = http.Client{Transport: transport} } - err = openstack.Authenticate(pClient, *ao) + err = openstack.Authenticate(ctx, pClient, *ao) if err != nil { return nil, err } diff --git a/openstack/compute/v2/availabilityzones/utils.go b/openstack/compute/v2/availabilityzones/utils.go index d62a2b1c..2d0d9dde 100644 --- a/openstack/compute/v2/availabilityzones/utils.go +++ b/openstack/compute/v2/availabilityzones/utils.go @@ -1,14 +1,16 @@ package availabilityzones import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/extensions/availabilityzones" ) // ListAvailableAvailabilityZones is a convenience function that return a slice of available Availability Zones. -func ListAvailableAvailabilityZones(client *gophercloud.ServiceClient) ([]string, error) { +func ListAvailableAvailabilityZones(ctx context.Context, client *gophercloud.ServiceClient) ([]string, error) { var ret []string - allPages, err := availabilityzones.List(client).AllPages() + allPages, err := availabilityzones.List(client).AllPages(ctx) if err != nil { return ret, err } diff --git a/openstack/compute/v2/flavors/utils.go b/openstack/compute/v2/flavors/utils.go index 09d5db01..e1f376dc 100644 --- a/openstack/compute/v2/flavors/utils.go +++ b/openstack/compute/v2/flavors/utils.go @@ -1,14 +1,16 @@ package flavors import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/flavors" ) // IDFromName is a convenience function that returns a flavor's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,8 +27,8 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { - pages, err := flavors.ListDetail(client, nil).AllPages() +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := flavors.ListDetail(client, nil).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/compute/v2/servers/utils.go b/openstack/compute/v2/servers/utils.go index 68706b43..42b80e86 100644 --- a/openstack/compute/v2/servers/utils.go +++ b/openstack/compute/v2/servers/utils.go @@ -1,6 +1,7 @@ package servers import ( + "context" "fmt" "github.com/gophercloud/gophercloud/v2" @@ -9,8 +10,8 @@ import ( // IDFromName is a convenience function that returns a server's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -27,11 +28,11 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := servers.List(client, servers.ListOpts{ // nova list uses a name field as a regexp Name: fmt.Sprintf("^%s$", name), - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/helpers/projectpurge.go b/openstack/helpers/projectpurge.go index 65d07578..d41e0c50 100644 --- a/openstack/helpers/projectpurge.go +++ b/openstack/helpers/projectpurge.go @@ -1,6 +1,7 @@ package helpers import ( + "context" "fmt" "github.com/gophercloud/gophercloud/v2" @@ -38,21 +39,21 @@ type NetworkPurgeOpts struct { // ProjectPurgeAll purges all the resources associated with a project. // This includes: servers, snapshosts, volumes, floating IPs, routers, networks, sub-networks and security groups -func ProjectPurgeAll(projectID string, purgeOpts ProjectPurgeOpts) (err error) { +func ProjectPurgeAll(ctx context.Context, projectID string, purgeOpts ProjectPurgeOpts) (err error) { if purgeOpts.ComputePurgeOpts != nil { - err = ProjectPurgeCompute(projectID, *purgeOpts.ComputePurgeOpts) + err = ProjectPurgeCompute(ctx, projectID, *purgeOpts.ComputePurgeOpts) if err != nil { return err } } if purgeOpts.StoragePurgeOpts != nil { - err = ProjectPurgeStorage(projectID, *purgeOpts.StoragePurgeOpts) + err = ProjectPurgeStorage(ctx, projectID, *purgeOpts.StoragePurgeOpts) if err != nil { return err } } if purgeOpts.NetworkPurgeOpts != nil { - err = ProjectPurgeNetwork(projectID, *purgeOpts.NetworkPurgeOpts) + err = ProjectPurgeNetwork(ctx, projectID, *purgeOpts.NetworkPurgeOpts) if err != nil { return err } @@ -62,14 +63,14 @@ func ProjectPurgeAll(projectID string, purgeOpts ProjectPurgeOpts) (err error) { // ProjectPurgeCompute purges the Compute v2 resources associated with a project. // This includes: servers -func ProjectPurgeCompute(projectID string, purgeOpts ComputePurgeOpts) (err error) { +func ProjectPurgeCompute(ctx context.Context, projectID string, purgeOpts ComputePurgeOpts) (err error) { // Delete servers listOpts := servers.ListOpts{ AllTenants: true, TenantID: projectID, } - allPages, err := servers.List(purgeOpts.Client, listOpts).AllPages() + allPages, err := servers.List(purgeOpts.Client, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding servers for project: " + projectID) } @@ -81,7 +82,7 @@ func ProjectPurgeCompute(projectID string, purgeOpts ComputePurgeOpts) (err erro if len(allServers) > 0 { for _, server := range allServers { - err = servers.Delete(purgeOpts.Client, server.ID).ExtractErr() + err = servers.Delete(ctx, purgeOpts.Client, server.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting server: " + server.Name + " from project: " + projectID) } @@ -93,14 +94,14 @@ func ProjectPurgeCompute(projectID string, purgeOpts ComputePurgeOpts) (err erro // ProjectPurgeStorage purges the Blockstorage v3 resources associated with a project. // This includes: snapshosts and volumes -func ProjectPurgeStorage(projectID string, purgeOpts StoragePurgeOpts) (err error) { +func ProjectPurgeStorage(ctx context.Context, projectID string, purgeOpts StoragePurgeOpts) (err error) { // Delete snapshots - err = clearBlockStorageSnaphosts(projectID, purgeOpts.Client) + err = clearBlockStorageSnaphosts(ctx, projectID, purgeOpts.Client) if err != nil { return err } // Delete volumes - err = clearBlockStorageVolumes(projectID, purgeOpts.Client) + err = clearBlockStorageVolumes(ctx, projectID, purgeOpts.Client) if err != nil { return err } @@ -110,29 +111,29 @@ func ProjectPurgeStorage(projectID string, purgeOpts StoragePurgeOpts) (err erro // ProjectPurgeNetwork purges the Networking v2 resources associated with a project. // This includes: floating IPs, routers, networks, sub-networks and security groups -func ProjectPurgeNetwork(projectID string, purgeOpts NetworkPurgeOpts) (err error) { +func ProjectPurgeNetwork(ctx context.Context, projectID string, purgeOpts NetworkPurgeOpts) (err error) { // Delete floating IPs - err = clearNetworkingFloatingIPs(projectID, purgeOpts.Client) + err = clearNetworkingFloatingIPs(ctx, projectID, purgeOpts.Client) if err != nil { return err } // Delete ports - err = clearNetworkingPorts(projectID, purgeOpts.Client) + err = clearNetworkingPorts(ctx, projectID, purgeOpts.Client) if err != nil { return err } // Delete routers - err = clearNetworkingRouters(projectID, purgeOpts.Client) + err = clearNetworkingRouters(ctx, projectID, purgeOpts.Client) if err != nil { return err } // Delete networks - err = clearNetworkingNetworks(projectID, purgeOpts.Client) + err = clearNetworkingNetworks(ctx, projectID, purgeOpts.Client) if err != nil { return err } // Delete security groups - err = clearNetworkingSecurityGroups(projectID, purgeOpts.Client) + err = clearNetworkingSecurityGroups(ctx, projectID, purgeOpts.Client) if err != nil { return err } @@ -140,12 +141,12 @@ func ProjectPurgeNetwork(projectID string, purgeOpts NetworkPurgeOpts) (err erro return nil } -func clearBlockStorageVolumes(projectID string, storageClient *gophercloud.ServiceClient) error { +func clearBlockStorageVolumes(ctx context.Context, projectID string, storageClient *gophercloud.ServiceClient) error { listOpts := volumes.ListOpts{ AllTenants: true, TenantID: projectID, } - allPages, err := volumes.List(storageClient, listOpts).AllPages() + allPages, err := volumes.List(storageClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding volumes for project: " + projectID) } @@ -158,7 +159,7 @@ func clearBlockStorageVolumes(projectID string, storageClient *gophercloud.Servi Cascade: true, } for _, volume := range allVolumes { - err = volumes.Delete(storageClient, volume.ID, deleteOpts).ExtractErr() + err = volumes.Delete(ctx, storageClient, volume.ID, deleteOpts).ExtractErr() if err != nil { return fmt.Errorf("Error deleting volume: " + volume.Name + " from project: " + projectID) } @@ -168,12 +169,12 @@ func clearBlockStorageVolumes(projectID string, storageClient *gophercloud.Servi return err } -func clearBlockStorageSnaphosts(projectID string, storageClient *gophercloud.ServiceClient) error { +func clearBlockStorageSnaphosts(ctx context.Context, projectID string, storageClient *gophercloud.ServiceClient) error { listOpts := snapshots.ListOpts{ AllTenants: true, TenantID: projectID, } - allPages, err := snapshots.List(storageClient, listOpts).AllPages() + allPages, err := snapshots.List(storageClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding snapshots for project: " + projectID) } @@ -183,7 +184,7 @@ func clearBlockStorageSnaphosts(projectID string, storageClient *gophercloud.Ser } if len(allSnapshots) > 0 { for _, snaphost := range allSnapshots { - err = snapshots.Delete(storageClient, snaphost.ID).ExtractErr() + err = snapshots.Delete(ctx, storageClient, snaphost.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting snaphost: " + snaphost.Name + " from project: " + projectID) } @@ -192,8 +193,8 @@ func clearBlockStorageSnaphosts(projectID string, storageClient *gophercloud.Ser return nil } -func clearPortforwarding(networkClient *gophercloud.ServiceClient, fipID string, projectID string) error { - allPages, err := portforwarding.List(networkClient, portforwarding.ListOpts{}, fipID).AllPages() +func clearPortforwarding(ctx context.Context, networkClient *gophercloud.ServiceClient, fipID string, projectID string) error { + allPages, err := portforwarding.List(networkClient, portforwarding.ListOpts{}, fipID).AllPages(ctx) if err != nil { return err } @@ -204,7 +205,7 @@ func clearPortforwarding(networkClient *gophercloud.ServiceClient, fipID string, } for _, pf := range allPFs { - err := portforwarding.Delete(networkClient, fipID, pf.ID).ExtractErr() + err := portforwarding.Delete(ctx, networkClient, fipID, pf.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting floating IP port forwarding: " + pf.ID + " from project: " + projectID) } @@ -213,11 +214,11 @@ func clearPortforwarding(networkClient *gophercloud.ServiceClient, fipID string, return nil } -func clearNetworkingFloatingIPs(projectID string, networkClient *gophercloud.ServiceClient) error { +func clearNetworkingFloatingIPs(ctx context.Context, projectID string, networkClient *gophercloud.ServiceClient) error { listOpts := floatingips.ListOpts{ TenantID: projectID, } - allPages, err := floatingips.List(networkClient, listOpts).AllPages() + allPages, err := floatingips.List(networkClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding floating IPs for project: " + projectID) } @@ -228,12 +229,12 @@ func clearNetworkingFloatingIPs(projectID string, networkClient *gophercloud.Ser if len(allFloatings) > 0 { for _, floating := range allFloatings { // Clear all portforwarding settings otherwise the floating IP can't be deleted - err = clearPortforwarding(networkClient, floating.ID, projectID) + err = clearPortforwarding(ctx, networkClient, floating.ID, projectID) if err != nil { return err } - err = floatingips.Delete(networkClient, floating.ID).ExtractErr() + err = floatingips.Delete(ctx, networkClient, floating.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting floating IP: " + floating.ID + " from project: " + projectID) } @@ -243,12 +244,12 @@ func clearNetworkingFloatingIPs(projectID string, networkClient *gophercloud.Ser return nil } -func clearNetworkingPorts(projectID string, networkClient *gophercloud.ServiceClient) error { +func clearNetworkingPorts(ctx context.Context, projectID string, networkClient *gophercloud.ServiceClient) error { listOpts := ports.ListOpts{ TenantID: projectID, } - allPages, err := ports.List(networkClient, listOpts).AllPages() + allPages, err := ports.List(networkClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding ports for project: " + projectID) } @@ -262,7 +263,7 @@ func clearNetworkingPorts(projectID string, networkClient *gophercloud.ServiceCl continue } - err = ports.Delete(networkClient, port.ID).ExtractErr() + err = ports.Delete(ctx, networkClient, port.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting port: " + port.ID + " from project: " + projectID) } @@ -273,13 +274,13 @@ func clearNetworkingPorts(projectID string, networkClient *gophercloud.ServiceCl } // We need all subnets to disassociate the router from the subnet -func getAllSubnets(projectID string, networkClient *gophercloud.ServiceClient) ([]string, error) { +func getAllSubnets(ctx context.Context, projectID string, networkClient *gophercloud.ServiceClient) ([]string, error) { subnets := make([]string, 0) listOpts := networks.ListOpts{ TenantID: projectID, } - allPages, err := networks.List(networkClient, listOpts).AllPages() + allPages, err := networks.List(networkClient, listOpts).AllPages(ctx) if err != nil { return subnets, fmt.Errorf("Error finding networks for project: " + projectID) } @@ -296,13 +297,13 @@ func getAllSubnets(projectID string, networkClient *gophercloud.ServiceClient) ( return subnets, nil } -func clearAllRouterInterfaces(projectID string, routerID string, subnets []string, networkClient *gophercloud.ServiceClient) error { +func clearAllRouterInterfaces(ctx context.Context, projectID string, routerID string, subnets []string, networkClient *gophercloud.ServiceClient) error { for _, subnet := range subnets { intOpts := routers.RemoveInterfaceOpts{ SubnetID: subnet, } - _, err := routers.RemoveInterface(networkClient, routerID, intOpts).Extract() + _, err := routers.RemoveInterface(ctx, networkClient, routerID, intOpts).Extract() if err != nil { return err } @@ -311,11 +312,11 @@ func clearAllRouterInterfaces(projectID string, routerID string, subnets []strin return nil } -func clearNetworkingRouters(projectID string, networkClient *gophercloud.ServiceClient) error { +func clearNetworkingRouters(ctx context.Context, projectID string, networkClient *gophercloud.ServiceClient) error { listOpts := routers.ListOpts{ TenantID: projectID, } - allPages, err := routers.List(networkClient, listOpts).AllPages() + allPages, err := routers.List(networkClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding routers for project: " + projectID) } @@ -324,14 +325,14 @@ func clearNetworkingRouters(projectID string, networkClient *gophercloud.Service return fmt.Errorf("Error extracting routers for project: " + projectID) } - subnets, err := getAllSubnets(projectID, networkClient) + subnets, err := getAllSubnets(ctx, projectID, networkClient) if err != nil { return fmt.Errorf("Error fetching subnets project: " + projectID) } if len(allRouters) > 0 { for _, router := range allRouters { - err = clearAllRouterInterfaces(projectID, router.ID, subnets, networkClient) + err = clearAllRouterInterfaces(ctx, projectID, router.ID, subnets, networkClient) if err != nil { return err } @@ -342,12 +343,12 @@ func clearNetworkingRouters(projectID string, networkClient *gophercloud.Service Routes: &routes, } - _, err := routers.Update(networkClient, router.ID, updateOpts).Extract() + _, err := routers.Update(ctx, networkClient, router.ID, updateOpts).Extract() if err != nil { return fmt.Errorf("Error deleting router: " + router.Name + " from project: " + projectID) } - err = routers.Delete(networkClient, router.ID).ExtractErr() + err = routers.Delete(ctx, networkClient, router.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting router: " + router.Name + " from project: " + projectID) } @@ -357,12 +358,12 @@ func clearNetworkingRouters(projectID string, networkClient *gophercloud.Service return nil } -func clearNetworkingNetworks(projectID string, networkClient *gophercloud.ServiceClient) error { +func clearNetworkingNetworks(ctx context.Context, projectID string, networkClient *gophercloud.ServiceClient) error { listOpts := networks.ListOpts{ TenantID: projectID, } - allPages, err := networks.List(networkClient, listOpts).AllPages() + allPages, err := networks.List(networkClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding networks for project: " + projectID) } @@ -372,7 +373,7 @@ func clearNetworkingNetworks(projectID string, networkClient *gophercloud.Servic } if len(allNetworks) > 0 { for _, network := range allNetworks { - err = networks.Delete(networkClient, network.ID).ExtractErr() + err = networks.Delete(ctx, networkClient, network.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting network: " + network.Name + " from project: " + projectID) } @@ -382,11 +383,11 @@ func clearNetworkingNetworks(projectID string, networkClient *gophercloud.Servic return nil } -func clearNetworkingSecurityGroups(projectID string, networkClient *gophercloud.ServiceClient) error { +func clearNetworkingSecurityGroups(ctx context.Context, projectID string, networkClient *gophercloud.ServiceClient) error { listOpts := groups.ListOpts{ TenantID: projectID, } - allPages, err := groups.List(networkClient, listOpts).AllPages() + allPages, err := groups.List(networkClient, listOpts).AllPages(ctx) if err != nil { return fmt.Errorf("Error finding security groups for project: " + projectID) } @@ -396,7 +397,7 @@ func clearNetworkingSecurityGroups(projectID string, networkClient *gophercloud. } if len(allSecGroups) > 0 { for _, group := range allSecGroups { - err = groups.Delete(networkClient, group.ID).ExtractErr() + err = groups.Delete(ctx, networkClient, group.ID).ExtractErr() if err != nil { return fmt.Errorf("Error deleting security group: " + group.Name + " from project: " + projectID) } diff --git a/openstack/imageservice/v2/images/utils.go b/openstack/imageservice/v2/images/utils.go index a84ae936..f73950b1 100644 --- a/openstack/imageservice/v2/images/utils.go +++ b/openstack/imageservice/v2/images/utils.go @@ -1,14 +1,16 @@ package images import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/imageservice/v2/images" ) // IDFromName is a convenience function that returns an image's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := images.List(client, images.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/networking/v2/extensions/security/groups/utils.go b/openstack/networking/v2/extensions/security/groups/utils.go index cfac5387..fbade3e8 100644 --- a/openstack/networking/v2/extensions/security/groups/utils.go +++ b/openstack/networking/v2/extensions/security/groups/utils.go @@ -1,13 +1,15 @@ package groups import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/groups" ) // IDFromName is a convenience function that returns a security group's ID, // given its name. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" @@ -15,7 +17,7 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) Name: name, } - pages, err := groups.List(client, listOpts).AllPages() + pages, err := groups.List(client, listOpts).AllPages(ctx) if err != nil { return "", err } diff --git a/openstack/networking/v2/networks/utils.go b/openstack/networking/v2/networks/utils.go index fd1bb05c..c39a5948 100644 --- a/openstack/networking/v2/networks/utils.go +++ b/openstack/networking/v2/networks/utils.go @@ -1,14 +1,16 @@ package networks import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks" ) // IDFromName is a convenience function that returns a network's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := networks.List(client, networks.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/networking/v2/ports/utils.go b/openstack/networking/v2/ports/utils.go index 0910d42f..ccaa6b8e 100644 --- a/openstack/networking/v2/ports/utils.go +++ b/openstack/networking/v2/ports/utils.go @@ -1,14 +1,16 @@ package ports import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports" ) // IDFromName is a convenience function that returns a port's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := ports.List(client, ports.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/networking/v2/subnets/utils.go b/openstack/networking/v2/subnets/utils.go index 1a488cc5..fbf3878d 100644 --- a/openstack/networking/v2/subnets/utils.go +++ b/openstack/networking/v2/subnets/utils.go @@ -1,14 +1,16 @@ package subnets import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets" ) // IDFromName is a convenience function that returns a subnet's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := subnets.List(client, subnets.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/objectstorage/v1/objects/download.go b/openstack/objectstorage/v1/objects/download.go index b54ca939..30b200de 100644 --- a/openstack/objectstorage/v1/objects/download.go +++ b/openstack/objectstorage/v1/objects/download.go @@ -1,6 +1,7 @@ package objects import ( + "context" "fmt" "io" "os" @@ -50,7 +51,7 @@ type DownloadOpts struct { // It is roughly based on the python-swiftclient implementation: // // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1024 -func Download(client *gophercloud.ServiceClient, containerName string, objectNames []string, opts *DownloadOpts) ([]DownloadResult, error) { +func Download(ctx context.Context, client *gophercloud.ServiceClient, containerName string, objectNames []string, opts *DownloadOpts) ([]DownloadResult, error) { var downloadResults []DownloadResult if strings.Contains(containerName, "/") { @@ -65,14 +66,14 @@ func Download(client *gophercloud.ServiceClient, containerName string, objectNam Prefix: opts.Prefix, } - err := containers.List(client, listOpts).EachPage(func(page pagination.Page) (bool, error) { + err := containers.List(client, listOpts).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) { containerList, err := containers.ExtractInfo(page) if err != nil { return false, fmt.Errorf("error listing containers: %s", err) } for _, c := range containerList { - results, err := downloadContainer(client, c.Name, opts) + results, err := downloadContainer(ctx, client, c.Name, opts) if err != nil { return false, fmt.Errorf("error downloading container %s: %s", c.Name, err) } @@ -92,7 +93,7 @@ func Download(client *gophercloud.ServiceClient, containerName string, objectNam } if len(objectNames) == 0 { - results, err := downloadContainer(client, containerName, opts) + results, err := downloadContainer(ctx, client, containerName, opts) if err != nil { return nil, fmt.Errorf("error downloading container %s: %s", containerName, err) } @@ -102,7 +103,7 @@ func Download(client *gophercloud.ServiceClient, containerName string, objectNam } for _, objectName := range objectNames { - result, err := downloadObject(client, containerName, objectName, opts) + result, err := downloadObject(ctx, client, containerName, objectName, opts) if err != nil { return nil, fmt.Errorf("error downloading object %s/%s: %s", containerName, objectName, err) } @@ -113,12 +114,12 @@ func Download(client *gophercloud.ServiceClient, containerName string, objectNam } // downloadObject will download a specified object. -func downloadObject(client *gophercloud.ServiceClient, containerName string, objectName string, opts *DownloadOpts) (*DownloadResult, error) { +func downloadObject(ctx context.Context, client *gophercloud.ServiceClient, containerName string, objectName string, opts *DownloadOpts) (*DownloadResult, error) { var objectDownloadOpts objects.DownloadOpts var pseudoDir bool // Perform a get on the object in order to get its metadata. - originalObject := objects.Get(client, containerName, objectName, nil) + originalObject := objects.Get(ctx, client, containerName, objectName, nil) if originalObject.Err != nil { return nil, fmt.Errorf("error retrieving object %s/%s: %s", containerName, objectName, originalObject.Err) } @@ -165,7 +166,7 @@ func downloadObject(client *gophercloud.ServiceClient, containerName string, obj } // Attempt to download the object - res := objects.Download(client, containerName, objectName, objectDownloadOpts) + res := objects.Download(ctx, client, containerName, objectName, objectDownloadOpts) if res.Err != nil { // Ignore the error if SkipIdentical is set. // This is because a second attempt to download the object will happen later. @@ -208,7 +209,7 @@ func downloadObject(client *gophercloud.ServiceClient, containerName string, obj StaticLargeObject: headers.StaticLargeObject, } - manifestData, err := GetManifest(client, mo) + manifestData, err := GetManifest(ctx, client, mo) if err != nil { return nil, fmt.Errorf("unable to get manifest for %s/%s: %s", containerName, objectName, err) } @@ -234,7 +235,7 @@ func downloadObject(client *gophercloud.ServiceClient, containerName string, obj // This is a Large object objectDownloadOpts.MultipartManifest = "" - res = objects.Download(client, containerName, objectName, objectDownloadOpts) + res = objects.Download(ctx, client, containerName, objectName, objectDownloadOpts) if res.Err != nil { return nil, fmt.Errorf("error downloading object %s/%s: %s", containerName, objectName, err) } @@ -348,21 +349,21 @@ func downloadObject(client *gophercloud.ServiceClient, containerName string, obj } // downloadContainer will download all objects in a given container. -func downloadContainer(client *gophercloud.ServiceClient, containerName string, opts *DownloadOpts) ([]DownloadResult, error) { +func downloadContainer(ctx context.Context, client *gophercloud.ServiceClient, containerName string, opts *DownloadOpts) ([]DownloadResult, error) { listOpts := objects.ListOpts{ Prefix: opts.Prefix, Delimiter: opts.Delimiter, } var downloadResults []DownloadResult - err := objects.List(client, containerName, listOpts).EachPage(func(page pagination.Page) (bool, error) { + err := objects.List(client, containerName, listOpts).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) { objectList, err := objects.ExtractNames(page) if err != nil { return false, fmt.Errorf("error listing container %s: %s", containerName, err) } for _, objectName := range objectList { - result, err := downloadObject(client, containerName, objectName, opts) + result, err := downloadObject(ctx, client, containerName, objectName, opts) if err != nil { return false, fmt.Errorf("error downloading object %s/%s: %s", containerName, objectName, err) } diff --git a/openstack/objectstorage/v1/objects/manifest.go b/openstack/objectstorage/v1/objects/manifest.go index 9bc10a5e..8c6cf411 100644 --- a/openstack/objectstorage/v1/objects/manifest.go +++ b/openstack/objectstorage/v1/objects/manifest.go @@ -2,6 +2,7 @@ package objects import ( "bufio" + "context" "crypto/md5" "encoding/json" "fmt" @@ -60,7 +61,7 @@ type GetManifestOpts struct { } // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1916 -func GetManifest(client *gophercloud.ServiceClient, opts GetManifestOpts) ([]Manifest, error) { +func GetManifest(ctx context.Context, client *gophercloud.ServiceClient, opts GetManifestOpts) ([]Manifest, error) { var manifest []Manifest // TODO: test this @@ -77,7 +78,7 @@ func GetManifest(client *gophercloud.ServiceClient, opts GetManifestOpts) ([]Man Prefix: sPrefix, } - allPages, err := objects.List(client, sContainer, listOpts).AllPages() + allPages, err := objects.List(client, sContainer, listOpts).AllPages(ctx) if err != nil { return nil, fmt.Errorf("unable to list %s: %s", sContainer, err) } @@ -88,7 +89,7 @@ func GetManifest(client *gophercloud.ServiceClient, opts GetManifestOpts) ([]Man } for _, obj := range allObjects { - objInfo, err := objects.Get(client, sContainer, obj, nil).Extract() + objInfo, err := objects.Get(ctx, client, sContainer, obj, nil).Extract() if err != nil { return nil, fmt.Errorf("unable to get object %s:%s: %s", sContainer, obj, err) } @@ -112,7 +113,7 @@ func GetManifest(client *gophercloud.ServiceClient, opts GetManifestOpts) ([]Man downloadOpts := objects.DownloadOpts{ MultipartManifest: "get", } - res := objects.Download(client, opts.ContainerName, opts.ObjectName, downloadOpts) + res := objects.Download(ctx, client, opts.ContainerName, opts.ObjectName, downloadOpts) if res.Err != nil { return nil, res.Err } diff --git a/openstack/objectstorage/v1/objects/testing/manifest_test.go b/openstack/objectstorage/v1/objects/testing/manifest_test.go index 7c892bd3..dde5f569 100644 --- a/openstack/objectstorage/v1/objects/testing/manifest_test.go +++ b/openstack/objectstorage/v1/objects/testing/manifest_test.go @@ -1,6 +1,7 @@ package testing import ( + "context" "testing" o "github.com/gophercloud/gophercloud/v2/openstack/objectstorage/v1/objects" @@ -49,7 +50,7 @@ func TestChunkData(t *testing.T) { MultipartManifest: "get", } - res := o.Download(fake.ServiceClient(), "testContainer", "testObject", downloadOpts) + res := o.Download(context.TODO(), fake.ServiceClient(), "testContainer", "testObject", downloadOpts) defer res.Body.Close() th.AssertNoErr(t, res.Err) @@ -66,7 +67,7 @@ func TestChunkData(t *testing.T) { StaticLargeObject: true, } - actualChunkData, err := objects.GetManifest(fake.ServiceClient(), gmo) + actualChunkData, err := objects.GetManifest(context.TODO(), fake.ServiceClient(), gmo) th.AssertNoErr(t, err) th.AssertDeepEquals(t, actualChunkData, expectedMultipartManifest) } diff --git a/openstack/objectstorage/v1/objects/upload.go b/openstack/objectstorage/v1/objects/upload.go index 1d972bb1..15ce4675 100644 --- a/openstack/objectstorage/v1/objects/upload.go +++ b/openstack/objectstorage/v1/objects/upload.go @@ -2,6 +2,7 @@ package objects import ( "bytes" + "context" "crypto/md5" "encoding/json" "fmt" @@ -117,7 +118,7 @@ type sloManifest struct { // Upload uploads a single object to swift. // // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1371 -func Upload(client *gophercloud.ServiceClient, containerName, objectName string, opts *UploadOpts) (*UploadResult, error) { +func Upload(ctx context.Context, client *gophercloud.ServiceClient, containerName, objectName string, opts *UploadOpts) (*UploadResult, error) { var sourceFileInfo os.FileInfo origObject := new(originalObject) @@ -145,10 +146,10 @@ func Upload(client *gophercloud.ServiceClient, containerName, objectName string, // Try to create the container, but ignore any errors. // TODO: add X-Storage-Policy to Gophercloud. // If a storage policy was specified, create the container with that policy. - containers.Create(client, containerName, nil) + containers.Create(ctx, client, containerName, nil) // Check and see if the object being requested already exists. - objectResult := objects.Get(client, containerName, objectName, nil) + objectResult := objects.Get(ctx, client, containerName, objectName, nil) if objectResult.Err != nil { if _, ok := objectResult.Err.(gophercloud.ErrDefault404); ok { origObject = nil @@ -206,7 +207,7 @@ func Upload(client *gophercloud.ServiceClient, containerName, objectName string, // TODO: add X-Storage-Policy to Gophercloud. // Create the segment container in either the specified policy or the same // policy as the above container. - res := containers.Create(client, opts.SegmentContainer, nil) + res := containers.Create(ctx, client, opts.SegmentContainer, nil) if res.Err != nil { return nil, fmt.Errorf("error creating segment container %s: %s", opts.SegmentContainer, res.Err) } @@ -214,7 +215,7 @@ func Upload(client *gophercloud.ServiceClient, containerName, objectName string, // If an io.Reader (streaming) was specified... if opts.Content != nil { - return uploadObject(client, containerName, objectName, opts, origObject, sourceFileInfo) + return uploadObject(ctx, client, containerName, objectName, opts, origObject, sourceFileInfo) } // If a local path was specified... @@ -222,25 +223,26 @@ func Upload(client *gophercloud.ServiceClient, containerName, objectName string, if sourceFileInfo.IsDir() { // If the source path is a directory, then create a Directory Marker, // even if DirMarker wasn't specified. - return createDirMarker(client, containerName, objectName, opts, origObject, sourceFileInfo) + return createDirMarker(ctx, client, containerName, objectName, opts, origObject, sourceFileInfo) } - return uploadObject(client, containerName, objectName, opts, origObject, sourceFileInfo) + return uploadObject(ctx, client, containerName, objectName, opts, origObject, sourceFileInfo) } if opts.DirMarker { - return createDirMarker(client, containerName, objectName, opts, origObject, sourceFileInfo) + return createDirMarker(ctx, client, containerName, objectName, opts, origObject, sourceFileInfo) } // Finally, create an empty object. opts.Content = strings.NewReader("") - return uploadObject(client, containerName, objectName, opts, origObject, sourceFileInfo) + return uploadObject(ctx, client, containerName, objectName, opts, origObject, sourceFileInfo) } // createDirMarker will create a pseudo-directory in Swift. // // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1656 func createDirMarker( + ctx context.Context, client *gophercloud.ServiceClient, containerName string, objectName string, @@ -291,7 +293,7 @@ func createDirMarker( Metadata: opts.Metadata, } - res := objects.Create(client, containerName, objectName, createOpts) + res := objects.Create(ctx, client, containerName, objectName, createOpts) if res.Err != nil { return uploadResult, res.Err } @@ -306,6 +308,7 @@ func createDirMarker( // // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L2006 func uploadObject( + ctx context.Context, client *gophercloud.ServiceClient, containerName string, objectName string, @@ -350,7 +353,7 @@ func uploadObject( StaticLargeObject: origHeaders.StaticLargeObject, } - manifestData, err = GetManifest(client, mo) + manifestData, err = GetManifest(ctx, client, mo) if err != nil { return nil, fmt.Errorf("unable to get manifest for %s/%s: %s", containerName, objectName, err) } @@ -447,7 +450,7 @@ func uploadObject( SegmentStart: segStart, } - result, err := uploadSegment(client, uso) + result, err := uploadSegment(ctx, client, uso) if err != nil { return nil, err } @@ -466,7 +469,7 @@ func uploadObject( Metadata: opts.Metadata, } - err := uploadSLOManifest(client, uploadOpts) + err := uploadSLOManifest(ctx, client, uploadOpts) if err != nil { return nil, err } @@ -494,7 +497,7 @@ func uploadObject( ObjectManifest: newObjectManifest, } - res := objects.Create(client, containerName, objectName, createOpts) + res := objects.Create(ctx, client, containerName, objectName, createOpts) if res.Err != nil { return nil, res.Err } @@ -519,7 +522,7 @@ func uploadObject( SegmentSize: opts.SegmentSize, } - uploadSegmentResult, err := uploadStreamingSegment(client, uso) + uploadSegmentResult, err := uploadStreamingSegment(ctx, client, uso) if err != nil { return nil, fmt.Errorf("error uploading segment %d of %s/%s: %s", segIndex, containerName, objectName, err) } @@ -548,7 +551,7 @@ func uploadObject( Metadata: opts.Metadata, } - err := uploadSLOManifest(client, uploadOpts) + err := uploadSLOManifest(ctx, client, uploadOpts) if err != nil { return nil, fmt.Errorf("error uploading SLO manifest for %s/%s: %s", containerName, objectName, err) } @@ -649,7 +652,7 @@ func uploadObject( NoETag: noETag, } - createHeader, err := objects.Create(client, containerName, objectName, createOpts).Extract() + createHeader, err := objects.Create(ctx, client, containerName, objectName, createOpts).Extract() if err != nil { return nil, err } @@ -676,7 +679,7 @@ func uploadObject( listOpts := objects.ListOpts{ Prefix: sPrefix, } - allPages, err := objects.List(client, sContainer, listOpts).AllPages() + allPages, err := objects.List(client, sContainer, listOpts).AllPages(ctx) if err != nil { return nil, err } @@ -722,7 +725,7 @@ func uploadObject( for sContainer, oldObjects := range delObjectMap { for _, oldObject := range oldObjects { - res := objects.Delete(client, sContainer, oldObject, nil) + res := objects.Delete(ctx, client, sContainer, oldObject, nil) if res.Err != nil { return nil, res.Err } @@ -736,7 +739,7 @@ func uploadObject( } // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1966 -func uploadSLOManifest(client *gophercloud.ServiceClient, opts *uploadSLOManifestOpts) error { +func uploadSLOManifest(ctx context.Context, client *gophercloud.ServiceClient, opts *uploadSLOManifestOpts) error { var manifest []sloManifest for _, result := range opts.Results { m := sloManifest{ @@ -761,7 +764,7 @@ func uploadSLOManifest(client *gophercloud.ServiceClient, opts *uploadSLOManifes NoETag: true, } - res := objects.Create(client, opts.ContainerName, opts.ObjectName, createOpts) + res := objects.Create(ctx, client, opts.ContainerName, opts.ObjectName, createOpts) if res.Err != nil { return res.Err } @@ -770,7 +773,7 @@ func uploadSLOManifest(client *gophercloud.ServiceClient, opts *uploadSLOManifes } // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1719 -func uploadSegment(client *gophercloud.ServiceClient, opts *uploadSegmentOpts) (*uploadSegmentResult, error) { +func uploadSegment(ctx context.Context, client *gophercloud.ServiceClient, opts *uploadSegmentOpts) (*uploadSegmentResult, error) { f, err := os.Open(opts.Path) if err != nil { return nil, err @@ -808,7 +811,7 @@ func uploadSegment(client *gophercloud.ServiceClient, opts *uploadSegmentOpts) ( NoETag: noETag, } - createHeader, err := objects.Create(client, opts.SegmentContainer, opts.SegmentName, createOpts).Extract() + createHeader, err := objects.Create(ctx, client, opts.SegmentContainer, opts.SegmentName, createOpts).Extract() if err != nil { return nil, err } @@ -833,7 +836,7 @@ func uploadSegment(client *gophercloud.ServiceClient, opts *uploadSegmentOpts) ( // uploadStreamingSegment will upload an object segment from a streaming source. // // https://github.com/openstack/python-swiftclient/blob/e65070964c7b1e04119c87e5f344d39358780d18/swiftclient/service.py#L1846 -func uploadStreamingSegment(client *gophercloud.ServiceClient, opts *uploadSegmentOpts) (*uploadSegmentResult, error) { +func uploadStreamingSegment(ctx context.Context, client *gophercloud.ServiceClient, opts *uploadSegmentOpts) (*uploadSegmentResult, error) { var result uploadSegmentResult // Checksum is always done when streaming. @@ -863,14 +866,14 @@ func uploadStreamingSegment(client *gophercloud.ServiceClient, opts *uploadSegme } if opts.SegmentIndex == 0 && n < opts.SegmentSize { - res := objects.Create(client, opts.ContainerName, opts.ObjectName, createOpts) + res := objects.Create(ctx, client, opts.ContainerName, opts.ObjectName, createOpts) if res.Err != nil { return nil, res.Err } result.Location = fmt.Sprintf("/%s/%s", opts.ContainerName, opts.ObjectName) } else { - res := objects.Create(client, opts.SegmentContainer, opts.SegmentName, createOpts) + res := objects.Create(ctx, client, opts.SegmentContainer, opts.SegmentName, createOpts) if res.Err != nil { return nil, res.Err } diff --git a/openstack/sharedfilesystems/v2/shares/utils.go b/openstack/sharedfilesystems/v2/shares/utils.go index cf7934d9..c62d821e 100644 --- a/openstack/sharedfilesystems/v2/shares/utils.go +++ b/openstack/sharedfilesystems/v2/shares/utils.go @@ -1,14 +1,16 @@ package shares import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/sharedfilesystems/v2/shares" ) // IDFromName is a convenience function that returns a share's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := shares.ListDetail(client, shares.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/sharedfilesystems/v2/sharetypes/utils.go b/openstack/sharedfilesystems/v2/sharetypes/utils.go index d6eb4d43..88ac8656 100644 --- a/openstack/sharedfilesystems/v2/sharetypes/utils.go +++ b/openstack/sharedfilesystems/v2/sharetypes/utils.go @@ -1,14 +1,16 @@ package sharetypes import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/sharedfilesystems/v2/sharetypes" ) // IDFromName is a convenience function that returns a share type's ID given // its name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,8 +27,8 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { - pages, err := sharetypes.List(client, nil).AllPages() +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := sharetypes.List(client, nil).AllPages(ctx) if err != nil { return nil, err } diff --git a/openstack/sharedfilesystems/v2/snapshots/utils.go b/openstack/sharedfilesystems/v2/snapshots/utils.go index b40a2247..eb7a65a6 100644 --- a/openstack/sharedfilesystems/v2/snapshots/utils.go +++ b/openstack/sharedfilesystems/v2/snapshots/utils.go @@ -1,14 +1,16 @@ package snapshots import ( + "context" + "github.com/gophercloud/gophercloud/v2" "github.com/gophercloud/gophercloud/v2/openstack/sharedfilesystems/v2/snapshots" ) // IDFromName is a convenience function that returns a network's ID given its // name. Errors when the number of items found is not one. -func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - IDs, err := IDsFromName(client, name) +func IDFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) (string, error) { + IDs, err := IDsFromName(ctx, client, name) if err != nil { return "", err } @@ -25,10 +27,10 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) // IDsFromName returns zero or more IDs corresponding to a name. The returned // error is only non-nil in case of failure. -func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { +func IDsFromName(ctx context.Context, client *gophercloud.ServiceClient, name string) ([]string, error) { pages, err := snapshots.ListDetail(client, snapshots.ListOpts{ Name: name, - }).AllPages() + }).AllPages(ctx) if err != nil { return nil, err } diff --git a/terraform/auth/config.go b/terraform/auth/config.go index b61b58f0..d1207b14 100644 --- a/terraform/auth/config.go +++ b/terraform/auth/config.go @@ -69,7 +69,7 @@ type Config struct { // authenticates to an OpenStack cloud. // // Individual Service Clients are created later in this file. -func (c *Config) LoadAndValidate() error { +func (c *Config) LoadAndValidate(ctx context.Context) error { // Make sure at least one of auth_url or cloud was specified. if c.IdentityEndpoint == "" && c.Cloud == "" { return fmt.Errorf("One of 'auth_url' or 'cloud' must be specified") @@ -211,7 +211,7 @@ func (c *Config) LoadAndValidate() error { } if !c.DelayedAuth && !c.Swauth { - err = openstack.Authenticate(client, *ao) + err = openstack.Authenticate(ctx, client, *ao) if err != nil { return err } @@ -223,7 +223,7 @@ func (c *Config) LoadAndValidate() error { return nil } -func (c *Config) Authenticate() error { +func (c *Config) Authenticate(ctx context.Context) error { if !c.DelayedAuth { return nil } @@ -236,7 +236,7 @@ func (c *Config) Authenticate() error { } if !c.authenticated { - if err := openstack.Authenticate(c.OsClient, *c.AuthOpts); err != nil { + if err := openstack.Authenticate(ctx, c.OsClient, *c.AuthOpts); err != nil { c.authFailed = err return err } @@ -282,8 +282,8 @@ func (c *Config) DetermineRegion(region string) string { type commonCommonServiceClientInitFunc func(*gophercloud.ProviderClient, gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) -func (c *Config) CommonServiceClientInit(newClient commonCommonServiceClientInitFunc, region, service string) (*gophercloud.ServiceClient, error) { - if err := c.Authenticate(); err != nil { +func (c *Config) CommonServiceClientInit(ctx context.Context, newClient commonCommonServiceClientInitFunc, region, service string) (*gophercloud.ServiceClient, error) { + if err := c.Authenticate(ctx); err != nil { return nil, err } @@ -302,36 +302,36 @@ func (c *Config) CommonServiceClientInit(newClient commonCommonServiceClientInit return client, nil } -func (c *Config) BlockStorageV1Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewBlockStorageV1, region, "volume") +func (c *Config) BlockStorageV1Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewBlockStorageV1, region, "volume") } -func (c *Config) BlockStorageV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewBlockStorageV2, region, "volumev2") +func (c *Config) BlockStorageV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewBlockStorageV2, region, "volumev2") } -func (c *Config) BlockStorageV3Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewBlockStorageV3, region, "volumev3") +func (c *Config) BlockStorageV3Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewBlockStorageV3, region, "volumev3") } -func (c *Config) ComputeV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewComputeV2, region, "compute") +func (c *Config) ComputeV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewComputeV2, region, "compute") } -func (c *Config) DNSV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewDNSV2, region, "dns") +func (c *Config) DNSV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewDNSV2, region, "dns") } -func (c *Config) IdentityV3Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewIdentityV3, region, "identity") +func (c *Config) IdentityV3Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewIdentityV3, region, "identity") } -func (c *Config) ImageV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewImageServiceV2, region, "image") +func (c *Config) ImageV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewImageServiceV2, region, "image") } -func (c *Config) MessagingV2Client(region string) (*gophercloud.ServiceClient, error) { - if err := c.Authenticate(); err != nil { +func (c *Config) MessagingV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + if err := c.Authenticate(ctx); err != nil { return nil, err } @@ -350,11 +350,11 @@ func (c *Config) MessagingV2Client(region string) (*gophercloud.ServiceClient, e return client, nil } -func (c *Config) NetworkingV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewNetworkV2, region, "network") +func (c *Config) NetworkingV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewNetworkV2, region, "network") } -func (c *Config) ObjectStorageV1Client(region string) (*gophercloud.ServiceClient, error) { +func (c *Config) ObjectStorageV1Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { var client *gophercloud.ServiceClient var err error @@ -362,7 +362,7 @@ func (c *Config) ObjectStorageV1Client(region string) (*gophercloud.ServiceClien // Otherwise, use a Keystone-based client. if c.Swauth { if !c.DelayedAuth { - client, err = swauth.NewObjectStorageV1(c.OsClient, swauth.AuthOpts{ + client, err = swauth.NewObjectStorageV1(ctx, c.OsClient, swauth.AuthOpts{ User: c.Username, Key: c.Password, }) @@ -378,7 +378,7 @@ func (c *Config) ObjectStorageV1Client(region string) (*gophercloud.ServiceClien } if c.swClient == nil { - c.swClient, err = swauth.NewObjectStorageV1(c.OsClient, swauth.AuthOpts{ + c.swClient, err = swauth.NewObjectStorageV1(ctx, c.OsClient, swauth.AuthOpts{ User: c.Username, Key: c.Password, }) @@ -392,7 +392,7 @@ func (c *Config) ObjectStorageV1Client(region string) (*gophercloud.ServiceClien client = c.swClient } } else { - if err := c.Authenticate(); err != nil { + if err := c.Authenticate(ctx); err != nil { return nil, err } @@ -412,28 +412,28 @@ func (c *Config) ObjectStorageV1Client(region string) (*gophercloud.ServiceClien return client, nil } -func (c *Config) OrchestrationV1Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewOrchestrationV1, region, "orchestration") +func (c *Config) OrchestrationV1Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewOrchestrationV1, region, "orchestration") } -func (c *Config) LoadBalancerV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewLoadBalancerV2, region, "octavia") +func (c *Config) LoadBalancerV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewLoadBalancerV2, region, "octavia") } -func (c *Config) DatabaseV1Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewDBV1, region, "database") +func (c *Config) DatabaseV1Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewDBV1, region, "database") } -func (c *Config) ContainerInfraV1Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewContainerInfraV1, region, "container-infra") +func (c *Config) ContainerInfraV1Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewContainerInfraV1, region, "container-infra") } -func (c *Config) SharedfilesystemV2Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewSharedFileSystemV2, region, "sharev2") +func (c *Config) SharedfilesystemV2Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewSharedFileSystemV2, region, "sharev2") } -func (c *Config) KeyManagerV1Client(region string) (*gophercloud.ServiceClient, error) { - return c.CommonServiceClientInit(openstack.NewKeyManagerV1, region, "key-manager") +func (c *Config) KeyManagerV1Client(ctx context.Context, region string) (*gophercloud.ServiceClient, error) { + return c.CommonServiceClientInit(ctx, openstack.NewKeyManagerV1, region, "key-manager") } // A wrapper to determine if logging in gophercloud should be enabled, with a fallback