github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/waf/v1/alarmnotifications_test.go (about)

     1  package v1
     2  
     3  import (
     4  	"testing"
     5  
     6  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/smn/v2/topics"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf/v1/alarmnotifications"
    11  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    12  )
    13  
    14  func TestAlarmNotificationWorkflow(t *testing.T) {
    15  	client, err := clients.NewWafV1Client()
    16  	th.AssertNoErr(t, err)
    17  
    18  	smnClient, err := clients.NewSmnV2Client()
    19  	th.AssertNoErr(t, err)
    20  
    21  	topic := createTopic(t, smnClient)
    22  	defer deleteTopic(t, smnClient, topic)
    23  
    24  	t.Logf("Attempting to get Alarm Notification")
    25  	alarmNotification, err := alarmnotifications.List(client).Extract()
    26  	th.AssertNoErr(t, err)
    27  	t.Logf("Got Alarm Notification: %s", alarmNotification.ID)
    28  
    29  	t.Logf("Attempting to update Alarm Notification: %s", alarmNotification.ID)
    30  	enabled := true
    31  	updateOpts := alarmnotifications.UpdateOpts{
    32  		Enabled:       &enabled,
    33  		TopicURN:      &topic,
    34  		SendFrequency: 5,
    35  		Times:         200,
    36  		Threat:        []string{"xss", "sqli"},
    37  	}
    38  
    39  	_, err = alarmnotifications.Update(client, alarmNotification.ID, updateOpts).Extract()
    40  	th.AssertNoErr(t, err)
    41  	t.Logf("Updated Alarm Notification: %s", alarmNotification.ID)
    42  	defer func() {
    43  		t.Logf("Attempting to disable Alarm Notification: %s", alarmNotification.ID)
    44  		disabled := false
    45  		emptyTopic := ""
    46  		updateOpts := alarmnotifications.UpdateOpts{
    47  			Enabled:       &disabled,
    48  			TopicURN:      &emptyTopic,
    49  			SendFrequency: 5,
    50  			Times:         1,
    51  			Threat:        []string{"all"},
    52  		}
    53  		alarm, err := alarmnotifications.Update(client, alarmNotification.ID, updateOpts).Extract()
    54  		th.AssertNoErr(t, err)
    55  		t.Logf("Disabled Alarm Notification: %s", alarm.ID)
    56  	}()
    57  
    58  	newAlarmNotification, err := alarmnotifications.List(client).Extract()
    59  	th.AssertNoErr(t, err)
    60  	th.AssertEquals(t, enabled, newAlarmNotification.Enabled)
    61  	th.AssertEquals(t, updateOpts.SendFrequency, newAlarmNotification.SendFrequency)
    62  	th.AssertEquals(t, updateOpts.Times, newAlarmNotification.Times)
    63  }
    64  
    65  func createTopic(t *testing.T, client *golangsdk.ServiceClient) string {
    66  	t.Logf("Attempting to create SMN topic")
    67  	opts := topics.CreateOps{
    68  		Name: tools.RandomString("topic-", 3),
    69  	}
    70  	topic, err := topics.Create(client, opts).Extract()
    71  	th.AssertNoErr(t, err)
    72  	t.Logf("Created SMN topic: %s", topic.TopicUrn)
    73  	return topic.TopicUrn
    74  }
    75  
    76  func deleteTopic(t *testing.T, client *golangsdk.ServiceClient, topicURN string) {
    77  	t.Logf("Attempting to delete SMN topic: %s", topicURN)
    78  	err := topics.Delete(client, topicURN).ExtractErr()
    79  	th.AssertNoErr(t, err)
    80  	t.Logf("Deleted SMN topic: %s", topicURN)
    81  }