Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/SCALRCORE-…
Browse files Browse the repository at this point in the history
…26514
  • Loading branch information
penja committed Jun 16, 2023
2 parents f34d92b + b79ba53 commit 3cd4759
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ export SCALR_TOKEN=
You can run the acceptance tests like this:
```
make test
```
To run specific test:
```
TESTARGS="-run TestAccessPoliciesList/without_list_options" make test
```
25 changes: 25 additions & 0 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Environments interface {
Read(ctx context.Context, environmentID string) (*Environment, error)
Create(ctx context.Context, options EnvironmentCreateOptions) (*Environment, error)
Update(ctx context.Context, environmentID string, options EnvironmentUpdateOptions) (*Environment, error)
UpdateDefaultProviderConfigurationOnly(ctx context.Context, environmentID string, options EnvironmentUpdateOptionsDefaultProviderConfigurationOnly) (*Environment, error)
Delete(ctx context.Context, environmentID string) error
}

Expand Down Expand Up @@ -189,6 +190,12 @@ type EnvironmentUpdateOptions struct {
DefaultProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,default-provider-configurations"`
}

type EnvironmentUpdateOptionsDefaultProviderConfigurationOnly struct {
ID string `jsonapi:"primary,environments"`
// Relations
DefaultProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,default-provider-configurations"`
}

// Update settings of an existing environment.
func (s *environments) Update(ctx context.Context, environmentID string, options EnvironmentUpdateOptions) (*Environment, error) {
// Make sure we don't send a user provided ID.
Expand All @@ -209,6 +216,24 @@ func (s *environments) Update(ctx context.Context, environmentID string, options
return env, nil
}

func (s *environments) UpdateDefaultProviderConfigurationOnly(ctx context.Context, environmentID string, options EnvironmentUpdateOptionsDefaultProviderConfigurationOnly) (*Environment, error) {
options.ID = ""

u := fmt.Sprintf("environments/%s", url.QueryEscape(environmentID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}

env := &Environment{}
err = s.client.do(ctx, req, env)
if err != nil {
return nil, err
}

return env, nil
}

// Delete an environment by its ID.
func (s *environments) Delete(ctx context.Context, environmentID string) error {
if !validStringID(&environmentID) {
Expand Down
30 changes: 30 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,33 @@ func createWebhookIntegration(
}
}
}

func createSlackIntegration(
t *testing.T, client *Client, slackConnection *SlackConnection, environment *Environment,
) (*SlackIntegration, func()) {
ctx := context.Background()
options := SlackIntegrationCreateOptions{
Name: String("test-" + randomString(t)),
Events: []string{
SlackIntegrationEventRunApprovalRequired,
SlackIntegrationEventRunSuccess,
SlackIntegrationEventRunErrored,
},
ChannelId: String("C123"),
Account: &Account{ID: defaultAccountID},
Connection: slackConnection,
Environments: []*Environment{environment},
}
si, err := client.SlackIntegrations.Create(ctx, options)
if err != nil {
t.Fatal(err)
}

return si, func() {
if err := client.SlackIntegrations.Delete(ctx, si.ID); err != nil {
t.Errorf("Error deleting slack integration! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Webhook: %s\nError: %s", si.ID, err)
}
}
}
9 changes: 9 additions & 0 deletions integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalr

type IntegrationStatus string

const (
IntegrationStatusActive IntegrationStatus = "active"
IntegrationStatusDisabled IntegrationStatus = "disabled"
IntegrationStatusFailed IntegrationStatus = "failed"
)
9 changes: 9 additions & 0 deletions scalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -138,6 +139,7 @@ type Client struct {
Runs Runs
ServiceAccountTokens ServiceAccountTokens
ServiceAccounts ServiceAccounts
SlackIntegrations SlackIntegrations
Tags Tags
Teams Teams
Users Users
Expand Down Expand Up @@ -236,6 +238,7 @@ func NewClient(cfg *Config) (*Client, error) {
client.Runs = &runs{client: client}
client.ServiceAccountTokens = &serviceAccountTokens{client: client}
client.ServiceAccounts = &serviceAccounts{client: client}
client.SlackIntegrations = &slackIntegrations{client: client}
client.Tags = &tags{client: client}
client.Teams = &teams{client: client}
client.Users = &users{client: client}
Expand Down Expand Up @@ -265,6 +268,12 @@ func (c *Client) retryHTTPCheck(ctx context.Context, resp *http.Response, err er
return c.retryServerErrors, err
}
if resp.StatusCode == 429 || (c.retryServerErrors && resp.StatusCode >= 500) {
if resp.StatusCode == 429 {
log.Printf(
"[DEBUG] API rate limit reached for %s%s, retrying...",
resp.Request.URL.Host, resp.Request.URL.Path,
)
}
return true, nil
}
return false, nil
Expand Down
21 changes: 17 additions & 4 deletions scalr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
Expand Down Expand Up @@ -190,26 +191,38 @@ func TestClient_retryHTTPCheck(t *testing.T) {
checkErr error
}{
"429-no-server-errors": {
resp: &http.Response{StatusCode: 429},
resp: &http.Response{
StatusCode: 429,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
checkOK: true,
checkErr: nil,
},
"429-with-server-errors": {
resp: &http.Response{StatusCode: 429},
resp: &http.Response{
StatusCode: 429,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
retryServerErrors: true,
checkOK: true,
checkErr: nil,
},
"500-no-server-errors": {
resp: &http.Response{StatusCode: 500},
resp: &http.Response{
StatusCode: 500,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
checkOK: false,
checkErr: nil,
},
"500-with-server-errors": {
resp: &http.Response{StatusCode: 500},
resp: &http.Response{
StatusCode: 500,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
retryServerErrors: true,
checkOK: true,
Expand Down
Loading

0 comments on commit 3cd4759

Please sign in to comment.