Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Scalr/go-scalr into revert-106-r…
Browse files Browse the repository at this point in the history
…evert-104-feature/SCALRCORE-24861
  • Loading branch information
kozliuk committed Apr 24, 2023
2 parents 75bf8ca + 39bde2c commit 5437ed6
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
.idea/
covprofile
27 changes: 27 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,30 @@ func assignTagsToEnvironment(t *testing.T, client *Client, environment *Environm
t.Fatal(err)
}
}

func createWebhookIntegration(
t *testing.T, client *Client, isShared bool, envs []*Environment,
) (*WebhookIntegration, func()) {
ctx := context.Background()
opts := WebhookIntegrationCreateOptions{
Name: String("tst-" + randomString(t)),
Enabled: Bool(true),
IsShared: Bool(isShared),
Url: String("https://example.com"),
Account: &Account{ID: defaultAccountID},
Events: []*EventDefinition{{ID: "run:completed"}},
Environments: envs,
}
w, err := client.WebhookIntegrations.Create(ctx, opts)
if err != nil {
t.Fatal(err)
}

return w, func() {
if err := client.WebhookIntegrations.Delete(ctx, w.ID); err != nil {
t.Errorf("Error destroying webhook integration! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"Webhook: %s\nError: %s", w.ID, err)
}
}
}
31 changes: 0 additions & 31 deletions module_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type ModuleVersions interface {
List(ctx context.Context, options ModuleVersionListOptions) (*ModuleVersionList, error)
// Read a module version by its ID.
Read(ctx context.Context, moduleVersionID string) (*ModuleVersion, error)
// ReadBySemanticVersion read module version by module and semantic version
ReadBySemanticVersion(ctx context.Context, moduleId string, version string) (*ModuleVersion, error)
}

// moduleVersions implements ModuleVersions.
Expand Down Expand Up @@ -105,32 +103,3 @@ func (s *moduleVersions) List(ctx context.Context, options ModuleVersionListOpti

return mv, nil
}

func (s *moduleVersions) ReadBySemanticVersion(ctx context.Context, moduleID string, version string) (*ModuleVersion, error) {
if !validStringID(&moduleID) {
return nil, errors.New("invalid value for module id")
}

v := &version
if !validString(v) {
return nil, errors.New("invalid value for version")
}

req, err := s.client.newRequest("GET", "module-versions", &ModuleVersionListOptions{Module: moduleID, Version: v})
if err != nil {
return nil, err
}

mvl := &ModuleVersionList{}
err = s.client.do(ctx, req, mvl)
if err != nil {
return nil, err
}
if len(mvl.Items) != 1 {
return nil, ResourceNotFoundError{
Message: fmt.Sprintf("ModuleVersion with Module ID '%v' and version '%v' not found.", moduleID, version),
}
}

return mvl.Items[0], nil
}
2 changes: 2 additions & 0 deletions scalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type Client struct {
VcsProviders VcsProviders
VcsRevisions VcsRevisions
Webhooks Webhooks
WebhookIntegrations WebhookIntegrations
WorkspaceTags WorkspaceTags
Workspaces Workspaces
}
Expand Down Expand Up @@ -242,6 +243,7 @@ func NewClient(cfg *Config) (*Client, error) {
client.VcsProviders = &vcsProviders{client: client}
client.VcsRevisions = &vcsRevisions{client: client}
client.Webhooks = &webhooks{client: client}
client.WebhookIntegrations = &webhookIntegrations{client: client}
client.WorkspaceTags = &workspaceTag{client: client}
client.Workspaces = &workspaces{client: client}
return client, nil
Expand Down
196 changes: 196 additions & 0 deletions webhook_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package scalr

import (
"context"
"errors"
"fmt"
"net/url"
"time"
)

// Compile-time proof of interface implementation.
var _ WebhookIntegrations = (*webhookIntegrations)(nil)

type WebhookIntegrations interface {
List(ctx context.Context, options WebhookIntegrationListOptions) (*WebhookIntegrationList, error)
Create(ctx context.Context, options WebhookIntegrationCreateOptions) (*WebhookIntegration, error)
Read(ctx context.Context, wi string) (*WebhookIntegration, error)
Update(ctx context.Context, wi string, options WebhookIntegrationUpdateOptions) (*WebhookIntegration, error)
Delete(ctx context.Context, wi string) error
}

// webhookIntegrations implements WebhookIntegrations.
type webhookIntegrations struct {
client *Client
}

type WebhookIntegrationList struct {
*Pagination
Items []*WebhookIntegration
}

// WebhookIntegration represents a Scalr IACP webhook integration.
type WebhookIntegration struct {
ID string `jsonapi:"primary,webhook-integrations"`
Name string `jsonapi:"attr,name"`
Enabled bool `jsonapi:"attr,enabled"`
IsShared bool `jsonapi:"attr,is-shared"`
LastTriggeredAt *time.Time `jsonapi:"attr,last-triggered-at,iso8601"`
Url string `jsonapi:"attr,url"`
SecretKey string `jsonapi:"attr,secret-key"`
Timeout int `jsonapi:"attr,timeout"`
MaxAttempts int `jsonapi:"attr,max-attempts"`
HttpMethod string `jsonapi:"attr,http-method"`
Headers []*WebhookHeader `jsonapi:"attr,headers"`

// Relations
Environments []*Environment `jsonapi:"relation,environments"`
Account *Account `jsonapi:"relation,account"`
Events []*EventDefinition `jsonapi:"relation,events"`
}

type WebhookHeader struct {
Name string `json:"name"`
Value string `json:"value"`
Sensitive bool `json:"sensitive"`
}

type WebhookIntegrationListOptions struct {
ListOptions

Query *string `url:"query,omitempty"`
Sort *string `url:"sort,omitempty"`
Enabled *bool `url:"filter[enabled],omitempty"`
Event *string `url:"filter[event],omitempty"`
Environment *string `url:"filter[environment],omitempty"`
Account *string `url:"filter[account],omitempty"`
}

type WebhookIntegrationCreateOptions struct {
ID string `jsonapi:"primary,webhook-integrations"`
Name *string `jsonapi:"attr,name"`
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
IsShared *bool `jsonapi:"attr,is-shared,omitempty"`

Url *string `jsonapi:"attr,url"`
SecretKey *string `jsonapi:"attr,secret-key,omitempty"`
Timeout *int `jsonapi:"attr,timeout,omitempty"`
MaxAttempts *int `jsonapi:"attr,max-attempts,omitempty"`
Headers []*WebhookHeader `jsonapi:"attr,headers,omitempty"`

Environments []*Environment `jsonapi:"relation,environments,omitempty"`
Account *Account `jsonapi:"relation,account"`
Events []*EventDefinition `jsonapi:"relation,events,omitempty"`
}

type WebhookIntegrationUpdateOptions struct {
ID string `jsonapi:"primary,webhook-integrations"`
Name *string `jsonapi:"attr,name,omitempty"`
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
IsShared *bool `jsonapi:"attr,is-shared,omitempty"`

Url *string `jsonapi:"attr,url,omitempty"`
SecretKey *string `jsonapi:"attr,secret-key,omitempty"`
Timeout *int `jsonapi:"attr,timeout,omitempty"`
MaxAttempts *int `jsonapi:"attr,max-attempts,omitempty"`
Headers []*WebhookHeader `jsonapi:"attr,headers,omitempty"`

Environments []*Environment `jsonapi:"relation,environments"`
Events []*EventDefinition `jsonapi:"relation,events"`
}

func (s *webhookIntegrations) List(
ctx context.Context, options WebhookIntegrationListOptions,
) (*WebhookIntegrationList, error) {
req, err := s.client.newRequest("GET", "integrations/webhooks", &options)
if err != nil {
return nil, err
}

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

return wl, nil
}

func (s *webhookIntegrations) Create(
ctx context.Context, options WebhookIntegrationCreateOptions,
) (*WebhookIntegration, error) {
// Make sure we don't send a user provided ID.
options.ID = ""

req, err := s.client.newRequest("POST", "integrations/webhooks", &options)
if err != nil {
return nil, err
}

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

return w, nil
}

func (s *webhookIntegrations) Read(ctx context.Context, wi string) (*WebhookIntegration, error) {
if !validStringID(&wi) {
return nil, errors.New("invalid value for webhook ID")
}

u := fmt.Sprintf("integrations/webhooks/%s", url.QueryEscape(wi))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}

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

return w, nil
}

func (s *webhookIntegrations) Update(
ctx context.Context, wi string, options WebhookIntegrationUpdateOptions,
) (*WebhookIntegration, error) {
if !validStringID(&wi) {
return nil, errors.New("invalid value for webhook ID")
}

// Make sure we don't send a user provided ID.
options.ID = ""

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

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

return w, nil
}

func (s *webhookIntegrations) Delete(ctx context.Context, wi string) error {
if !validStringID(&wi) {
return errors.New("invalid value for webhook ID")
}

u := fmt.Sprintf("integrations/webhooks/%s", url.QueryEscape(wi))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}

return s.client.do(ctx, req, nil)
}

0 comments on commit 5437ed6

Please sign in to comment.