|
| 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