Skip to content
Merged
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
7 changes: 6 additions & 1 deletion .mockery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
all: false
dir: '{{.InterfaceDir}}'
force-file-write: true
filename: 'mocks.go'
formatter: gofmt
log-level: info
structname: '{{.Mock}}{{.InterfaceName}}'
pkgname: '{{.SrcPackageName}}_test'
pkgname: '{{.SrcPackageName}}'
recursive: false
require-template-schema-exists: true
template: testify
Expand All @@ -16,3 +17,7 @@ packages:
github.com/codesphere-cloud/cs-go/cmd:
interfaces:
Client:
github.com/codesphere-cloud/cs-go/api/openapi_client:
config:
all: true
interfaces:
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ lint: install-build-deps
golangci-lint run

test:
go test ./...
go test ./... -count=1

generate: install-build-deps
generate:
go generate ./...

build:
Expand All @@ -25,8 +25,8 @@ install:
generate-client:
rm -rf ${OPENAPI_DIR}
openapi-generator-cli generate -g go -o ${OPENAPI_DIR} -i https://codesphere.com/api/docs \
--additional-properties=isGoSubmodule=true,withGoMod=false,packageName=openapi_client \
--type-mappings=integer=int \
--additional-properties=generateInterfaces=true,isGoSubmodule=true,withGoMod=false,packageName=openapi_client \
--type-mappings=integer=int \
--template-dir openapi-template \
--skip-validate-spec # TODO: remove once the Codesphere openapi spec is fixed
# Remove all non-go files
Expand All @@ -40,6 +40,7 @@ generate-client:
${OPENAPI_DIR}/git_push.sh \
${OPENAPI_DIR}/README.md \
${OPENAPI_DIR}/test
make generate


generate-api: generate-client format
Expand Down
49 changes: 9 additions & 40 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,27 @@ func (c Configuration) GetApiUrl() *url.URL {
return defaultUrl
}

// For use in tests
func NewClientWithCustomApi(ctx context.Context, opts Configuration, api *openapi_client.APIClient) *Client {
return &Client{
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
api: api,
}
}

func NewClient(ctx context.Context, opts Configuration) *Client {
cfg := openapi_client.NewConfiguration()
cfg.Servers = []openapi_client.ServerConfiguration{{
URL: opts.BaseUrl.String(),
}}

return &Client{
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
api: openapi_client.NewAPIClient(cfg),
}
return NewClientWithCustomApi(ctx, opts, openapi_client.NewAPIClient(cfg))
}

func (c *Client) ListDataCenters() ([]DataCenter, error) {
datacenters, _, err := c.api.MetadataAPI.MetadataGetDatacenters(c.ctx).Execute()
return datacenters, err
}

func (c *Client) ListWorkspacePlans() ([]WorkspacePlan, error) {
plans, _, err := c.api.MetadataAPI.MetadataGetWorkspacePlans(c.ctx).Execute()
return plans, err
}

func (c *Client) ListTeams() ([]Team, error) {
teams, _, err := c.api.TeamsAPI.TeamsListTeams(c.ctx).Execute()
return teams, err
}

func (c *Client) GetTeam(teamId int) (*Team, error) {
team, _, err := c.api.TeamsAPI.TeamsGetTeam(c.ctx, float32(teamId)).Execute()
return ConvertToTeam(team), err
}

func (c *Client) CreateTeam(name string, dc int) (*Team, error) {
team, _, err := c.api.TeamsAPI.TeamsCreateTeam(c.ctx).
TeamsCreateTeamRequest(openapi_client.TeamsCreateTeamRequest{
Name: name,
Dc: dc,
}).
Execute()
return ConvertToTeam(team), err
}

func (c *Client) DeleteTeam(teamId int) error {
_, err := c.api.TeamsAPI.TeamsDeleteTeam(c.ctx, float32(teamId)).Execute()
return err
}

func (c *Client) ListDomains(teamId int) ([]Domain, error) {
domains, _, err := c.api.DomainsAPI.DomainsListDomains(c.ctx, float32(teamId)).Execute()
return domains, err
Expand Down Expand Up @@ -135,8 +109,3 @@ func (c *Client) UpdateWorkspaceConnections(
RequestBody(req).Execute()
return domain, err
}

func (c *Client) ListWorkspaces(teamId int) ([]Workspace, error) {
workspaces, _, err := c.api.WorkspacesAPI.WorkspacesListWorkspaces(c.ctx, float32(teamId)).Execute()
return workspaces, err
}
48 changes: 48 additions & 0 deletions api/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package errors

import (
"fmt"
"time"
)

type TimedOutError struct {
msg string
}

func (e *TimedOutError) Error() string {
return e.msg
}

func TimedOut(operation string, timeout time.Duration) *TimedOutError {
return &TimedOutError{
msg: fmt.Sprintf("%s timed out after %s", operation, timeout.String()),
}
}

type NotFoundError struct {
msg string
}

func (e *NotFoundError) Error() string {
return e.msg
}

func NotFound(msg string) *NotFoundError {
return &NotFoundError{
msg: msg,
}
}

type DuplicatedError struct {
msg string
}

func (e *DuplicatedError) Error() string {
return e.msg
}

func Duplicated(msg string) *DuplicatedError {
return &DuplicatedError{
msg: msg,
}
}
113 changes: 106 additions & 7 deletions api/openapi_client/api_domains.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions api/openapi_client/api_metadata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading