Skip to content

Commit

Permalink
update sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Jun 19, 2020
1 parent b1a6f72 commit 64f3dfd
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.14.4

ARG BEETLE_VERSION=0.2.0
ARG BEETLE_VERSION=0.2.1

ENV GO111MODULE=on

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<p align="center">
<img src="https://raw.githubusercontent.com/clivern/Beetle/master/assets/img/gopher.png?v=0.2.0" width="180" />
<img src="https://raw.githubusercontent.com/clivern/Beetle/master/assets/img/gopher.png?v=0.2.1" width="180" />
<h3 align="center">Beetle</h3>
<p align="center">Kubernetes multi-cluster deployment service</p>
<p align="center">
<a href="https://travis-ci.com/Clivern/Beetle"><img src="https://travis-ci.com/Clivern/Beetle.svg?branch=master"></a>
<a href="https://github.com/Clivern/Beetle/releases"><img src="https://img.shields.io/badge/Version-v0.2.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Beetle"><img src="https://goreportcard.com/badge/github.com/clivern/Beetle?v=0.2.0"></a>
<a href="https://github.com/Clivern/Beetle/releases"><img src="https://img.shields.io/badge/Version-v0.2.1-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Beetle"><img src="https://goreportcard.com/badge/github.com/clivern/Beetle?v=0.2.1"></a>
<a href="https://hub.docker.com/r/clivern/beetle"><img src="https://img.shields.io/badge/Docker-Latest-orange"></a>
<a href="https://github.com/Clivern/Beetle/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg"></a>
</p>
</p>
<br/>
<p align="center">
<img src="https://raw.githubusercontent.com/Clivern/Beetle/master/assets/img/chart.png?v=0.2.0" width="100%" />
<img src="https://raw.githubusercontent.com/Clivern/Beetle/master/assets/img/chart.png?v=0.2.1" width="100%" />
</p>
<br/>

Expand Down
25 changes: 12 additions & 13 deletions sdk/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@ import (
"net/http"

"github.com/clivern/beetle/internal/app/model"
"github.com/clivern/beetle/internal/app/module"
)

// GetApplications Get Applications List
func GetApplications(ctx context.Context, httpClient *module.HTTPClient, serverURL, cluster, namespace, apiKey string) (model.Applications, error) {
func (c *Client) GetApplications(ctx context.Context, cluster, namespace string) (model.Applications, error) {
var result model.Applications

response, err := httpClient.Get(
response, err := c.HTTPClient.Get(
ctx,
fmt.Sprintf("%s/api/v1/cluster/%s/namespace/%s/app", serverURL, cluster, namespace),
fmt.Sprintf("%s/api/v1/cluster/%s/namespace/%s/app", c.APIURL, cluster, namespace),
map[string]string{},
map[string]string{"X-API-KEY": apiKey},
map[string]string{"X-API-KEY": c.APIKey},
)

if err != nil {
return result, err
}

statusCode := httpClient.GetStatusCode(response)
statusCode := c.HTTPClient.GetStatusCode(response)

if statusCode != http.StatusOK {
return result, fmt.Errorf(fmt.Sprintf("Invalid status code %d", statusCode))
}

body, err := httpClient.ToString(response)
body, err := c.HTTPClient.ToString(response)

if err != nil {
return result, fmt.Errorf(fmt.Sprintf("Invalid response: %s", err.Error()))
Expand All @@ -54,27 +53,27 @@ func GetApplications(ctx context.Context, httpClient *module.HTTPClient, serverU
}

// GetApplication Get Application
func GetApplication(ctx context.Context, httpClient *module.HTTPClient, serverURL, cluster, namespace, application, apiKey string) (model.Application, error) {
func (c *Client) GetApplication(ctx context.Context, cluster, namespace, application string) (model.Application, error) {
var result model.Application

response, err := httpClient.Get(
response, err := c.HTTPClient.Get(
ctx,
fmt.Sprintf("%s/api/v1/cluster/%s/namespace/%s/app/%s", serverURL, cluster, namespace, application),
fmt.Sprintf("%s/api/v1/cluster/%s/namespace/%s/app/%s", c.APIURL, cluster, namespace, application),
map[string]string{},
map[string]string{"X-API-KEY": apiKey},
map[string]string{"X-API-KEY": c.APIKey},
)

if err != nil {
return result, err
}

statusCode := httpClient.GetStatusCode(response)
statusCode := c.HTTPClient.GetStatusCode(response)

if statusCode != http.StatusOK {
return result, fmt.Errorf(fmt.Sprintf("Invalid status code %d", statusCode))
}

body, err := httpClient.ToString(response)
body, err := c.HTTPClient.ToString(response)

if err != nil {
return result, fmt.Errorf(fmt.Sprintf("Invalid response: %s", err.Error()))
Expand Down
11 changes: 8 additions & 3 deletions sdk/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
// TestApplicationCRUD test cases
func TestApplicationCRUD(t *testing.T) {
testingConfig := "config.testing.yml"
httpClient := module.NewHTTPClient()

httpClient := Client{}
httpClient.SetHTTPClient(module.NewHTTPClient())
httpClient.SetAPIKey("")

// LoadConfigFile
t.Run("LoadConfigFile", func(t *testing.T) {
Expand Down Expand Up @@ -60,7 +63,8 @@ func TestApplicationCRUD(t *testing.T) {

defer srv.Close()

result, err := GetApplications(context.TODO(), httpClient, srv.URL, "production", "default", "")
httpClient.SetAPIURL(srv.URL)
result, err := httpClient.GetApplications(context.TODO(), "production", "default")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, model.Applications{
Expand Down Expand Up @@ -95,7 +99,8 @@ func TestApplicationCRUD(t *testing.T) {

defer srv.Close()

result, err := GetApplication(context.TODO(), httpClient, srv.URL, "production", "default", "toad", "")
httpClient.SetAPIURL(srv.URL)
result, err := httpClient.GetApplication(context.TODO(), "production", "default", "toad")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, model.Application{
Expand Down
31 changes: 31 additions & 0 deletions sdk/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package sdk

import (
"github.com/clivern/beetle/internal/app/module"
)

// Client struct
type Client struct {
APIKey string
APIURL string
HTTPClient *module.HTTPClient
}

// SetHTTPClient sets http client
func (c *Client) SetHTTPClient(httpClient *module.HTTPClient) {
c.HTTPClient = httpClient
}

// SetAPIURL sets api url
func (c *Client) SetAPIURL(APIURL string) {
c.APIURL = APIURL
}

// SetAPIKey sets api key
func (c *Client) SetAPIKey(APIKey string) {
c.APIKey = APIKey
}
25 changes: 12 additions & 13 deletions sdk/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@ import (
"net/http"

"github.com/clivern/beetle/internal/app/model"
"github.com/clivern/beetle/internal/app/module"
)

// GetClusters Get Clusters List
func GetClusters(ctx context.Context, httpClient *module.HTTPClient, serverURL, apiKey string) (model.Clusters, error) {
func (c *Client) GetClusters(ctx context.Context) (model.Clusters, error) {
var result model.Clusters

response, err := httpClient.Get(
response, err := c.HTTPClient.Get(
ctx,
fmt.Sprintf("%s/api/v1/cluster", serverURL),
fmt.Sprintf("%s/api/v1/cluster", c.APIURL),
map[string]string{},
map[string]string{"X-API-KEY": apiKey},
map[string]string{"X-API-KEY": c.APIKey},
)

if err != nil {
return result, err
}

statusCode := httpClient.GetStatusCode(response)
statusCode := c.HTTPClient.GetStatusCode(response)

if statusCode != http.StatusOK {
return result, fmt.Errorf(fmt.Sprintf("Invalid status code %d", statusCode))
}

body, err := httpClient.ToString(response)
body, err := c.HTTPClient.ToString(response)

if err != nil {
return result, fmt.Errorf(fmt.Sprintf("Invalid response: %s", err.Error()))
Expand All @@ -54,27 +53,27 @@ func GetClusters(ctx context.Context, httpClient *module.HTTPClient, serverURL,
}

// GetCluster Get Cluster
func GetCluster(ctx context.Context, httpClient *module.HTTPClient, serverURL, cluster, apiKey string) (model.Cluster, error) {
func (c *Client) GetCluster(ctx context.Context, cluster string) (model.Cluster, error) {
var result model.Cluster

response, err := httpClient.Get(
response, err := c.HTTPClient.Get(
ctx,
fmt.Sprintf("%s/api/v1/cluster/%s", serverURL, cluster),
fmt.Sprintf("%s/api/v1/cluster/%s", c.APIURL, cluster),
map[string]string{},
map[string]string{"X-API-KEY": apiKey},
map[string]string{"X-API-KEY": c.APIKey},
)

if err != nil {
return result, err
}

statusCode := httpClient.GetStatusCode(response)
statusCode := c.HTTPClient.GetStatusCode(response)

if statusCode != http.StatusOK {
return result, fmt.Errorf(fmt.Sprintf("Invalid status code %d", statusCode))
}

body, err := httpClient.ToString(response)
body, err := c.HTTPClient.ToString(response)

if err != nil {
return result, fmt.Errorf(fmt.Sprintf("Invalid response: %s", err.Error()))
Expand Down
11 changes: 8 additions & 3 deletions sdk/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
// TestClusterCRUD test cases
func TestClusterCRUD(t *testing.T) {
testingConfig := "config.testing.yml"
httpClient := module.NewHTTPClient()

httpClient := Client{}
httpClient.SetHTTPClient(module.NewHTTPClient())
httpClient.SetAPIKey("")

// LoadConfigFile
t.Run("LoadConfigFile", func(t *testing.T) {
Expand Down Expand Up @@ -60,7 +63,8 @@ func TestClusterCRUD(t *testing.T) {

defer srv.Close()

result, err := GetClusters(context.TODO(), httpClient, srv.URL, "")
httpClient.SetAPIURL(srv.URL)
result, err := httpClient.GetClusters(context.TODO())

pkg.Expect(t, nil, err)
pkg.Expect(t, result, model.Clusters{
Expand All @@ -81,7 +85,8 @@ func TestClusterCRUD(t *testing.T) {

defer srv.Close()

result, err := GetCluster(context.TODO(), httpClient, srv.URL, "staging", "")
httpClient.SetAPIURL(srv.URL)
result, err := httpClient.GetCluster(context.TODO(), "staging")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, model.Cluster{Name: "staging", Health: false})
Expand Down
13 changes: 6 additions & 7 deletions sdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"net/http"

"github.com/clivern/beetle/internal/app/model"
"github.com/clivern/beetle/internal/app/module"
)

// CreateDeployment Get Application
func CreateDeployment(ctx context.Context, httpClient *module.HTTPClient, serverURL string, request model.DeploymentRequest, apiKey string) (model.Job, error) {
func (c *Client) CreateDeployment(ctx context.Context, request model.DeploymentRequest) (model.Job, error) {
var result model.Job

requestBody, err := request.ConvertToJSON()
Expand All @@ -23,25 +22,25 @@ func CreateDeployment(ctx context.Context, httpClient *module.HTTPClient, server
return result, err
}

response, err := httpClient.Post(
response, err := c.HTTPClient.Post(
ctx,
fmt.Sprintf("%s/api/v1/cluster/%s/namespace/%s/app/%s", serverURL, request.Cluster, request.Namespace, request.Application),
fmt.Sprintf("%s/api/v1/cluster/%s/namespace/%s/app/%s", c.APIURL, request.Cluster, request.Namespace, request.Application),
requestBody,
map[string]string{},
map[string]string{"X-API-KEY": apiKey},
map[string]string{"X-API-KEY": c.APIKey},
)

if err != nil {
return result, err
}

statusCode := httpClient.GetStatusCode(response)
statusCode := c.HTTPClient.GetStatusCode(response)

if statusCode != http.StatusAccepted {
return result, fmt.Errorf(fmt.Sprintf("Invalid status code %d", statusCode))
}

body, err := httpClient.ToString(response)
body, err := c.HTTPClient.ToString(response)

if err != nil {
return result, fmt.Errorf(fmt.Sprintf("Invalid response: %s", err.Error()))
Expand Down
8 changes: 6 additions & 2 deletions sdk/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
// TestDeploymentCRUD test cases
func TestDeploymentCRUD(t *testing.T) {
testingConfig := "config.testing.yml"
httpClient := module.NewHTTPClient()

httpClient := Client{}
httpClient.SetHTTPClient(module.NewHTTPClient())
httpClient.SetAPIKey("")

// LoadConfigFile
t.Run("LoadConfigFile", func(t *testing.T) {
Expand Down Expand Up @@ -68,7 +71,8 @@ func TestDeploymentCRUD(t *testing.T) {
Strategy: "recreate",
}

result, err := CreateDeployment(context.TODO(), httpClient, srv.URL, deploymentRequest, "")
httpClient.SetAPIURL(srv.URL)
result, err := httpClient.CreateDeployment(context.TODO(), deploymentRequest)

pkg.Expect(t, err, nil)
pkg.Expect(t, 1, result.ID)
Expand Down
Loading

0 comments on commit 64f3dfd

Please sign in to comment.