Skip to content

Commit

Permalink
Add X-Auth-Token support (#164)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
milkrage authored Jul 20, 2023
1 parent 0bfd18e commit 831abf4
Show file tree
Hide file tree
Showing 82 changed files with 1,712 additions and 1,793 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.14'
go-version: '1.20'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.49.0
version: v1.53.3
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.14'
go-version: '1.20'

- name: Run test
run: go test -v ./...
18 changes: 16 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ linters:
- dogsled
- errcheck
- exportloopref
# - gci
- gci
- gocognit
- goconst
- gocritic
Expand All @@ -32,7 +32,7 @@ linters:
- misspell
- nakedret
- noctx
# - nolintlint
- nolintlint
- prealloc
- rowserrcheck
- scopelint
Expand All @@ -45,3 +45,17 @@ linters:
- unused
- varcheck
- whitespace

linters-settings:
depguard:
rules:
main:
allow:
- $gostd
- github.com/selectel/go-selvpcclient
- github.com/gophercloud/gophercloud
- github.com/google/go-querystring
goimports:
local-prefixes: github.com/selectel/go-selvpcclient
gci:
local-prefixes: github.com/selectel/go-selvpcclient
160 changes: 41 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
# go-selvpcclient: a Go library for the Selectel VPC API
[![GoDoc](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient?status.svg)](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient)
[![Go Report Card](https://goreportcard.com/badge/github.com/selectel/go-selvpcclient)](https://goreportcard.com/report/github.com/selectel/go-selvpcclient)
[![Build Status](https://travis-ci.org/selectel/go-selvpcclient.svg?branch=master)](https://travis-ci.org/selectel/go-selvpcclient)
[![GoDoc](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient?status.svg)](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient)
[![Go Report Card](https://goreportcard.com/badge/github.com/selectel/go-selvpcclient/v3)](https://goreportcard.com/report/github.com/selectel/go-selvpcclient/v3)
[![Coverage Status](https://coveralls.io/repos/github/selectel/go-selvpcclient/badge.svg?branch=master)](https://coveralls.io/github/selectel/go-selvpcclient?branch=master)

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

## Documentation

The Go library documentation is available at [godoc.org](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient).

The API usage examples are available at [knowledge base](https://kb.selectel.com/24381383.html).
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)).
The Go library documentation is available at [godoc.org](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient).

## What this library is capable of

You can use this library to work with the following objects of the Selectel VPC API:

* [capabilities](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/capabilities)
* [floating ips](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/floatingips)
* [keypairs](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/keypairs)
* [licenses](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/licenses)
* [projects](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/projects)
* [quotas](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager/quotas)
* [roles](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/roles)
* [subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/subnets)
* [tokens](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/tokens)
* [traffic](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/traffic)
* [users](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/users)
* [vrrp subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/vrrpsubnets)
You can use this library to work with the following objects of the
[Cloud Management API](https://developers.selectel.ru/docs/selectel-cloud-platform/main-services/selectel_cloud_management_api/) and
[Cloud Quota Management API](https://developers.selectel.ru/docs/selectel-cloud-platform/main-services/cloud-quota-management/).

Cloud Management API:
* [capabilities](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/capabilities)
* [floating ips](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/floatingips)
* [keypairs](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/keypairs)
* [licenses](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/licenses)
* [projects](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/projects)
* [roles](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/roles)
* [subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/subnets)
* [tokens](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/tokens)
* [traffic](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/traffic)
* [users](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/users)
* [vrrp subnets](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/vrrpsubnets)

Cloud Quota Management API:
* [quotas](https://godoc.org/github.com/selectel/go-selvpcclient/v3/selvpcclient/quotamanager/quotas)

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.
You can use the [Gophercloud](https://github.com/gophercloud/gophercloud) project to work with the OpenStack objects.
Expand All @@ -40,15 +43,15 @@ You can use the [Gophercloud](https://github.com/gophercloud/gophercloud) projec
You can install `go-selvpcclient` as a Go package:

```bash
go get github.com/selectel/go-selvpcclient/selvpcclient/v2
go get github.com/selectel/go-selvpcclient/selvpcclient/v3
```

### Authentication

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

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

### Usage example

Expand All @@ -60,115 +63,34 @@ import (
"fmt"
"log"

resell "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2"
"github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/projects"
)

// API token from the https://my.selectel.ru.
var token = "token_key"

func main() {
// Initialize the Resell V2 client.
resellClient := resell.NewV2ResellClient(token)

// Get and print all projects.
ctx := context.Background()
allProjects, _, err := projects.List(ctx, resellClient)
if err != nil {
log.Fatal(err)
}
for _, myProject := range allProjects {
fmt.Println(myProject)
}
}
```

### Quota usage example

```go
package main

import (
"context"
"fmt"
"log"

"github.com/selectel/go-selvpcclient/v2/selvpcclient"
"github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager"
"github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager/quotas"
resell "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2"
reselTokens "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2/tokens"
)

// token from the https://my.selectel.ru.
var (
token = "token_key"
accountName = "account_name"
projectID = "project_uuid"
region = "region_name"
ramQuotaZone = "zone_name"
ramQuotaValue = 123
"github.com/selectel/go-selvpcclient/v3/selvpcclient"
"github.com/selectel/go-selvpcclient/v3/selvpcclient/resell/v2/projects"
)

func main() {
// Init resell client with API token.
resellClient := resell.NewV2ResellClient(token)
ctx := context.Background()

APIToken, _, err := reselTokens.Create(ctx, resellClient, reselTokens.TokenOpts{
AccountName: accountName,
})
if err != nil {
log.Fatal(err)
}

// Init Identity and Quota Manager.
client := resell.NewOpenstackClient(APIToken.ID)
identity := quotamanager.NewIdentityManager(resellClient, client, accountName)
quotaMgr := quotamanager.NewQuotaRegionalClient(selvpcclient.NewHTTPClient(), identity)

// Get limits for project <projectID> in region <region>.
limits, _, err := quotas.GetLimits(ctx, quotaMgr, projectID, region)
if err != nil {
log.Fatal(err)
}

for _, limit := range limits {
fmt.Println(limit.Name, limit.ResourceQuotasEntities)
options := &selvpcclient.ClientOptions{
Context: ctx,
DomainName: "999999",
Username: "admin",
Password: "m1-sup3r-p@ssw0rd-p3w-p3w",
}

// Get quotas for project <projectID> in region <region>.
quotasData, _, err := quotas.GetProjectQuotas(ctx, quotaMgr, projectID, region)
client, err := selvpcclient.NewClient(options)
if err != nil {
log.Fatal(err)
}

for _, quota := range quotasData {
fmt.Println(quota.Name, quota.ResourceQuotasEntities)
}

// Update quotas for project <projectID> in region <region>.
UpdateQuotasOpts := quotas.UpdateProjectQuotasOpts{
QuotasOpts: []quotas.QuotaOpts{
{
Name: "compute_cores",
ResourceQuotasOpts: []quotas.ResourceQuotaOpts{
{
Zone: &ramQuotaZone,
Value: &ramQuotaValue,
},
},
},
},
}
updatedQuotasData, _, err := quotas.UpdateProjectQuotas(ctx, quotaMgr,
projectID, region, UpdateQuotasOpts)
result, resp, err := projects.List(client)
if err != nil {
log.Fatal(err)
}

for _, quota := range updatedQuotasData {
fmt.Println(quota.Name, quota.ResourceQuotasEntities)

fmt.Printf("Response StatusCode: %d \n", resp.StatusCode)

for _, project := range result {
fmt.Printf("Project name: %s, enabled: %t \n", project.Name, project.Enabled)
}
}
```
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/selectel/go-selvpcclient/v2
module github.com/selectel/go-selvpcclient/v3

go 1.14
go 1.20

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

import (
"fmt"

clientservices "github.com/selectel/go-selvpcclient/v3/selvpcclient/clients/services"
)

const (
QuotaManagerServiceType = "quota-manager"
)

// QuotaManagerClient quota-manager client with X-Auth-Token authorization.
type QuotaManagerClient struct {
Requests *clientservices.RequestService
catalog *clientservices.CatalogService
}

func NewQuotaManagerClient(
requestService *clientservices.RequestService,
catalogService *clientservices.CatalogService,
) *QuotaManagerClient {
return &QuotaManagerClient{
Requests: requestService,
catalog: catalogService,
}
}

// GetEndpoint - returns service url in specific region.
func (c *QuotaManagerClient) GetEndpoint(regionName string) (string, error) {
endpoint, err := c.catalog.GetEndpoint(QuotaManagerServiceType, regionName)
if err != nil {
return "", fmt.Errorf(
"failed to resolve endpoint for %s in %s, err: %w", QuotaManagerServiceType, regionName, err,
)
}

return endpoint.URL, nil
}
40 changes: 40 additions & 0 deletions selvpcclient/clients/resell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package clients

import (
"fmt"

clientservices "github.com/selectel/go-selvpcclient/v3/selvpcclient/clients/services"
)

const (
ResellServiceType = "resell"
ResellAPIVersion = "v2"
)

// ResellClient resell client with X-Auth-Token authorization.
type ResellClient struct {
Requests *clientservices.RequestService
catalog *clientservices.CatalogService
}

func NewResellClient(
requestService *clientservices.RequestService,
catalogService *clientservices.CatalogService,
) *ResellClient {
return &ResellClient{
Requests: requestService,
catalog: catalogService,
}
}

// GetEndpoint - returns service url.
func (c *ResellClient) GetEndpoint() (string, error) {
endpoints, err := c.catalog.GetEndpoints(ResellServiceType)
if err != nil {
return "", fmt.Errorf("failed to resolve endpoint for %s, err: %w", ResellServiceType, err)
}

url := fmt.Sprintf("%s/%s", endpoints[0].URL, ResellAPIVersion)

return url, nil
}
Loading

0 comments on commit 831abf4

Please sign in to comment.