generated from hashicorp/terraform-provider-scaffolding
/
workspaces.go
87 lines (66 loc) · 2.39 KB
/
workspaces.go
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
package client
import (
"context"
"fmt"
"strings"
)
func (c *TowerClient) CreateWorkspace(ctx context.Context, name string, fullName string, description string, visibility string) (int64, error) {
payload := map[string]interface{}{
"workspace": map[string]string{
"name": name,
"fullName": fullName,
"description": description,
"visibility": visibility,
},
}
res, err := c.requestWithJsonPayload(ctx, "POST", fmt.Sprintf("/orgs/%d/workspaces", c.orgId), nil, payload)
if err != nil {
return -1, err
}
if res == nil {
return -1, fmt.Errorf("Empty response from server")
}
workspaceObj := res.(map[string]interface{})
workspace := workspaceObj["workspace"].(map[string]interface{})
return int64(workspace["id"].(float64)), nil
}
func (c *TowerClient) GetWorkspace(ctx context.Context, id int64) (map[string]interface{}, error) {
res, err := c.requestWithoutPayload(ctx, "GET", fmt.Sprintf("/orgs/%d/workspaces/%d", c.orgId, id), nil)
if err != nil {
return nil, err
}
workspaceObj := res.(map[string]interface{})
workspace := workspaceObj["workspace"].(map[string]interface{})
if strings.HasPrefix(workspace["name"].(string), "deleted-"){
return nil, nil
}
return workspace, nil
}
func (c *TowerClient) GetWorkspaceByName(ctx context.Context, name string) (map[string]interface{}, error) {
res, err := c.requestWithoutPayload(ctx, "GET", fmt.Sprintf("/orgs/%d/workspaces", c.orgId), nil)
if err != nil {
return nil, err
}
if workspaces, ok := res.(map[string]interface{}); ok {
for _, workspace := range workspaces["workspaces"].([]interface{}) {
o, _ := workspace.(map[string]interface{})
if o["name"].(string) == name {
return c.GetWorkspace(ctx, int64(o["id"].(float64)))
}
}
}
return nil, fmt.Errorf("Could not find a workspace with the name '%s'", name)
}
func (c *TowerClient) DeleteWorkspace(ctx context.Context, id int64) error {
_, err := c.requestWithoutPayload(ctx, "DELETE", fmt.Sprintf("/orgs/%d/workspaces/%d", c.orgId, id), nil)
return err
}
func (c *TowerClient) UpdateWorkspace(ctx context.Context, id int64, fullName string, description string, visibility string) error {
payload := map[string]interface{}{
"fullName": fullName,
"description": description,
"visibility": visibility,
}
_, err := c.requestWithJsonPayload(ctx, "PUT", fmt.Sprintf("/orgs/%d/workspaces/%d", c.orgId, id), nil, payload)
return err
}