Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CES] Reimplement Alarm Rules v1 API #381

Merged
merged 27 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d4cedf4
[DCS] Change Capacity to float32
Aloento Jul 27, 2022
3b3c5eb
[DCS] Change Capacity to float32
Aloento Jul 27, 2022
4ec013c
Merge remote-tracking branch 'Aloento/devel' into Aloento-devel
Jul 27, 2022
cc99daa
[DCS]: update test to check float capacity
Jul 27, 2022
6263f7b
[CES] ListAlarms
Aloento Jul 27, 2022
2964595
Merge remote-tracking branch 'origin/devel' into devel
Aloento Jul 27, 2022
f0df08b
Merge branch 'opentelekomcloud:devel' into devel
Aloento Jul 27, 2022
6cab7cb
[CES] Finished
Aloento Jul 27, 2022
72755d7
Merge remote-tracking branch 'origin/devel' into devel
Aloento Jul 27, 2022
115b91e
[CES] err
Aloento Jul 27, 2022
bd5e5f0
[CES] *
Aloento Jul 27, 2022
dcb7542
[CES] *
Aloento Jul 27, 2022
aeec8fa
Merge branch 'opentelekomcloud:devel' into devel
Aloento Jul 29, 2022
9c494e0
Merge branch 'opentelekomcloud:devel' into devel
Aloento Jul 29, 2022
9d2f573
Merge branch 'opentelekomcloud:devel' into devel
Aloento Aug 1, 2022
e3a980c
remove cloudeyeservice
Aloento Aug 2, 2022
26dd452
Add TestAlarms
Aloento Aug 3, 2022
31772d8
AlarmActionEnabled: false
Aloento Aug 3, 2022
4287916
pass
Aloento Aug 3, 2022
7f0dd95
change test
Aloento Aug 4, 2022
71aaef0
Remove Extract
Aloento Aug 9, 2022
d35f3c5
Merge branch 'opentelekomcloud:devel' into devel
Aloento Aug 9, 2022
87fd9b9
to opts
Aloento Aug 9, 2022
ef8b11b
Merge branch 'opentelekomcloud:devel' into devel
Aloento Aug 10, 2022
0b0e9eb
move to split files
Aloento Aug 12, 2022
a5c9401
Merge branch 'opentelekomcloud:devel' into devel
Aloento Aug 12, 2022
c44188f
nil
Aloento Aug 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions acceptance/openstack/ces/v1/alarms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package v1

import (
"testing"

"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/ces/v1/alarms"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

func TestAlarms(t *testing.T) {
client, err := clients.NewCesV1Client()
th.AssertNoErr(t, err)

alarmsRes, err := alarms.ListAlarms(client, alarms.ListAlarmsRequest{
Limit: 10,
Order: "desc",
})

th.AssertNoErr(t, err)
th.AssertEquals(t, alarmsRes.MetaData.Count <= 10, true)

f := false
newAlarm, err := alarms.CreateAlarm(client, alarms.CreateAlarmRequest{
AlarmName: "alarm-acc-test",
Metric: alarms.MetricForAlarm{
Namespace: "SYS.VPC",
MetricName: "upstream_bandwidth",
Dimensions: []alarms.MetricsDimension{
{
Name: "bandwidth_id",
Value: "026c495c-fake-test-8b11-a113ba530d11",
},
},
},
Condition: alarms.Condition{
ComparisonOperator: ">=",
Count: 3,
Filter: "average",
Period: 300,
Value: 4000000,
},
AlarmEnabled: &f,
AlarmActionEnabled: &f,
Aloento marked this conversation as resolved.
Show resolved Hide resolved
})
th.AssertNoErr(t, err)

t.Cleanup(func() {
err = alarms.DeleteAlarm(client, newAlarm)
th.AssertNoErr(t, err)
})

err = alarms.UpdateAlarmAction(client, newAlarm, alarms.ModifyAlarmActionRequest{
AlarmEnabled: true,
})
th.AssertNoErr(t, err)

showAlarm, err := alarms.ShowAlarm(client, newAlarm)
th.AssertNoErr(t, err)
th.AssertEquals(t, showAlarm[0].AlarmEnabled, true)
}
138 changes: 138 additions & 0 deletions openstack/ces/v1/alarms/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package alarms

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
)

type ListAlarmsRequest struct {
Aloento marked this conversation as resolved.
Show resolved Hide resolved
// The value ranges from 1 to 100, and is 100 by default.
// This parameter is used to limit the number of query results.
Limit int `q:"limit"`
// Specifies the result sorting method, which is sorted by timestamp.
// The default value is desc.
// asc: The query results are displayed in the ascending order.
// desc: The query results are displayed in the descending order.
Order string `q:"order"`
// Specifies the first queried alarm to be displayed on a page.
Start string `q:"start"`
}

func ListAlarms(client *golangsdk.ServiceClient, opts ListAlarmsRequest) (*ListAlarmsResponse, error) {
Aloento marked this conversation as resolved.
Show resolved Hide resolved
url := alarmsURL(client)
query, err := golangsdk.BuildQueryString(opts)
if err != nil {
return nil, err
}

raw := golangsdk.Result{}
url += query.String()

_, err = client.Get(url, &raw.Body, nil)
if err != nil {
return nil, err
}

var s ListAlarmsResponse
err = raw.ExtractInto(&s)
return &s, err
}

// ------------------------------------------------------------------------------------------------

func ShowAlarm(client *golangsdk.ServiceClient, id string) ([]MetricAlarms, error) {
raw := golangsdk.Result{}
_, err := client.Get(alarmIdURL(client, id), &raw.Body, nil)
if err != nil {
return nil, err
}

var res []MetricAlarms
err = raw.ExtractIntoSlicePtr(&res, "metric_alarms")
return res, err
}

// ------------------------------------------------------------------------------------------------

type ModifyAlarmActionRequest struct {
// Specifies whether the alarm rule is enabled.
AlarmEnabled bool `json:"alarm_enabled"`
}

func UpdateAlarmAction(client *golangsdk.ServiceClient, id string, req ModifyAlarmActionRequest) (err error) {
reqBody, err := golangsdk.BuildRequestBody(req, "")
if err != nil {
return
}

_, err = client.Put(alarmActionURL(client, id), reqBody, nil, &golangsdk.RequestOpts{
OkCodes: []int{204},
Aloento marked this conversation as resolved.
Show resolved Hide resolved
})
return
}

// ------------------------------------------------------------------------------------------------

func DeleteAlarm(client *golangsdk.ServiceClient, id string) (err error) {
_, err = client.Delete(alarmIdURL(client, id), &golangsdk.RequestOpts{
OkCodes: []int{204},
})
return
}

// ------------------------------------------------------------------------------------------------

type CreateAlarmRequest struct {
// Specifies the alarm rule name.
// Enter 1 to 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.
AlarmName string `json:"alarm_name"`
// Provides supplementary information about the alarm rule. Enter 0 to 256 characters.
AlarmDescription string `json:"alarm_description,omitempty"`
// Specifies the alarm metric.
Metric MetricForAlarm `json:"metric"`
// Specifies the alarm triggering condition.
Condition Condition `json:"condition"`
// Specifies whether to enable the alarm.
AlarmEnabled *bool `json:"alarm_enabled,omitempty"`
// Specifies whether to enable the action to be triggered by an alarm. The default value is true.
AlarmActionEnabled *bool `json:"alarm_action_enabled,omitempty"`
// Specifies the alarm severity. Possible values are 1, 2 (default), 3 and 4,
// indicating critical, major, minor, and informational, respectively.
AlarmLevel int `json:"alarm_level,omitempty"`
// Specifies the action to be triggered by an alarm.
AlarmActions []AlarmActions `json:"alarm_actions,omitempty"`
// Specifies the action to be triggered after the alarm is cleared.
OkActions []AlarmActions `json:"ok_actions,omitempty"`
}

type MetricForAlarm struct {
// Specifies the namespace of a service.
// The value must be in the service.item format and can contain 3 to 32 characters.
// service and item each must start with a letter and contain only letters, digits, and underscores (_).
Namespace string `json:"namespace"`
// Specifies the metric name.
// Start with a letter. Enter 1 to 64 characters. Only letters, digits, and underscores (_) are allowed.
MetricName string `json:"metric_name"`
// Specifies the list of metric dimensions.
Dimensions []MetricsDimension `json:"dimensions,omitempty"`
// Specifies the resource group ID selected during the alarm rule creation.
ResourceGroupId string `json:"resource_group_id,omitempty"`
}

func CreateAlarm(client *golangsdk.ServiceClient, opts CreateAlarmRequest) (string, error) {
reqBody, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return "", err
}

raw := golangsdk.Result{}
_, err = client.Post(alarmsURL(client), reqBody, &raw.Body, nil)
if err != nil {
return "", err
}

var s struct {
AlarmId string `json:"alarm_id"`
}
err = raw.ExtractInto(&s)
return s.AlarmId, err
}
88 changes: 88 additions & 0 deletions openstack/ces/v1/alarms/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package alarms

type ListAlarmsResponse struct {
MetricAlarms []MetricAlarms `json:"metric_alarms,omitempty"`
MetaData MetaData `json:"meta_data,omitempty"`
}

type MetaData struct {
Count int `json:"count"`
Total int `json:"total"`
Marker string `json:"marker"`
}

type MetricAlarms struct {
// Specifies the alarm rule name.
AlarmName string `json:"alarm_name"`
// Provides supplementary information about the alarm rule.
AlarmDescription string `json:"alarm_description,omitempty"`
// Specifies the alarm metric.
Metric MetricForAlarm `json:"metric"`
// Specifies the alarm triggering condition.
Condition Condition `json:"condition"`
// Specifies whether to enable the alarm rule.
AlarmEnabled bool `json:"alarm_enabled,omitempty"`
// Specifies the alarm severity. Possible values are 1, 2, 3 and 4, indicating critical, major, minor, and informational, respectively.
AlarmLevel int `json:"alarm_level,omitempty"`
// Specifies whether to enable the action to be triggered by an alarm.
AlarmActionEnabled bool `json:"alarm_action_enabled,omitempty"`
// Specifies the action to be triggered by an alarm.
AlarmActions []AlarmActions `json:"alarm_actions,omitempty"`
// Specifies the action to be triggered after the alarm is cleared.
OkActions []AlarmActions `json:"ok_actions,omitempty"`
// Specifies the alarm rule ID.
AlarmId string `json:"alarm_id"`
Aloento marked this conversation as resolved.
Show resolved Hide resolved
// Specifies when the alarm status changed. The value is a UNIX timestamp and the unit is ms.
UpdateTime int64 `json:"update_time"`
// Specifies the alarm status. The value can be:
//
// ok: The alarm status is normal.
// alarm: An alarm is generated.
// insufficient_data: The required data is insufficient.
AlarmState string `json:"alarm_state"`
}

type MetricsDimension struct {
// Specifies the dimension. For example, the ECS dimension is instance_id.
Name string `json:"name"`
// Specifies the dimension value, for example, an ECS ID.
Value string `json:"value"`
}

type Condition struct {
// Specifies the operator of alarm thresholds. Possible values are >, =, <, >=, and <=.
ComparisonOperator string `json:"comparison_operator"`
// Specifies the number of consecutive occurrence times that the alarm policy was met.
// The value ranges from 1 to 5.
Count int `json:"count"`
// Specifies the data rollup method. The following methods are supported:
//
// average: Cloud Eye calculates the average value of metric data within a rollup period.
// max: Cloud Eye calculates the maximum value of metric data within a rollup period.
// min: Cloud Eye calculates the minimum value of metric data within a rollup period.
// sum: Cloud Eye calculates the sum of metric data within a rollup period.
// variance: Cloud Eye calculates the variance value of metric data within a rollup period.
Filter string `json:"filter"`
// Specifies the interval (seconds) for checking whether the configured alarm rules are met.
Period int `json:"period"`
// Specifies the data unit. Enter up to 32 characters.
Unit string `json:"unit,omitempty"`
// Specifies the alarm threshold. The value ranges from 0 to Number. MAX_VALUE (1.7976931348623157e+108).
//
// For detailed thresholds, see the value range of each metric in the appendix.
// For example, you can set ECS cpu_util in Services Interconnected with Cloud Eye to 80.
Value float64 `json:"value"`
}

type AlarmActions struct {
// Specifies the alarm notification type.
// notification: indicates that a notification will be sent.
// autoscaling: indicates that a scaling action will be triggered.
Type string `json:"type"`
// Specifies the list of objects to be notified if the alarm status changes.
// You can configure up to 5 object IDs. You can obtain the topicUrn value from SMN in the following format:
// urn:smn:([a-z]|[A-Z]|[0-9]|\-){1,32}:([a-z]|[A-Z]|[0-9]){32}:([a-z]|[A-Z]|[0-9]|\-|\_){1,256}.
// If you set type to notification, you must specify notificationList.
// If you set type to autoscaling, you must set notificationList to [].
NotificationList []string `json:"notificationList"`
}
22 changes: 22 additions & 0 deletions openstack/ces/v1/alarms/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package alarms

import golangsdk "github.com/opentelekomcloud/gophertelekomcloud"

const alarms = "alarms"

// GET /V1.0/{project_id}/alarms
// POST /V1.0/{project_id}/alarms
func alarmsURL(c *golangsdk.ServiceClient) string {
return c.ServiceURL(alarms)
}

// GET /V1.0/{project_id}/alarms/{alarm_id}
// DELETE /V1.0/{project_id}/alarms/{alarm_id}
func alarmIdURL(c *golangsdk.ServiceClient, id string) string {
return c.ServiceURL(alarms, id)
}

// PUT /V1.0/{project_id}/alarms/{alarm_id}/action
func alarmActionURL(c *golangsdk.ServiceClient, id string) string {
return c.ServiceURL(alarms, id, "action")
}
Loading