@@ -7,19 +7,17 @@ import (
7
7
"net/http"
8
8
"strings"
9
9
10
- "github.com/google/go-querystring/query"
11
-
12
10
"github.com/bytebase/terraform-provider-bytebase/api"
13
11
)
14
12
15
13
// CreateEnvironment creates the environment.
16
- func (c * client ) CreateEnvironment (ctx context.Context , create * api.EnvironmentUpsert ) (* api.Environment , error ) {
14
+ func (c * client ) CreateEnvironment (ctx context.Context , environmentID string , create * api.EnvironmentMessage ) (* api.EnvironmentMessage , error ) {
17
15
payload , err := json .Marshal (create )
18
16
if err != nil {
19
17
return nil , err
20
18
}
21
19
22
- req , err := http .NewRequestWithContext (ctx , "POST" , fmt .Sprintf ("%s/environment " , c .HostURL ), strings .NewReader (string (payload )))
20
+ req , err := http .NewRequestWithContext (ctx , "POST" , fmt .Sprintf ("%s/environments?environmentId=%s " , c .HostURL , environmentID ), strings .NewReader (string (payload )))
23
21
if err != nil {
24
22
return nil , err
25
23
}
@@ -29,7 +27,7 @@ func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentU
29
27
return nil , err
30
28
}
31
29
32
- var env api.Environment
30
+ var env api.EnvironmentMessage
33
31
err = json .Unmarshal (body , & env )
34
32
if err != nil {
35
33
return nil , err
@@ -39,8 +37,8 @@ func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentU
39
37
}
40
38
41
39
// GetEnvironment gets the environment by id.
42
- func (c * client ) GetEnvironment (ctx context.Context , environmentID int ) (* api.Environment , error ) {
43
- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/environment/%d " , c .HostURL , environmentID ), nil )
40
+ func (c * client ) GetEnvironment (ctx context.Context , environmentID string ) (* api.EnvironmentMessage , error ) {
41
+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/environments/%s " , c .HostURL , environmentID ), nil )
44
42
if err != nil {
45
43
return nil , err
46
44
}
@@ -50,7 +48,7 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.En
50
48
return nil , err
51
49
}
52
50
53
- var env api.Environment
51
+ var env api.EnvironmentMessage
54
52
err = json .Unmarshal (body , & env )
55
53
if err != nil {
56
54
return nil , err
@@ -60,13 +58,8 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.En
60
58
}
61
59
62
60
// ListEnvironment finds all environments.
63
- func (c * client ) ListEnvironment (ctx context.Context , find * api.EnvironmentFind ) ([]* api.Environment , error ) {
64
- q , err := query .Values (find )
65
- if err != nil {
66
- return nil , err
67
- }
68
-
69
- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/environment?%s" , c .HostURL , q .Encode ()), nil )
61
+ func (c * client ) ListEnvironment (ctx context.Context , showDeleted bool ) (* api.ListEnvironmentMessage , error ) {
62
+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("%s/environments?showDeleted=%v" , c .HostURL , showDeleted ), nil )
70
63
if err != nil {
71
64
return nil , err
72
65
}
@@ -76,23 +69,34 @@ func (c *client) ListEnvironment(ctx context.Context, find *api.EnvironmentFind)
76
69
return nil , err
77
70
}
78
71
79
- res := [] * api.Environment {}
72
+ var res api.ListEnvironmentMessage
80
73
err = json .Unmarshal (body , & res )
81
74
if err != nil {
82
75
return nil , err
83
76
}
84
77
85
- return res , nil
78
+ return & res , nil
86
79
}
87
80
88
81
// UpdateEnvironment updates the environment.
89
- func (c * client ) UpdateEnvironment (ctx context.Context , environmentID int , patch * api.EnvironmentUpsert ) (* api.Environment , error ) {
82
+ func (c * client ) UpdateEnvironment (ctx context.Context , environmentID string , patch * api.EnvironmentPatchMessage ) (* api.EnvironmentMessage , error ) {
90
83
payload , err := json .Marshal (patch )
91
84
if err != nil {
92
85
return nil , err
93
86
}
94
87
95
- req , err := http .NewRequestWithContext (ctx , "PATCH" , fmt .Sprintf ("%s/environment/%d" , c .HostURL , environmentID ), strings .NewReader (string (payload )))
88
+ paths := []string {}
89
+ if patch .Title != nil {
90
+ paths = append (paths , "environment.title" )
91
+ }
92
+ if patch .Order != nil {
93
+ paths = append (paths , "environment.order" )
94
+ }
95
+ if patch .Tier != nil {
96
+ paths = append (paths , "environment.tier" )
97
+ }
98
+
99
+ req , err := http .NewRequestWithContext (ctx , "PATCH" , fmt .Sprintf ("%s/environments/%s?update_mask=%s" , c .HostURL , environmentID , strings .Join (paths , "," )), strings .NewReader (string (payload )))
96
100
if err != nil {
97
101
return nil , err
98
102
}
@@ -102,7 +106,7 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch
102
106
return nil , err
103
107
}
104
108
105
- var env api.Environment
109
+ var env api.EnvironmentMessage
106
110
err = json .Unmarshal (body , & env )
107
111
if err != nil {
108
112
return nil , err
@@ -112,8 +116,8 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch
112
116
}
113
117
114
118
// DeleteEnvironment deletes the environment.
115
- func (c * client ) DeleteEnvironment (ctx context.Context , environmentID int ) error {
116
- req , err := http .NewRequestWithContext (ctx , "DELETE" , fmt .Sprintf ("%s/environment/%d " , c .HostURL , environmentID ), nil )
119
+ func (c * client ) DeleteEnvironment (ctx context.Context , environmentID string ) error {
120
+ req , err := http .NewRequestWithContext (ctx , "DELETE" , fmt .Sprintf ("%s/environments/%s " , c .HostURL , environmentID ), nil )
117
121
if err != nil {
118
122
return err
119
123
}
@@ -123,3 +127,24 @@ func (c *client) DeleteEnvironment(ctx context.Context, environmentID int) error
123
127
}
124
128
return nil
125
129
}
130
+
131
+ // UndeleteEnvironment undeletes the environment.
132
+ func (c * client ) UndeleteEnvironment (ctx context.Context , environmentID string ) (* api.EnvironmentMessage , error ) {
133
+ req , err := http .NewRequestWithContext (ctx , "POST" , fmt .Sprintf ("%s/environments/%s:undelete" , c .HostURL , environmentID ), nil )
134
+ if err != nil {
135
+ return nil , err
136
+ }
137
+
138
+ body , err := c .doRequest (req )
139
+ if err != nil {
140
+ return nil , err
141
+ }
142
+
143
+ var res api.EnvironmentMessage
144
+ err = json .Unmarshal (body , & res )
145
+ if err != nil {
146
+ return nil , err
147
+ }
148
+
149
+ return & res , nil
150
+ }
0 commit comments