Skip to content
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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
run:
deadline: 5m
tests: true
modules-download-mode: vendor

linters:
disable-all: true
Expand Down Expand Up @@ -45,4 +46,3 @@ issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
new: true
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ verify: ## Verify 'vendor' dependencies
.PHONY: lint
lint: ## Run linter
@ $(MAKE) --no-print-directory log-$@
GO111MODULE=on golangci-lint run ./...
GO111MODULE=on golangci-lint run --tests=false ./...

.PHONY: fmt
fmt: ## Format go files
@ $(MAKE) --no-print-directory log-$@
goimports -w $(GOFILES)

.PHONY: checkfmt
checkfmt: RESULT = $(shell goimports -l $(GOFILES) | tee >(if [ "$$(wc -l)" = 0 ]; then echo "OK"; fi))
checkfmt: SHELL := /usr/bin/env bash
checkfmt: RESULT ?= $(shell goimports -l $(GOFILES) | tee >(if [ "$$(wc -l)" = 0 ]; then echo "OK"; fi))
checkfmt: SHELL := /usr/bin/env bash
checkfmt: ## Check formatting of go files
@ $(MAKE) --no-print-directory log-$@
@ echo "$(RESULT)"
Expand Down
6 changes: 3 additions & 3 deletions cmd/cca/cca.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/cloud-ca/cca/cmd/cca/connection"
"github.com/cloud-ca/cca/cmd/cca/version"
"github.com/cloud-ca/cca/pkg/cli"
"github.com/cloud-ca/cca/pkg/client"
"github.com/cloud-ca/cca/pkg/flags"
"github.com/cloud-ca/cca/pkg/output"
"github.com/cloud-ca/cca/pkg/cli/client"
"github.com/cloud-ca/cca/pkg/cli/flags"
"github.com/cloud-ca/cca/pkg/cli/output"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion cmd/cca/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cloud-ca/cca/cmd/cca/completion/bash"
"github.com/cloud-ca/cca/cmd/cca/completion/zsh"
"github.com/cloud-ca/cca/pkg/cli"
"github.com/cloud-ca/cca/pkg/util"
"github.com/cloud-ca/cca/pkg/cli/util"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/cca/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cloud-ca/cca/cmd/cca/connection/get"
"github.com/cloud-ca/cca/cmd/cca/connection/list"
"github.com/cloud-ca/cca/pkg/cli"
"github.com/cloud-ca/cca/pkg/util"
"github.com/cloud-ca/cca/pkg/cli/util"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/cca/connection/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package get

import (
"github.com/cloud-ca/cca/pkg/cli"
"github.com/cloud-ca/cca/pkg/output"
"github.com/cloud-ca/cca/pkg/cli/output"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/cca/connection/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package list

import (
"github.com/cloud-ca/cca/pkg/cli"
"github.com/cloud-ca/cca/pkg/output"
"github.com/cloud-ca/cca/pkg/cli/output"
"github.com/spf13/cobra"
)

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/cloud-ca/cca
go 1.12

require (
github.com/cloud-ca/go-cloudca v1.4.0
github.com/golang/mock v1.1.1
github.com/lithammer/dedent v1.1.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.2.2
github.com/tidwall/pretty v1.0.0
gopkg.in/yaml.v2 v2.2.2
sigs.k8s.io/kind v0.4.0
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloud-ca/go-cloudca v1.4.0 h1:R8ZWca1EtZrMS3t05Un11LwGX21/phuPp7kpMNT82DU=
github.com/cloud-ca/go-cloudca v1.4.0/go.mod h1:+BpT2i2e5Zn+IDBVcZiqOBrZXSQpo/ObZ/MMTUh/RhI=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
Expand All @@ -24,6 +22,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand All @@ -49,6 +48,7 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -102,6 +102,7 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
Expand Down Expand Up @@ -138,6 +139,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
Expand All @@ -164,6 +166,7 @@ golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190514140710-3ec191127204/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/client.go → pkg/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
package client

import (
gocca "github.com/cloud-ca/go-cloudca"
"github.com/cloud-ca/cca/pkg/cloudca"
)

// Client to interact with cloud.ca infrastructure
type Client struct {
*gocca.CcaClient
*cloudca.Client
}

// NewClient returns a new client to interact with cloud.ca
// infrastructure with provided API URL and Key
func NewClient(url string, key string) *Client {
return &Client{
CcaClient: gocca.NewCcaClientWithURL(url, key),
Client: cloudca.NewClientWithURL(url, key),
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/flags/flags.go → pkg/cli/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package flags

import (
"github.com/cloud-ca/cca/pkg/output"
"github.com/cloud-ca/cca/pkg/cli/output"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions pkg/cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package cli

import (
"github.com/cloud-ca/cca/pkg/client"
"github.com/cloud-ca/cca/pkg/flags"
"github.com/cloud-ca/cca/pkg/output"
"github.com/cloud-ca/cca/pkg/cli/client"
"github.com/cloud-ca/cca/pkg/cli/flags"
"github.com/cloud-ca/cca/pkg/cli/output"
)

// Wrapper of different parts of cca cli
Expand Down
111 changes: 111 additions & 0 deletions pkg/cloudca/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright © 2019 cloud.ca Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)

// Client Represent the Client interface for interacting with cloud.ca API
type Client interface {
Do(request Request) (*Response, error)
GetAPIURL() string
GetAPIKey() string
}

// CcaClient for interacting with cloud.ca API
type CcaClient struct {
apiURL string
apiKey string
httpClient *http.Client
}

// Do Execute the API call to server and returns a Response. cloud.ca errors will
// be returned in the Response body, not in the error return value. The error
// return value is reserved for unexpected errors.
func (c CcaClient) Do(request Request) (*Response, error) {
var bodyBuffer io.Reader
if request.Body != nil {
bodyBuffer = bytes.NewBuffer(request.Body)
}
method := request.Method
if method == "" {
method = "GET"
}
req, err := http.NewRequest(request.Method, c.buildURL(request.Endpoint, request.Options), bodyBuffer)
if err != nil {
return nil, err
}
req.Header.Add("MC-Api-Key", c.apiKey)
req.Header.Add("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
err := resp.Body.Close()
fmt.Printf("%s", err)
}()
return NewResponse(resp)
}

// GetAPIKey Return the API key being used by API client
func (c CcaClient) GetAPIKey() string {
return c.apiKey
}

// GetAPIURL Return the API URL being used by API client
func (c CcaClient) GetAPIURL() string {
return c.apiURL
}

// NewClient Create a new Client with provided API URL and key
func NewClient(apiURL, apiKey string) Client {
return CcaClient{
apiURL: apiURL,
apiKey: apiKey,
httpClient: &http.Client{},
}
}

// NewInsecureClient Create a new Client with provided API URL and key that accepts insecure connections
func NewInsecureClient(apiURL, apiKey string) Client {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return CcaClient{
apiURL: apiURL,
apiKey: apiKey,
httpClient: &http.Client{Transport: tr},
}
}

// buildURL Builds a URL by using endpoint and options. Options will be set as query parameters.
func (c CcaClient) buildURL(endpoint string, options map[string]string) string {
query := url.Values{}
if options != nil {
for k, v := range options {
query.Add(k, v)
}
}
u, _ := url.Parse(c.apiURL + "/" + strings.Trim(endpoint, "/") + "?" + query.Encode())
return u.String()
}
91 changes: 91 additions & 0 deletions pkg/cloudca/api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright © 2019 cloud.ca Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetTaskReturnTaskIfSuccess(t *testing.T) {
//given
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"taskId": "test_task_id", `+
`"taskStatus": "test_task_status", `+
`"data": {"key":"value"}, `+
`"metadata": {"meta_key":"meta_value"}}`)
}))
defer server.Close()

transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}

httpClient := &http.Client{Transport: transport}
ccaClient := CcaClient{server.URL, "api-key", httpClient}

expectedResp := Response{
TaskID: "test_task_id",
TaskStatus: "test_task_status",
Data: []byte(`{"key":"value"}`),
MetaData: map[string]interface{}{"meta_key": "meta_value"},
StatusCode: 200,
}

//when
resp, _ := ccaClient.Do(Request{Method: "GET", Endpoint: "/fooo"})

//then
assert.Equal(t, expectedResp, *resp)
}

func TestGetTaskReturnErrorsIfErrorOccured(t *testing.T) {
//given
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"errors": [{"errorCode": "FOO_ERROR", "message": "message1"}, {"errorCode": "BAR_ERROR", "message":"message2"}]}`)
}))
defer server.Close()

transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}

httpClient := &http.Client{Transport: transport}
ccaClient := CcaClient{server.URL, "api-key", httpClient}

expectedResp := Response{
Errors: []Error{{ErrorCode: "FOO_ERROR", Message: "message1"}, {ErrorCode: "BAR_ERROR", Message: "message2"}},
StatusCode: 400,
}

//when
resp, _ := ccaClient.Do(Request{Method: "GET", Endpoint: "/fooo"})

//then
assert.Equal(t, expectedResp, *resp)
}
Loading