Skip to content

Commit

Permalink
Merge branch 'master' into update-readme-with-new-client-init
Browse files Browse the repository at this point in the history
  • Loading branch information
bentranter authored Mar 16, 2020
2 parents d3c5fa5 + 37c786e commit 014c98e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

## unreleased

- #310 Add BillingHistory service and List endpoint - @rbutler

## [v1.32.0] - 2020-03-04

- #311 Add reset database user auth method - @zbarahal-do

## [v1.31.0] - 2020-02-28

- #305 invoices: GetPDF and GetCSV methods - @rbutler
- #304 Add NewFromToken convenience method to init client - @bentranter
- #301 invoices: Get, Summary, and List methods - @rbutler
- #299 Fix param expiry_seconds for kubernetes.GetCredentials request - @velp

## [v1.30.0] - 2020-02-03

Expand Down
72 changes: 72 additions & 0 deletions billing_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package godo

import (
"context"
"net/http"
"time"
)

const billingHistoryBasePath = "v2/customers/my/billing_history"

// BillingHistoryService is an interface for interfacing with the BillingHistory
// endpoints of the DigitalOcean API
// See: https://developers.digitalocean.com/documentation/v2/#billing_history
type BillingHistoryService interface {
List(context.Context, *ListOptions) (*BillingHistory, *Response, error)
}

// BillingHistoryServiceOp handles communication with the BillingHistory related methods of
// the DigitalOcean API.
type BillingHistoryServiceOp struct {
client *Client
}

var _ BillingHistoryService = &BillingHistoryServiceOp{}

// BillingHistory represents a DigitalOcean Billing History
type BillingHistory struct {
BillingHistory []BillingHistoryEntry `json:"billing_history"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

// BillingHistoryEntry represents an entry in a customer's Billing History
type BillingHistoryEntry struct {
Description string `json:"description"`
Amount string `json:"amount"`
InvoiceID *string `json:"invoice_id"`
InvoiceUUID *string `json:"invoice_uuid"`
Date time.Time `json:"date"`
Type string `json:"type"`
}

func (b BillingHistory) String() string {
return Stringify(b)
}

// List the Billing History for a customer
func (s *BillingHistoryServiceOp) List(ctx context.Context, opt *ListOptions) (*BillingHistory, *Response, error) {
path, err := addOptions(billingHistoryBasePath, opt)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(BillingHistory)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root, resp, err
}
71 changes: 71 additions & 0 deletions billing_history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package godo

import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)

func TestBillingHistory_List(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/customers/my/billing_history", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"billing_history": [
{
"description": "Invoice for May 2018",
"amount": "12.34",
"invoice_id": "123",
"invoice_uuid": "example-uuid",
"date": "2018-06-01T08:44:38Z",
"type": "Invoice"
},
{
"description": "Payment (MC 2018)",
"amount": "-12.34",
"date": "2018-06-02T08:44:38Z",
"type": "Payment"
}
],
"meta": {
"total": 2
}
}`)
})

history, resp, err := client.BillingHistory.List(ctx, nil)
if err != nil {
t.Errorf("BillingHistory.List returned error: %v", err)
}

expectedBillingHistory := []BillingHistoryEntry{
{
Description: "Invoice for May 2018",
Amount: "12.34",
InvoiceID: String("123"),
InvoiceUUID: String("example-uuid"),
Date: time.Date(2018, 6, 1, 8, 44, 38, 0, time.UTC),
Type: "Invoice",
},
{
Description: "Payment (MC 2018)",
Amount: "-12.34",
InvoiceID: nil,
InvoiceUUID: nil,
Date: time.Date(2018, 6, 2, 8, 44, 38, 0, time.UTC),
Type: "Payment",
},
}
entries := history.BillingHistory
if !reflect.DeepEqual(entries, expectedBillingHistory) {
t.Errorf("BillingHistory.List\nBillingHistory: got=%#v\nwant=%#v", entries, expectedBillingHistory)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("BillingHistory.List\nMeta: got=%#v\nwant=%#v", resp.Meta, expectedMeta)
}
}
21 changes: 21 additions & 0 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
databaseBackupsPath = databaseBasePath + "/%s/backups"
databaseUsersPath = databaseBasePath + "/%s/users"
databaseUserPath = databaseBasePath + "/%s/users/%s"
databaseResetUserAuthPath = databaseUserPath + "/reset_auth"
databaseDBPath = databaseBasePath + "/%s/dbs/%s"
databaseDBsPath = databaseBasePath + "/%s/dbs"
databasePoolPath = databaseBasePath + "/%s/pools/%s"
Expand Down Expand Up @@ -100,6 +101,7 @@ type DatabasesService interface {
ListUsers(context.Context, string, *ListOptions) ([]DatabaseUser, *Response, error)
CreateUser(context.Context, string, *DatabaseCreateUserRequest) (*DatabaseUser, *Response, error)
DeleteUser(context.Context, string, string) (*Response, error)
ResetUserAuth(context.Context, string, string, *DatabaseResetUserAuthRequest) (*DatabaseUser, *Response, error)
ListDBs(context.Context, string, *ListOptions) ([]DatabaseDB, *Response, error)
CreateDB(context.Context, string, *DatabaseCreateDBRequest) (*DatabaseDB, *Response, error)
GetDB(context.Context, string, string) (*DatabaseDB, *Response, error)
Expand Down Expand Up @@ -267,6 +269,11 @@ type DatabaseCreateUserRequest struct {
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
}

// DatabaseResetUserAuth request is used to reset a users DB auth
type DatabaseResetUserAuthRequest struct {
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
}

// DatabaseCreateDBRequest is used to create a new engine-specific database within the cluster
type DatabaseCreateDBRequest struct {
Name string `json:"name"`
Expand Down Expand Up @@ -530,6 +537,20 @@ func (svc *DatabasesServiceOp) CreateUser(ctx context.Context, databaseID string
return root.User, resp, nil
}

func (svc *DatabasesServiceOp) ResetUserAuth(ctx context.Context, databaseID, userID string, resetAuth *DatabaseResetUserAuthRequest) (*DatabaseUser, *Response, error) {
path := fmt.Sprintf(databaseResetUserAuthPath, databaseID, userID)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, resetAuth)
if err != nil {
return nil, nil, err
}
root := new(databaseUserRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.User, resp, nil
}

// DeleteUser will delete an existing database user
func (svc *DatabasesServiceOp) DeleteUser(ctx context.Context, databaseID, userID string) (*Response, error) {
path := fmt.Sprintf(databaseUserPath, databaseID, userID)
Expand Down
39 changes: 39 additions & 0 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,45 @@ func TestDatabases_DeleteUser(t *testing.T) {
require.NoError(t, err)
}

func TestDatabases_ResetUserAuth(t *testing.T) {
setup()
defer teardown()
dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"
path := fmt.Sprintf("/v2/databases/%s/users/user/reset_auth", dbID)

body := `
{
"user": {
"name": "name",
"role": "foo",
"password": "pass",
"mysql_settings": {
"auth_plugin": "caching_sha2_password"
}
}
}
`
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, body)
})

want := &DatabaseUser{
Name: "name",
Role: "foo",
Password: "pass",
MySQLSettings: &DatabaseMySQLUserSettings{AuthPlugin: SQLAuthPluginCachingSHA2},
}

got, _, err := client.Databases.ResetUserAuth(ctx, dbID, "user", &DatabaseResetUserAuthRequest{
MySQLSettings: &DatabaseMySQLUserSettings{
AuthPlugin: SQLAuthPluginCachingSHA2,
}})

require.NoError(t, err)
require.Equal(t, want, got)
}

func TestDatabases_ListDBs(t *testing.T) {
setup()
defer teardown()
Expand Down
4 changes: 3 additions & 1 deletion godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
libraryVersion = "1.30.0"
libraryVersion = "1.32.0"
defaultBaseURL = "https://api.digitalocean.com/"
userAgent = "godo/" + libraryVersion
mediaType = "application/json"
Expand Down Expand Up @@ -47,6 +47,7 @@ type Client struct {
Account AccountService
Actions ActionsService
Balance BalanceService
BillingHistory BillingHistoryService
CDNs CDNService
Domains DomainsService
Droplets DropletsService
Expand Down Expand Up @@ -180,6 +181,7 @@ func NewClient(httpClient *http.Client) *Client {
c.Account = &AccountServiceOp{client: c}
c.Actions = &ActionsServiceOp{client: c}
c.Balance = &BalanceServiceOp{client: c}
c.BillingHistory = &BillingHistoryServiceOp{client: c}
c.CDNs = &CDNServiceOp{client: c}
c.Certificates = &CertificatesServiceOp{client: c}
c.Domains = &DomainsServiceOp{client: c}
Expand Down
1 change: 1 addition & 0 deletions godo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func testClientServices(t *testing.T, c *Client) {
"Account",
"Actions",
"Balance",
"BillingHistory",
"CDNs",
"Domains",
"Droplets",
Expand Down

0 comments on commit 014c98e

Please sign in to comment.