|
|
|
@ -36,7 +36,7 @@ func NewContactPointService(store store.AlertingStore, encryptionService secrets |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ecp *ContactPointService) GetContactPoints(ctx context.Context, orgID int64) ([]apimodels.EmbeddedContactPoint, error) { |
|
|
|
|
cfg, _, err := ecp.getCurrentConfig(ctx, orgID) |
|
|
|
|
revision, err := getLastConfiguration(ctx, orgID, ecp.amStore) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -45,7 +45,7 @@ func (ecp *ContactPointService) GetContactPoints(ctx context.Context, orgID int6 |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
contactPoints := []apimodels.EmbeddedContactPoint{} |
|
|
|
|
for _, contactPoint := range cfg.GetGrafanaReceiverMap() { |
|
|
|
|
for _, contactPoint := range revision.cfg.GetGrafanaReceiverMap() { |
|
|
|
|
embeddedContactPoint := apimodels.EmbeddedContactPoint{ |
|
|
|
|
UID: contactPoint.UID, |
|
|
|
|
Type: contactPoint.Type, |
|
|
|
@ -77,11 +77,11 @@ func (ecp *ContactPointService) GetContactPoints(ctx context.Context, orgID int6 |
|
|
|
|
|
|
|
|
|
// internal only
|
|
|
|
|
func (ecp *ContactPointService) getContactPointDecrypted(ctx context.Context, orgID int64, uid string) (apimodels.EmbeddedContactPoint, error) { |
|
|
|
|
cfg, _, err := ecp.getCurrentConfig(ctx, orgID) |
|
|
|
|
revision, err := getLastConfiguration(ctx, orgID, ecp.amStore) |
|
|
|
|
if err != nil { |
|
|
|
|
return apimodels.EmbeddedContactPoint{}, err |
|
|
|
|
} |
|
|
|
|
for _, receiver := range cfg.GetGrafanaReceiverMap() { |
|
|
|
|
for _, receiver := range revision.cfg.GetGrafanaReceiverMap() { |
|
|
|
|
if receiver.UID != uid { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
@ -114,7 +114,7 @@ func (ecp *ContactPointService) CreateContactPoint(ctx context.Context, orgID in |
|
|
|
|
return apimodels.EmbeddedContactPoint{}, fmt.Errorf("contact point is not valid: %w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cfg, fetchedHash, err := ecp.getCurrentConfig(ctx, orgID) |
|
|
|
|
revision, err := getLastConfiguration(ctx, orgID, ecp.amStore) |
|
|
|
|
if err != nil { |
|
|
|
|
return apimodels.EmbeddedContactPoint{}, err |
|
|
|
|
} |
|
|
|
@ -143,7 +143,7 @@ func (ecp *ContactPointService) CreateContactPoint(ctx context.Context, orgID in |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
receiverFound := false |
|
|
|
|
for _, receiver := range cfg.AlertmanagerConfig.Receivers { |
|
|
|
|
for _, receiver := range revision.cfg.AlertmanagerConfig.Receivers { |
|
|
|
|
if receiver.Name == contactPoint.Name { |
|
|
|
|
receiver.PostableGrafanaReceivers.GrafanaManagedReceivers = append(receiver.PostableGrafanaReceivers.GrafanaManagedReceivers, grafanaReceiver) |
|
|
|
|
receiverFound = true |
|
|
|
@ -151,7 +151,7 @@ func (ecp *ContactPointService) CreateContactPoint(ctx context.Context, orgID in |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !receiverFound { |
|
|
|
|
cfg.AlertmanagerConfig.Receivers = append(cfg.AlertmanagerConfig.Receivers, &apimodels.PostableApiReceiver{ |
|
|
|
|
revision.cfg.AlertmanagerConfig.Receivers = append(revision.cfg.AlertmanagerConfig.Receivers, &apimodels.PostableApiReceiver{ |
|
|
|
|
Receiver: config.Receiver{ |
|
|
|
|
Name: grafanaReceiver.Name, |
|
|
|
|
}, |
|
|
|
@ -161,7 +161,7 @@ func (ecp *ContactPointService) CreateContactPoint(ctx context.Context, orgID in |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(cfg) |
|
|
|
|
data, err := json.Marshal(revision.cfg) |
|
|
|
|
if err != nil { |
|
|
|
|
return apimodels.EmbeddedContactPoint{}, err |
|
|
|
|
} |
|
|
|
@ -169,8 +169,8 @@ func (ecp *ContactPointService) CreateContactPoint(ctx context.Context, orgID in |
|
|
|
|
err = ecp.xact.InTransaction(ctx, func(ctx context.Context) error { |
|
|
|
|
err = ecp.amStore.UpdateAlertmanagerConfiguration(ctx, &models.SaveAlertmanagerConfigurationCmd{ |
|
|
|
|
AlertmanagerConfiguration: string(data), |
|
|
|
|
FetchedConfigurationHash: fetchedHash, |
|
|
|
|
ConfigurationVersion: "v1", |
|
|
|
|
FetchedConfigurationHash: revision.concurrencyToken, |
|
|
|
|
ConfigurationVersion: revision.version, |
|
|
|
|
Default: false, |
|
|
|
|
OrgID: orgID, |
|
|
|
|
}) |
|
|
|
@ -242,11 +242,11 @@ func (ecp *ContactPointService) UpdateContactPoint(ctx context.Context, orgID in |
|
|
|
|
SecureSettings: extractedSecrets, |
|
|
|
|
} |
|
|
|
|
// save to store
|
|
|
|
|
cfg, fetchedHash, err := ecp.getCurrentConfig(ctx, orgID) |
|
|
|
|
revision, err := getLastConfiguration(ctx, orgID, ecp.amStore) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
for _, receiver := range cfg.AlertmanagerConfig.Receivers { |
|
|
|
|
for _, receiver := range revision.cfg.AlertmanagerConfig.Receivers { |
|
|
|
|
if receiver.Name == contactPoint.Name { |
|
|
|
|
receiverNotFound := true |
|
|
|
|
for i, grafanaReceiver := range receiver.GrafanaManagedReceivers { |
|
|
|
@ -261,15 +261,15 @@ func (ecp *ContactPointService) UpdateContactPoint(ctx context.Context, orgID in |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
data, err := json.Marshal(cfg) |
|
|
|
|
data, err := json.Marshal(revision.cfg) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
return ecp.xact.InTransaction(ctx, func(ctx context.Context) error { |
|
|
|
|
err = ecp.amStore.UpdateAlertmanagerConfiguration(ctx, &models.SaveAlertmanagerConfigurationCmd{ |
|
|
|
|
AlertmanagerConfiguration: string(data), |
|
|
|
|
FetchedConfigurationHash: fetchedHash, |
|
|
|
|
ConfigurationVersion: "v1", |
|
|
|
|
FetchedConfigurationHash: revision.concurrencyToken, |
|
|
|
|
ConfigurationVersion: revision.version, |
|
|
|
|
Default: false, |
|
|
|
|
OrgID: orgID, |
|
|
|
|
}) |
|
|
|
@ -286,7 +286,7 @@ func (ecp *ContactPointService) UpdateContactPoint(ctx context.Context, orgID in |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ecp *ContactPointService) DeleteContactPoint(ctx context.Context, orgID int64, uid string) error { |
|
|
|
|
cfg, fetchedHash, err := ecp.getCurrentConfig(ctx, orgID) |
|
|
|
|
revision, err := getLastConfiguration(ctx, orgID, ecp.amStore) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -297,7 +297,7 @@ func (ecp *ContactPointService) DeleteContactPoint(ctx context.Context, orgID in |
|
|
|
|
// Name of the contact point that will be removed, might be used if a
|
|
|
|
|
// full removal is done to check if it's referenced in any route.
|
|
|
|
|
name := "" |
|
|
|
|
for i, receiver := range cfg.AlertmanagerConfig.Receivers { |
|
|
|
|
for i, receiver := range revision.cfg.AlertmanagerConfig.Receivers { |
|
|
|
|
for j, grafanaReceiver := range receiver.GrafanaManagedReceivers { |
|
|
|
|
if grafanaReceiver.UID == uid { |
|
|
|
|
name = grafanaReceiver.Name |
|
|
|
@ -305,16 +305,16 @@ func (ecp *ContactPointService) DeleteContactPoint(ctx context.Context, orgID in |
|
|
|
|
// if this was the last receiver we removed, we remove the whole receiver
|
|
|
|
|
if len(receiver.GrafanaManagedReceivers) == 0 { |
|
|
|
|
fullRemoval = true |
|
|
|
|
cfg.AlertmanagerConfig.Receivers = append(cfg.AlertmanagerConfig.Receivers[:i], cfg.AlertmanagerConfig.Receivers[i+1:]...) |
|
|
|
|
revision.cfg.AlertmanagerConfig.Receivers = append(revision.cfg.AlertmanagerConfig.Receivers[:i], revision.cfg.AlertmanagerConfig.Receivers[i+1:]...) |
|
|
|
|
} |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if fullRemoval && isContactPointInUse(name, []*apimodels.Route{cfg.AlertmanagerConfig.Route}) { |
|
|
|
|
if fullRemoval && isContactPointInUse(name, []*apimodels.Route{revision.cfg.AlertmanagerConfig.Route}) { |
|
|
|
|
return fmt.Errorf("contact point '%s' is currently used by a notification policy", name) |
|
|
|
|
} |
|
|
|
|
data, err := json.Marshal(cfg) |
|
|
|
|
data, err := json.Marshal(revision.cfg) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -328,29 +328,14 @@ func (ecp *ContactPointService) DeleteContactPoint(ctx context.Context, orgID in |
|
|
|
|
} |
|
|
|
|
return ecp.amStore.UpdateAlertmanagerConfiguration(ctx, &models.SaveAlertmanagerConfigurationCmd{ |
|
|
|
|
AlertmanagerConfiguration: string(data), |
|
|
|
|
FetchedConfigurationHash: fetchedHash, |
|
|
|
|
ConfigurationVersion: "v1", |
|
|
|
|
FetchedConfigurationHash: revision.concurrencyToken, |
|
|
|
|
ConfigurationVersion: revision.version, |
|
|
|
|
Default: false, |
|
|
|
|
OrgID: orgID, |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ecp *ContactPointService) getCurrentConfig(ctx context.Context, orgID int64) (*apimodels.PostableUserConfig, string, error) { |
|
|
|
|
query := &models.GetLatestAlertmanagerConfigurationQuery{ |
|
|
|
|
OrgID: orgID, |
|
|
|
|
} |
|
|
|
|
err := ecp.amStore.GetLatestAlertmanagerConfiguration(ctx, query) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, "", err |
|
|
|
|
} |
|
|
|
|
cfg, err := DeserializeAlertmanagerConfig([]byte(query.Result.AlertmanagerConfiguration)) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, "", err |
|
|
|
|
} |
|
|
|
|
return cfg, query.Result.ConfigurationHash, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func isContactPointInUse(name string, routes []*apimodels.Route) bool { |
|
|
|
|
if len(routes) == 0 { |
|
|
|
|
return false |
|
|
|
|