@@ -7,19 +7,17 @@ import (
77 "net/http"
88 "strings"
99
10- "github.com/google/go-querystring/query"
11-
1210 "github.com/bytebase/terraform-provider-bytebase/api"
1311)
1412
1513// 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 ) {
1715 payload , err := json .Marshal (create )
1816 if err != nil {
1917 return nil , err
2018 }
2119
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 )))
2321 if err != nil {
2422 return nil , err
2523 }
@@ -29,7 +27,7 @@ func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentU
2927 return nil , err
3028 }
3129
32- var env api.Environment
30+ var env api.EnvironmentMessage
3331 err = json .Unmarshal (body , & env )
3432 if err != nil {
3533 return nil , err
@@ -39,8 +37,8 @@ func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentU
3937}
4038
4139// 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 )
4442 if err != nil {
4543 return nil , err
4644 }
@@ -50,7 +48,7 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.En
5048 return nil , err
5149 }
5250
53- var env api.Environment
51+ var env api.EnvironmentMessage
5452 err = json .Unmarshal (body , & env )
5553 if err != nil {
5654 return nil , err
@@ -60,13 +58,8 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.En
6058}
6159
6260// 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 )
7063 if err != nil {
7164 return nil , err
7265 }
@@ -76,23 +69,34 @@ func (c *client) ListEnvironment(ctx context.Context, find *api.EnvironmentFind)
7669 return nil , err
7770 }
7871
79- res := [] * api.Environment {}
72+ var res api.ListEnvironmentMessage
8073 err = json .Unmarshal (body , & res )
8174 if err != nil {
8275 return nil , err
8376 }
8477
85- return res , nil
78+ return & res , nil
8679}
8780
8881// 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 ) {
9083 payload , err := json .Marshal (patch )
9184 if err != nil {
9285 return nil , err
9386 }
9487
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 )))
96100 if err != nil {
97101 return nil , err
98102 }
@@ -102,7 +106,7 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch
102106 return nil , err
103107 }
104108
105- var env api.Environment
109+ var env api.EnvironmentMessage
106110 err = json .Unmarshal (body , & env )
107111 if err != nil {
108112 return nil , err
@@ -112,8 +116,8 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch
112116}
113117
114118// 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 )
117121 if err != nil {
118122 return err
119123 }
@@ -123,3 +127,24 @@ func (c *client) DeleteEnvironment(ctx context.Context, environmentID int) error
123127 }
124128 return nil
125129}
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