Skip to content

Commit

Permalink
Ingestion Policy (New API Updates) (#55)
Browse files Browse the repository at this point in the history
Bumping the major version of the repository to 2.0.0.

Changes to the API broke the old API contract. Old versions of this code no longer work against the currently deployed versions of the server.

The ingestion policy response and request payloads were changed (e.g. types of some parameters changed from strings to arrays or maps).

* I fixed all IngestionPolicy functions.
* Update Ingestion Policy test fixtures.
* Created new acceptance tests for IngestionPolicy functions; so, API parity is easier to maintain.

* Misc:
  * Tidy typos, comment clarity and whitespace.
    * Most notable: I converted a couple multi-line function signatures into one-liners. (Seems to be the more common pattern in the repo and I found them confusing.)
  * Rebuilt and standardized the GitHub Workflows.
    * Build against multiple versions, ensure tests are run (to name a few.)
  * Add a Makefile to help simplify / standardize development in this repository.
  * Fixed broken helper scripts.
  * Add `.envrc` to .gitignore (best not to commit this file if you have one.)
  * Update mods.

Signed-off-by: Brendan Winter <[email protected]>
  • Loading branch information
bwinter authored Jul 25, 2023
1 parent 1b289ff commit 8cbee0a
Show file tree
Hide file tree
Showing 21 changed files with 1,552 additions and 192 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ name: Go

on:
push:
branches: [ master ]
tags:
- v*
branches:
- master
pull_request:
branches: [ master ]

env:
GO111MODULE: on
branches:
- master

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.13.x", "1.14.x", "1.15.x", "1.16.x", "1.17.x" ]
go: [ "1.18.x", "1.19.x", "1.20.x" ]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Run gofmt
run: diff -u <(echo -n) <(gofmt -d .)

- name: Test
run: go test -timeout 10m -v -race ./
run: go test -timeout=120s -v -race -short

- name: Run go vet
run: go vet ./
35 changes: 17 additions & 18 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
name: golangci-lint

on:
push:
tags:
- v*
- v*
branches:
- master
- master
pull_request:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.48
skip-pkg-cache: true
skip-build-cache: true

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
- uses: actions/checkout@v3
with:
fetch-depth: 0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with: # Required: the version of golangci-lint is required and must be specified without a patch version:
# we always use the latest patch version.
version: v1.52
# Optional: if set to `true` then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
skip-cache: true
args: --issues-exit-code=1
only-new-issues: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ vendor/*/

# Vim swap files
.*.swp

# direnv config.
.envrc
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Changelog

Changelog for go-wavefront-management-api.
## [2.0.0]

Breaking Change:
- The Ingestion Policies' API changed on Wavefront servers, breaking the functionality in the CLI.

Fix:
- Adds support for the new Ingestion Policy interface.

Enhancement:
- Add more testing to Ingestion Policies.
- Update go dependencies.
- Modernize GitHub actions.

## [1.16.0]

* Add support for Scope field in Ingestion Policy
Expand Down
38 changes: 38 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TEST?=$$(go list ./... | grep -v 'vendor')
GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor)

default: build

build: fmtcheck
go install

fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w $(GOFMT_FILES)

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

lint:
@echo "==> Checking source code against linters..."
golangci-lint run ./...

test: fmtcheck
go test $(TEST) -v $(TESTARGS) -timeout=120s -short -race

testacc: fmtcheck
go test $(TEST) -v $(TESTARGS) -timeout=120s -run "TestAcc"

tidy:
go mod tidy

vet:
@echo "go vet ."
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi

.PHONY: build fmt fmtcheck lint test testacc tidy vet
39 changes: 23 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Wavefronter interface {
Do(req *http.Request) (io.ReadCloser, error)
}

// Config is used to hold configuration used when constructing a Client
// Config is used to hold configuration, used when constructing a Client
type Config struct {
// Address is the address of the Wavefront API, of the form
// example.wavefront.com or http://localhost:8080.
Expand Down Expand Up @@ -75,10 +75,11 @@ func NewClient(config *Config) (*Client, error) {
return nil, err
}

// need to disable http/2 as it doesn't play nicely with nginx
// to do so we set TLSNextProto to an empty, non-nil map
// need to disable http/2 as it doesn't play nicely with nginx.
// To disable, set TLSNextProto to an empty, non-nil map
configCopy := *config
c := &Client{Config: &configCopy,
c := &Client{
Config: &configCopy,
BaseURL: baseURL,
httpClient: &http.Client{
Transport: &http.Transport{
Expand Down Expand Up @@ -333,21 +334,22 @@ func doParams(params map[string]string) doOption {
// client is the client object.
// options is a var arg list of options for the Rest API call:
// To use myStruct as the payload of the REST call, pass doPayload(&myStruct);
// To store the response of the REST call in result, pass doResponse(&result);
func doRest(
method string,
url string,
client Wavefronter,
options ...doOption) (err error) {
// To store the response of the REST call in the result, pass doResponse(&result);
func doRest(method string, url string, client Wavefronter, options ...doOption) (err error) {

// Apply options to the settings object.
var settings doSettings
settings.applyOptions(options)

// Validate Payload
var payload []byte
if settings.payloadPtr != nil {
payload, err = json.Marshal(settings.payloadPtr)
if err != nil {
return
}
}

var req *http.Request
if len(settings.params) == 0 {
req, err = client.NewRequest(method, url, nil, payload)
Expand All @@ -357,11 +359,13 @@ func doRest(
if err != nil {
return
}

resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Close()

if settings.responsePtr != nil {
decoder := json.NewDecoder(resp)
pointToZeroValue(settings.responsePtr)
Expand All @@ -380,7 +384,7 @@ func doRest(
// assignment operator. The way to fix this is to set the struct to its zero
// value before unmarshalling to it. Since the zero value has no allocated
// slices or maps, the json library will allocate new slices and maps rather
// than modifying existing ones in place which is what we need for the
// than modifying existing ones in place, which is what we need for the
// assignment operator to work as expected.
func pointToZeroValue(ptr interface{}) {
if reflect.TypeOf(ptr).Kind() != reflect.Ptr {
Expand All @@ -396,34 +400,37 @@ func pointToZeroValue(ptr interface{}) {
// For dashboards its "dashboard" It is what follows /api/v2/search.
// client is the wavefront client. slicePtr is a pointer to a slice. The
// results are appended to this slice.
func doSearch(
filter []*SearchCondition,
typ string,
client Wavefronter,
slicePtr interface{}) error {
func doSearch(filter []*SearchCondition, typ string, client Wavefronter, slicePtr interface{}) error {

resultValue := sliceValueFromP(slicePtr)

search := &Search{
client: client,
Type: typ,
Params: &SearchParams{
Conditions: filter,
},
}

moreItems := true
for moreItems {
resp, err := search.Execute()
if err != nil {
return err
}

pageValuePtr := reflect.New(resultValue.Type())
err = json.Unmarshal(resp.Response.Items, pageValuePtr.Interface())
if err != nil {
return err
}

resultValue.Set(reflect.AppendSlice(resultValue, pageValuePtr.Elem()))

moreItems = resp.Response.MoreItems
search.Params.Offset = resp.NextOffset
}

return nil
}

Expand Down
18 changes: 0 additions & 18 deletions fixtures/create-ingestion-policyresponse.json

This file was deleted.

Loading

0 comments on commit 8cbee0a

Please sign in to comment.