-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: PC-14087 Add SLO Status API to SDK (#537)
- Loading branch information
Showing
18 changed files
with
649 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package slostatusapi provides the API for managing SLO status API requests in the SDK. | ||
package slostatusapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package v1 provides the API for managing SLO status API V1 requests in the SDK. | ||
package v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
|
||
endpointsHelpers "github.com/nobl9/nobl9-go/internal/endpoints" | ||
"github.com/nobl9/nobl9-go/internal/sdk" | ||
) | ||
|
||
const ( | ||
baseAPIPath = "v1/slos" | ||
) | ||
|
||
//go:generate ../../../../bin/ifacemaker -y " " -f ./*.go -s endpoints -i Endpoints -o endpoints_interface.go -p "$GOPACKAGE" | ||
|
||
func NewEndpoints(client endpointsHelpers.Client) Endpoints { | ||
return endpoints{client: client} | ||
} | ||
|
||
type endpoints struct { | ||
client endpointsHelpers.Client | ||
} | ||
|
||
func (e endpoints) GetSLO(ctx context.Context, project, name string) (slo SLODetails, err error) { | ||
req, err := e.client.CreateRequest( | ||
ctx, | ||
http.MethodGet, | ||
path.Join(baseAPIPath, name), | ||
http.Header{sdk.HeaderProject: {project}}, | ||
nil, | ||
nil, | ||
) | ||
if err != nil { | ||
return slo, err | ||
} | ||
resp, err := e.client.Do(req) | ||
if err != nil { | ||
return slo, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&slo); err != nil { | ||
return slo, errors.Wrap(err, "failed to decode response body") | ||
} | ||
return slo, nil | ||
} | ||
|
||
func (e endpoints) GetSLOs(ctx context.Context, params GetSLOsRequest) (slos SLOListResponse, err error) { | ||
req, err := e.client.CreateRequest( | ||
ctx, | ||
http.MethodGet, | ||
baseAPIPath, | ||
nil, | ||
url.Values{ | ||
QueryKeyLimit: []string{strconv.Itoa(params.Limit)}, | ||
QueryKeyCursor: []string{params.Cursor}, | ||
}, | ||
nil, | ||
) | ||
if err != nil { | ||
return slos, err | ||
} | ||
resp, err := e.client.Do(req) | ||
if err != nil { | ||
return slos, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&slos); err != nil { | ||
return slos, errors.Wrap(err, "failed to decode response body") | ||
} | ||
if slos.Links.Next != "" { | ||
nextURL, err := url.Parse(slos.Links.Next) | ||
if err != nil { | ||
return slos, errors.Wrap(err, "failed to parse 'next' cursor link URL") | ||
} | ||
slos.Links.Cursor = nextURL.Query().Get("cursor") | ||
} | ||
return slos, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v1 | ||
|
||
const ( | ||
QueryKeyLimit = "limit" | ||
QueryKeyCursor = "cursor" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v1 | ||
|
||
type GetSLOsRequest struct { | ||
Limit int | ||
Cursor string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package v1 | ||
|
||
type SLOListResponse struct { | ||
Data []SLODetails `json:"data"` | ||
Links Links `json:"links"` | ||
} | ||
|
||
type Links struct { | ||
Self string `json:"self"` | ||
Next string `json:"next"` | ||
Cursor string `json:"cursor"` | ||
} | ||
|
||
type SLODetails struct { | ||
Name string `json:"name"` | ||
DisplayName string `json:"displayName"` | ||
Description string `json:"description"` | ||
Project string `json:"project"` | ||
Service string `json:"service"` | ||
CreatedAt string `json:"createdAt"` | ||
Objectives []Objective `json:"objectives"` | ||
Composite *Composite `json:"composite,omitempty"` | ||
Labels map[string][]string `json:"labels,omitempty"` | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
BudgetingMethod string `json:"budgetingMethod"` | ||
} | ||
|
||
type Objective struct { | ||
Name string `json:"name"` | ||
DisplayName string `json:"displayName"` | ||
Target float64 `json:"target"` | ||
BurnRate *float64 `json:"burnRate,omitempty"` | ||
ErrorBudgetRemaining *float64 `json:"errorBudgetRemaining,omitempty"` | ||
ErrorBudgetRemainingPercentage *float64 `json:"errorBudgetRemainingPercentage,omitempty"` | ||
Reliability *float64 `json:"reliability,omitempty"` | ||
Counts *Counts `json:"counts,omitempty"` | ||
SLIType string `json:"sliType"` | ||
} | ||
|
||
type Composite struct { | ||
Target *float64 `json:"target,omitempty"` | ||
BurnRateCondition *BurnRateCondition `json:"burnRateCondition,omitempty"` | ||
CompositeObjective | ||
} | ||
|
||
type CompositeObjective struct { | ||
BurnRate *float64 `json:"burnRate,omitempty"` | ||
ErrorBudgetRemaining *float64 `json:"errorBudgetRemaining,omitempty"` | ||
ErrorBudgetRemainingPercentage *float64 `json:"errorBudgetRemainingPercentage,omitempty"` | ||
Reliability *float64 `json:"reliability,omitempty"` | ||
} | ||
|
||
type BurnRateCondition struct { | ||
Value float64 `json:"value"` | ||
Operator string `json:"op"` | ||
} | ||
|
||
type Counts struct { | ||
Good *float64 `json:"good,omitempty"` | ||
Total *float64 `json:"total,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package v2 provides the API for managing SLO status API V2 requests in the SDK. | ||
package v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
|
||
endpointsHelpers "github.com/nobl9/nobl9-go/internal/endpoints" | ||
"github.com/nobl9/nobl9-go/internal/sdk" | ||
) | ||
|
||
const ( | ||
baseAPIPath = "v2/slos" | ||
) | ||
|
||
//go:generate ../../../../bin/ifacemaker -y " " -f ./*.go -s endpoints -i Endpoints -o endpoints_interface.go -p "$GOPACKAGE" | ||
|
||
func NewEndpoints(client endpointsHelpers.Client) Endpoints { | ||
return endpoints{client: client} | ||
} | ||
|
||
type endpoints struct { | ||
client endpointsHelpers.Client | ||
} | ||
|
||
func (e endpoints) GetSLO(ctx context.Context, project, name string) (slo SLODetails, err error) { | ||
req, err := e.client.CreateRequest( | ||
ctx, | ||
http.MethodGet, | ||
path.Join(baseAPIPath, name), | ||
http.Header{sdk.HeaderProject: {project}}, | ||
nil, | ||
nil, | ||
) | ||
if err != nil { | ||
return slo, err | ||
} | ||
resp, err := e.client.Do(req) | ||
if err != nil { | ||
return slo, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&slo); err != nil { | ||
return slo, errors.Wrap(err, "failed to decode response body") | ||
} | ||
return slo, nil | ||
} | ||
|
||
func (e endpoints) GetSLOs(ctx context.Context, params GetSLOsRequest) (slos SLOListResponse, err error) { | ||
req, err := e.client.CreateRequest( | ||
ctx, | ||
http.MethodGet, | ||
baseAPIPath, | ||
nil, | ||
url.Values{ | ||
QueryKeyLimit: []string{strconv.Itoa(params.Limit)}, | ||
QueryKeyCursor: []string{params.Cursor}, | ||
}, | ||
nil, | ||
) | ||
if err != nil { | ||
return slos, err | ||
} | ||
resp, err := e.client.Do(req) | ||
if err != nil { | ||
return slos, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&slos); err != nil { | ||
return slos, errors.Wrap(err, "failed to decode response body") | ||
} | ||
if slos.Links.Next != "" { | ||
nextURL, err := url.Parse(slos.Links.Next) | ||
if err != nil { | ||
return slos, errors.Wrap(err, "failed to parse 'next' cursor link URL") | ||
} | ||
slos.Links.Cursor = nextURL.Query().Get("cursor") | ||
} | ||
return slos, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v2 | ||
|
||
const ( | ||
QueryKeyLimit = "limit" | ||
QueryKeyCursor = "cursor" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v2 | ||
|
||
type GetSLOsRequest struct { | ||
Limit int | ||
Cursor string | ||
} |
Oops, something went wrong.