Skip to content

Commit 831abf4

Browse files
authored
Add X-Auth-Token support (#164)
* Add X-Auth-Token support - removed old Resell client - removed old QuotaManager client - created one client (all in one) that includes clients Resell and QuotaManager - new client uses X-Auth-Token instead of X-Token - updated Go version (1.14 -> 1.20) - Resell URL is taken from the identity service - added support auth with user from another domain (different from scope) - removed context for every request, because gophercloud doesn't support it
1 parent 0bfd18e commit 831abf4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1712
-1793
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v3
1515
with:
16-
go-version: '1.14'
16+
go-version: '1.20'
1717

1818
- name: golangci-lint
1919
uses: golangci/golangci-lint-action@v3
2020
with:
21-
version: v1.49.0
21+
version: v1.53.3

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v3
1515
with:
16-
go-version: '1.14'
16+
go-version: '1.20'
1717

1818
- name: Run test
1919
run: go test -v ./...

.golangci.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ linters:
1010
- dogsled
1111
- errcheck
1212
- exportloopref
13-
# - gci
13+
- gci
1414
- gocognit
1515
- goconst
1616
- gocritic
@@ -32,7 +32,7 @@ linters:
3232
- misspell
3333
- nakedret
3434
- noctx
35-
# - nolintlint
35+
- nolintlint
3636
- prealloc
3737
- rowserrcheck
3838
- scopelint
@@ -45,3 +45,17 @@ linters:
4545
- unused
4646
- varcheck
4747
- whitespace
48+
49+
linters-settings:
50+
depguard:
51+
rules:
52+
main:
53+
allow:
54+
- $gostd
55+
- github.com/selectel/go-selvpcclient
56+
- github.com/gophercloud/gophercloud
57+
- github.com/google/go-querystring
58+
goimports:
59+
local-prefixes: github.com/selectel/go-selvpcclient
60+
gci:
61+
local-prefixes: github.com/selectel/go-selvpcclient

README.md

Lines changed: 41 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
# go-selvpcclient: a Go library for the Selectel VPC API
2-
[![GoDoc](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient?status.svg)](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient)
3-
[![Go Report Card](https://goreportcard.com/badge/github.com/selectel/go-selvpcclient)](https://goreportcard.com/report/github.com/selectel/go-selvpcclient)
4-
[![Build Status](https://travis-ci.org/selectel/go-selvpcclient.svg?branch=master)](https://travis-ci.org/selectel/go-selvpcclient)
2+
[![GoDoc](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient?status.svg)](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient)
3+
[![Go Report Card](https://goreportcard.com/badge/github.com/selectel/go-selvpcclient/v3)](https://goreportcard.com/report/github.com/selectel/go-selvpcclient/v3)
54
[![Coverage Status](https://coveralls.io/repos/github/selectel/go-selvpcclient/badge.svg?branch=master)](https://coveralls.io/github/selectel/go-selvpcclient?branch=master)
65

7-
Package go-selvpcclient provides a Go library to work with the Selectel VPC API.
6+
Package go-selvpcclient provides a Go library to work with the Selectel API:
7+
- [Cloud Management API](https://developers.selectel.ru/docs/selectel-cloud-platform/main-services/selectel_cloud_management_api/)
8+
- [Cloud Quota Management API ](https://developers.selectel.ru/docs/selectel-cloud-platform/main-services/cloud-quota-management/)
89

910
## Documentation
1011

11-
The Go library documentation is available at [godoc.org](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient).
12-
13-
The API usage examples are available at [knowledge base](https://kb.selectel.com/24381383.html).
14-
API documentation is also available at the [VPC page](https://my.selectel.ru/vpc/docs) (if you've created an account on the [registration page](https://my.selectel.ru/registration)).
12+
The Go library documentation is available at [godoc.org](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient).
1513

1614
## What this library is capable of
1715

18-
You can use this library to work with the following objects of the Selectel VPC API:
19-
20-
* [capabilities](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/capabilities)
21-
* [floating ips](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/floatingips)
22-
* [keypairs](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/keypairs)
23-
* [licenses](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/licenses)
24-
* [projects](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/projects)
25-
* [quotas](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager/quotas)
26-
* [roles](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/roles)
27-
* [subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/subnets)
28-
* [tokens](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/tokens)
29-
* [traffic](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/traffic)
30-
* [users](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/users)
31-
* [vrrp subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/vrrpsubnets)
16+
You can use this library to work with the following objects of the
17+
[Cloud Management API](https://developers.selectel.ru/docs/selectel-cloud-platform/main-services/selectel_cloud_management_api/) and
18+
[Cloud Quota Management API](https://developers.selectel.ru/docs/selectel-cloud-platform/main-services/cloud-quota-management/).
19+
20+
Cloud Management API:
21+
* [capabilities](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/capabilities)
22+
* [floating ips](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/floatingips)
23+
* [keypairs](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/keypairs)
24+
* [licenses](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/licenses)
25+
* [projects](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/projects)
26+
* [roles](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/roles)
27+
* [subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/subnets)
28+
* [tokens](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/tokens)
29+
* [traffic](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/traffic)
30+
* [users](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/users)
31+
* [vrrp subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/vrrpsubnets)
32+
33+
Cloud Quota Management API:
34+
* [quotas](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/quotamanager/quotas)
3235

3336
Selectel VPC Cloud is based on the [OpenStack](https://www.openstack.org), so you don't need this library to work with actual servers, volumes, networks, etc.
3437
You can use the [Gophercloud](https://github.com/gophercloud/gophercloud) project to work with the OpenStack objects.
@@ -40,15 +43,15 @@ You can use the [Gophercloud](https://github.com/gophercloud/gophercloud) projec
4043
You can install `go-selvpcclient` as a Go package:
4144

4245
```bash
43-
go get github.com/selectel/go-selvpcclient/selvpcclient/v2
46+
go get github.com/selectel/go-selvpcclient/selvpcclient/v3
4447
```
4548

4649
### Authentication
4750

4851
To work with the Selectel VPC API you first need to:
4952

5053
* create a Selectel account: [registration page](https://my.selectel.ru/registration)
51-
* obtain an API token: [api keys](http://my.selectel.ru/profile/apikeys)
54+
* create the service user: [users and roles](https://docs.selectel.ru/control-panel-actions/users-and-roles)
5255

5356
### Usage example
5457

@@ -60,115 +63,34 @@ import (
6063
"fmt"
6164
"log"
6265

63-
resell "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2"
64-
"github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/projects"
65-
)
66-
67-
// API token from the https://my.selectel.ru.
68-
var token = "token_key"
69-
70-
func main() {
71-
// Initialize the Resell V2 client.
72-
resellClient := resell.NewV2ResellClient(token)
73-
74-
// Get and print all projects.
75-
ctx := context.Background()
76-
allProjects, _, err := projects.List(ctx, resellClient)
77-
if err != nil {
78-
log.Fatal(err)
79-
}
80-
for _, myProject := range allProjects {
81-
fmt.Println(myProject)
82-
}
83-
}
84-
```
85-
86-
### Quota usage example
87-
88-
```go
89-
package main
90-
91-
import (
92-
"context"
93-
"fmt"
94-
"log"
95-
96-
"github.com/selectel/go-selvpcclient/v2/selvpcclient"
97-
"github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager"
98-
"github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager/quotas"
99-
resell "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2"
100-
reselTokens "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/tokens"
101-
)
102-
103-
// token from the https://my.selectel.ru.
104-
var (
105-
token = "token_key"
106-
accountName = "account_name"
107-
projectID = "project_uuid"
108-
region = "region_name"
109-
ramQuotaZone = "zone_name"
110-
ramQuotaValue = 123
66+
"github.com/selectel/go-selvpcclient/v3/selvpcclient"
67+
"github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/projects"
11168
)
11269

11370
func main() {
114-
// Init resell client with API token.
115-
resellClient := resell.NewV2ResellClient(token)
11671
ctx := context.Background()
117-
118-
APIToken, _, err := reselTokens.Create(ctx, resellClient, reselTokens.TokenOpts{
119-
AccountName: accountName,
120-
})
121-
if err != nil {
122-
log.Fatal(err)
123-
}
124-
125-
// Init Identity and Quota Manager.
126-
client := resell.NewOpenstackClient(APIToken.ID)
127-
identity := quotamanager.NewIdentityManager(resellClient, client, accountName)
128-
quotaMgr := quotamanager.NewQuotaRegionalClient(selvpcclient.NewHTTPClient(), identity)
129-
130-
// Get limits for project <projectID> in region <region>.
131-
limits, _, err := quotas.GetLimits(ctx, quotaMgr, projectID, region)
132-
if err != nil {
133-
log.Fatal(err)
134-
}
13572

136-
for _, limit := range limits {
137-
fmt.Println(limit.Name, limit.ResourceQuotasEntities)
73+
options := &selvpcclient.ClientOptions{
74+
Context: ctx,
75+
DomainName: "999999",
76+
Username: "admin",
77+
Password: "m1-sup3r-p@ssw0rd-p3w-p3w",
13878
}
13979

140-
// Get quotas for project <projectID> in region <region>.
141-
quotasData, _, err := quotas.GetProjectQuotas(ctx, quotaMgr, projectID, region)
80+
client, err := selvpcclient.NewClient(options)
14281
if err != nil {
14382
log.Fatal(err)
14483
}
14584

146-
for _, quota := range quotasData {
147-
fmt.Println(quota.Name, quota.ResourceQuotasEntities)
148-
}
149-
150-
// Update quotas for project <projectID> in region <region>.
151-
UpdateQuotasOpts := quotas.UpdateProjectQuotasOpts{
152-
QuotasOpts: []quotas.QuotaOpts{
153-
{
154-
Name: "compute_cores",
155-
ResourceQuotasOpts: []quotas.ResourceQuotaOpts{
156-
{
157-
Zone: &ramQuotaZone,
158-
Value: &ramQuotaValue,
159-
},
160-
},
161-
},
162-
},
163-
}
164-
updatedQuotasData, _, err := quotas.UpdateProjectQuotas(ctx, quotaMgr,
165-
projectID, region, UpdateQuotasOpts)
85+
result, resp, err := projects.List(client)
16686
if err != nil {
16787
log.Fatal(err)
16888
}
169-
170-
for _, quota := range updatedQuotasData {
171-
fmt.Println(quota.Name, quota.ResourceQuotasEntities)
89+
90+
fmt.Printf("Response StatusCode: %d \n", resp.StatusCode)
91+
92+
for _, project := range result {
93+
fmt.Printf("Project name: %s, enabled: %t \n", project.Name, project.Enabled)
17294
}
17395
}
17496
```

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
module github.com/selectel/go-selvpcclient/v2
1+
module github.com/selectel/go-selvpcclient/v3
22

3-
go 1.14
3+
go 1.20
44

5-
require github.com/gophercloud/gophercloud v1.0.0
5+
require (
6+
github.com/google/go-querystring v1.1.0
7+
github.com/gophercloud/gophercloud v1.5.0
8+
)

go.sum

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
github.com/gophercloud/gophercloud v1.0.0 h1:9nTGx0jizmHxDobe4mck89FyQHVyA3CaXLIUSGJjP9k=
2-
github.com/gophercloud/gophercloud v1.0.0/go.mod h1:Q8fZtyi5zZxPS/j9aj3sSxtvj41AdQMDwyo1myduD5c=
3-
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
1+
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
2+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
4+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
5+
github.com/gophercloud/gophercloud v1.5.0 h1:cDN6XFCLKiiqvYpjQLq9AiM7RDRbIC9450WpPH+yvXo=
6+
github.com/gophercloud/gophercloud v1.5.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
7+
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
48
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
59
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
610
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
711
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
812
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
913
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
1014
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
15+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1116
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1217
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

selvpcclient/clients/quotamanager.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package clients
2+
3+
import (
4+
"fmt"
5+
6+
clientservices "github.com/selectel/go-selvpcclient/v3/selvpcclient/clients/services"
7+
)
8+
9+
const (
10+
QuotaManagerServiceType = "quota-manager"
11+
)
12+
13+
// QuotaManagerClient quota-manager client with X-Auth-Token authorization.
14+
type QuotaManagerClient struct {
15+
Requests *clientservices.RequestService
16+
catalog *clientservices.CatalogService
17+
}
18+
19+
func NewQuotaManagerClient(
20+
requestService *clientservices.RequestService,
21+
catalogService *clientservices.CatalogService,
22+
) *QuotaManagerClient {
23+
return &QuotaManagerClient{
24+
Requests: requestService,
25+
catalog: catalogService,
26+
}
27+
}
28+
29+
// GetEndpoint - returns service url in specific region.
30+
func (c *QuotaManagerClient) GetEndpoint(regionName string) (string, error) {
31+
endpoint, err := c.catalog.GetEndpoint(QuotaManagerServiceType, regionName)
32+
if err != nil {
33+
return "", fmt.Errorf(
34+
"failed to resolve endpoint for %s in %s, err: %w", QuotaManagerServiceType, regionName, err,
35+
)
36+
}
37+
38+
return endpoint.URL, nil
39+
}

selvpcclient/clients/resell.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package clients
2+
3+
import (
4+
"fmt"
5+
6+
clientservices "github.com/selectel/go-selvpcclient/v3/selvpcclient/clients/services"
7+
)
8+
9+
const (
10+
ResellServiceType = "resell"
11+
ResellAPIVersion = "v2"
12+
)
13+
14+
// ResellClient resell client with X-Auth-Token authorization.
15+
type ResellClient struct {
16+
Requests *clientservices.RequestService
17+
catalog *clientservices.CatalogService
18+
}
19+
20+
func NewResellClient(
21+
requestService *clientservices.RequestService,
22+
catalogService *clientservices.CatalogService,
23+
) *ResellClient {
24+
return &ResellClient{
25+
Requests: requestService,
26+
catalog: catalogService,
27+
}
28+
}
29+
30+
// GetEndpoint - returns service url.
31+
func (c *ResellClient) GetEndpoint() (string, error) {
32+
endpoints, err := c.catalog.GetEndpoints(ResellServiceType)
33+
if err != nil {
34+
return "", fmt.Errorf("failed to resolve endpoint for %s, err: %w", ResellServiceType, err)
35+
}
36+
37+
url := fmt.Sprintf("%s/%s", endpoints[0].URL, ResellAPIVersion)
38+
39+
return url, nil
40+
}

0 commit comments

Comments
 (0)