Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge main into VPC Feature Branch #404

Merged
merged 13 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ updates:
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
open-pull-requests-limit: 10
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: weekly
2 changes: 1 addition & 1 deletion .github/workflows/gosec_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
GO111MODULE: on
steps:
- name: Checkout Source
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Run Gosec Security Scanner on root directory
uses: securego/gosec@master
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/integration_tests_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: 'stable'
- run: go version
- uses: actions-ecosystem/action-regex-match@v2
id: disallowed-char-check
Expand All @@ -32,7 +32,7 @@ jobs:

# Check out merge commit
- name: Checkout PR
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ inputs.sha }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: micnncim/action-label-syncer@3abd5ab72fda571e69fffd97bd4e0033dd5f495c # pin@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly-smoke-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: main

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
update_release_draft:
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@569eb7ee3a85817ab916c8f8ff03a5bd96c9c83e # pin@v5.23.0
- uses: release-drafter/release-drafter@65c5fb495d1e69aa8c08a3317bc44ff8aabe9772 # pin@v5.24.0
with:
config-name: release-drafter.yml
env:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 'stable'
Expand Down Expand Up @@ -59,6 +59,7 @@ jobs:
SKIP_LINT: 1

- name: Convert JSON Report to XML
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: |
filename=$(ls | grep -E '^[0-9]{12}_linodego_test_report\.json')

Expand All @@ -76,6 +77,7 @@ jobs:
GO111MODULE: on

- name: Add additional information to XML report
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: |
filename=$(ls | grep -E '^[0-9]{12}_linodego_test_report\.xml$')
python scripts/add_to_xml_test_report.py \
Expand All @@ -85,6 +87,7 @@ jobs:
--xmlfile "${filename}"

- name: Upload test results to bucket
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
LINODE_CLI_OBJ_ACCESS_KEY: ${{ secrets.LINODE_CLI_OBJ_ACCESS_KEY }}
LINODE_CLI_OBJ_SECRET_KEY: ${{ secrets.LINODE_CLI_OBJ_SECRET_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

# Common IDE paths
.vscode/
.idea/

vendor/**/
.env
coverage.txt

6 changes: 4 additions & 2 deletions account_invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ type Invoice struct {
Date *time.Time `json:"-"`
}

// InvoiceItem structs reflect an single billable activity associate with an Invoice
// InvoiceItem structs reflect a single billable activity associate with an Invoice
type InvoiceItem struct {
Label string `json:"label"`
Type string `json:"type"`
UnitPrice int `json:"unitprice"`
Quantity int `json:"quantity"`
Amount float32 `json:"amount"`
Tax float32 `json:"tax"`
Region *string `json:"region"`
From *time.Time `json:"-"`
To *time.Time `json:"-"`
}
Expand Down Expand Up @@ -103,7 +105,7 @@ func (i *InvoiceItem) UnmarshalJSON(b []byte) error {
return nil
}

// GetInvoice gets the a single Invoice matching the provided ID
// GetInvoice gets a single Invoice matching the provided ID
func (c *Client) GetInvoice(ctx context.Context, invoiceID int) (*Invoice, error) {
req := c.R(ctx).SetResult(&Invoice{})
e := fmt.Sprintf("account/invoices/%d", invoiceID)
Expand Down
33 changes: 33 additions & 0 deletions account_transfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package linodego

import "context"

// AccountTransfer represents an Account's network utilization for the current month.
type AccountTransfer struct {
Billable int `json:"billable"`
Quota int `json:"quota"`
Used int `json:"used"`

RegionTransfers []AccountTransferRegion `json:"region_transfers"`
}

// AccountTransferRegion represents an Account's network utilization for the current month
// in a given region.
type AccountTransferRegion struct {
ID string `json:"id"`
Billable int `json:"billable"`
Quota int `json:"quota"`
Used int `json:"used"`
}

// GetAccountTransfer gets current Account's network utilization for the current month.
func (c *Client) GetAccountTransfer(ctx context.Context) (*AccountTransfer, error) {
req := c.R(ctx).SetResult(&AccountTransfer{})
e := "account/transfer"
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}

return r.Result().(*AccountTransfer), nil
}
20 changes: 15 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linodego
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -82,7 +81,10 @@ type clientCacheEntry struct {
ExpiryOverride *time.Duration
}

type Request = resty.Request
type (
Request = resty.Request
Logger = resty.Logger
)

func init() {
// Wether or not we will enable Resty debugging output
Expand Down Expand Up @@ -121,6 +123,14 @@ func (c *Client) SetDebug(debug bool) *Client {
return c
}

// SetLogger allows the user to override the output
// logger for debug logs.
func (c *Client) SetLogger(logger Logger) *Client {
c.resty.SetLogger(logger)

return c
}

// OnBeforeRequest adds a handler to the request body to run before the request is sent
func (c *Client) OnBeforeRequest(m func(request *Request) error) {
c.resty.OnBeforeRequest(func(client *resty.Client, req *resty.Request) error {
Expand Down Expand Up @@ -166,7 +176,7 @@ func (c *Client) updateHostURL() {
apiProto = c.apiProto
}

c.resty.SetHostURL(
c.resty.SetBaseURL(
fmt.Sprintf(
"%s://%s/%s",
apiProto,
Expand All @@ -183,7 +193,7 @@ func (c *Client) SetRootCertificate(path string) *Client {
}

// SetToken sets the API token for all requests from this client
// Only necessary if you haven't already provided an http client to NewClient() configured with the token.
// Only necessary if you haven't already provided the http client to NewClient() configured with the token.
func (c *Client) SetToken(token string) *Client {
c.resty.SetHeader("Authorization", fmt.Sprintf("Bearer %s", token))
return c
Expand Down Expand Up @@ -398,7 +408,7 @@ func NewClient(hc *http.Client) (client Client) {
certPath, certPathExists := os.LookupEnv(APIHostCert)

if certPathExists {
cert, err := ioutil.ReadFile(filepath.Clean(certPath))
cert, err := os.ReadFile(filepath.Clean(certPath))
if err != nil {
log.Fatalf("[ERROR] Error when reading cert at %s: %s\n", certPath, err.Error())
}
Expand Down
20 changes: 10 additions & 10 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@ func TestClient_SetAPIVersion(t *testing.T) {

client := NewClient(nil)

if client.resty.HostURL != defaultURL {
t.Fatal(cmp.Diff(client.resty.HostURL, defaultURL))
if client.resty.BaseURL != defaultURL {
t.Fatal(cmp.Diff(client.resty.BaseURL, defaultURL))
}

client.SetBaseURL(baseURL)
client.SetAPIVersion(apiVersion)

if client.resty.HostURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.HostURL, expectedHost))
if client.resty.BaseURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, expectedHost))
}

// Ensure setting twice does not cause conflicts
client.SetBaseURL(updatedBaseURL)
client.SetAPIVersion(updatedAPIVersion)

if client.resty.HostURL != updatedExpectedHost {
t.Fatal(cmp.Diff(client.resty.HostURL, updatedExpectedHost))
if client.resty.BaseURL != updatedExpectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, updatedExpectedHost))
}

// Revert
client.SetBaseURL(baseURL)
client.SetAPIVersion(apiVersion)

if client.resty.HostURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.HostURL, expectedHost))
if client.resty.BaseURL != expectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, expectedHost))
}

// Custom protocol
client.SetBaseURL(protocolBaseURL)
client.SetAPIVersion(protocolAPIVersion)

if client.resty.HostURL != protocolExpectedHost {
t.Fatal(cmp.Diff(client.resty.HostURL, expectedHost))
if client.resty.BaseURL != protocolExpectedHost {
t.Fatal(cmp.Diff(client.resty.BaseURL, expectedHost))
}
}

Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type LoadConfigOptions struct {
SkipLoadProfile bool
}

// LoadConfig loads a Linode config according to the options argument.
// LoadConfig loads a Linode config according to the option's argument.
// If no options are specified, the following defaults will be used:
// Path: ~/.config/linode
// Profile: default
Expand Down
11 changes: 5 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package linodego

import (
"fmt"
"io/ioutil"
"os"
"testing"
)
Expand Down Expand Up @@ -43,8 +42,8 @@ func TestConfig_LoadWithDefaults(t *testing.T) {

expectedURL := "https://api.cool.linode.com/v4beta"

if client.resty.HostURL != expectedURL {
t.Fatalf("mismatched host url: %s != %s", client.resty.HostURL, expectedURL)
if client.resty.BaseURL != expectedURL {
t.Fatalf("mismatched host url: %s != %s", client.resty.BaseURL, expectedURL)
}

if client.resty.Header.Get("Authorization") != "Bearer "+p.APIToken {
Expand Down Expand Up @@ -89,8 +88,8 @@ func TestConfig_OverrideDefaults(t *testing.T) {

expectedURL := "https://api.cool.linode.com/v4"

if client.resty.HostURL != expectedURL {
t.Fatalf("mismatched host url: %s != %s", client.resty.HostURL, expectedURL)
if client.resty.BaseURL != expectedURL {
t.Fatalf("mismatched host url: %s != %s", client.resty.BaseURL, expectedURL)
}

if client.resty.Header.Get("Authorization") != "Bearer "+p.APIToken {
Expand Down Expand Up @@ -131,7 +130,7 @@ func TestConfig_NoDefaults(t *testing.T) {
}

func createTestConfig(t *testing.T, conf string) *os.File {
file, err := ioutil.TempFile("", "linode")
file, err := os.CreateTemp("", "linode")
if err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package linodego

import (
"fmt"
"log"
"net/http"
"reflect"
"strings"

"github.com/go-resty/resty/v2"
)

const (
ErrorUnsupported = iota
// ErrorFromString is the Code identifying Errors created by string types
ErrorFromString = 1
ErrorFromString
// ErrorFromError is the Code identifying Errors created by error types
ErrorFromError = 2
ErrorFromError
// ErrorFromStringer is the Code identifying Errors created by fmt.Stringer types
ErrorFromStringer = 3
ErrorFromStringer
)

// Error wraps the LinodeGo error with the relevant http.Response
Expand Down Expand Up @@ -113,7 +114,7 @@ func NewError(err any) *Error {
apiError, ok := e.Error().(*APIError)

if !ok {
log.Fatalln("Unexpected Resty Error Response")
return &Error{Code: ErrorUnsupported, Message: "Unexpected Resty Error Response, no error"}
}

return &Error{
Expand All @@ -128,7 +129,6 @@ func NewError(err any) *Error {
case fmt.Stringer:
return &Error{Code: ErrorFromStringer, Message: e.String()}
default:
log.Fatalln("Unsupported type to linodego.NewError")
panic(err)
return &Error{Code: ErrorUnsupported, Message: fmt.Sprintf("Unsupported type to linodego.NewError: %s", reflect.TypeOf(e))}
}
}
Loading
Loading