|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +func (c *Client) CreateSpaceEmailNotification(space_name string, notification_name string, environment_launched bool, |
| 12 | + environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool, |
| 13 | + drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool, |
| 14 | + collaborator_added bool, action_failed bool, environment_ending_failed bool, environment_ended bool, |
| 15 | + environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, idle_reminder []int64) (string, error) { |
| 16 | + |
| 17 | + data := SubscriptionsRequest{ |
| 18 | + Name: notification_name, |
| 19 | + Description: "", |
| 20 | + Target: SubscriptionsTargetRequest{ |
| 21 | + Type: "Email", |
| 22 | + Description: "", |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + if environment_launched { |
| 27 | + data.Events = append(data.Events, "EnvironmentLaunched") |
| 28 | + } |
| 29 | + if environment_deployed { |
| 30 | + data.Events = append(data.Events, "EnvironmentDeployed") |
| 31 | + } |
| 32 | + if environment_force_ended { |
| 33 | + data.Events = append(data.Events, "EnvironmentForceEnded") |
| 34 | + } |
| 35 | + |
| 36 | + if environment_idle { |
| 37 | + data.Events = append(data.Events, "EnvironmentIdle") |
| 38 | + data.IdleReminder = []ReminderRequest{} |
| 39 | + for _, idleNumber := range idle_reminder { |
| 40 | + data.IdleReminder = append(data.IdleReminder, ReminderRequest{TimeInHours: idleNumber}) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if environment_extended { |
| 45 | + data.Events = append(data.Events, "EnvironmentExtended") |
| 46 | + } |
| 47 | + if drift_detected { |
| 48 | + data.Events = append(data.Events, "DriftDetected") |
| 49 | + } |
| 50 | + if workflow_failed { |
| 51 | + data.Events = append(data.Events, "WorkflowFailed") |
| 52 | + } |
| 53 | + if workflow_started { |
| 54 | + data.Events = append(data.Events, "WorkflowStarted") |
| 55 | + data.WorkflowStartReminder = workflow_start_reminder |
| 56 | + } |
| 57 | + if updates_detected { |
| 58 | + data.Events = append(data.Events, "UpdatesDetected") |
| 59 | + } |
| 60 | + if collaborator_added { |
| 61 | + data.Events = append(data.Events, "CollaboratorAdded") |
| 62 | + } |
| 63 | + if action_failed { |
| 64 | + data.Events = append(data.Events, "ActionFailed") |
| 65 | + } |
| 66 | + if environment_ending_failed { |
| 67 | + data.Events = append(data.Events, "EnvironmentEndingFailed") |
| 68 | + } |
| 69 | + if environment_ended { |
| 70 | + data.Events = append(data.Events, "EnvironmentEnded") |
| 71 | + data.EndThreshold = end_threshold |
| 72 | + } |
| 73 | + if environment_active_with_error { |
| 74 | + data.Events = append(data.Events, "EnvironmentActiveWithError") |
| 75 | + } |
| 76 | + |
| 77 | + payload, err := json.Marshal(data) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("impossible to marshall update space request: %s", err) |
| 80 | + } |
| 81 | + |
| 82 | + req, err := http.NewRequest("POST", fmt.Sprintf("%sapi/spaces/%s/subscriptions", c.HostURL, space_name), bytes.NewReader(payload)) |
| 83 | + if err != nil { |
| 84 | + return "", err |
| 85 | + } |
| 86 | + |
| 87 | + req.Header.Add("Content-Type", "application/json") |
| 88 | + req.Header.Add("Accept", "application/json") |
| 89 | + |
| 90 | + body, err := c.doRequest(req, &c.Token) |
| 91 | + if err != nil { |
| 92 | + return "", err |
| 93 | + } |
| 94 | + |
| 95 | + return string(body), nil |
| 96 | +} |
| 97 | + |
| 98 | +func (c *Client) UpdateSpaceEmailNotification(notification_id string, space_name string, notification_name string, environment_launched bool, |
| 99 | + environment_deployed bool, environment_force_ended bool, environment_idle bool, environment_extended bool, |
| 100 | + drift_detected bool, workflow_failed bool, workflow_started bool, updates_detected bool, |
| 101 | + collaborator_added bool, action_failed bool, environment_ending_failed bool, environment_ended bool, |
| 102 | + environment_active_with_error bool, workflow_start_reminder int64, end_threshold int64, idle_reminder []int64) (string, error) { |
| 103 | + |
| 104 | + data := SubscriptionsRequest{ |
| 105 | + Name: notification_name, |
| 106 | + Description: "", |
| 107 | + Target: SubscriptionsTargetRequest{ |
| 108 | + Type: "Email", |
| 109 | + Description: "", |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + if environment_launched { |
| 114 | + data.Events = append(data.Events, "EnvironmentLaunched") |
| 115 | + } |
| 116 | + if environment_deployed { |
| 117 | + data.Events = append(data.Events, "EnvironmentDeployed") |
| 118 | + } |
| 119 | + if environment_force_ended { |
| 120 | + data.Events = append(data.Events, "EnvironmentForceEnded") |
| 121 | + } |
| 122 | + |
| 123 | + if environment_idle { |
| 124 | + data.Events = append(data.Events, "EnvironmentIdle") |
| 125 | + data.IdleReminder = []ReminderRequest{} |
| 126 | + for _, idleNumber := range idle_reminder { |
| 127 | + data.IdleReminder = append(data.IdleReminder, ReminderRequest{TimeInHours: idleNumber}) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if environment_extended { |
| 132 | + data.Events = append(data.Events, "EnvironmentExtended") |
| 133 | + } |
| 134 | + if drift_detected { |
| 135 | + data.Events = append(data.Events, "DriftDetected") |
| 136 | + } |
| 137 | + if workflow_failed { |
| 138 | + data.Events = append(data.Events, "WorkflowFailed") |
| 139 | + } |
| 140 | + if workflow_started { |
| 141 | + data.Events = append(data.Events, "WorkflowStarted") |
| 142 | + data.WorkflowStartReminder = workflow_start_reminder |
| 143 | + } |
| 144 | + if updates_detected { |
| 145 | + data.Events = append(data.Events, "UpdatesDetected") |
| 146 | + } |
| 147 | + if collaborator_added { |
| 148 | + data.Events = append(data.Events, "CollaboratorAdded") |
| 149 | + } |
| 150 | + if action_failed { |
| 151 | + data.Events = append(data.Events, "ActionFailed") |
| 152 | + } |
| 153 | + if environment_ending_failed { |
| 154 | + data.Events = append(data.Events, "EnvironmentEndingFailed") |
| 155 | + } |
| 156 | + if environment_ended { |
| 157 | + data.Events = append(data.Events, "EnvironmentEnded") |
| 158 | + data.EndThreshold = end_threshold |
| 159 | + } |
| 160 | + if environment_active_with_error { |
| 161 | + data.Events = append(data.Events, "EnvironmentActiveWithError") |
| 162 | + } |
| 163 | + |
| 164 | + payload, err := json.Marshal(data) |
| 165 | + if err != nil { |
| 166 | + log.Fatalf("impossible to marshall update space request: %s", err) |
| 167 | + } |
| 168 | + |
| 169 | + req, err := http.NewRequest("PUT", fmt.Sprintf("%sapi/spaces/%s/subscriptions?subscriptionId=%s", c.HostURL, space_name, notification_id), bytes.NewReader(payload)) |
| 170 | + if err != nil { |
| 171 | + return "", err |
| 172 | + } |
| 173 | + |
| 174 | + req.Header.Add("Content-Type", "application/json") |
| 175 | + req.Header.Add("Accept", "application/json") |
| 176 | + |
| 177 | + body, err := c.doRequest(req, &c.Token) |
| 178 | + if err != nil { |
| 179 | + return "", err |
| 180 | + } |
| 181 | + |
| 182 | + return string(body), nil |
| 183 | +} |
| 184 | + |
| 185 | +func (c *Client) DeleteSpaceNotification(space_name string, notification_id string) error { |
| 186 | + req, err := http.NewRequest("DELETE", fmt.Sprintf("%sapi/spaces/%s/subscriptions?subscriptionId=%s", c.HostURL, space_name, notification_id), nil) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + req.Header.Add("Content-Type", "application/json") |
| 192 | + req.Header.Add("Accept", "application/json") |
| 193 | + |
| 194 | + _, err = c.doRequest(req, &c.Token) |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + |
| 199 | + return nil |
| 200 | +} |
0 commit comments