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

Support Cloud Monitoring #99

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
108 changes: 108 additions & 0 deletions vkcs/monitoring.go
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"`
}
93 changes: 93 additions & 0 deletions vkcs/monitoring_channel.go
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")
}
Copy link
Collaborator

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


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
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ftersin @schirevko
In public DNS I decided to use capitalized names for result methods, because it's more convenient and it would be easier to move client logic into separate package, which I believe should be done. If we stick to this way, you could just use gophercloud.ErrResult for deleteChannelResult.


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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
}
39 changes: 39 additions & 0 deletions vkcs/monitoring_template.go
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
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't stick to common way with returning (r commonTemplateResult)?

103 changes: 103 additions & 0 deletions vkcs/monitoring_trigger.go
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
}
Loading