-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
113 lines (96 loc) · 3.1 KB
/
client.go
File metadata and controls
113 lines (96 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package api
import (
"context"
"net/url"
"github.com/codesphere-cloud/cs-go/api/openapi_client"
)
type Client struct {
ctx context.Context
api *openapi_client.APIClient
time Time
}
type Configuration struct {
// Url of the codesphere environment
// Defaults to https://codesphere.com
BaseUrl *url.URL
// Codesphere api token
Token string
}
func (c Configuration) GetApiUrl() *url.URL {
if c.BaseUrl != nil {
return c.BaseUrl
}
// url.Parse() won't return an error on this static string,
// hence it's safe to ignore it.
defaultUrl, _ := url.Parse("https://codesphere.com/api")
return defaultUrl
}
// For use in tests
func NewClientWithCustomDeps(ctx context.Context, opts Configuration, api *openapi_client.APIClient, time Time) *Client {
return &Client{
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
api: api,
time: time,
}
}
func NewClient(ctx context.Context, opts Configuration) *Client {
cfg := openapi_client.NewConfiguration()
cfg.Servers = []openapi_client.ServerConfiguration{{
URL: opts.BaseUrl.String(),
}}
return NewClientWithCustomDeps(ctx, opts, openapi_client.NewAPIClient(cfg), &RealTime{})
}
func (c *Client) ListDataCenters() ([]DataCenter, error) {
datacenters, _, err := c.api.MetadataAPI.MetadataGetDatacenters(c.ctx).Execute()
return datacenters, err
}
func (c *Client) ListDomains(teamId int) ([]Domain, error) {
domains, _, err := c.api.DomainsAPI.DomainsListDomains(c.ctx, float32(teamId)).Execute()
return domains, err
}
func (c *Client) GetDomain(teamId int, domainName string) (*Domain, error) {
domain, _, err := c.api.DomainsAPI.DomainsGetDomain(c.ctx, float32(teamId), domainName).Execute()
return domain, err
}
func (c *Client) CreateDomain(teamId int, domainName string) (*Domain, error) {
domain, _, err := c.api.DomainsAPI.DomainsCreateDomain(c.ctx, float32(teamId), domainName).Execute()
return domain, err
}
func (c *Client) DeleteDomain(teamId int, domainName string) error {
_, err := c.api.DomainsAPI.DomainsDeleteDomain(c.ctx, float32(teamId), domainName).Execute()
return err
}
func (c *Client) UpdateDomain(
teamId int, domainName string, args UpdateDomainArgs,
) (*Domain, error) {
domain, _, err := c.api.DomainsAPI.
DomainsUpdateDomain(c.ctx, float32(teamId), domainName).
DomainsGetDomain200ResponseCustomConfig(args).
Execute()
return domain, err
}
func (c *Client) VerifyDomain(
teamId int, domainName string,
) (*DomainVerificationStatus, error) {
status, _, err := c.api.DomainsAPI.
DomainsVerifyDomain(c.ctx, float32(teamId), domainName).Execute()
return status, err
}
func (c *Client) UpdateWorkspaceConnections(
teamId int, domainName string, connections PathToWorkspaces,
) (*Domain, error) {
req := make(map[string][]int)
for path, workspaces := range connections {
ids := make([]int, len(workspaces))
for i, w := range workspaces {
ids[i] = w.Id
}
req[path] = ids
}
domain, _, err := c.api.DomainsAPI.
DomainsUpdateWorkspaceConnections(c.ctx, float32(teamId), domainName).
RequestBody(req).Execute()
return domain, err
}