|
| 1 | +package scalr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | +) |
| 9 | + |
| 10 | +// Compile-time proof of interface implementation. |
| 11 | +var _ EnvironmentHooks = (*environmentHooks)(nil) |
| 12 | + |
| 13 | +// EnvironmentHooks interface for environment hook related operations |
| 14 | +type EnvironmentHooks interface { |
| 15 | + List(ctx context.Context, options EnvironmentHookListOptions) (*EnvironmentHookList, error) |
| 16 | + Create(ctx context.Context, options EnvironmentHookCreateOptions) (*EnvironmentHook, error) |
| 17 | + Read(ctx context.Context, id string) (*EnvironmentHook, error) |
| 18 | + Update(ctx context.Context, id string, options EnvironmentHookUpdateOptions) (*EnvironmentHook, error) |
| 19 | + Delete(ctx context.Context, id string) error |
| 20 | +} |
| 21 | + |
| 22 | +// environmentHooks implements EnvironmentHooks interface |
| 23 | +type environmentHooks struct { |
| 24 | + client *Client |
| 25 | +} |
| 26 | + |
| 27 | +// EnvironmentHookList represents a list of environment hooks |
| 28 | +type EnvironmentHookList struct { |
| 29 | + *Pagination |
| 30 | + Items []*EnvironmentHook |
| 31 | +} |
| 32 | + |
| 33 | +// EnvironmentHook represents a Scalr environment hook |
| 34 | +type EnvironmentHook struct { |
| 35 | + ID string `jsonapi:"primary,hook-environment-links"` |
| 36 | + Events []string `jsonapi:"attr,events"` |
| 37 | + |
| 38 | + // Relations |
| 39 | + Environment *Environment `jsonapi:"relation,environment"` |
| 40 | + Hook *Hook `jsonapi:"relation,hook"` |
| 41 | +} |
| 42 | + |
| 43 | +// EnvironmentHookListOptions represents the options for listing environment hooks |
| 44 | +type EnvironmentHookListOptions struct { |
| 45 | + ListOptions |
| 46 | + |
| 47 | + Environment *string `url:"filter[environment],omitempty"` |
| 48 | + Events *string `url:"filter[events],omitempty"` |
| 49 | + Query *string `url:"query,omitempty"` |
| 50 | + Sort *string `url:"sort,omitempty"` |
| 51 | + Include *string `url:"include,omitempty"` |
| 52 | +} |
| 53 | + |
| 54 | +// EnvironmentHookCreateOptions represents the options for creating an environment hook |
| 55 | +type EnvironmentHookCreateOptions struct { |
| 56 | + ID string `jsonapi:"primary,hook-environment-links"` |
| 57 | + Events []string `jsonapi:"attr,events"` |
| 58 | + |
| 59 | + // Relations |
| 60 | + Environment *Environment `jsonapi:"relation,environment"` |
| 61 | + Hook *Hook `jsonapi:"relation,hook"` |
| 62 | +} |
| 63 | + |
| 64 | +// EnvironmentHookUpdateOptions represents the options for updating an environment hook |
| 65 | +type EnvironmentHookUpdateOptions struct { |
| 66 | + ID string `jsonapi:"primary,hook-environment-links"` |
| 67 | + Events *[]string `jsonapi:"attr,events,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +// List lists all environment hooks based on the provided options |
| 71 | +func (s *environmentHooks) List(ctx context.Context, options EnvironmentHookListOptions) (*EnvironmentHookList, error) { |
| 72 | + if options.Environment == nil { |
| 73 | + return nil, errors.New("environment is required") |
| 74 | + } |
| 75 | + |
| 76 | + req, err := s.client.newRequest("GET", "hook-environment-links", &options) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + hookList := &EnvironmentHookList{} |
| 82 | + err = s.client.do(ctx, req, hookList) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + return hookList, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Create creates a new environment hook |
| 91 | +func (s *environmentHooks) Create(ctx context.Context, options EnvironmentHookCreateOptions) (*EnvironmentHook, error) { |
| 92 | + if err := options.valid(); err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + // Make sure we don't send a user provided ID |
| 97 | + options.ID = "" |
| 98 | + |
| 99 | + req, err := s.client.newRequest("POST", "hook-environment-links", &options) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + hook := &EnvironmentHook{} |
| 105 | + err = s.client.do(ctx, req, hook) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + return hook, nil |
| 111 | +} |
| 112 | + |
| 113 | +// Read reads an environment hook by its ID |
| 114 | +func (s *environmentHooks) Read(ctx context.Context, id string) (*EnvironmentHook, error) { |
| 115 | + if !validStringID(&id) { |
| 116 | + return nil, errors.New("invalid value for Environment Hook ID") |
| 117 | + } |
| 118 | + |
| 119 | + u := fmt.Sprintf("hook-environment-links/%s", url.QueryEscape(id)) |
| 120 | + req, err := s.client.newRequest("GET", u, nil) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + hook := &EnvironmentHook{} |
| 126 | + err = s.client.do(ctx, req, hook) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + return hook, nil |
| 132 | +} |
| 133 | + |
| 134 | +// Update updates an environment hook by its ID |
| 135 | +func (s *environmentHooks) Update(ctx context.Context, id string, options EnvironmentHookUpdateOptions) (*EnvironmentHook, error) { |
| 136 | + if !validStringID(&id) { |
| 137 | + return nil, errors.New("invalid value for Environment Hook ID") |
| 138 | + } |
| 139 | + |
| 140 | + if err := options.valid(); err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + // Make sure we don't send a user provided ID |
| 145 | + options.ID = "" |
| 146 | + |
| 147 | + u := fmt.Sprintf("hook-environment-links/%s", url.QueryEscape(id)) |
| 148 | + req, err := s.client.newRequest("PATCH", u, &options) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + hook := &EnvironmentHook{} |
| 154 | + err = s.client.do(ctx, req, hook) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + return hook, nil |
| 160 | +} |
| 161 | + |
| 162 | +// Delete deletes an environment hook by its ID |
| 163 | +func (s *environmentHooks) Delete(ctx context.Context, id string) error { |
| 164 | + if !validStringID(&id) { |
| 165 | + return errors.New("invalid value for Environment Hook ID") |
| 166 | + } |
| 167 | + |
| 168 | + u := fmt.Sprintf("hook-environment-links/%s", url.QueryEscape(id)) |
| 169 | + req, err := s.client.newRequest("DELETE", u, nil) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + return s.client.do(ctx, req, nil) |
| 175 | +} |
| 176 | + |
| 177 | +// valid validates the environment hook create options |
| 178 | +func (o EnvironmentHookCreateOptions) valid() error { |
| 179 | + if o.Environment == nil { |
| 180 | + return errors.New("environment is required") |
| 181 | + } |
| 182 | + if o.Hook == nil { |
| 183 | + return errors.New("hook is required") |
| 184 | + } |
| 185 | + return nil |
| 186 | +} |
| 187 | + |
| 188 | +// valid validates the environment hook update options |
| 189 | +func (o EnvironmentHookUpdateOptions) valid() error { |
| 190 | + return nil |
| 191 | +} |
0 commit comments