Skip to content

Commit

Permalink
feat(cockpit): change emails to contact_points
Browse files Browse the repository at this point in the history
  • Loading branch information
jremy42 committed May 22, 2024
1 parent 1ed7b93 commit 6eee8b7
Show file tree
Hide file tree
Showing 6 changed files with 693 additions and 643 deletions.
83 changes: 59 additions & 24 deletions internal/services/cockpit/alert_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

Expand All @@ -32,11 +31,21 @@ func ResourceCockpitAlertManager() *schema.Resource {
Default: true,
Description: "Enable or disable the alert manager",
},
"emails": {

"contact_points": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: verify.IsEmail()},
Optional: true,
Description: "A list of email addresses for the alert receivers",
Description: "A list of contact points",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.IsEmail(),
Description: "Email addresses for the alert receivers",
},
},
},
},
"region": regional.Schema(),
"alert_manager_url": {
Expand All @@ -55,7 +64,7 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
}

projectID := d.Get("project_id").(string)
emails := d.Get("emails").([]interface{})
contactPoints := d.Get("contact_points").([]interface{})
EnableManagedAlerts := d.Get("enable_managed_alerts").(bool)

_, err = api.EnableAlertManager(&cockpit.RegionalAPIEnableAlertManagerRequest{
Expand All @@ -75,16 +84,23 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
}
}

if len(emails) > 0 {
for _, email := range emails {
emailStr, ok := email.(string)
if len(contactPoints) > 0 {
for _, cp := range contactPoints {
cpMap, ok := cp.(map[string]interface{})
if !ok {
return diag.FromErr(errors.New("invalid contact point format"))
}

email, ok := cpMap["email"].(string)
if !ok {
return diag.FromErr(errors.New("invalid email format"))
}

emailCP := &cockpit.ContactPointEmail{
To: emailStr,
To: email,
}
_, err := api.CreateContactPoint(&cockpit.RegionalAPICreateContactPointRequest{

_, err = api.CreateContactPoint(&cockpit.RegionalAPICreateContactPointRequest{
ProjectID: projectID,
Email: emailCP,
Region: region,
Expand Down Expand Up @@ -127,13 +143,16 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}

var emails []string
var contactPointsList []map[string]interface{}
for _, cp := range contactPoints.ContactPoints {
if cp.Email != nil {
emails = append(emails, cp.Email.To)
contactPoint := map[string]interface{}{
"email": cp.Email.To,
}
contactPointsList = append(contactPointsList, contactPoint)
}
}
_ = d.Set("emails", emails)
_ = d.Set("contact_points", contactPointsList)
return nil
}

Expand Down Expand Up @@ -161,13 +180,28 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}
}
if d.HasChange("emails") {
oldEmailsInterface, newEmailsInterface := d.GetChange("emails")
oldEmails := types.ExpandStrings(oldEmailsInterface.([]interface{}))
newEmails := types.ExpandStrings(newEmailsInterface.([]interface{}))
if d.HasChange("contact_points") {

Check failure on line 183 in internal/services/cockpit/alert_manager.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

for _, email := range oldEmails {
if !types.SliceContainsString(newEmails, email) {
oldContactPointsInterface, newContactPointsInterface := d.GetChange("contact_points")
oldContactPoints := oldContactPointsInterface.([]interface{})
newContactPoints := newContactPointsInterface.([]interface{})

oldContactMap := make(map[string]map[string]interface{})
for _, oldCP := range oldContactPoints {
cp := oldCP.(map[string]interface{})
email := cp["email"].(string)
oldContactMap[email] = cp
}

newContactMap := make(map[string]map[string]interface{})
for _, newCP := range newContactPoints {
cp := newCP.(map[string]interface{})
email := cp["email"].(string)
newContactMap[email] = cp
}

for email, _ := range oldContactMap {

Check failure on line 203 in internal/services/cockpit/alert_manager.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
if _, found := newContactMap[email]; !found {
err := api.DeleteContactPoint(&cockpit.RegionalAPIDeleteContactPointRequest{
Region: region,
ProjectID: projectID,
Expand All @@ -179,12 +213,13 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
}
}

for _, email := range newEmails {
if !types.SliceContainsString(oldEmails, email) {
_, err := api.CreateContactPoint(&cockpit.RegionalAPICreateContactPointRequest{
for email, _ := range newContactMap {
if _, found := oldContactMap[email]; !found {
contactPointEmail := &cockpit.ContactPointEmail{To: email}
_, err = api.CreateContactPoint(&cockpit.RegionalAPICreateContactPointRequest{
Region: region,
ProjectID: projectID,
Email: &cockpit.ContactPointEmail{To: email},
Email: contactPointEmail,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -214,7 +249,7 @@ func ResourceCockpitAlertManagerDelete(ctx context.Context, d *schema.ResourceDa

for _, cp := range contactPoints.ContactPoints {
if cp.Email != nil {
err := api.DeleteContactPoint(&cockpit.RegionalAPIDeleteContactPointRequest{
err = api.DeleteContactPoint(&cockpit.RegionalAPIDeleteContactPointRequest{
Region: region,
ProjectID: projectID,
Email: &cockpit.ContactPointEmail{To: cp.Email.To},
Expand Down
71 changes: 43 additions & 28 deletions internal/services/cockpit/alert_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ func TestAccCockpitAlertManager_CreateWithSingleContact(t *testing.T) {
CheckDestroy: testAccCockpitAlertManagerAndContactsDestroy(tt),
Steps: []resource.TestStep{
{
Config: testAccCockpitAlertManagerConfigWithContacts([]string{"initial@example.com"}),
Config: testAccCockpitAlertManagerConfigWithContacts([]map[string]string{
{"email": "initial@example.com"},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "project_id"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "enable_managed_alerts", "true"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.0", "initial@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.0.email", "initial@example.com"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "region"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "alert_manager_url"),
testAccCheckCockpitContactPointExists(tt, "scaleway_cockpit_alert_manager.alert_manager"),
),
},
{
Config: testAccCockpitAlertManagerConfigWithContacts([]string{"updated@example.com"}),
Config: testAccCockpitAlertManagerConfigWithContacts([]map[string]string{
{"email": "updated@example.com"},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "enable_managed_alerts", "true"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.0", "updated@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.0.email", "updated@example.com"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "region"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "alert_manager_url"),
testAccCheckCockpitContactPointExists(tt, "scaleway_cockpit_alert_manager.alert_manager"),
Expand All @@ -57,23 +61,29 @@ func TestAccCockpitAlertManager_CreateWithMultipleContacts(t *testing.T) {
CheckDestroy: testAccCockpitAlertManagerAndContactsDestroy(tt),
Steps: []resource.TestStep{
{
Config: testAccCockpitAlertManagerConfigWithContacts([]string{"initial1@example.com", "initial2@example.com"}),
Config: testAccCockpitAlertManagerConfigWithContacts([]map[string]string{
{"email": "initial1@example.com"},
{"email": "initial2@example.com"},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "project_id"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "enable_managed_alerts", "true"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.0", "initial1@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.1", "initial2@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.0.email", "initial1@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.1.email", "initial2@example.com"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "region"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "alert_manager_url"),
testAccCheckCockpitContactPointExists(tt, "scaleway_cockpit_alert_manager.alert_manager"),
),
},
{
Config: testAccCockpitAlertManagerConfigWithContacts([]string{"updated1@example.com", "updated2@example.com"}),
Config: testAccCockpitAlertManagerConfigWithContacts([]map[string]string{
{"email": "updated1@example.com"},
{"email": "updated2@example.com"},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "enable_managed_alerts", "true"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.0", "updated1@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.1", "updated2@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.0.email", "updated1@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.1.email", "updated2@example.com"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "region"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "alert_manager_url"),
testAccCheckCockpitContactPointExists(tt, "scaleway_cockpit_alert_manager.alert_manager"),
Expand All @@ -93,23 +103,29 @@ func TestAccCockpitAlertManager_UpdateSingleContact(t *testing.T) {
CheckDestroy: testAccCockpitAlertManagerAndContactsDestroy(tt),
Steps: []resource.TestStep{
{
Config: testAccCockpitAlertManagerConfigWithContacts([]string{"notupdated@example.com", "initial1@example.com"}),
Config: testAccCockpitAlertManagerConfigWithContacts([]map[string]string{
{"email": "notupdated@example.com"},
{"email": "initial1@example.com"},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "project_id"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "enable_managed_alerts", "true"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.0", "notupdated@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.1", "initial1@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.0.email", "notupdated@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.1.email", "initial1@example.com"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "region"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "alert_manager_url"),
testAccCheckCockpitContactPointExists(tt, "scaleway_cockpit_alert_manager.alert_manager"),
),
},
{
Config: testAccCockpitAlertManagerConfigWithContacts([]string{"notupdated@example.com", "updated1@example.com"}),
Config: testAccCockpitAlertManagerConfigWithContacts([]map[string]string{
{"email": "notupdated@example.com"},
{"email": "updated1@example.com"},
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "enable_managed_alerts", "true"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.0", "notupdated@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "emails.1", "updated1@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.0.email", "notupdated@example.com"),
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.alert_manager", "contact_points.1.email", "updated1@example.com"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "region"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.alert_manager", "alert_manager_url"),
testAccCheckCockpitContactPointExists(tt, "scaleway_cockpit_alert_manager.alert_manager"),
Expand Down Expand Up @@ -148,15 +164,14 @@ func TestAccCockpitAlertManager_EnableDisable(t *testing.T) {
})
}

func testAccCockpitAlertManagerConfigWithContacts(emails []string) string {
emailsConfig := "["
for _, email := range emails {
emailsConfig += fmt.Sprintf(`"%s", `, email)
func testAccCockpitAlertManagerConfigWithContacts(contactPoints []map[string]string) string {
contactsConfig := ""
for _, contact := range contactPoints {
contactsConfig += fmt.Sprintf(`
contact_points {
email = "%s"
}`, contact["email"])
}
if len(emails) > 0 {
emailsConfig = emailsConfig[:len(emailsConfig)-2] // Remove the last comma and space
}
emailsConfig += "]"

return fmt.Sprintf(`
resource "scaleway_account_project" "project" {
Expand All @@ -165,10 +180,10 @@ func testAccCockpitAlertManagerConfigWithContacts(emails []string) string {
resource "scaleway_cockpit_alert_manager" "alert_manager" {
project_id = scaleway_account_project.project.id
enable_managed_alerts = true
emails = %s
enable_managed_alerts = true
%s
}
`, emailsConfig)
`, contactsConfig)
}

func testAccCockpitAlertManagerEnableConfig(enable bool) string {
Expand Down Expand Up @@ -224,7 +239,7 @@ func testAccCheckCockpitContactPointExists(tt *acctest.TestTools, resourceName s
return err
}
for _, cp := range contactPoints.ContactPoints {
if cp.Email != nil && cp.Email.To == rs.Primary.Attributes["emails.0"] {
if cp.Email != nil && cp.Email.To == rs.Primary.Attributes["contact_points.0.email"] {
return nil
}
}
Expand Down
Loading

0 comments on commit 6eee8b7

Please sign in to comment.