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

Remove query escapes #14

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *Client) UpdateApp(appID string, app *Application) (*Response, error) {
}
r, err := c.request(options)
if r != nil {
if r.Code == 204 {
if (r.Code == 204) || (r.Code == 200) {
return r, nil
}
}
Expand All @@ -113,7 +113,21 @@ func (c *Client) DeleteApp(appID string) (*Response, error) {
}
r, err := c.request(options)
if r != nil {
if r.Code == 204 {
if r.Code == 200 {
return r, nil
}
}
return nil, err
}

func (c *Client) DeleteDeployment(deploymentID string) (*Response, error) {
options := &RequestOptions{
Path: fmt.Sprintf("deployments/%s", deploymentID),
Method: "DELETE",
}
r, err := c.request(options)
if r != nil {
if (r.Code == 200) || (r.Code == 202) {
return r, nil
}
}
Expand Down
49 changes: 42 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ import (

// Client is containing the configured http.Client
// and the host url
type HttpBasicAuth struct {
User string
Pass string
}

type Client struct {
Host *url.URL
Url string
HTTPClient *http.Client
Auth *HttpBasicAuth
}

type UpdateResp struct {
DeploymentID string `json:"deploymentId"`
}

// Actual version of the marathon api
Expand All @@ -26,16 +36,17 @@ const (
)

// NewClient return a pointer to the new client
func NewClient(host string, tlsConfig *tls.Config) (*Client, error) {
func NewClient(host string, auth *HttpBasicAuth, tlsConfig *tls.Config) (*Client, error) {
// ValIDate url
h, err := url.Parse(host)
if err != nil {
return nil, fmt.Errorf("can't parse host %s", host)
}

return &Client{
Host: h,
Url: h.String(),
HTTPClient: newHTTPClient(h, tlsConfig),
Auth: auth,
}, nil
}

Expand All @@ -52,7 +63,7 @@ func (c *Client) do(method, path string, data interface{}) ([]byte, int, error)
params = bytes.NewBuffer(buf)
}

req, err := http.NewRequest(method, c.Host.String()+path, params)
req, err := http.NewRequest(method, c.Url+path, params)
if err != nil {
return nil, -1, err
}
Expand All @@ -61,6 +72,10 @@ func (c *Client) do(method, path string, data interface{}) ([]byte, int, error)
req.Header.Set("User-Agent", "gomarathon")
req.Header.Set("Content-Type", "application/json")

if c.Auth != nil {
req.SetBasicAuth(c.Auth.User, c.Auth.Pass)
}

resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, -1, err
Expand Down Expand Up @@ -98,19 +113,19 @@ func (c *Client) request(options *RequestOptions) (*Response, error) {
v := url.Values{}

if options.Params.Cmd != "" {
v.Set("cmd", url.QueryEscape(options.Params.Cmd))
v.Set("cmd", options.Params.Cmd)
}

if options.Params.Host != "" {
v.Set("host", url.QueryEscape(options.Params.Host))
v.Set("host", options.Params.Host)
}

if options.Params.Scale {
v.Set("scale", "true")
}

if options.Params.CallbackURL != "" {
v.Set("CallbackURL", url.QueryEscape(options.Params.CallbackURL))
v.Set("CallbackURL", options.Params.CallbackURL)
}

path = fmt.Sprintf("%s?%s", path, v.Encode())
Expand All @@ -124,6 +139,26 @@ func (c *Client) request(options *RequestOptions) (*Response, error) {
Code: code,
}

//updated
if resp.Code == 200 {
updateResp := UpdateResp{}
err := json.Unmarshal(data, &updateResp)
if err == nil {
resp.DeploymentId = updateResp.DeploymentID
} else {
fmt.Println("Error unmashaling data response")
}
//created
} else if resp.Code == 201 {
app := Application{}
err := json.Unmarshal(data, &app)
if err == nil {
resp.DeploymentId = app.Deployments[0].ID
} else {
fmt.Println("Error unmashaling data response")
}
}

err = json.Unmarshal(data, resp)
if err != nil {
return resp, err
Expand Down
5 changes: 2 additions & 3 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gomarathon

import (
"fmt"
"net/url"
)

// RegisterCallbackURL register a new callback url
Expand All @@ -11,7 +10,7 @@ func (c *Client) RegisterCallbackURL(uri string) (*Response, error) {
Path: "eventSubscriptions",
Method: "POST",
Params: &Parameters{
CallbackURL: url.QueryEscape(uri),
CallbackURL: uri,
},
}
r, err := c.request(options)
Expand Down Expand Up @@ -44,7 +43,7 @@ func (c *Client) DeleteCallbackURL(uri string) (*Response, error) {
Path: "eventSubscriptions",
Method: "DELETE",
Params: &Parameters{
CallbackURL: url.QueryEscape(uri),
CallbackURL: uri,
},
}
r, err := c.request(options)
Expand Down
49 changes: 31 additions & 18 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ type Parameters struct {

// Response representation of a full marathon response
type Response struct {
Code int
Apps []*Application `json:"apps,omitempty"`
App *Application `json:"app,omitempty"`
Versions []string `json:",omitempty"`
Tasks []*Task `json:"tasks,omitempty"`
Code int
Apps []*Application `json:"apps,omitempty"`
App *Application `json:"app,omitempty"`
Versions []string `json:",omitempty"`
Tasks []*Task `json:"tasks,omitempty"`
DeploymentId string `json:"deployment_id,omitempty"`
}

// Application marathon application see :
Expand All @@ -48,6 +49,7 @@ type Application struct {
UpgradeStrategy *UpgradeStrategy `json:"upgradeStrategy,omitempty"`
Uris []string `json:"uris,omitempty"`
Version string `json:"version,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

// Container is docker parameters
Expand Down Expand Up @@ -87,24 +89,35 @@ type UpgradeStrategy struct {
// HealthCheck is described here:
// https://github.com/mesosphere/marathon/blob/master/REST.md#healthchecks
type HealthCheck struct {
Protocol string `json:"protocol,omitempty"`
Path string `json:"path,omitempty"`
GracePeriodSeconds int `json:"gracePeriodSeconds,omitempty"`
IntervalSeconds int `json:"intervalSeconds,omitempty"`
PortIndex int `json:"portIndex,omitempty"`
TimeoutSeconds int `json:"timeoutSeconds,omitempty"`
Protocol string `json:"protocol,omitempty"`
Path string `json:"path,omitempty"`
GracePeriodSeconds int `json:"gracePeriodSeconds,omitempty"`
IntervalSeconds int `json:"intervalSeconds,omitempty"`
PortIndex int `json:"portIndex,omitempty"`
TimeoutSeconds int `json:"timeoutSeconds,omitempty"`
MaxConsecutiveFailures int `json:"maxConsecutiveFailures"`
}

type HealthCheckResult struct {
Alive bool `json:"alive,omitempty"`
ConsecutiveFailures int `json:"consecutiveFailures,omitempty"`
FirstSuccess string `json:"firstSuccess,omitempty"`
LastFailure string `json:"lastFailure,omitempty"`
LastSuccess string `json:"lastSuccess,omitempty"`
TaskID string `json:"taskId,omitempty"`
}

// Task is described here:
// https://github.com/mesosphere/marathon/blob/master/REST.md#tasks
type Task struct {
AppID string `json:"appId"`
Host string `json:"host"`
ID string `json:"id"`
Ports []int `json:"ports"`
StagedAt string `json:"stagedAt"`
StartedAt string `json:"startedAt"`
Version string `json:"version"`
AppID string `json:"appId"`
Host string `json:"host"`
ID string `json:"id"`
Ports []int `json:"ports"`
StagedAt string `json:"stagedAt"`
StartedAt string `json:"startedAt"`
Version string `json:"version"`
HealthCheckResults []*HealthCheckResult `json:"healthCheckResults"`
}

// Deployment is described here:
Expand Down