Skip to content
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
2 changes: 1 addition & 1 deletion diode-server/auth/mocks/clientinfodecorator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion diode-server/auth/mocks/clientmanager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion diode-server/auth/mocks/metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion diode-server/auth/mocks/tokenownershipprovider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion diode-server/auth/mocks/tokenparser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion diode-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/google/uuid v1.6.0
github.com/gosimple/slug v1.15.0
github.com/gowebpki/jcs v1.0.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/jackc/pgx/v5 v5.7.5
github.com/kelseyhightower/envconfig v1.4.0
github.com/oklog/run v1.1.0
Expand Down Expand Up @@ -67,7 +69,6 @@ require (
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/gowebpki/jcs v1.0.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions diode-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
Expand Down
2 changes: 1 addition & 1 deletion diode-server/ingester/mocks/metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion diode-server/ingester/mocks/streamrouter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions diode-server/netboxdiodeplugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const (
// ErrInvalidTimeout is an error for invalid timeout value
var ErrInvalidTimeout = errors.New("invalid timeout value")

// ErrDefaultBranchNotFound is returned when the default branch is not found (e.g. no endpoint available in older plugin versions)
var ErrDefaultBranchNotFound = errors.New("default branch not found")

// ChangeSetResult represents a change set result
type ChangeSetResult struct {
ID string `json:"id"`
Expand Down Expand Up @@ -263,6 +266,9 @@ type NetBoxAPI interface {

// ApplyChangeSet applies a change set
ApplyChangeSet(context.Context, ApplyChangeSetRequest) (*ChangeSetResult, error)

// GetDefaultBranch gets the default branch from NetBox plugin settings
GetDefaultBranch(context.Context) (*Branch, error)
}

// GenerateDiffRequest represents a generate diff request
Expand Down Expand Up @@ -412,3 +418,62 @@ func (c *Client) ApplyChangeSet(ctx context.Context, payload ApplyChangeSetReque

return &changeSetResult, nil
}

// GetDefaultBranchResponse represents the response from the default-branch endpoint
type GetDefaultBranchResponse struct {
Branch *Branch `json:"branch"`
}

// GetDefaultBranch gets the default branch from NetBox plugin settings
func (c *Client) GetDefaultBranch(ctx context.Context) (*Branch, error) {
endpointURL, err := url.Parse(fmt.Sprintf("%s/default-branch/", c.baseURL.String()))
if err != nil {
return nil, err
}

if err := c.limiter.Wait(ctx); err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpointURL.String(), nil)
if err != nil {
return nil, err
}

resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
c.logger.Warn("failed to close response body", "error", closeErr)
}
}()

respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body %w", err)
}

// Log response body only for successful responses to avoid noise from 404s (endpoint not available on older plugin versions)
if resp.StatusCode < http.StatusBadRequest {
c.logger.Debug("get default branch", "statusCode", resp.StatusCode, "response", string(respBytes))
} else {
c.logger.Debug("get default branch", "statusCode", resp.StatusCode)
}

if resp.StatusCode >= http.StatusBadRequest {
// Return sentinel error for 404 to allow callers to cache and handle gracefully
if resp.StatusCode == http.StatusNotFound {
return nil, ErrDefaultBranchNotFound
}
return nil, fmt.Errorf("get default branch failed with status %d: %s", resp.StatusCode, string(respBytes))
}

var defaultBranchResponse GetDefaultBranchResponse
if err = json.Unmarshal(respBytes, &defaultBranchResponse); err != nil {
return nil, fmt.Errorf("failed to unmarshal response body %w", err)
}

return defaultBranchResponse.Branch, nil
}
Loading
Loading