-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_capabilities.go
More file actions
114 lines (96 loc) · 4.28 KB
/
app_capabilities.go
File metadata and controls
114 lines (96 loc) · 4.28 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
package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
)
type AppCapabilities struct {
Client *Client
}
func (e AppCapabilities) basePath(stackId, appId, envId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/apps/%d/envs/%d/capabilities", e.Client.Config.OrgName, stackId, appId, envId)
}
func (e AppCapabilities) capPath(stackId, appId, envId int64, capName string) string {
return fmt.Sprintf("orgs/%s/stacks/%d/apps/%d/envs/%d/capabilities/%s", e.Client.Config.OrgName, stackId, appId, envId, capName)
}
// List - GET /orgs/:orgName/stacks/:stackId/apps/:app_id/envs/:env_id/capabilities
func (e AppCapabilities) List(ctx context.Context, stackId, appId, envId int64) (types.CapabilityConfigs, error) {
res, err := e.Client.Do(ctx, http.MethodGet, e.basePath(stackId, appId, envId), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[types.CapabilityConfigs](res)
}
// Get - GET /orgs/:orgName/stacks/:stackId/apps/:app_id/envs/:env_id/capabilities/:name
func (e AppCapabilities) Get(ctx context.Context, stackId, appId, envId int64, capName string) (*types.CapabilityConfig, error) {
res, err := e.Client.Do(ctx, http.MethodGet, e.capPath(stackId, appId, envId, capName), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.CapabilityConfig](res)
}
type CreateCapabilitiesInput struct {
Capabilities []CreateCapabilityInput `json:"capabilities"`
Updates []UpdateCapabilityOnCreateInput `json:"updates"`
Blocks []types.Block `json:"blocks"`
}
type CreateCapabilityInput struct {
Name string `json:"name"`
ModuleSource string `json:"moduleSource"`
ModuleSourceVersion string `json:"moduleSourceVersion"`
Namespace string `json:"namespace"`
Connections types.ConnectionTargets `json:"connections"`
}
type UpdateCapabilityOnCreateInput struct {
// Name is used to identify and cannot be changed
Name string `json:"name"`
// Namespace is modified in the create API endpoint
Namespace string `json:"namespace"`
}
// Create - POST /orgs/:orgName/stacks/:stackId/apps/:app_id/envs/:env_id/capabilities
func (e AppCapabilities) Create(ctx context.Context, stackId, appId, envId int64, capabilities []CreateCapabilityInput, updates []UpdateCapabilityOnCreateInput, blocks []types.Block) (types.CapabilityConfigs, *http.Response, error) {
input := CreateCapabilitiesInput{
Capabilities: capabilities,
Updates: updates,
Blocks: blocks,
}
rawPayload, _ := json.Marshal(input)
res, err := e.Client.Do(ctx, http.MethodPost, e.basePath(stackId, appId, envId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, res, err
}
result, err := response.ReadJsonVal[types.CapabilityConfigs](res)
return result, res, err
}
type UpdateCapabilityInput struct {
Namespace string `json:"namespace"`
ModuleSource string `json:"moduleSource"`
ModuleConstraint string `json:"moduleConstraint"`
Connections types.ConnectionTargets `json:"connections"`
}
// Update - PUT /orgs/:orgName/stacks/:stackId/apps/:appId/envs/:envId/capabilities/:name
func (e AppCapabilities) Update(ctx context.Context, stackId, appId, envId int64, capName string, input UpdateCapabilityInput) (*types.CapabilityConfig, *http.Response, error) {
rawPayload, _ := json.Marshal(input)
res, err := e.Client.Do(ctx, http.MethodPut, e.capPath(stackId, appId, envId, capName), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, res, err
}
result, err := response.ReadJsonPtr[types.CapabilityConfig](res)
return result, res, err
}
// Destroy - DELETE /orgs/:orgName/stacks/:stackId/apps/:app_id/envs/:env_id/capabilities/:name
func (e AppCapabilities) Destroy(ctx context.Context, stackId, appId, envId int64, capName string) (bool, error) {
res, err := e.Client.Do(ctx, http.MethodDelete, e.capPath(stackId, appId, envId, capName), 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
}