Skip to content

Commit 5437ed6

Browse files
committed
Merge branch 'develop' of github.com:Scalr/go-scalr into revert-106-revert-104-feature/SCALRCORE-24861
2 parents 75bf8ca + 39bde2c commit 5437ed6

File tree

6 files changed

+389
-31
lines changed

6 files changed

+389
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode/
22
.idea/
3+
covprofile

helper_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,30 @@ func assignTagsToEnvironment(t *testing.T, client *Client, environment *Environm
486486
t.Fatal(err)
487487
}
488488
}
489+
490+
func createWebhookIntegration(
491+
t *testing.T, client *Client, isShared bool, envs []*Environment,
492+
) (*WebhookIntegration, func()) {
493+
ctx := context.Background()
494+
opts := WebhookIntegrationCreateOptions{
495+
Name: String("tst-" + randomString(t)),
496+
Enabled: Bool(true),
497+
IsShared: Bool(isShared),
498+
Url: String("https://example.com"),
499+
Account: &Account{ID: defaultAccountID},
500+
Events: []*EventDefinition{{ID: "run:completed"}},
501+
Environments: envs,
502+
}
503+
w, err := client.WebhookIntegrations.Create(ctx, opts)
504+
if err != nil {
505+
t.Fatal(err)
506+
}
507+
508+
return w, func() {
509+
if err := client.WebhookIntegrations.Delete(ctx, w.ID); err != nil {
510+
t.Errorf("Error destroying webhook integration! WARNING: Dangling resources\n"+
511+
"may exist! The full error is shown below.\n\n"+
512+
"Webhook: %s\nError: %s", w.ID, err)
513+
}
514+
}
515+
}

module_version.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ type ModuleVersions interface {
1616
List(ctx context.Context, options ModuleVersionListOptions) (*ModuleVersionList, error)
1717
// Read a module version by its ID.
1818
Read(ctx context.Context, moduleVersionID string) (*ModuleVersion, error)
19-
// ReadBySemanticVersion read module version by module and semantic version
20-
ReadBySemanticVersion(ctx context.Context, moduleId string, version string) (*ModuleVersion, error)
2119
}
2220

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

106104
return mv, nil
107105
}
108-
109-
func (s *moduleVersions) ReadBySemanticVersion(ctx context.Context, moduleID string, version string) (*ModuleVersion, error) {
110-
if !validStringID(&moduleID) {
111-
return nil, errors.New("invalid value for module id")
112-
}
113-
114-
v := &version
115-
if !validString(v) {
116-
return nil, errors.New("invalid value for version")
117-
}
118-
119-
req, err := s.client.newRequest("GET", "module-versions", &ModuleVersionListOptions{Module: moduleID, Version: v})
120-
if err != nil {
121-
return nil, err
122-
}
123-
124-
mvl := &ModuleVersionList{}
125-
err = s.client.do(ctx, req, mvl)
126-
if err != nil {
127-
return nil, err
128-
}
129-
if len(mvl.Items) != 1 {
130-
return nil, ResourceNotFoundError{
131-
Message: fmt.Sprintf("ModuleVersion with Module ID '%v' and version '%v' not found.", moduleID, version),
132-
}
133-
}
134-
135-
return mvl.Items[0], nil
136-
}

scalr.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ type Client struct {
145145
VcsProviders VcsProviders
146146
VcsRevisions VcsRevisions
147147
Webhooks Webhooks
148+
WebhookIntegrations WebhookIntegrations
148149
WorkspaceTags WorkspaceTags
149150
Workspaces Workspaces
150151
}
@@ -242,6 +243,7 @@ func NewClient(cfg *Config) (*Client, error) {
242243
client.VcsProviders = &vcsProviders{client: client}
243244
client.VcsRevisions = &vcsRevisions{client: client}
244245
client.Webhooks = &webhooks{client: client}
246+
client.WebhookIntegrations = &webhookIntegrations{client: client}
245247
client.WorkspaceTags = &workspaceTag{client: client}
246248
client.Workspaces = &workspaces{client: client}
247249
return client, nil

webhook_integration.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package scalr
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/url"
8+
"time"
9+
)
10+
11+
// Compile-time proof of interface implementation.
12+
var _ WebhookIntegrations = (*webhookIntegrations)(nil)
13+
14+
type WebhookIntegrations interface {
15+
List(ctx context.Context, options WebhookIntegrationListOptions) (*WebhookIntegrationList, error)
16+
Create(ctx context.Context, options WebhookIntegrationCreateOptions) (*WebhookIntegration, error)
17+
Read(ctx context.Context, wi string) (*WebhookIntegration, error)
18+
Update(ctx context.Context, wi string, options WebhookIntegrationUpdateOptions) (*WebhookIntegration, error)
19+
Delete(ctx context.Context, wi string) error
20+
}
21+
22+
// webhookIntegrations implements WebhookIntegrations.
23+
type webhookIntegrations struct {
24+
client *Client
25+
}
26+
27+
type WebhookIntegrationList struct {
28+
*Pagination
29+
Items []*WebhookIntegration
30+
}
31+
32+
// WebhookIntegration represents a Scalr IACP webhook integration.
33+
type WebhookIntegration struct {
34+
ID string `jsonapi:"primary,webhook-integrations"`
35+
Name string `jsonapi:"attr,name"`
36+
Enabled bool `jsonapi:"attr,enabled"`
37+
IsShared bool `jsonapi:"attr,is-shared"`
38+
LastTriggeredAt *time.Time `jsonapi:"attr,last-triggered-at,iso8601"`
39+
Url string `jsonapi:"attr,url"`
40+
SecretKey string `jsonapi:"attr,secret-key"`
41+
Timeout int `jsonapi:"attr,timeout"`
42+
MaxAttempts int `jsonapi:"attr,max-attempts"`
43+
HttpMethod string `jsonapi:"attr,http-method"`
44+
Headers []*WebhookHeader `jsonapi:"attr,headers"`
45+
46+
// Relations
47+
Environments []*Environment `jsonapi:"relation,environments"`
48+
Account *Account `jsonapi:"relation,account"`
49+
Events []*EventDefinition `jsonapi:"relation,events"`
50+
}
51+
52+
type WebhookHeader struct {
53+
Name string `json:"name"`
54+
Value string `json:"value"`
55+
Sensitive bool `json:"sensitive"`
56+
}
57+
58+
type WebhookIntegrationListOptions struct {
59+
ListOptions
60+
61+
Query *string `url:"query,omitempty"`
62+
Sort *string `url:"sort,omitempty"`
63+
Enabled *bool `url:"filter[enabled],omitempty"`
64+
Event *string `url:"filter[event],omitempty"`
65+
Environment *string `url:"filter[environment],omitempty"`
66+
Account *string `url:"filter[account],omitempty"`
67+
}
68+
69+
type WebhookIntegrationCreateOptions struct {
70+
ID string `jsonapi:"primary,webhook-integrations"`
71+
Name *string `jsonapi:"attr,name"`
72+
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
73+
IsShared *bool `jsonapi:"attr,is-shared,omitempty"`
74+
75+
Url *string `jsonapi:"attr,url"`
76+
SecretKey *string `jsonapi:"attr,secret-key,omitempty"`
77+
Timeout *int `jsonapi:"attr,timeout,omitempty"`
78+
MaxAttempts *int `jsonapi:"attr,max-attempts,omitempty"`
79+
Headers []*WebhookHeader `jsonapi:"attr,headers,omitempty"`
80+
81+
Environments []*Environment `jsonapi:"relation,environments,omitempty"`
82+
Account *Account `jsonapi:"relation,account"`
83+
Events []*EventDefinition `jsonapi:"relation,events,omitempty"`
84+
}
85+
86+
type WebhookIntegrationUpdateOptions struct {
87+
ID string `jsonapi:"primary,webhook-integrations"`
88+
Name *string `jsonapi:"attr,name,omitempty"`
89+
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
90+
IsShared *bool `jsonapi:"attr,is-shared,omitempty"`
91+
92+
Url *string `jsonapi:"attr,url,omitempty"`
93+
SecretKey *string `jsonapi:"attr,secret-key,omitempty"`
94+
Timeout *int `jsonapi:"attr,timeout,omitempty"`
95+
MaxAttempts *int `jsonapi:"attr,max-attempts,omitempty"`
96+
Headers []*WebhookHeader `jsonapi:"attr,headers,omitempty"`
97+
98+
Environments []*Environment `jsonapi:"relation,environments"`
99+
Events []*EventDefinition `jsonapi:"relation,events"`
100+
}
101+
102+
func (s *webhookIntegrations) List(
103+
ctx context.Context, options WebhookIntegrationListOptions,
104+
) (*WebhookIntegrationList, error) {
105+
req, err := s.client.newRequest("GET", "integrations/webhooks", &options)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
wl := &WebhookIntegrationList{}
111+
err = s.client.do(ctx, req, wl)
112+
if err != nil {
113+
return nil, err
114+
}
115+
116+
return wl, nil
117+
}
118+
119+
func (s *webhookIntegrations) Create(
120+
ctx context.Context, options WebhookIntegrationCreateOptions,
121+
) (*WebhookIntegration, error) {
122+
// Make sure we don't send a user provided ID.
123+
options.ID = ""
124+
125+
req, err := s.client.newRequest("POST", "integrations/webhooks", &options)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
w := &WebhookIntegration{}
131+
err = s.client.do(ctx, req, w)
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
return w, nil
137+
}
138+
139+
func (s *webhookIntegrations) Read(ctx context.Context, wi string) (*WebhookIntegration, error) {
140+
if !validStringID(&wi) {
141+
return nil, errors.New("invalid value for webhook ID")
142+
}
143+
144+
u := fmt.Sprintf("integrations/webhooks/%s", url.QueryEscape(wi))
145+
req, err := s.client.newRequest("GET", u, nil)
146+
if err != nil {
147+
return nil, err
148+
}
149+
150+
w := &WebhookIntegration{}
151+
err = s.client.do(ctx, req, w)
152+
if err != nil {
153+
return nil, err
154+
}
155+
156+
return w, nil
157+
}
158+
159+
func (s *webhookIntegrations) Update(
160+
ctx context.Context, wi string, options WebhookIntegrationUpdateOptions,
161+
) (*WebhookIntegration, error) {
162+
if !validStringID(&wi) {
163+
return nil, errors.New("invalid value for webhook ID")
164+
}
165+
166+
// Make sure we don't send a user provided ID.
167+
options.ID = ""
168+
169+
u := fmt.Sprintf("integrations/webhooks/%s", url.QueryEscape(wi))
170+
req, err := s.client.newRequest("PATCH", u, &options)
171+
if err != nil {
172+
return nil, err
173+
}
174+
175+
w := &WebhookIntegration{}
176+
err = s.client.do(ctx, req, w)
177+
if err != nil {
178+
return nil, err
179+
}
180+
181+
return w, nil
182+
}
183+
184+
func (s *webhookIntegrations) Delete(ctx context.Context, wi string) error {
185+
if !validStringID(&wi) {
186+
return errors.New("invalid value for webhook ID")
187+
}
188+
189+
u := fmt.Sprintf("integrations/webhooks/%s", url.QueryEscape(wi))
190+
req, err := s.client.newRequest("DELETE", u, nil)
191+
if err != nil {
192+
return err
193+
}
194+
195+
return s.client.do(ctx, req, nil)
196+
}

0 commit comments

Comments
 (0)