-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocks.go
More file actions
126 lines (107 loc) · 3.78 KB
/
blocks.go
File metadata and controls
126 lines (107 loc) · 3.78 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
114
115
116
117
118
119
120
121
122
123
124
125
126
package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
)
type Blocks struct {
Client *Client
}
func (s Blocks) basePath(stackId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/blocks", s.Client.Config.OrgName, stackId)
}
func (s Blocks) bulkPath(stackId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/blocks/bulk", s.Client.Config.OrgName, stackId)
}
func (s Blocks) blockPath(stackId, blockId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/blocks/%d", s.Client.Config.OrgName, stackId, blockId)
}
// List - GET /orgs/:orgName/stacks/:stack_id/blocks
func (s Blocks) List(ctx context.Context, stackId int64, includeCapabilities bool) ([]types.Block, error) {
var q url.Values
if includeCapabilities {
q = url.Values{"include_capabilities": []string{"true"}}
}
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(stackId), q, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Block](res)
}
// Get - GET /orgs/:orgName/stacks/:stack_id/blocks/:id
func (s Blocks) Get(ctx context.Context, stackId, blockId int64, includeArchived bool) (*types.Block, error) {
q := url.Values{
"include_archived": []string{strconv.FormatBool(includeArchived)},
}
res, err := s.Client.Do(ctx, http.MethodGet, s.blockPath(stackId, blockId), q, nil, nil)
if err != nil {
return nil, err
}
var block types.Block
if err := response.ReadJson(res, &block); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &block, nil
}
type CreateBlockInput struct {
types.Block `json:",inline"`
WorkspaceConfigs []CreateBlockWorkspaceInput `json:"workspaceConfigs,omitempty"`
Template *types.WorkspaceTemplateConfig `json:"template,omitempty"`
}
type CreateBlockWorkspaceInput struct {
EnvName string `json:"envName"`
Extra types.ExtraWorkspaceConfig `json:"extra"`
}
// Create - POST /orgs/:orgName/stacks/:stack_id/blocks
func (s Blocks) Create(ctx context.Context, stackId int64, payload CreateBlockInput) (*types.Block, error) {
rawPayload, _ := json.Marshal(payload)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(stackId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Block](res)
}
// CreateBulk - POST /orgs/:orgName/stacks/:stack_id/blocks/bulk
func (s Blocks) CreateBulk(ctx context.Context, stackId int64, blocks []types.Block) ([]types.Block, error) {
rawPayload, _ := json.Marshal(blocks)
res, err := s.Client.Do(ctx, http.MethodPost, s.bulkPath(stackId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Block](res)
}
// Update - PUT/PATCH /orgs/:orgName/stacks/:stack_id/blocks/:id
func (s Blocks) Update(ctx context.Context, stackId, blockId int64, block *types.Block) (*types.Block, error) {
rawPayload, _ := json.Marshal(block)
res, err := s.Client.Do(ctx, http.MethodPut, s.blockPath(stackId, blockId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedBlock types.Block
if err := response.ReadJson(res, &updatedBlock); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedBlock, nil
}
// Destroy - DELETE /orgs/:orgName/stacks/:stack_id/blocks/:id
func (s Blocks) Destroy(ctx context.Context, stackId, blockId int64) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.blockPath(stackId, blockId), nil, nil, nil)
if err != nil {
return false, err
}
if err := response.Verify(res); response.IsNotFoundError(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}