-
Notifications
You must be signed in to change notification settings - Fork 13
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
Support Cloud Monitoring #99
base: master
Are you sure you want to change the base?
Changes from 2 commits
4c4cf37
b068908
14c2d5c
fc10f4a
1c9f2d3
b37fd66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package vkcs | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
) | ||
|
||
// monitoringClient performs request to cloud monitoring api | ||
type monitoringClient interface { | ||
Get(url string, JSONResponse interface{}, opts *gophercloud.RequestOpts) (*http.Response, error) | ||
Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *gophercloud.RequestOpts) (*http.Response, error) | ||
Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *gophercloud.RequestOpts) (*http.Response, error) | ||
Delete(url string, opts *gophercloud.RequestOpts) (*http.Response, error) | ||
Head(url string, opts *gophercloud.RequestOpts) (*http.Response, error) | ||
Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *gophercloud.RequestOpts) (*http.Response, error) | ||
ServiceURL(parts ...string) string | ||
} | ||
|
||
type CreateTrigger struct { | ||
Name string `json:"name"` | ||
Status string `json:"status"` | ||
Namespace string `json:"namespace"` | ||
Query string `json:"query"` | ||
Interval int `json:"interval"` | ||
NotificationTitle string `json:"notification_title"` | ||
NotificationChannels []string `json:"notification_channels"` | ||
} | ||
|
||
type TriggerIn struct { | ||
Trigger CreateTrigger `json:"trigger"` | ||
} | ||
|
||
// Map converts opts to a map (for a request body) | ||
func (opts TriggerIn) Map() (map[string]interface{}, error) { | ||
return gophercloud.BuildRequestBody(opts, "") | ||
} | ||
|
||
type TriggerData struct { | ||
CreatedAt time.Time `json:"created_at"` | ||
Id string `json:"id"` | ||
Namespace string `json:"namespace"` | ||
Name string `json:"name"` | ||
Interval int `json:"interval"` | ||
NotificationChannels []string `json:"notification_channels"` | ||
NotificationTitle string `json:"notification_title"` | ||
Query string `json:"query"` | ||
Status string `json:"status"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} | ||
|
||
type TriggerOut struct { | ||
Trigger TriggerData `json:"trigger"` | ||
} | ||
|
||
type TriggersList struct { | ||
Triggers []TriggerData `json:"triggers"` | ||
} | ||
|
||
type ChannelIn struct { | ||
Name string `json:"name" required:"true"` | ||
ChannelType string `json:"channel_type" required:"true"` | ||
Address string `json:"address" required:"true"` | ||
} | ||
|
||
// Map converts opts to a map (for a request body) | ||
func (opts ChannelIn) Map() (map[string]interface{}, error) { | ||
return gophercloud.BuildRequestBody(opts, "") | ||
} | ||
|
||
type ChannelOut struct { | ||
Channel struct { | ||
Address string `json:"address"` | ||
ChannelType string `json:"channel_type"` | ||
CreatedAt time.Time `json:"created_at"` | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} `json:"channel"` | ||
} | ||
|
||
type ChannelList struct { | ||
Channels []struct { | ||
Address string `json:"address"` | ||
ChannelType string `json:"channel_type"` | ||
CreatedAt time.Time `json:"created_at"` | ||
Id string `json:"id"` | ||
InUse bool `json:"in_use"` | ||
Name string `json:"name"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} `json:"channels"` | ||
} | ||
|
||
type TemplateIn struct { | ||
InstanceId string `json:"instance_id"` | ||
Capabilities []string `json:"capabilities"` | ||
} | ||
|
||
// Map converts opts to a map (for a request body) | ||
func (opts TemplateIn) Map() (map[string]interface{}, error) { | ||
return gophercloud.BuildRequestBody(opts, "") | ||
} | ||
|
||
type TemplateOut struct { | ||
Script string `json:"script"` | ||
LinkId string `json:"link_id"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package vkcs | ||
|
||
import ( | ||
"github.com/gophercloud/gophercloud" | ||
"net/http" | ||
) | ||
|
||
func instanceChannelURL(c ContainerClient, pid string, id string) string { | ||
return c.ServiceURL(pid, "notification_channels", id) | ||
} | ||
|
||
func createChannelURL(c ContainerClient, pid string) string { | ||
return c.ServiceURL(pid, "notification_channels") | ||
} | ||
|
||
func listChannelURL(c ContainerClient, pid string) string { | ||
return c.ServiceURL(pid, "notification_channels") | ||
} | ||
|
||
type commonChannelResult struct { | ||
gophercloud.Result | ||
} | ||
|
||
// extract is used to extract result into short response struct | ||
func (r commonChannelResult) extract() (*ChannelOut, error) { | ||
var c *ChannelOut | ||
if err := r.ExtractInto(&c); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
type deleteChannelResult struct { | ||
gophercloud.Result | ||
} | ||
|
||
func (r deleteChannelResult) extractErr() error { | ||
return r.Err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ftersin @schirevko |
||
|
||
func channelCreate(client monitoringClient, pid string, opts createOptsBuilder) commonChannelResult { | ||
b, err := opts.Map() | ||
var r commonChannelResult | ||
if err != nil { | ||
r.Err = err | ||
return r | ||
} | ||
var result *http.Response | ||
reqOpts := getMonRequestOpts(http.StatusCreated) | ||
result, r.Err = client.Post(createChannelURL(client, pid), b, &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return r | ||
} | ||
|
||
func channelUpdate(client monitoringClient, pid string, id string, opts createOptsBuilder) commonChannelResult { | ||
b, err := opts.Map() | ||
var r commonChannelResult | ||
if err != nil { | ||
r.Err = err | ||
return r | ||
} | ||
var result *http.Response | ||
reqOpts := getMonRequestOpts(http.StatusCreated) | ||
result, r.Err = client.Patch(instanceChannelURL(client, pid, id), b, &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return r | ||
} | ||
|
||
// triggerGet performs request to get trigger instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. channelGet |
||
func channelGet(client monitoringClient, pid string, id string) (r commonChannelResult) { | ||
reqOpts := getMonRequestOpts(http.StatusOK) | ||
var result *http.Response | ||
result, r.Err = client.Get(instanceChannelURL(client, pid, id), &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return | ||
} | ||
|
||
// triggerDelete performs request to delete trigger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. channelDelete |
||
func channelDelete(client monitoringClient, pid string, id string) (r deleteChannelResult) { | ||
reqOpts := getMonRequestOpts(http.StatusNoContent) | ||
var result *http.Response | ||
result, r.Err = client.Delete(instanceChannelURL(client, pid, id), reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package vkcs | ||
|
||
import ( | ||
"github.com/gophercloud/gophercloud" | ||
"net/http" | ||
) | ||
|
||
func createTemplateURL(c ContainerClient, pid string) string { | ||
return c.ServiceURL("project", pid, "link") | ||
} | ||
|
||
type commonTemplateResult struct { | ||
gophercloud.Result | ||
} | ||
|
||
// extract is used to extract result into short response struct | ||
func (r commonTemplateResult) extract() (*TemplateOut, error) { | ||
var t *TemplateOut | ||
if err := r.ExtractInto(&t); err != nil { | ||
return nil, err | ||
} | ||
return t, nil | ||
} | ||
|
||
func templateCreate(client monitoringClient, pid string, opts createOptsBuilder) commonTemplateResult { | ||
b, err := opts.Map() | ||
var r commonTemplateResult | ||
if err != nil { | ||
r.Err = err | ||
return r | ||
} | ||
var result *http.Response | ||
reqOpts := getMonRequestOpts(http.StatusCreated) | ||
result, r.Err = client.Post(createTemplateURL(client, pid), b, &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return r | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't stick to common way with returning (r commonTemplateResult)? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package vkcs | ||
|
||
import ( | ||
"github.com/gophercloud/gophercloud" | ||
"net/http" | ||
) | ||
|
||
type commonTriggerResult struct { | ||
gophercloud.Result | ||
} | ||
|
||
// extract is used to extract result into short response struct | ||
func (r commonTriggerResult) extract() (*TriggerOut, error) { | ||
var c *TriggerOut | ||
if err := r.ExtractInto(&c); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
type triggerDeleteResult struct { | ||
gophercloud.Result | ||
} | ||
|
||
func (r triggerDeleteResult) extractErr() error { | ||
return r.Err | ||
} | ||
|
||
func getMonRequestOpts(codes ...int) *gophercloud.RequestOpts { | ||
reqOpts := &gophercloud.RequestOpts{ | ||
OkCodes: codes, | ||
} | ||
if len(codes) != 0 { | ||
reqOpts.OkCodes = codes | ||
} | ||
return reqOpts | ||
} | ||
|
||
func instanceTriggerURL(c ContainerClient, pid string, id string) string { | ||
return c.ServiceURL(pid, "triggers", id) | ||
} | ||
|
||
func createTriggerURL(c ContainerClient, pid string) string { | ||
return c.ServiceURL(pid, "triggers") | ||
} | ||
|
||
func listTriggerURL(c ContainerClient, pid string) string { | ||
return c.ServiceURL(pid, "triggers") | ||
} | ||
|
||
func triggerCreate(client monitoringClient, pid string, opts createOptsBuilder) commonTriggerResult { | ||
b, err := opts.Map() | ||
var r commonTriggerResult | ||
if err != nil { | ||
r.Err = err | ||
return r | ||
} | ||
var result *http.Response | ||
reqOpts := getMonRequestOpts(http.StatusCreated) | ||
result, r.Err = client.Post(createTriggerURL(client, pid), b, &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return r | ||
} | ||
|
||
func triggerUpdate(client monitoringClient, pid string, id string, opts createOptsBuilder) commonTriggerResult { | ||
b, err := opts.Map() | ||
var r commonTriggerResult | ||
if err != nil { | ||
r.Err = err | ||
return r | ||
} | ||
var result *http.Response | ||
reqOpts := getMonRequestOpts(http.StatusOK) | ||
result, r.Err = client.Patch(instanceTriggerURL(client, pid, id), b, &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return r | ||
} | ||
|
||
// triggerGet performs request to get trigger instance | ||
func triggerGet(client monitoringClient, pid string, id string) (r commonTriggerResult) { | ||
reqOpts := getMonRequestOpts(http.StatusOK) | ||
var result *http.Response | ||
result, r.Err = client.Get(instanceTriggerURL(client, pid, id), &r.Body, reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return | ||
} | ||
|
||
// triggerDelete performs request to delete trigger | ||
func triggerDelete(client monitoringClient, pid string, id string) (r triggerDeleteResult) { | ||
reqOpts := getMonRequestOpts(http.StatusNoContent) | ||
var result *http.Response | ||
result, r.Err = client.Delete(instanceTriggerURL(client, pid, id), reqOpts) | ||
if r.Err == nil { | ||
r.Header = result.Header | ||
} | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Custom urls are usually defined in urls.go