(ctx context.Context, orgID string, param params.UpdateEntityParams)
| 152 | } |
| 153 | |
| 154 | func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (paramOrg params.Organization, err error) { |
| 155 | var rowsAffected int64 |
| 156 | defer func() { |
| 157 | if err == nil && rowsAffected > 0 { |
| 158 | s.sendNotify(common.OrganizationEntityType, common.UpdateOperation, paramOrg) |
| 159 | } |
| 160 | }() |
| 161 | var org Organization |
| 162 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 163 | var err error |
| 164 | org, err = s.getOrgByID(ctx, tx, orgID, "Endpoint") |
| 165 | if err != nil { |
| 166 | return fmt.Errorf("error fetching org: %w", err) |
| 167 | } |
| 168 | if org.EndpointName == nil { |
| 169 | return fmt.Errorf("error org has no endpoint: %w", runnerErrors.ErrUnprocessable) |
| 170 | } |
| 171 | |
| 172 | updates := make(map[string]interface{}) |
| 173 | |
| 174 | if err := s.updateEntityCredentials(ctx, tx, &org, param.CredentialsName, updates); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | if param.WebhookSecret != "" { |
| 179 | // Decrypt existing secret to compare |
| 180 | existingSecret, err := util.Unseal(org.WebhookSecret, []byte(s.cfg.Passphrase)) |
| 181 | if err != nil { |
| 182 | return fmt.Errorf("failed to decrypt existing webhook secret: %w", err) |
| 183 | } |
| 184 | // Only update if the webhook secret has changed |
| 185 | if string(existingSecret) != param.WebhookSecret { |
| 186 | secret, err := util.Seal([]byte(param.WebhookSecret), []byte(s.cfg.Passphrase)) |
| 187 | if err != nil { |
| 188 | return fmt.Errorf("saving org: failed to encrypt string: %w", err) |
| 189 | } |
| 190 | updates["webhook_secret"] = secret |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if param.PoolManagerStatus != nil { |
| 195 | if param.PoolManagerStatus.IsRunning != org.PoolManagerRunning { |
| 196 | updates["pool_manager_running"] = param.PoolManagerStatus.IsRunning |
| 197 | } |
| 198 | if param.PoolManagerStatus.FailureReason != org.PoolManagerFailureReason { |
| 199 | updates["pool_manager_failure_reason"] = param.PoolManagerStatus.FailureReason |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if param.PoolBalancerType != "" && param.PoolBalancerType != org.PoolBalancerType { |
| 204 | updates["pool_balancer_type"] = param.PoolBalancerType |
| 205 | } |
| 206 | |
| 207 | if param.AgentMode != nil && *param.AgentMode != org.AgentMode { |
| 208 | updates["agent_mode"] = *param.AgentMode |
| 209 | } |
| 210 | |
| 211 | if len(updates) > 0 { |
no test coverage detected